Search in sources :

Example 21 with Server

use of org.h2.tools.Server in project h2database by h2database.

the class TestWeb method testTools.

private void testTools() throws Exception {
    if (config.memory || config.cipher != null) {
        return;
    }
    deleteDb(getTestName());
    Connection conn = getConnection(getTestName());
    conn.createStatement().execute("create table test(id int) as select 1");
    conn.close();
    Server server = new Server();
    server.setOut(new PrintStream(new ByteArrayOutputStream()));
    server.runTool("-web", "-webPort", "8182", "-properties", "null", "-tcp", "-tcpPort", "9101");
    try {
        String url = "http://localhost:8182";
        WebClient client;
        String result;
        client = new WebClient();
        result = client.get(url);
        client.readSessionId(result);
        result = client.get(url, "tools.jsp");
        FileUtils.delete(getBaseDir() + "/backup.zip");
        result = client.get(url, "tools.do?tool=Backup&args=-dir," + getBaseDir() + ",-db," + getTestName() + ",-file," + getBaseDir() + "/backup.zip");
        deleteDb(getTestName());
        assertTrue(FileUtils.exists(getBaseDir() + "/backup.zip"));
        result = client.get(url, "tools.do?tool=DeleteDbFiles&args=-dir," + getBaseDir() + ",-db," + getTestName());
        String fn = getBaseDir() + "/" + getTestName();
        if (config.mvStore) {
            fn += Constants.SUFFIX_MV_FILE;
        } else {
            fn += Constants.SUFFIX_PAGE_FILE;
        }
        assertFalse(FileUtils.exists(fn));
        result = client.get(url, "tools.do?tool=Restore&args=-dir," + getBaseDir() + ",-db," + getTestName() + ",-file," + getBaseDir() + "/backup.zip");
        assertTrue(FileUtils.exists(fn));
        FileUtils.delete(getBaseDir() + "/web.h2.sql");
        FileUtils.delete(getBaseDir() + "/backup.zip");
        result = client.get(url, "tools.do?tool=Recover&args=-dir," + getBaseDir() + ",-db," + getTestName());
        assertTrue(FileUtils.exists(getBaseDir() + "/" + getTestName() + ".h2.sql"));
        FileUtils.delete(getBaseDir() + "/web.h2.sql");
        result = client.get(url, "tools.do?tool=RunScript&args=-script," + getBaseDir() + "/" + getTestName() + ".h2.sql,-url," + getURL(getTestName(), true) + ",-user," + getUser() + ",-password," + getPassword());
        FileUtils.delete(getBaseDir() + "/" + getTestName() + ".h2.sql");
        assertTrue(FileUtils.exists(fn));
        deleteDb(getTestName());
    } finally {
        server.shutdown();
    }
}
Also used : PrintStream(java.io.PrintStream) Server(org.h2.tools.Server) Connection(java.sql.Connection) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 22 with Server

use of org.h2.tools.Server in project h2database by h2database.

the class TestWeb method testServer.

private void testServer() throws Exception {
    Server server = new Server();
    server.setOut(new PrintStream(new ByteArrayOutputStream()));
    server.runTool("-web", "-webPort", "8182", "-properties", "null", "-tcp", "-tcpPort", "9101");
    try {
        String url = "http://localhost:8182";
        WebClient client;
        String result;
        client = new WebClient();
        client.setAcceptLanguage("de-de,de;q=0.5");
        result = client.get(url);
        client.readSessionId(result);
        result = client.get(url, "login.jsp");
        assertEquals("text/html", client.getContentType());
        assertContains(result, "Einstellung");
        client.get(url, "favicon.ico");
        assertEquals("image/x-icon", client.getContentType());
        client.get(url, "ico_ok.gif");
        assertEquals("image/gif", client.getContentType());
        client.get(url, "tree.js");
        assertEquals("text/javascript", client.getContentType());
        client.get(url, "stylesheet.css");
        assertEquals("text/css", client.getContentType());
        client.get(url, "admin.do");
        try {
            client.get(url, "adminShutdown.do");
        } catch (IOException e) {
            // expected
            Thread.sleep(1000);
        }
    } finally {
        server.shutdown();
    }
    // it should be stopped now
    server = Server.createTcpServer("-tcpPort", "9101");
    server.start();
    server.stop();
}
Also used : PrintStream(java.io.PrintStream) Server(org.h2.tools.Server) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 23 with Server

