/* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Properties; import java.util.logging.*; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import static org.junit.Assert.*; /** * * @author Administrator */ public class ConnectorJ_Bug_isValid_NPE_Test extends junit.framework.TestCase { private Connection conn; private Logger log = Logger.getLogger( ConnectorJ_Bug_isValid_NPE_Test.class.getName() ); public ConnectorJ_Bug_isValid_NPE_Test() throws Exception { } @BeforeClass public static void setUpClass() throws Exception { } @AfterClass public static void tearDownClass() throws Exception { } @Before public void setUp() { try { Connect(); } catch( Exception ex ) { log.log( Level.SEVERE, "Can't connect.", ex ); fail("Can't connect"); } } @After public void tearDown() { try { conn.close(); } catch( SQLException ex ) { log.log( Level.SEVERE, "Can't close.", ex ); } } /** * Connects to the DB. * @throws java.lang.Exception */ private void Connect() throws Exception { // DB properties Properties propsDbProperties; propsDbProperties = new Properties(); propsDbProperties.setProperty("name","test"); propsDbProperties.setProperty("pass","test"); propsDbProperties.setProperty("host","localhost"); propsDbProperties.setProperty("port","3350"); propsDbProperties.setProperty("schema",""); propsDbProperties.setProperty( "driver", "com.mysql.jdbc.Driver" ); // Assemble URL String sURL = String.format("jdbc:mysql://%s:%s/%s", propsDbProperties.getProperty("host"), propsDbProperties.getProperty("port"), propsDbProperties.getProperty("schema") ); // MySQL - connection try{ Class.forName (propsDbProperties.getProperty("driver")).newInstance (); conn = DriverManager.getConnection( sURL, propsDbProperties.getProperty("name"), propsDbProperties.getProperty("pass") ); log.info("Connection established."); }catch( Exception ex ){ throw new Exception(String.format("Can't connect to s %s / %s / ***.", sURL, propsDbProperties.getProperty("jmeno")), ex); } }// Connect() /** * This test invokes the bug by repeatedly calling isValid(). */ @Test public void testInvokeBug() { try{ PreparedStatement ps = conn.prepareStatement( "SELECT ?" ); for( int i = 0; i < 10000; i++ ) { if( i % 100 == 0 ) log.info( "Round " + i ); this.conn.isValid(3); ps.setInt( 1, 1 ); ResultSet rs = ps.executeQuery(); Thread.sleep( 50 ); rs.close(); } } catch( NullPointerException ex ){ log.log( Level.SEVERE, "And here comes the NPE.", ex ); fail("NPE occured, too bad.."); } catch( SQLException ex ){ log.log( Level.SEVERE, "SQL Exception???", ex ); fail("SQL Exception???"); } catch( InterruptedException ex ){ log.warning( "Thread interrupted in sleep()." ); } }// testInvokeBug() }// class ConnectorJ_Bug_isValid_NPE_Test