use of org.h2.engine.User in project h2database by h2database.
the class TestPgServer method testDateTime.
private void testDateTime() throws SQLException {
if (!getPgJdbcDriver()) {
return;
}
Server server = createPgServer("-pgPort", "5535", "-pgDaemon", "-key", "pgserver", "mem:pgserver");
try {
Properties props = new Properties();
props.setProperty("user", "sa");
props.setProperty("password", "sa");
// force binary
props.setProperty("prepareThreshold", "-1");
Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5535/pgserver", props);
Statement stat = conn.createStatement();
stat.execute("create table test(x1 date, x2 time, x3 timestamp)");
Date[] dates = { null, Date.valueOf("2017-02-20"), Date.valueOf("1970-01-01"), Date.valueOf("1969-12-31"), Date.valueOf("1940-01-10"), Date.valueOf("1950-11-10"), Date.valueOf("1500-01-01") };
Time[] times = { null, Time.valueOf("14:15:16"), Time.valueOf("00:00:00"), Time.valueOf("23:59:59"), Time.valueOf("00:10:59"), Time.valueOf("08:30:42"), Time.valueOf("10:00:00") };
Timestamp[] timestamps = { null, Timestamp.valueOf("2017-02-20 14:15:16.763"), Timestamp.valueOf("1970-01-01 00:00:00"), Timestamp.valueOf("1969-12-31 23:59:59"), Timestamp.valueOf("1940-01-10 00:10:59"), Timestamp.valueOf("1950-11-10 08:30:42.12"), Timestamp.valueOf("1500-01-01 10:00:10") };
int count = dates.length;
PreparedStatement ps = conn.prepareStatement("insert into test values (?,?,?)");
for (int i = 0; i < count; i++) {
ps.setDate(1, dates[i]);
ps.setTime(2, times[i]);
ps.setTimestamp(3, timestamps[i]);
ps.execute();
}
ResultSet rs = stat.executeQuery("select * from test");
for (int i = 0; i < count; i++) {
assertTrue(rs.next());
assertEquals(dates[i], rs.getDate(1));
assertEquals(times[i], rs.getTime(2));
assertEquals(timestamps[i], rs.getTimestamp(3));
}
assertFalse(rs.next());
conn.close();
} finally {
server.stop();
}
}
use of org.h2.engine.User in project h2database by h2database.
the class TestReopen method logDb.
private synchronized void logDb(String fileName) {
writeCount++;
if ((writeCount & (testEvery - 1)) != 0) {
return;
}
if (FileUtils.size(fileName) > maxFileSize) {
// System.out.println(fileName + " " + IOUtils.length(fileName));
return;
}
System.out.println("+ write #" + writeCount + " verify #" + verifyCount);
try {
if (fileName.endsWith(Constants.SUFFIX_PAGE_FILE)) {
IOUtils.copyFiles(fileName, testDatabase + Constants.SUFFIX_PAGE_FILE);
} else {
IOUtils.copyFiles(fileName, testDatabase + Constants.SUFFIX_MV_FILE);
}
verifyCount++;
// avoid using the Engine class to avoid deadlocks
Properties p = new Properties();
String userName = getUser();
p.setProperty("user", userName);
p.setProperty("password", getPassword());
String url = "jdbc:h2:" + testDatabase + ";FILE_LOCK=NO;TRACE_LEVEL_FILE=0";
ConnectionInfo ci = new ConnectionInfo(url, p);
Database database = new Database(ci, null);
// close the database
Session session = database.getSystemSession();
session.prepare("script to '" + testDatabase + ".sql'").query(0);
session.prepare("shutdown immediately").update();
database.removeSession(null);
// everything OK - return
return;
} catch (DbException e) {
SQLException e2 = DbException.toSQLException(e);
int errorCode = e2.getErrorCode();
if (errorCode == ErrorCode.WRONG_USER_OR_PASSWORD) {
return;
} else if (errorCode == ErrorCode.FILE_ENCRYPTION_ERROR_1) {
return;
}
e.printStackTrace(System.out);
throw e;
} catch (Exception e) {
// failed
int errorCode = 0;
if (e instanceof SQLException) {
errorCode = ((SQLException) e).getErrorCode();
}
if (errorCode == ErrorCode.WRONG_USER_OR_PASSWORD) {
return;
} else if (errorCode == ErrorCode.FILE_ENCRYPTION_ERROR_1) {
return;
}
e.printStackTrace(System.out);
}
System.out.println("begin ------------------------------ " + writeCount);
try {
Recover.execute(fileName.substring(0, fileName.lastIndexOf('/')), null);
} catch (SQLException e) {
// ignore
}
testDatabase += "X";
try {
if (fileName.endsWith(Constants.SUFFIX_PAGE_FILE)) {
IOUtils.copyFiles(fileName, testDatabase + Constants.SUFFIX_PAGE_FILE);
} else {
IOUtils.copyFiles(fileName, testDatabase + Constants.SUFFIX_MV_FILE);
}
// avoid using the Engine class to avoid deadlocks
Properties p = new Properties();
String url = "jdbc:h2:" + testDatabase + ";FILE_LOCK=NO";
ConnectionInfo ci = new ConnectionInfo(url, p);
Database database = new Database(ci, null);
// close the database
database.removeSession(null);
} catch (Exception e) {
int errorCode = 0;
if (e instanceof DbException) {
e = ((DbException) e).getSQLException();
errorCode = ((SQLException) e).getErrorCode();
}
if (errorCode == ErrorCode.WRONG_USER_OR_PASSWORD) {
return;
} else if (errorCode == ErrorCode.FILE_ENCRYPTION_ERROR_1) {
return;
}
StringBuilder buff = new StringBuilder();
StackTraceElement[] list = e.getStackTrace();
for (int i = 0; i < 10 && i < list.length; i++) {
buff.append(list[i].toString()).append('\n');
}
String s = buff.toString();
if (!knownErrors.contains(s)) {
System.out.println(writeCount + " code: " + errorCode + " " + e.toString());
e.printStackTrace(System.out);
knownErrors.add(s);
} else {
System.out.println(writeCount + " code: " + errorCode);
}
}
}
use of org.h2.engine.User in project h2database by h2database.
the class TestShell method test.
@Override
public void test() throws Exception {
Shell shell = new Shell();
ByteArrayOutputStream buff = new ByteArrayOutputStream();
shell.setOut(new PrintStream(buff));
shell.runTool("-url", "jdbc:h2:mem:", "-driver", "org.h2.Driver", "-user", "sa", "-password", "sa", "-properties", "null", "-sql", "select 'Hello ' || 'World' as hi");
String s = new String(buff.toByteArray());
assertContains(s, "HI");
assertContains(s, "Hello World");
assertContains(s, "(1 row, ");
shell = new Shell();
buff = new ByteArrayOutputStream();
shell.setOut(new PrintStream(buff));
shell.runTool("-help");
s = new String(buff.toByteArray());
assertContains(s, "Interactive command line tool to access a database using JDBC.");
test(true);
test(false);
}
use of org.h2.engine.User in project h2database by h2database.
the class TestShell method test.
private void test(final boolean commandLineArgs) throws IOException {
PipedInputStream testIn = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream(testIn);
toolOut = new PrintStream(out, true);
out = new PipedOutputStream();
PrintStream testOut = new PrintStream(out, true);
toolIn = new PipedInputStream(out);
Task task = new Task() {
@Override
public void call() throws Exception {
try {
Shell shell = new Shell();
shell.setIn(toolIn);
shell.setOut(toolOut);
shell.setErr(toolOut);
if (commandLineArgs) {
shell.runTool("-url", "jdbc:h2:mem:", "-user", "sa", "-password", "sa");
} else {
shell.runTool();
}
} finally {
toolOut.close();
}
}
};
task.execute();
InputStreamReader reader = new InputStreamReader(testIn);
lineReader = new LineNumberReader(reader);
read("");
read("Welcome to H2 Shell");
read("Exit with");
if (!commandLineArgs) {
read("[Enter]");
testOut.println("jdbc:h2:mem:");
read("URL");
testOut.println("");
read("Driver");
testOut.println("sa");
read("User");
testOut.println("sa");
read("Password");
}
read("Commands are case insensitive");
read("help or ?");
read("list");
read("maxwidth");
read("autocommit");
read("history");
read("quit or exit");
read("");
testOut.println("history");
read("sql> No history");
testOut.println("1");
read("sql> Not found");
testOut.println("select 1 a;");
read("sql> A");
read("1");
read("(1 row,");
testOut.println("history");
read("sql> #1: select 1 a");
read("To re-run a statement, type the number and press and enter");
testOut.println("1");
read("sql> select 1 a");
read("A");
read("1");
read("(1 row,");
testOut.println("select 'x' || space(1000) large, 'y' small;");
read("sql> LARGE");
read("x");
read("(data is partially truncated)");
read("(1 row,");
testOut.println("select x, 's' s from system_range(0, 10001);");
read("sql> X | S");
for (int i = 0; i < 10000; i++) {
read((i + " ").substring(0, 4) + " | s");
}
for (int i = 10000; i <= 10001; i++) {
read((i + " ").substring(0, 5) + " | s");
}
read("(10002 rows,");
testOut.println("select error;");
read("sql> Error:");
if (read("").startsWith("Column \"ERROR\" not found")) {
read("");
}
testOut.println("create table test(id int primary key, name varchar)\n;");
read("sql> ...>");
testOut.println("insert into test values(1, 'Hello');");
read("sql>");
testOut.println("select null n, * from test;");
read("sql> N | ID | NAME");
read("null | 1 | Hello");
read("(1 row,");
// test history
for (int i = 0; i < 30; i++) {
testOut.println("select " + i + " ID from test;");
read("sql> ID");
read("" + i);
read("(1 row,");
}
testOut.println("20");
read("sql> select 10 ID from test");
read("ID");
read("10");
read("(1 row,");
testOut.println("maxwidth");
read("sql> Usage: maxwidth <integer value>");
read("Maximum column width is now 100");
testOut.println("maxwidth 80");
read("sql> Maximum column width is now 80");
testOut.println("autocommit");
read("sql> Usage: autocommit [true|false]");
read("Autocommit is now true");
testOut.println("autocommit false");
read("sql> Autocommit is now false");
testOut.println("autocommit true");
read("sql> Autocommit is now true");
testOut.println("list");
read("sql> Result list mode is now on");
testOut.println("select 1 first, 2 second;");
read("sql> FIRST : 1");
read("SECOND: 2");
read("(1 row, ");
testOut.println("select x from system_range(1, 3);");
read("sql> X: 1");
read("");
read("X: 2");
read("");
read("X: 3");
read("(3 rows, ");
testOut.println("select x, 2 as y from system_range(1, 3) where 1 = 0;");
read("sql> X");
read("Y");
read("(0 rows, ");
testOut.println("list");
read("sql> Result list mode is now off");
testOut.println("help");
read("sql> Commands are case insensitive");
read("help or ?");
read("list");
read("maxwidth");
read("autocommit");
read("history");
read("quit or exit");
read("");
testOut.println("exit");
read("sql>");
task.get();
}
use of org.h2.engine.User 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();
}
}
Aggregations