/** * Specifically, this opens a mem-only DB, populates it, starts a HyperSQL Server to server it, * and opens network JDBC Connection "netConn" to it, * * <p>Invoked before each test*() invocation by JUnit. */ protected void setUp() { try { Connection setupConn = DriverManager.getConnection("jdbc:hsqldb:mem:test", "SA", ""); setupConn.setAutoCommit(false); Statement st = setupConn.createStatement(); st.executeUpdate("SET PASSWORD 'sapwd'"); populate(st); st.close(); setupConn.commit(); setupConn.close(); } catch (SQLException se) { throw new RuntimeException("Failed to set up in-memory database", se); } try { server = new Server(); HsqlProperties properties = new HsqlProperties(); if (System.getProperty("VERBOSE") == null) { server.setLogWriter(null); server.setErrWriter(null); } else { properties.setProperty("server.silent", "false"); properties.setProperty("server.trace", "true"); } properties.setProperty("server.database.0", "mem:test"); properties.setProperty("server.dbname.0", ""); properties.setProperty("server.port", AbstractTestOdbc.portString); server.setProperties(properties); server.start(); try { Thread.sleep(1000); } catch (InterruptedException ie) { } } catch (Exception e) { throw new RuntimeException("Failed to set up in-memory database", e); } if (server.getState() != ServerConstants.SERVER_STATE_ONLINE) { throw new RuntimeException("Server failed to start up"); } try { netConn = DriverManager.getConnection("jdbc:odbc:" + dsnName, "SA", "sapwd"); // netConn.setAutoCommit(false); } catch (SQLException se) { if (se.getMessage().indexOf("No suitable driver") > -1) { throw new RuntimeException( "You must install the native library for Sun's jdbc:odbc " + "JDBC driver"); } if (se.getMessage().indexOf("Data source name not found") > -1) { throw new RuntimeException( "You must configure ODBC DSN '" + dsnName + "' (you may change the name and/or port by setting Java " + "system properties 'test.hsqlodbc.port' or " + "'test.hsqlodbc.dsnname'"); } throw new RuntimeException("Failed to set up JDBC/ODBC network connection", se); } }
/** * JUnit convention for cleanup. * * <p>Called after each test*() method. */ protected void tearDown() throws SQLException { if (netConn != null) { netConn.rollback(); // Necessary to prevent the SHUTDOWN command from causing implied // transaction control commands, which will not be able to // complete. netConn.createStatement().executeUpdate("SHUTDOWN"); netConn.close(); netConn = null; try { Thread.sleep(1000); } catch (InterruptedException ie) { } } if (server != null && server.getState() != ServerConstants.SERVER_STATE_SHUTDOWN) { throw new RuntimeException("Server failed to shut down"); } }