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();
}
}
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();
}
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);
}
}
}
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;
}
}
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
}
}
Aggregations