use of org.h2.tools.Server in project h2database by h2database.

the class TestWeb method testStartWebServerWithConnection.

private void testStartWebServerWithConnection() throws Exception {
    String old = System.getProperty(SysProperties.H2_BROWSER);
    try {
        System.setProperty(SysProperties.H2_BROWSER, "call:" + TestWeb.class.getName() + ".openBrowser");
        Server.openBrowser("testUrl");
        assertEquals("testUrl", lastUrl);
        String oldUrl = lastUrl;
        final Connection conn = getConnection(getTestName());
        Task t = new Task() {

            @Override
            public void call() throws Exception {
                Server.startWebServer(conn, true);
            }
        };
        t.execute();
        for (int i = 0; lastUrl == oldUrl; i++) {
            if (i > 100) {
                throw new Exception("Browser not started");
            }
            Thread.sleep(100);
        }
        String url = lastUrl;
        WebClient client = new WebClient();
        client.readSessionId(url);
        url = client.getBaseUrl(url);
        try {
            client.get(url, "logout.do");
        } catch (ConnectException e) {
        // the server stops on logout
        }
        t.get();
        conn.close();
    } finally {
        if (old != null) {
            System.setProperty(SysProperties.H2_BROWSER, old);
        } else {
            System.clearProperty(SysProperties.H2_BROWSER);
        }
    }
}
Also used : Task(org.h2.util.Task) Connection(java.sql.Connection) ServletException(javax.servlet.ServletException) SQLException(java.sql.SQLException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ConnectException(java.net.ConnectException)

Example 24 with Server

use of org.h2.tools.Server in project h2database by h2database.

the class Server method stopAll.

private void stopAll() {
    Server s = web;
    if (s != null && s.isRunning(false)) {
        s.stop();
        web = null;
    }
    s = tcp;
    if (s != null && s.isRunning(false)) {
        s.stop();
        tcp = null;
    }
    s = pg;
    if (s != null && s.isRunning(false)) {
        s.stop();
        pg = null;
    }
}
Also used : TcpServer(org.h2.server.TcpServer) WebServer(org.h2.server.web.WebServer) PgServer(org.h2.server.pg.PgServer)

Example 25 with Server

use of org.h2.tools.Server in project h2database by h2database.

the class Server method startWebServer.

/**
 * Start a web server and a browser that uses the given connection. The
 * current transaction is preserved. This is specially useful to manually
 * inspect the database when debugging. This method return as soon as the
 * user has disconnected.
 *
 * @param conn the database connection (the database must be open)
 * @param ignoreProperties if {@code true} properties from
 *         {@code .h2.server.properties} will be ignored
 */
public static void startWebServer(Connection conn, boolean ignoreProperties) throws SQLException {
    WebServer webServer = new WebServer();
    String[] args;
    if (ignoreProperties) {
        args = new String[] { "-webPort", "0", "-properties", "null" };
    } else {
        args = new String[] { "-webPort", "0" };
    }
    Server web = new Server(webServer, args);
    web.start();
    Server server = new Server();
    server.web = web;
    webServer.setShutdownHandler(server);
    String url = webServer.addSession(conn);
    try {
        Server.openBrowser(url);
        while (!webServer.isStopped()) {
            Thread.sleep(1000);
        }
    } catch (Exception e) {
    // ignore
    }
}
Also used : TcpServer(org.h2.server.TcpServer) WebServer(org.h2.server.web.WebServer) PgServer(org.h2.server.pg.PgServer) WebServer(org.h2.server.web.WebServer) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException)

Aggregations

Server (org.h2.tools.Server)47 Connection (java.sql.Connection)27 SQLException (java.sql.SQLException)22 Statement (java.sql.Statement)17 PreparedStatement (java.sql.PreparedStatement)15 IOException (java.io.IOException)13 ResultSet (java.sql.ResultSet)10 DbException (org.h2.message.DbException)9 Socket (java.net.Socket)8 Test (org.junit.Test)8 Properties (java.util.Properties)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 File (java.io.File)6 ServerSocket (java.net.ServerSocket)6 PrintStream (java.io.PrintStream)5 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)5 Task (org.h2.util.Task)5 TcpServer (org.h2.server.TcpServer)4 PgServer (org.h2.server.pg.PgServer)4 WebServer (org.h2.server.web.WebServer)4