//package bug20888;

import testsuite.*;
import java.sql.*;

public class bug20888_3 extends BaseTestCase {
	private static int insertedRow = 0;
	
	public bug20888_3(String name) {
		super(name);
	}
	
	public static void main(String[] args) {
		junit.textui.TestRunner.run(bug20888_3.class);
	}
	
	public void setUp() throws Exception {
		super.setUp();

		createTestTable();
	}
	
	public void tearDown() throws Exception {
		try {
			this.stmt.executeUpdate("DROP TABLE IF EXISTS BUG20888_TEST");
		} finally {
			super.tearDown();
		}
	}
	
	private void createTestTable() throws Exception {
		//
		// Catch the error, the table might exist
		//
		try {
			this.stmt.executeUpdate("DROP TABLE IF EXISTS BUG20888_TEST");
		} catch (SQLException SQLE) {
			;
		}

		this.stmt
				.executeUpdate("CREATE TABLE BUG20888_TEST (id int unsigned not null, test varchar(31) NOT NULL default '')");
	}

	public void testPreparedStatement() throws Exception {
		this.pstmt = this.conn
				.prepareStatement("INSERT INTO BUG20888_TEST(id, test) VALUES (?, ?)");
		this.pstmt.setInt(1, ++insertedRow);
		this.pstmt.setString(2, "D\\'artanian ps");
		this.pstmt.executeUpdate();

		this.pstmt.clearParameters();
		
		doRetrieval("Prepared statement test", "D'artanian ps");
	}
	
	public void testStatement() throws Exception {
		this.stmt.executeUpdate("INSERT INTO BUG20888_TEST(id, test) VALUES (" + ++insertedRow + ", 'D\\'artanian')");
		
		doRetrieval("Simple statement test", "D'artanian");
	}
	
	private void doRetrieval(String message, String expectedResult) throws Exception {
		this.rs = this.stmt.executeQuery("SELECT test from BUG20888_TEST WHERE id=" + insertedRow);
		this.rs.next();
		
		String actualResult = this.rs.getString(1);
		
		assertTrue(
			message + " has failed with expected result: '" + expectedResult
			+ "' and actual: '" + actualResult + "'",
			0 == expectedResult.compareTo(actualResult));
	}
	
 }


