Search in sources :

Example 1 with DbStarter

use of org.h2.server.web.DbStarter in project h2database by h2database.

the class TestServlet method test.

@Override
public void test() throws SQLException {
    if (config.networked || config.memory) {
        return;
    }
    DbStarter listener = new DbStarter();
    TestServletContext context = new TestServletContext();
    String url = getURL("servlet", true);
    context.setInitParameter("db.url", url);
    context.setInitParameter("db.user", getUser());
    context.setInitParameter("db.password", getPassword());
    context.setInitParameter("db.tcpServer", "-tcpPort 8888");
    ServletContextEvent event = new ServletContextEvent(context);
    listener.contextInitialized(event);
    Connection conn1 = listener.getConnection();
    Connection conn1a = (Connection) context.getAttribute("connection");
    assertTrue(conn1 == conn1a);
    Statement stat1 = conn1.createStatement();
    stat1.execute("CREATE TABLE T(ID INT)");
    String u2 = url.substring(url.indexOf("servlet"));
    u2 = "jdbc:h2:tcp://localhost:8888/" + getBaseDir() + "/" + u2;
    Connection conn2 = DriverManager.getConnection(u2, getUser(), getPassword());
    Statement stat2 = conn2.createStatement();
    stat2.execute("SELECT * FROM T");
    stat2.execute("DROP TABLE T");
    assertThrows(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, stat1).execute("SELECT * FROM T");
    conn2.close();
    listener.contextDestroyed(event);
    // listener must be stopped
    assertThrows(ErrorCode.CONNECTION_BROKEN_1, this).getConnection("jdbc:h2:tcp://localhost:8888/" + getBaseDir() + "/servlet", getUser(), getPassword());
    // connection must be closed
    assertThrows(ErrorCode.OBJECT_CLOSED, stat1).execute("SELECT * FROM DUAL");
    deleteDb("servlet");
}
Also used : DbStarter(org.h2.server.web.DbStarter) Statement(java.sql.Statement) Connection(java.sql.Connection) ServletContextEvent(javax.servlet.ServletContextEvent)

Aggregations

Connection (java.sql.Connection)1 Statement (java.sql.Statement)1 ServletContextEvent (javax.servlet.ServletContextEvent)1 DbStarter (org.h2.server.web.DbStarter)1