package testsuite.simple;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.Properties;

import testsuite.BaseTestCase;

public class TestBug27412 extends BaseTestCase {

	public TestBug27412(String name) {
		super(name);
		// TODO Auto-generated constructor stub
	}
	/**
	 * Tests functionality of cacheResultSetMetadata in (actually not-prepareable) CS PS. 
	 * 
	 * If you don't know ahead of time whether the SQL statement will be a SELECT or an UPDATE/INSERT,
	 * then you can use the execute(String SQL) method. This method will return true if the SQL query
	 * was a SELECT, or false if it was an UPDATE, INSERT, or DELETE statement.
	 * 
	 * Using latest SVN sources, updated 2007-04-12 
	 * 
	 * We have two problems here, first one (ExecuteQuery vs Execute) you have found. The thing is that Execute tries to figure out wether or not statement returns resultset; obviously it guessed wrong as resultSet is NULL thus raising exception:
java.lang.NullPointerException
	at com.mysql.jdbc.Connection.initializeResultsMetadataFromCache(Connection.java:5891)
cachedMetaData.fields = resultSet.fields;

This is due to problem 2. Putting props.put("useServerPrepStmts", "false"); you made driver guess about "preparability" of statement, and, in this case, driver correctly concludes that preparing this statement is waste of time:
		     10 Connect     root@localhost on test
		     10 Query       SHOW VARIABLES
070412  8:38:03	     10 Query       SHOW COLLATION
070412  8:38:04	     10 Query       SET character_set_results = NULL
		     10 Query       SET autocommit=1
		     10 Query       SET sql_mode='STRICT_TRANS_TABLES'
070412  8:38:31	     10 Query       SELECT 1
070412  8:41:38	     10 Quit       

But, by putting props.put("useServerPrepStmts", "true");, you have another situation:
		     12 Connect     root@localhost on test
		     12 Query       SHOW VARIABLES
		     12 Query       SHOW COLLATION
		     12 Query       SET character_set_results = NULL
		     12 Query       SET autocommit=1
		     12 Query       SET sql_mode='STRICT_TRANS_TABLES'
		     12 Prepare     [1] SELECT 1
		     12 Execute     [1] SELECT 1
		     12 Quit       
and there's no error thrown:
Connected to 5.0.38-log
java.vm.version         : 1.5.0_11-b03
java.vm.vendor          : Sun Microsystems Inc.
java.runtime.version    : 1.5.0_11-b03
os.name                 : Windows XP
os.version              : null
sun.management.compiler : HotSpot Client Compiler

Time: 0,485

OK (1 test)


	 * 
	 * 
	 * @author Tonci Grgin
	 */

	public void testBug27412() throws Exception {
	    	try {
	    		System.out.println("java.vm.version         : " + System.getProperty("java.vm.version"));
	    		System.out.println("java.vm.vendor          : " + System.getProperty("java.vm.vendor"));
	    		System.out.println("java.runtime.version    : " + System.getProperty("java.runtime.version"));
	    		System.out.println("os.name                 : " + System.getProperty("os.name"));
	    		System.out.println("os.version              : " + System.getProperty("os.version "));
	    		System.out.println("sun.management.compiler : " + System.getProperty("sun.management.compiler"));

				Properties props = new Properties();
				props.put("useServerPrepStmts", "false");
				props.put("CachePreparedStatements", "true");
				props.put("cacheResultSetMetadata", "true");
				Connection conn2 = getConnectionWithProps(props);
				PreparedStatement pstm = conn2.prepareStatement("SELECT 1");
				try {
					assertTrue(pstm.execute());
				}
			    catch (Exception e)
			    {
			      e.printStackTrace();
			    }
				pstm.close();
				conn2.close();
		} finally {
			closeMemberJDBCResources();
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		junit.textui.TestRunner.run(TestBug27412.class);
	}

}
