コード例 #1
0
 /**
  * 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);
   }
 }
コード例 #2
0
  @Override
  protected void before() throws Throwable {
    super.before();

    File hsqlHome = new File("target/temporary/hsqldb/" + name);
    if (hsqlHome.exists()) {
      hsqlHome.delete();
    }
    hsqlHome.mkdirs();

    String dbHome = "file:" + hsqlHome.getAbsolutePath();

    HsqlProperties p = new HsqlProperties();
    p.setProperty("server.database.0", dbHome);
    p.setProperty("server.dbname.0", name);
    p.setProperty("server.port", port);
    server = new Server();
    server.setProperties(p);
    server.setLogWriter(new PrintWriter(System.out));
    server.setErrWriter(new PrintWriter(System.err));
    server.start();
  }