提交 b063c9cf authored 作者: Thomas Mueller's avatar Thomas Mueller

Test need to clean up the file system after running.

上级 43fde846
...@@ -61,13 +61,13 @@ public class TestAutoReconnect extends TestBase implements DatabaseEventListener ...@@ -61,13 +61,13 @@ public class TestAutoReconnect extends TestBase implements DatabaseEventListener
private void testReconnect() throws Exception { private void testReconnect() throws Exception {
deleteDb("autoReconnect"); deleteDb("autoReconnect");
if (autoServer) { if (autoServer) {
url = "jdbc:h2:" + baseDir + "/autoReconnect;" + url = "jdbc:h2:" + getBaseDir() + "/autoReconnect;" +
"FILE_LOCK=SOCKET;" + "FILE_LOCK=SOCKET;" +
"AUTO_SERVER=TRUE;OPEN_NEW=TRUE"; "AUTO_SERVER=TRUE;OPEN_NEW=TRUE";
restart(); restart();
} else { } else {
server = Server.createTcpServer("-tcpPort", "8181").start(); server = Server.createTcpServer("-tcpPort", "8181").start();
url = "jdbc:h2:tcp://localhost:8181/" + baseDir + "/autoReconnect;" + url = "jdbc:h2:tcp://localhost:8181/" + getBaseDir() + "/autoReconnect;" +
"FILE_LOCK=SOCKET;AUTO_RECONNECT=TRUE"; "FILE_LOCK=SOCKET;AUTO_RECONNECT=TRUE";
} }
......
...@@ -30,6 +30,9 @@ public class TestExit extends TestBase implements DatabaseEventListener { ...@@ -30,6 +30,9 @@ public class TestExit extends TestBase implements DatabaseEventListener {
if (config.codeCoverage || config.networked) { if (config.codeCoverage || config.networked) {
return; return;
} }
if (getBaseDir().indexOf(':') > 0) {
return;
}
deleteDb("exit"); deleteDb("exit");
String selfDestruct = SelfDestructor.getPropertyString(60); String selfDestruct = SelfDestructor.getPropertyString(60);
String[] procDef = { "java", selfDestruct, String[] procDef = { "java", selfDestruct,
...@@ -88,11 +91,11 @@ public class TestExit extends TestBase implements DatabaseEventListener { ...@@ -88,11 +91,11 @@ public class TestExit extends TestBase implements DatabaseEventListener {
String url = ""; String url = "";
switch (action) { switch (action) {
case OPEN_WITH_CLOSE_ON_EXIT: case OPEN_WITH_CLOSE_ON_EXIT:
url = "jdbc:h2:" + baseDir + "/exit;database_event_listener='" + getClass().getName() url = "jdbc:h2:" + getBaseDir() + "/exit;database_event_listener='" + getClass().getName()
+ "';db_close_on_exit=true"; + "';db_close_on_exit=true";
break; break;
case OPEN_WITHOUT_CLOSE_ON_EXIT: case OPEN_WITHOUT_CLOSE_ON_EXIT:
url = "jdbc:h2:" + baseDir + "/exit;database_event_listener='" + getClass().getName() url = "jdbc:h2:" + getBaseDir() + "/exit;database_event_listener='" + getClass().getName()
+ "';db_close_on_exit=false"; + "';db_close_on_exit=false";
break; break;
default: default:
...@@ -124,7 +127,7 @@ public class TestExit extends TestBase implements DatabaseEventListener { ...@@ -124,7 +127,7 @@ public class TestExit extends TestBase implements DatabaseEventListener {
} }
private static File getClosedFile() { private static File getClosedFile() {
return new File(baseDir + "/closed.txt"); return new File(TestBase.BASE_TEST_DIR + "/closed.txt");
} }
public void setProgress(int state, String name, int x, int max) { public void setProgress(int state, String name, int x, int max) {
......
...@@ -18,7 +18,6 @@ import org.h2.test.TestBase; ...@@ -18,7 +18,6 @@ import org.h2.test.TestBase;
*/ */
public class TestFileLock extends TestBase implements Runnable { public class TestFileLock extends TestBase implements Runnable {
private static final String FILE = baseDir + "/test.lock";
private static volatile int locks; private static volatile int locks;
private static volatile boolean stop; private static volatile boolean stop;
private TestBase base; private TestBase base;
...@@ -34,6 +33,10 @@ public class TestFileLock extends TestBase implements Runnable { ...@@ -34,6 +33,10 @@ public class TestFileLock extends TestBase implements Runnable {
this.allowSockets = allowSockets; this.allowSockets = allowSockets;
} }
private String getFile() {
return getBaseDir() + "/test.lock";
}
/** /**
* Run just this test. * Run just this test.
* *
...@@ -44,6 +47,9 @@ public class TestFileLock extends TestBase implements Runnable { ...@@ -44,6 +47,9 @@ public class TestFileLock extends TestBase implements Runnable {
} }
public void test() throws Exception { public void test() throws Exception {
if (!getFile().startsWith(TestBase.BASE_TEST_DIR)) {
return;
}
testFutureModificationDate(); testFutureModificationDate();
testSimple(); testSimple();
test(false); test(false);
...@@ -51,18 +57,18 @@ public class TestFileLock extends TestBase implements Runnable { ...@@ -51,18 +57,18 @@ public class TestFileLock extends TestBase implements Runnable {
} }
private void testFutureModificationDate() throws Exception { private void testFutureModificationDate() throws Exception {
File f = new File(FILE); File f = new File(getFile());
f.delete(); f.delete();
f.createNewFile(); f.createNewFile();
f.setLastModified(System.currentTimeMillis() + 10000); f.setLastModified(System.currentTimeMillis() + 10000);
FileLock lock = new FileLock(new TraceSystem(null), FILE, Constants.LOCK_SLEEP); FileLock lock = new FileLock(new TraceSystem(null), getFile(), Constants.LOCK_SLEEP);
lock.lock(FileLock.LOCK_FILE); lock.lock(FileLock.LOCK_FILE);
lock.unlock(); lock.unlock();
} }
private void testSimple() { private void testSimple() {
FileLock lock1 = new FileLock(new TraceSystem(null), FILE, Constants.LOCK_SLEEP); FileLock lock1 = new FileLock(new TraceSystem(null), getFile(), Constants.LOCK_SLEEP);
FileLock lock2 = new FileLock(new TraceSystem(null), FILE, Constants.LOCK_SLEEP); FileLock lock2 = new FileLock(new TraceSystem(null), getFile(), Constants.LOCK_SLEEP);
lock1.lock(FileLock.LOCK_FILE); lock1.lock(FileLock.LOCK_FILE);
try { try {
lock2.lock(FileLock.LOCK_FILE); lock2.lock(FileLock.LOCK_FILE);
...@@ -71,7 +77,7 @@ public class TestFileLock extends TestBase implements Runnable { ...@@ -71,7 +77,7 @@ public class TestFileLock extends TestBase implements Runnable {
// expected // expected
} }
lock1.unlock(); lock1.unlock();
lock2 = new FileLock(new TraceSystem(null), FILE, Constants.LOCK_SLEEP); lock2 = new FileLock(new TraceSystem(null), getFile(), Constants.LOCK_SLEEP);
lock2.lock(FileLock.LOCK_FILE); lock2.lock(FileLock.LOCK_FILE);
lock2.unlock(); lock2.unlock();
} }
...@@ -80,7 +86,7 @@ public class TestFileLock extends TestBase implements Runnable { ...@@ -80,7 +86,7 @@ public class TestFileLock extends TestBase implements Runnable {
int threadCount = getSize(3, 5); int threadCount = getSize(3, 5);
wait = getSize(20, 200); wait = getSize(20, 200);
Thread[] threads = new Thread[threadCount]; Thread[] threads = new Thread[threadCount];
new File(FILE).delete(); new File(getFile()).delete();
for (int i = 0; i < threadCount; i++) { for (int i = 0; i < threadCount; i++) {
threads[i] = new Thread(new TestFileLock(this, allowSocketsLock)); threads[i] = new Thread(new TestFileLock(this, allowSocketsLock));
threads[i].start(); threads[i].start();
...@@ -99,7 +105,7 @@ public class TestFileLock extends TestBase implements Runnable { ...@@ -99,7 +105,7 @@ public class TestFileLock extends TestBase implements Runnable {
public void run() { public void run() {
FileLock lock = null; FileLock lock = null;
while (!stop) { while (!stop) {
lock = new FileLock(new TraceSystem(null), FILE, 100); lock = new FileLock(new TraceSystem(null), getFile(), 100);
try { try {
lock.lock(allowSockets ? FileLock.LOCK_SOCKET : FileLock.LOCK_FILE); lock.lock(allowSockets ? FileLock.LOCK_SOCKET : FileLock.LOCK_FILE);
base.trace(lock + " locked"); base.trace(lock + " locked");
......
...@@ -6,7 +6,6 @@ ...@@ -6,7 +6,6 @@
*/ */
package org.h2.test.unit; package org.h2.test.unit;
import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.sql.Connection; import java.sql.Connection;
...@@ -18,6 +17,7 @@ import java.sql.Statement; ...@@ -18,6 +17,7 @@ import java.sql.Statement;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import org.h2.jdbc.JdbcConnection; import org.h2.jdbc.JdbcConnection;
import org.h2.store.fs.FileSystem;
import org.h2.test.TestBase; import org.h2.test.TestBase;
import org.h2.util.SortedProperties; import org.h2.util.SortedProperties;
...@@ -68,11 +68,12 @@ public class TestFileLockSerialized extends TestBase { ...@@ -68,11 +68,12 @@ public class TestFileLockSerialized extends TestBase {
testKillWriter(); testKillWriter();
println("testConcurrentReadWrite"); println("testConcurrentReadWrite");
testConcurrentReadWrite(); testConcurrentReadWrite();
deleteDb("fileLockSerialized");
} }
private void testSequenceFlush() throws Exception { private void testSequenceFlush() throws Exception {
deleteDb("fileLockSerialized"); deleteDb("fileLockSerialized");
String url = "jdbc:h2:" + baseDir + "/fileLockSerialized;FILE_LOCK=SERIALIZED;OPEN_NEW=TRUE"; String url = "jdbc:h2:" + getBaseDir() + "/fileLockSerialized;FILE_LOCK=SERIALIZED;OPEN_NEW=TRUE";
ResultSet rs; ResultSet rs;
Connection conn1 = DriverManager.getConnection(url); Connection conn1 = DriverManager.getConnection(url);
Statement stat1 = conn1.createStatement(); Statement stat1 = conn1.createStatement();
...@@ -92,7 +93,7 @@ public class TestFileLockSerialized extends TestBase { ...@@ -92,7 +93,7 @@ public class TestFileLockSerialized extends TestBase {
private void testThreeMostlyReaders(final boolean write) throws Exception { private void testThreeMostlyReaders(final boolean write) throws Exception {
boolean longRun = false; boolean longRun = false;
deleteDb("fileLockSerialized"); deleteDb("fileLockSerialized");
final String url = "jdbc:h2:" + baseDir + "/fileLockSerialized;FILE_LOCK=SERIALIZED;OPEN_NEW=TRUE"; final String url = "jdbc:h2:" + getBaseDir() + "/fileLockSerialized;FILE_LOCK=SERIALIZED;OPEN_NEW=TRUE";
Connection conn = DriverManager.getConnection(url); Connection conn = DriverManager.getConnection(url);
conn.createStatement().execute("create table test(id int) as select 1"); conn.createStatement().execute("create table test(id int) as select 1");
...@@ -145,7 +146,7 @@ public class TestFileLockSerialized extends TestBase { ...@@ -145,7 +146,7 @@ public class TestFileLockSerialized extends TestBase {
private void testTwoReaders() throws Exception { private void testTwoReaders() throws Exception {
deleteDb("fileLockSerialized"); deleteDb("fileLockSerialized");
String url = "jdbc:h2:" + baseDir + "/fileLockSerialized;FILE_LOCK=SERIALIZED;OPEN_NEW=TRUE"; String url = "jdbc:h2:" + getBaseDir() + "/fileLockSerialized;FILE_LOCK=SERIALIZED;OPEN_NEW=TRUE";
Connection conn1 = DriverManager.getConnection(url); Connection conn1 = DriverManager.getConnection(url);
conn1.createStatement().execute("create table test(id int)"); conn1.createStatement().execute("create table test(id int)");
Connection conn2 = DriverManager.getConnection(url); Connection conn2 = DriverManager.getConnection(url);
...@@ -159,7 +160,7 @@ public class TestFileLockSerialized extends TestBase { ...@@ -159,7 +160,7 @@ public class TestFileLockSerialized extends TestBase {
private void testTwoWriters() throws Exception { private void testTwoWriters() throws Exception {
deleteDb("fileLockSerialized"); deleteDb("fileLockSerialized");
String url = "jdbc:h2:" + baseDir + "/fileLockSerialized"; String url = "jdbc:h2:" + getBaseDir() + "/fileLockSerialized";
final String writeUrl = url + ";FILE_LOCK=SERIALIZED;OPEN_NEW=TRUE"; final String writeUrl = url + ";FILE_LOCK=SERIALIZED;OPEN_NEW=TRUE";
final boolean[] stop = { false }; final boolean[] stop = { false };
Connection conn = DriverManager.getConnection(writeUrl, "sa", "sa"); Connection conn = DriverManager.getConnection(writeUrl, "sa", "sa");
...@@ -197,14 +198,14 @@ public class TestFileLockSerialized extends TestBase { ...@@ -197,14 +198,14 @@ public class TestFileLockSerialized extends TestBase {
private void testPendingWrite() throws Exception { private void testPendingWrite() throws Exception {
deleteDb("fileLockSerialized"); deleteDb("fileLockSerialized");
String url = "jdbc:h2:" + baseDir + "/fileLockSerialized"; String url = "jdbc:h2:" + getBaseDir() + "/fileLockSerialized";
String writeUrl = url + ";FILE_LOCK=SERIALIZED;OPEN_NEW=TRUE;WRITE_DELAY=0"; String writeUrl = url + ";FILE_LOCK=SERIALIZED;OPEN_NEW=TRUE;WRITE_DELAY=0";
Connection conn = DriverManager.getConnection(writeUrl, "sa", "sa"); Connection conn = DriverManager.getConnection(writeUrl, "sa", "sa");
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
stat.execute("create table test(id int primary key)"); stat.execute("create table test(id int primary key)");
Thread.sleep(100); Thread.sleep(100);
String propFile = baseDir + "/fileLockSerialized.lock.db"; String propFile = getBaseDir() + "/fileLockSerialized.lock.db";
SortedProperties p = SortedProperties.loadProperties(propFile); SortedProperties p = SortedProperties.loadProperties(propFile);
p.setProperty("changePending", "true"); p.setProperty("changePending", "true");
p.setProperty("modificationDataId", "1000"); p.setProperty("modificationDataId", "1000");
...@@ -221,7 +222,7 @@ public class TestFileLockSerialized extends TestBase { ...@@ -221,7 +222,7 @@ public class TestFileLockSerialized extends TestBase {
private void testKillWriter() throws Exception { private void testKillWriter() throws Exception {
deleteDb("fileLockSerialized"); deleteDb("fileLockSerialized");
String url = "jdbc:h2:" + baseDir + "/fileLockSerialized"; String url = "jdbc:h2:" + getBaseDir() + "/fileLockSerialized";
String writeUrl = url + ";FILE_LOCK=SERIALIZED;OPEN_NEW=TRUE;WRITE_DELAY=0"; String writeUrl = url + ";FILE_LOCK=SERIALIZED;OPEN_NEW=TRUE;WRITE_DELAY=0";
Connection conn = DriverManager.getConnection(writeUrl, "sa", "sa"); Connection conn = DriverManager.getConnection(writeUrl, "sa", "sa");
...@@ -252,7 +253,7 @@ public class TestFileLockSerialized extends TestBase { ...@@ -252,7 +253,7 @@ public class TestFileLockSerialized extends TestBase {
private void testConcurrentReadWrite() throws Exception { private void testConcurrentReadWrite() throws Exception {
deleteDb("fileLockSerialized"); deleteDb("fileLockSerialized");
String url = "jdbc:h2:" + baseDir + "/fileLockSerialized"; String url = "jdbc:h2:" + getBaseDir() + "/fileLockSerialized";
String writeUrl = url + ";FILE_LOCK=SERIALIZED;OPEN_NEW=TRUE"; String writeUrl = url + ";FILE_LOCK=SERIALIZED;OPEN_NEW=TRUE";
// ;TRACE_LEVEL_SYSTEM_OUT=3 // ;TRACE_LEVEL_SYSTEM_OUT=3
// String readUrl = writeUrl + ";ACCESS_MODE_DATA=R"; // String readUrl = writeUrl + ";ACCESS_MODE_DATA=R";
...@@ -326,7 +327,7 @@ public class TestFileLockSerialized extends TestBase { ...@@ -326,7 +327,7 @@ public class TestFileLockSerialized extends TestBase {
private void testAutoIncrement(final int waitTime, int howManyThreads, int runTime) throws Exception { private void testAutoIncrement(final int waitTime, int howManyThreads, int runTime) throws Exception {
println("testAutoIncrement waitTime: " + waitTime + " howManyThreads: " + howManyThreads + " runTime: " + runTime); println("testAutoIncrement waitTime: " + waitTime + " howManyThreads: " + howManyThreads + " runTime: " + runTime);
deleteDb("fileLockSerialized"); deleteDb("fileLockSerialized");
final String url = "jdbc:h2:" + baseDir + "/fileLockSerialized;FILE_LOCK=SERIALIZED;OPEN_NEW=TRUE;" + final String url = "jdbc:h2:" + getBaseDir() + "/fileLockSerialized;FILE_LOCK=SERIALIZED;OPEN_NEW=TRUE;" +
"AUTO_RECONNECT=TRUE;MAX_LENGTH_INPLACE_LOB=8192;COMPRESS_LOB=DEFLATE;CACHE_SIZE=65536"; "AUTO_RECONNECT=TRUE;MAX_LENGTH_INPLACE_LOB=8192;COMPRESS_LOB=DEFLATE;CACHE_SIZE=65536";
Connection conn = DriverManager.getConnection(url); Connection conn = DriverManager.getConnection(url);
...@@ -393,7 +394,7 @@ public class TestFileLockSerialized extends TestBase { ...@@ -393,7 +394,7 @@ public class TestFileLockSerialized extends TestBase {
private void testConcurrentUpdates(final int waitTime, int howManyThreads, int runTime) throws Exception { private void testConcurrentUpdates(final int waitTime, int howManyThreads, int runTime) throws Exception {
println("testConcurrentUpdates waitTime: " + waitTime + " howManyThreads: " + howManyThreads + " runTime: " + runTime); println("testConcurrentUpdates waitTime: " + waitTime + " howManyThreads: " + howManyThreads + " runTime: " + runTime);
deleteDb("fileLockSerialized"); deleteDb("fileLockSerialized");
final String url = "jdbc:h2:" + baseDir + "/fileLockSerialized;FILE_LOCK=SERIALIZED;OPEN_NEW=TRUE;" + final String url = "jdbc:h2:" + getBaseDir() + "/fileLockSerialized;FILE_LOCK=SERIALIZED;OPEN_NEW=TRUE;" +
"AUTO_RECONNECT=TRUE;MAX_LENGTH_INPLACE_LOB=8192;COMPRESS_LOB=DEFLATE;CACHE_SIZE=65536"; "AUTO_RECONNECT=TRUE;MAX_LENGTH_INPLACE_LOB=8192;COMPRESS_LOB=DEFLATE;CACHE_SIZE=65536";
Connection conn = DriverManager.getConnection(url); Connection conn = DriverManager.getConnection(url);
...@@ -464,7 +465,7 @@ public class TestFileLockSerialized extends TestBase { ...@@ -464,7 +465,7 @@ public class TestFileLockSerialized extends TestBase {
private void testCheckpointInUpdateRaceCondition() throws Exception { private void testCheckpointInUpdateRaceCondition() throws Exception {
boolean longRun = false; boolean longRun = false;
deleteDb("fileLockSerialized"); deleteDb("fileLockSerialized");
String url = "jdbc:h2:" + baseDir + "/fileLockSerialized;FILE_LOCK=SERIALIZED;OPEN_NEW=TRUE"; String url = "jdbc:h2:" + getBaseDir() + "/fileLockSerialized;FILE_LOCK=SERIALIZED;OPEN_NEW=TRUE";
Connection conn = DriverManager.getConnection(url); Connection conn = DriverManager.getConnection(url);
conn.createStatement().execute("create table test(id int)"); conn.createStatement().execute("create table test(id int)");
...@@ -484,7 +485,7 @@ public class TestFileLockSerialized extends TestBase { ...@@ -484,7 +485,7 @@ public class TestFileLockSerialized extends TestBase {
private void testCache() throws Exception { private void testCache() throws Exception {
deleteDb("fileLockSerialized"); deleteDb("fileLockSerialized");
String urlShared = "jdbc:h2:" + baseDir + "/fileLockSerialized;FILE_LOCK=SERIALIZED"; String urlShared = "jdbc:h2:" + getBaseDir() + "/fileLockSerialized;FILE_LOCK=SERIALIZED";
Connection connShared1 = DriverManager.getConnection(urlShared); Connection connShared1 = DriverManager.getConnection(urlShared);
Statement statement1 = connShared1.createStatement(); Statement statement1 = connShared1.createStatement();
...@@ -515,7 +516,7 @@ public class TestFileLockSerialized extends TestBase { ...@@ -515,7 +516,7 @@ public class TestFileLockSerialized extends TestBase {
private void testWrongDatabaseInstanceOnReconnect() throws Exception { private void testWrongDatabaseInstanceOnReconnect() throws Exception {
deleteDb("fileLockSerialized"); deleteDb("fileLockSerialized");
String urlShared = "jdbc:h2:" + baseDir + "/fileLockSerialized;FILE_LOCK=SERIALIZED"; String urlShared = "jdbc:h2:" + getBaseDir() + "/fileLockSerialized;FILE_LOCK=SERIALIZED";
String urlForNew = urlShared + ";OPEN_NEW=TRUE"; String urlForNew = urlShared + ";OPEN_NEW=TRUE";
Connection connShared1 = DriverManager.getConnection(urlShared); Connection connShared1 = DriverManager.getConnection(urlShared);
...@@ -535,7 +536,7 @@ public class TestFileLockSerialized extends TestBase { ...@@ -535,7 +536,7 @@ public class TestFileLockSerialized extends TestBase {
deleteDb("fileLockSerialized"); deleteDb("fileLockSerialized");
int cacheSizeKb = withCache ? 5000 : 0; int cacheSizeKb = withCache ? 5000 : 0;
final String url = "jdbc:h2:" + baseDir + "/fileLockSerialized;FILE_LOCK=SERIALIZED;OPEN_NEW=TRUE;CACHE_SIZE=" + cacheSizeKb; final String url = "jdbc:h2:" + getBaseDir() + "/fileLockSerialized;FILE_LOCK=SERIALIZED;OPEN_NEW=TRUE;CACHE_SIZE=" + cacheSizeKb;
final boolean[] importFinished = { false }; final boolean[] importFinished = { false };
final Exception[] ex = { null }; final Exception[] ex = { null };
final Thread importUpdate = new Thread() { final Thread importUpdate = new Thread() {
...@@ -599,17 +600,20 @@ public class TestFileLockSerialized extends TestBase { ...@@ -599,17 +600,20 @@ public class TestFileLockSerialized extends TestBase {
// without serialized // without serialized
String url; String url;
url = "jdbc:h2:" + baseDir + "/fileLockSerialized"; url = "jdbc:h2:" + getBaseDir() + "/fileLockSerialized";
Connection conn = DriverManager.getConnection(url); Connection conn = DriverManager.getConnection(url);
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
stat.execute("create table test(id int)"); stat.execute("create table test(id int)");
stat.execute("insert into test values(0)"); stat.execute("insert into test values(0)");
conn.close(); conn.close();
List<String> filesWithoutSerialized = Arrays.asList(new File(baseDir).list());
FileSystem fs = FileSystem.getInstance(getBaseDir());
List<String> filesWithoutSerialized = Arrays.asList(fs.listFiles(getBaseDir()));
deleteDb("fileLockSerialized"); deleteDb("fileLockSerialized");
// with serialized // with serialized
url = "jdbc:h2:" + baseDir + "/fileLockSerialized;FILE_LOCK=SERIALIZED"; url = "jdbc:h2:" + getBaseDir() + "/fileLockSerialized;FILE_LOCK=SERIALIZED";
conn = DriverManager.getConnection(url); conn = DriverManager.getConnection(url);
stat = conn.createStatement(); stat = conn.createStatement();
stat.execute("create table test(id int)"); stat.execute("create table test(id int)");
...@@ -617,7 +621,7 @@ public class TestFileLockSerialized extends TestBase { ...@@ -617,7 +621,7 @@ public class TestFileLockSerialized extends TestBase {
stat.execute("insert into test values(0)"); stat.execute("insert into test values(0)");
conn.close(); conn.close();
List<String> filesWithSerialized = Arrays.asList(new File(baseDir).list()); List<String> filesWithSerialized = Arrays.asList(fs.listFiles(getBaseDir()));
if (filesWithoutSerialized.size() != filesWithSerialized.size()) { if (filesWithoutSerialized.size() != filesWithSerialized.size()) {
for (int i = 0; i < filesWithoutSerialized.size(); i++) { for (int i = 0; i < filesWithoutSerialized.size(); i++) {
if (!filesWithSerialized.contains(filesWithoutSerialized.get(i))) { if (!filesWithSerialized.contains(filesWithoutSerialized.get(i))) {
......
...@@ -22,6 +22,7 @@ import org.h2.store.fs.FileObject; ...@@ -22,6 +22,7 @@ import org.h2.store.fs.FileObject;
import org.h2.store.fs.FileSystem; import org.h2.store.fs.FileSystem;
import org.h2.store.fs.FileSystemMemory; import org.h2.store.fs.FileSystemMemory;
import org.h2.test.TestBase; import org.h2.test.TestBase;
import org.h2.util.IOUtils;
/** /**
* Tests various file system. * Tests various file system.
...@@ -41,9 +42,9 @@ public class TestFileSystem extends TestBase { ...@@ -41,9 +42,9 @@ public class TestFileSystem extends TestBase {
testDatabaseInMemFileSys(); testDatabaseInMemFileSys();
testDatabaseInJar(); testDatabaseInJar();
// set default part size to 1 << 10 // set default part size to 1 << 10
FileSystem.getInstance("split:10:" + baseDir + "/fs"); FileSystem.getInstance("split:10:" + getBaseDir() + "/fs");
testFileSystem("split:" + baseDir + "/fs"); testFileSystem("split:" + getBaseDir() + "/fs");
testFileSystem(baseDir + "/fs"); testFileSystem(getBaseDir() + "/fs");
testFileSystem(FileSystemMemory.PREFIX); testFileSystem(FileSystemMemory.PREFIX);
FileSystemDatabase fs = FileSystemDatabase.register("jdbc:h2:mem:fs"); FileSystemDatabase fs = FileSystemDatabase.register("jdbc:h2:mem:fs");
// testFileSystem("jdbc:h2:mem:fs;TRACE_LEVEL_FILE=3"); // testFileSystem("jdbc:h2:mem:fs;TRACE_LEVEL_FILE=3");
...@@ -51,29 +52,35 @@ public class TestFileSystem extends TestBase { ...@@ -51,29 +52,35 @@ public class TestFileSystem extends TestBase {
testFileSystem(FileSystemMemory.PREFIX_LZF); testFileSystem(FileSystemMemory.PREFIX_LZF);
testUserHome(); testUserHome();
fs.unregister(); fs.unregister();
IOUtils.delete(getBaseDir() + "/fs");
} }
private void testDatabaseInMemFileSys() throws SQLException { private void testDatabaseInMemFileSys() throws SQLException {
org.h2.Driver.load(); org.h2.Driver.load();
deleteDb("fsMem"); deleteDb("fsMem");
String url = "jdbc:h2:" + baseDir + "/fsMem"; String url = "jdbc:h2:" + getBaseDir() + "/fsMem";
Connection conn = DriverManager.getConnection(url, "sa", "sa"); Connection conn = DriverManager.getConnection(url, "sa", "sa");
conn.createStatement().execute("CREATE TABLE TEST AS SELECT * FROM DUAL"); conn.createStatement().execute("CREATE TABLE TEST AS SELECT * FROM DUAL");
conn.createStatement().execute("BACKUP TO '" + baseDir + "/fsMem.zip'"); conn.createStatement().execute("BACKUP TO '" + getBaseDir() + "/fsMem.zip'");
conn.close(); conn.close();
org.h2.tools.Restore.main("-file", baseDir + "/fsMem.zip", "-dir", "memFS:"); org.h2.tools.Restore.main("-file", getBaseDir() + "/fsMem.zip", "-dir", "memFS:");
conn = DriverManager.getConnection("jdbc:h2:memFS:fsMem", "sa", "sa"); conn = DriverManager.getConnection("jdbc:h2:memFS:fsMem", "sa", "sa");
ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM TEST"); ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM TEST");
rs.close(); rs.close();
conn.close(); conn.close();
deleteDb("fsMem");
IOUtils.delete(getBaseDir() + "/fsMem.zip");
} }
private void testDatabaseInJar() throws SQLException { private void testDatabaseInJar() throws SQLException {
if (getBaseDir().indexOf(':') > 0) {
return;
}
if (config.networked) { if (config.networked) {
return; return;
} }
org.h2.Driver.load(); org.h2.Driver.load();
String url = "jdbc:h2:" + baseDir + "/fsJar"; String url = "jdbc:h2:" + getBaseDir() + "/fsJar";
Connection conn = DriverManager.getConnection(url, "sa", "sa"); Connection conn = DriverManager.getConnection(url, "sa", "sa");
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
stat.execute("create table test(id int primary key, name varchar, b blob, c clob)"); stat.execute("create table test(id int primary key, name varchar, b blob, c clob)");
...@@ -86,18 +93,18 @@ public class TestFileSystem extends TestBase { ...@@ -86,18 +93,18 @@ public class TestFileSystem extends TestBase {
conn.close(); conn.close();
conn = DriverManager.getConnection(url, "sa", "sa"); conn = DriverManager.getConnection(url, "sa", "sa");
stat = conn.createStatement(); stat = conn.createStatement();
stat.execute("backup to '" + baseDir + "/fsJar.zip'"); stat.execute("backup to '" + getBaseDir() + "/fsJar.zip'");
conn.close(); conn.close();
deleteDb("fsJar"); deleteDb("fsJar");
FileSystem fs = FileSystem.getInstance("zip:" + baseDir + "/fsJar.zip"); FileSystem fs = FileSystem.getInstance("zip:" + getBaseDir() + "/fsJar.zip");
for (String f : fs.listFiles("zip:" + baseDir + "/fsJar.zip")) { for (String f : fs.listFiles("zip:" + getBaseDir() + "/fsJar.zip")) {
assertTrue(fs.isAbsolute(f)); assertTrue(fs.isAbsolute(f));
assertTrue(!fs.isDirectory(f)); assertTrue(!fs.isDirectory(f));
assertTrue(fs.length(f) > 0); assertTrue(fs.length(f) > 0);
assertTrue(f.endsWith(fs.getFileName(f))); assertTrue(f.endsWith(fs.getFileName(f)));
} }
String urlJar = "jdbc:h2:zip:" + baseDir + "/fsJar.zip!/fsJar"; String urlJar = "jdbc:h2:zip:" + getBaseDir() + "/fsJar.zip!/fsJar";
conn = DriverManager.getConnection(urlJar, "sa", "sa"); conn = DriverManager.getConnection(urlJar, "sa", "sa");
stat = conn.createStatement(); stat = conn.createStatement();
rs = stat.executeQuery("select * from test"); rs = stat.executeQuery("select * from test");
...@@ -112,6 +119,7 @@ public class TestFileSystem extends TestBase { ...@@ -112,6 +119,7 @@ public class TestFileSystem extends TestBase {
assertEquals(s1, s2); assertEquals(s1, s2);
assertFalse(rs.next()); assertFalse(rs.next());
conn.close(); conn.close();
IOUtils.delete(getBaseDir() + "/fsJar.zip");
} }
private void testUserHome() { private void testUserHome() {
...@@ -176,7 +184,7 @@ public class TestFileSystem extends TestBase { ...@@ -176,7 +184,7 @@ public class TestFileSystem extends TestBase {
assertTrue(fs.tryDelete(fsBase + "/test2")); assertTrue(fs.tryDelete(fsBase + "/test2"));
fs.delete(fsBase + "/test"); fs.delete(fsBase + "/test");
if (!fsBase.startsWith(FileSystemMemory.PREFIX) && !fsBase.startsWith(FileSystemMemory.PREFIX_LZF)) { if (fsBase.indexOf(FileSystemMemory.PREFIX) < 0 && fsBase.indexOf(FileSystemMemory.PREFIX_LZF) < 0) {
fs.createDirs(fsBase + "/testDir/test"); fs.createDirs(fsBase + "/testDir/test");
assertTrue(fs.isDirectory(fsBase + "/testDir")); assertTrue(fs.isDirectory(fsBase + "/testDir"));
if (!fsBase.startsWith("jdbc:")) { if (!fsBase.startsWith("jdbc:")) {
...@@ -189,7 +197,8 @@ public class TestFileSystem extends TestBase { ...@@ -189,7 +197,8 @@ public class TestFileSystem extends TestBase {
private void testRandomAccess(String fsBase) throws Exception { private void testRandomAccess(String fsBase) throws Exception {
FileSystem fs = FileSystem.getInstance(fsBase); FileSystem fs = FileSystem.getInstance(fsBase);
String s = fs.createTempFile(fsBase + "/temp", ".tmp", false, false); String s = fs.createTempFile(fsBase + "/temp", ".tmp", false, false);
File file = new File(baseDir + "/temp"); File file = new File(TestBase.BASE_TEST_DIR + "/temp");
file.getParentFile().mkdirs();
file.delete(); file.delete();
RandomAccessFile ra = new RandomAccessFile(file, "rw"); RandomAccessFile ra = new RandomAccessFile(file, "rw");
fs.delete(s); fs.delete(s);
...@@ -266,6 +275,8 @@ public class TestFileSystem extends TestBase { ...@@ -266,6 +275,8 @@ public class TestFileSystem extends TestBase {
} }
f.close(); f.close();
ra.close(); ra.close();
file.delete();
fs.delete(s);
} }
private void testTempFile(String fsBase) throws Exception { private void testTempFile(String fsBase) throws Exception {
...@@ -286,7 +297,7 @@ public class TestFileSystem extends TestBase { ...@@ -286,7 +297,7 @@ public class TestFileSystem extends TestBase {
assertEquals(-1, in.read()); assertEquals(-1, in.read());
in.close(); in.close();
out.close(); out.close();
fs.delete(s);
} }
} }
...@@ -12,6 +12,7 @@ import org.h2.dev.ftp.server.FtpEventListener; ...@@ -12,6 +12,7 @@ import org.h2.dev.ftp.server.FtpEventListener;
import org.h2.dev.ftp.server.FtpServer; import org.h2.dev.ftp.server.FtpServer;
import org.h2.test.TestBase; import org.h2.test.TestBase;
import org.h2.tools.Server; import org.h2.tools.Server;
import org.h2.util.IOUtils;
/** /**
* Tests the FTP server tool. * Tests the FTP server tool.
...@@ -30,7 +31,11 @@ public class TestFtp extends TestBase implements FtpEventListener { ...@@ -30,7 +31,11 @@ public class TestFtp extends TestBase implements FtpEventListener {
} }
public void test() throws Exception { public void test() throws Exception {
test(baseDir); if (getBaseDir().indexOf(':') > 0) {
return;
}
test(getBaseDir());
IOUtils.delete(getBaseDir() + "/test");
} }
private void test(String dir) throws Exception { private void test(String dir) throws Exception {
......
...@@ -17,6 +17,7 @@ import java.util.Set; ...@@ -17,6 +17,7 @@ import java.util.Set;
import java.util.TreeSet; import java.util.TreeSet;
import org.h2.api.DatabaseEventListener; import org.h2.api.DatabaseEventListener;
import org.h2.test.TestBase; import org.h2.test.TestBase;
import org.h2.util.IOUtils;
/** /**
* Test the page store. * Test the page store.
...@@ -57,6 +58,7 @@ public class TestPageStore extends TestBase implements DatabaseEventListener { ...@@ -57,6 +58,7 @@ public class TestPageStore extends TestBase implements DatabaseEventListener {
testUniqueIndex(); testUniqueIndex();
testCreateIndexLater(); testCreateIndexLater();
testFuzzOperations(); testFuzzOperations();
deleteDb("pageStore");
} }
private void testCheckpoint() throws SQLException { private void testCheckpoint() throws SQLException {
...@@ -73,7 +75,9 @@ public class TestPageStore extends TestBase implements DatabaseEventListener { ...@@ -73,7 +75,9 @@ public class TestPageStore extends TestBase implements DatabaseEventListener {
stat.execute("select nextval('SEQ') from system_range(1, 100000)"); stat.execute("select nextval('SEQ') from system_range(1, 100000)");
long after = System.currentTimeMillis(); long after = System.currentTimeMillis();
// it's hard to test - basically it shouldn't checkpoint too often // it's hard to test - basically it shouldn't checkpoint too often
assertTrue(after - before < 10000); if (after - before > 10000) {
fail("Checkpoint took " + (after - before) + " ms");
}
stat.execute("drop table test"); stat.execute("drop table test");
stat.execute("drop sequence seq"); stat.execute("drop sequence seq");
conn.close(); conn.close();
...@@ -317,7 +321,7 @@ public class TestPageStore extends TestBase implements DatabaseEventListener { ...@@ -317,7 +321,7 @@ public class TestPageStore extends TestBase implements DatabaseEventListener {
Connection conn; Connection conn;
deleteDb("pageStore"); deleteDb("pageStore");
String url; String url;
url = "jdbc:h2:" + baseDir + "/pageStore"; url = "jdbc:h2:" + getBaseDir() + "/pageStore";
conn = DriverManager.getConnection(url); conn = DriverManager.getConnection(url);
conn.createStatement().execute("create table test(id int) as select 1"); conn.createStatement().execute("create table test(id int) as select 1");
conn.close(); conn.close();
...@@ -396,8 +400,9 @@ public class TestPageStore extends TestBase implements DatabaseEventListener { ...@@ -396,8 +400,9 @@ public class TestPageStore extends TestBase implements DatabaseEventListener {
conn.close(); conn.close();
conn = DriverManager.getConnection(url); conn = DriverManager.getConnection(url);
stat = conn.createStatement(); stat = conn.createStatement();
stat.execute("script to '" + baseDir + "/pageStore.sql'"); stat.execute("script to '" + getBaseDir() + "/pageStore.sql'");
conn.close(); conn.close();
IOUtils.delete(getBaseDir() + "/pageStore.sql");
} catch (Exception e) { } catch (Exception e) {
try { try {
stat.execute("shutdown immediately"); stat.execute("shutdown immediately");
......
/*
* Copyright 2004-2010 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.test.unit;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.h2.engine.Constants;
import org.h2.store.fs.FileObject;
import org.h2.store.fs.FileSystem;
import org.h2.test.TestBase;
import org.h2.tools.Restore;
import org.h2.util.IOUtils;
/**
* Test the page store.
*/
public class TestPageStoreCoverage extends TestBase {
private static final String URL = "pageStore;PAGE_SIZE=64;CACHE_SIZE=16;MAX_LOG_SIZE=1";
/**
* Run just this test.
*
* @param a ignored
*/
public static void main(String... a) throws Exception {
TestBase.createCaller().init().test();
}
public void test() throws Exception {
// TODO mvcc, 2-phase commit
if (config.memory) {
return;
}
deleteDb("pageStore");
// testMoveRoot();
// testBasic();
// testReadOnly();
// testIncompleteCreate();
// testBackupRestore();
testTrim();
testLongTransaction();
testRecoverTemp();
deleteDb("pageStore");
}
private void testMoveRoot() throws SQLException {
Connection conn;
conn = getConnection(URL);
Statement stat = conn.createStatement();
stat.execute("create table test(id int primary key) as select x from system_range(1, 100)");
for (int i = 0; i < 10; i++) {
stat.execute("create table test" + i + "(id int primary key) as select x from system_range(1, 2)");
}
stat.execute("drop table test");
conn.close();
conn = getConnection(URL);
stat = conn.createStatement();
for (int i = 0; i < 10; i++) {
ResultSet rs = stat.executeQuery("select * from test" + i);
while (rs.next()) {
// ignore
}
}
stat.execute("drop all objects delete files");
conn.close();
}
private void testRecoverTemp() throws SQLException {
Connection conn;
conn = getConnection(URL);
Statement stat = conn.createStatement();
stat.execute("create cached temporary table test(id identity, name varchar)");
stat.execute("create index idx_test_name on test(name)");
stat.execute("create index idx_test_name2 on test(name, id)");
stat.execute("create table test2(id identity, name varchar)");
stat.execute("create index idx_test2_name on test2(name desc)");
stat.execute("create index idx_test2_name2 on test2(name, id)");
stat.execute("insert into test2 select null, space(10) from system_range(1, 10)");
stat.execute("create table test3(id identity, name varchar)");
stat.execute("checkpoint");
conn.setAutoCommit(false);
stat.execute("create table test4(id identity, name varchar)");
stat.execute("create index idx_test4_name2 on test(name, id)");
stat.execute("insert into test select null, space(10) from system_range(1, 10)");
stat.execute("insert into test3 select null, space(10) from system_range(1, 10)");
stat.execute("insert into test4 select null, space(10) from system_range(1, 10)");
stat.execute("truncate table test2");
stat.execute("drop index idx_test_name");
stat.execute("drop index idx_test2_name");
stat.execute("drop table test2");
stat.execute("insert into test select null, space(10) from system_range(1, 10)");
stat.execute("shutdown immediately");
try {
conn.close();
fail();
} catch (SQLException e) {
// ignore
}
conn = getConnection(URL);
stat = conn.createStatement();
stat.execute("drop all objects");
// re-allocate index root pages
for (int i = 0; i < 10; i++) {
stat.execute("create table test" + i + "(id identity, name varchar)");
}
stat.execute("checkpoint");
for (int i = 0; i < 10; i++) {
stat.execute("drop table test" + i);
}
for (int i = 0; i < 10; i++) {
stat.execute("create table test" + i + "(id identity, name varchar)");
}
stat.execute("shutdown immediately");
try {
conn.close();
fail();
} catch (SQLException e) {
// ignore
}
conn = getConnection(URL);
conn.createStatement().execute("drop all objects");
conn.close();
}
private void testLongTransaction() throws SQLException {
Connection conn;
conn = getConnection(URL);
Statement stat = conn.createStatement();
stat.execute("create table test(id identity, name varchar)");
conn.setAutoCommit(false);
stat.execute("insert into test select null, space(10) from system_range(1, 10)");
Connection conn2;
conn2 = getConnection(URL);
Statement stat2 = conn2.createStatement();
stat2.execute("checkpoint");
// large transaction
stat2.execute("create table test2(id identity, name varchar)");
stat2.execute("create index idx_test2_name on test2(name)");
stat2.execute("insert into test2 select null, x || space(10000) from system_range(1, 100)");
stat2.execute("drop table test2");
conn2.close();
stat.execute("drop table test");
conn.close();
}
private void testTrim() throws SQLException {
Connection conn;
conn = getConnection(URL);
Statement stat = conn.createStatement();
stat.execute("create table test(id int primary key, name varchar)");
stat.execute("create index idx_name on test(name, id)");
stat.execute("insert into test select x, x || space(10) from system_range(1, 20)");
stat.execute("create table test2(id int primary key, name varchar)");
stat.execute("create index idx_test2_name on test2(name, id)");
stat.execute("insert into test2 select x, x || space(10) from system_range(1, 20)");
stat.execute("create table test3(id int primary key, name varchar)");
stat.execute("create index idx_test3_name on test3(name, id)");
stat.execute("insert into test3 select x, x || space(3) from system_range(1, 3)");
stat.execute("delete from test");
stat.execute("checkpoint");
stat.execute("checkpoint sync");
stat.execute("shutdown compact");
conn.close();
conn = getConnection(URL);
conn.createStatement().execute("drop all objects");
conn.close();
}
private void testBasic() throws Exception {
Connection conn;
conn = getConnection(URL);
conn.close();
conn = getConnection(URL);
conn.close();
}
private void testReadOnly() throws Exception {
Connection conn;
conn = getConnection(URL);
conn.createStatement().execute("shutdown compact");
conn.close();
conn = getConnection(URL + ";access_mode_data=r");
conn.close();
}
private void testBackupRestore() throws Exception {
Connection conn;
conn = getConnection(URL);
Statement stat = conn.createStatement();
stat.execute("create table test(id int primary key, name varchar)");
stat.execute("create index idx_name on test(name, id)");
stat.execute("insert into test select x, x || space(200 * x) from system_range(1, 10)");
conn.setAutoCommit(false);
stat.execute("delete from test where id > 5");
stat.execute("backup to '" + getBaseDir() + "/backup.zip'");
conn.rollback();
Restore.execute(getBaseDir() + "/backup.zip", getBaseDir(), "pageStore2", true);
Connection conn2;
conn2 = getConnection("pageStore2");
Statement stat2 = conn2.createStatement();
assertEqualDatabases(stat, stat2);
conn.createStatement().execute("drop table test");
conn2.close();
conn.close();
IOUtils.delete(getBaseDir() + "/backup.zip");
deleteDb("pageStore2");
}
private void testIncompleteCreate() throws Exception {
deleteDb("pageStore");
Connection conn;
String fileName = getBaseDir() + "/pageStore" + Constants.SUFFIX_PAGE_FILE;
conn = getConnection("pageStore");
Statement stat = conn.createStatement();
stat.execute("drop table if exists INFORMATION_SCHEMA.LOB_DATA");
stat.execute("drop table if exists INFORMATION_SCHEMA.LOB_MAP");
conn.close();
FileObject f = FileSystem.getInstance(fileName).openFileObject(fileName, "rw");
// create a new database
conn = getConnection("pageStore");
conn.close();
f = FileSystem.getInstance(fileName).openFileObject(fileName, "rw");
f.setFileLength(16);
// create a new database
conn = getConnection("pageStore");
deleteDb("pageStore");
}
}
...@@ -36,7 +36,7 @@ public class TestPgServer extends TestBase { ...@@ -36,7 +36,7 @@ public class TestPgServer extends TestBase {
public void test() throws SQLException { public void test() throws SQLException {
deleteDb("test"); deleteDb("test");
Server server = Server.createPgServer("-baseDir", baseDir, "-pgPort", "5535"); Server server = Server.createPgServer("-baseDir", getBaseDir(), "-pgPort", "5535");
server.start(); server.start();
try { try {
Class.forName("org.postgresql.Driver"); Class.forName("org.postgresql.Driver");
......
...@@ -11,9 +11,11 @@ import java.io.PrintStream; ...@@ -11,9 +11,11 @@ import java.io.PrintStream;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import org.h2.store.fs.FileSystem;
import org.h2.test.TestBase; import org.h2.test.TestBase;
import org.h2.tools.DeleteDbFiles; import org.h2.tools.DeleteDbFiles;
import org.h2.tools.Recover; import org.h2.tools.Recover;
import org.h2.util.IOUtils;
/** /**
* Tests database recovery. * Tests database recovery.
...@@ -34,8 +36,8 @@ public class TestRecovery extends TestBase { ...@@ -34,8 +36,8 @@ public class TestRecovery extends TestBase {
} }
private void testRunScript() throws SQLException { private void testRunScript() throws SQLException {
DeleteDbFiles.execute(baseDir, "recovery", true); DeleteDbFiles.execute(getBaseDir(), "recovery", true);
DeleteDbFiles.execute(baseDir, "recovery2", true); DeleteDbFiles.execute(getBaseDir(), "recovery2", true);
org.h2.Driver.load(); org.h2.Driver.load();
Connection conn = getConnection("recovery"); Connection conn = getConnection("recovery");
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
...@@ -53,7 +55,7 @@ public class TestRecovery extends TestBase { ...@@ -53,7 +55,7 @@ public class TestRecovery extends TestBase {
Recover rec = new Recover(); Recover rec = new Recover();
ByteArrayOutputStream buff = new ByteArrayOutputStream(); ByteArrayOutputStream buff = new ByteArrayOutputStream();
rec.setOut(new PrintStream(buff)); rec.setOut(new PrintStream(buff));
rec.runTool("-dir", baseDir, "-db", "recovery", "-trace"); rec.runTool("-dir", getBaseDir(), "-db", "recovery", "-trace");
String out = new String(buff.toByteArray()); String out = new String(buff.toByteArray());
assertTrue(out.indexOf("Created file") >= 0); assertTrue(out.indexOf("Created file") >= 0);
...@@ -61,7 +63,7 @@ public class TestRecovery extends TestBase { ...@@ -61,7 +63,7 @@ public class TestRecovery extends TestBase {
Statement stat2 = conn2.createStatement(); Statement stat2 = conn2.createStatement();
String name = "recovery.h2.sql"; String name = "recovery.h2.sql";
stat2.execute("runscript from '" + baseDir + "/" + name + "'"); stat2.execute("runscript from '" + getBaseDir() + "/" + name + "'");
stat2.execute("select * from test"); stat2.execute("select * from test");
stat2.execute("drop user diff"); stat2.execute("drop user diff");
conn2.close(); conn2.close();
...@@ -75,7 +77,13 @@ public class TestRecovery extends TestBase { ...@@ -75,7 +77,13 @@ public class TestRecovery extends TestBase {
conn.close(); conn.close();
conn2.close(); conn2.close();
Recover.execute(baseDir, "recovery"); Recover.execute(getBaseDir(), "recovery");
deleteDb("recovery");
deleteDb("recovery2");
IOUtils.delete(getBaseDir() + "/recovery.h2.sql");
String dir = getBaseDir() + "/recovery.lobs.db";
FileSystem.getInstance(dir).deleteRecursive(dir, false);
} }
......
/*
* Copyright 2004-2010 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.test.unit;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Properties;
import org.h2.constant.ErrorCode;
import org.h2.engine.ConnectionInfo;
import org.h2.engine.Constants;
import org.h2.engine.Database;
import org.h2.store.fs.FileSystem;
import org.h2.test.TestBase;
import org.h2.test.utils.Recorder;
import org.h2.test.utils.RecordingFileSystem;
import org.h2.util.JdbcUtils;
import org.h2.util.New;
/**
* A test that calls another test, and after each write operation to the
* database file, it copies the file, and tries to reopen it.
*/
public class TestReopen extends TestBase implements Recorder {
private String testDatabase = "memFS:" + TestBase.BASE_TEST_DIR + "/reopen";
private long lastCheck;
private int counter;
private int testEvery = 1 << 10;
private HashSet<String> knownErrors = New.hashSet();
private int max = 103128;
/**
* Run just this test.
*
* @param a ignored
*/
public static void main(String... a) throws Exception {
TestBase.createCaller().init().test();
}
public void test() throws Exception {
System.setProperty("h2.delayWrongPasswordMin", "0");
RecordingFileSystem.register();
RecordingFileSystem.setRecorder(this);
config.record = true;
long time = System.currentTimeMillis();
// Profiler p = new Profiler();
// p.startCollecting();
new TestPageStoreCoverage().init(config).test();
// System.out.println(p.getTop(3));
System.out.println(System.currentTimeMillis() - time);
System.out.println("counter: " + counter);
}
public synchronized void log(int op, String fileName, byte[] data, long x) {
if (op != Recorder.WRITE) {
return;
}
if (!fileName.endsWith(Constants.SUFFIX_PAGE_FILE)) {
return;
}
counter++;
if ((counter & 1023) == 0) {
long now = System.currentTimeMillis();
if (now > lastCheck + 5000) {
System.out.println(" at " + counter + " of " + max + " " + (100. / max * counter));
//new Exception("currentPosition").printStackTrace(System.out);
lastCheck = now;
}
}
if ((counter & (testEvery - 1)) != 0) {
return;
}
FileSystem.getInstance(fileName).copy(fileName, testDatabase + Constants.SUFFIX_PAGE_FILE);
try {
// avoid using the Engine class to avoid deadlocks
Properties p = new Properties();
ConnectionInfo ci = new ConnectionInfo("jdbc:h2:" + testDatabase + ";FILE_LOCK=NO", p);
Database database = new Database(ci, null);
// close the database
database.removeSession(null);
// everything OK - return
return;
} 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 ------------------------------ " + counter);
testDatabase += "X";
FileSystem.getInstance(fileName).copy(fileName, testDatabase + Constants.SUFFIX_PAGE_FILE);
try {
// avoid using the Engine class to avoid deadlocks
Properties p = new Properties();
ConnectionInfo ci = new ConnectionInfo("jdbc:h2:" + testDatabase + ";FILE_LOCK=NO", p);
Database database = new Database(ci, null);
// close the database
database.removeSession(null);
} catch (Exception e) {
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;
}
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(counter + " code: " + errorCode + " " + e.toString());
e.printStackTrace(System.out);
knownErrors.add(s);
} else {
System.out.println(counter + " code: " + errorCode);
}
}
}
}
...@@ -34,14 +34,17 @@ public class TestSampleApps extends TestBase { ...@@ -34,14 +34,17 @@ public class TestSampleApps extends TestBase {
} }
public void test() throws Exception { public void test() throws Exception {
if (!getBaseDir().startsWith(TestBase.BASE_TEST_DIR)) {
return;
}
deleteDb("optimizations"); deleteDb("optimizations");
InputStream in = getClass().getClassLoader().getResourceAsStream("org/h2/samples/optimizations.sql"); InputStream in = getClass().getClassLoader().getResourceAsStream("org/h2/samples/optimizations.sql");
new File(baseDir).mkdirs(); new File(getBaseDir()).mkdirs();
FileOutputStream out = new FileOutputStream(baseDir + "/optimizations.sql"); FileOutputStream out = new FileOutputStream(getBaseDir() + "/optimizations.sql");
IOUtils.copyAndClose(in, out); IOUtils.copyAndClose(in, out);
String url = "jdbc:h2:" + baseDir + "/optimizations"; String url = "jdbc:h2:" + getBaseDir() + "/optimizations";
testApp("", org.h2.tools.RunScript.class, "-url", url, "-user", "sa", "-password", "sa", "-script", testApp("", org.h2.tools.RunScript.class, "-url", url, "-user", "sa", "-password", "sa", "-script",
baseDir + "/optimizations.sql", "-checkResults"); getBaseDir() + "/optimizations.sql", "-checkResults");
deleteDb("optimizations"); deleteDb("optimizations");
testApp("Compacting...\nDone.", org.h2.samples.Compact.class); testApp("Compacting...\nDone.", org.h2.samples.Compact.class);
testApp("NAME: Bob Meier\n" + "EMAIL: bob.meier@abcde.abc\n" testApp("NAME: Bob Meier\n" + "EMAIL: bob.meier@abcde.abc\n"
...@@ -65,6 +68,7 @@ public class TestSampleApps extends TestBase { ...@@ -65,6 +68,7 @@ public class TestSampleApps extends TestBase {
org.h2.tools.ChangeFileEncryption.class); org.h2.tools.ChangeFileEncryption.class);
testApp("Deletes all files belonging to a database.*", testApp("Deletes all files belonging to a database.*",
org.h2.tools.DeleteDbFiles.class, "-help"); org.h2.tools.DeleteDbFiles.class, "-help");
IOUtils.delete(getBaseDir() + "/optimizations.sql");
} }
private void testApp(String expected, Class< ? > clazz, String... args) throws Exception { private void testApp(String expected, Class< ? > clazz, String... args) throws Exception {
......
...@@ -193,7 +193,7 @@ public class TestServlet extends TestBase { ...@@ -193,7 +193,7 @@ public class TestServlet extends TestBase {
stat1.execute("CREATE TABLE T(ID INT)"); stat1.execute("CREATE TABLE T(ID INT)");
String u2 = url.substring(url.indexOf("servlet")); String u2 = url.substring(url.indexOf("servlet"));
u2 = "jdbc:h2:tcp://localhost:8888/" + baseDir + "/" + u2; u2 = "jdbc:h2:tcp://localhost:8888/" + getBaseDir() + "/" + u2;
Connection conn2 = DriverManager.getConnection( Connection conn2 = DriverManager.getConnection(
u2, getUser(), getPassword()); u2, getUser(), getPassword());
Statement stat2 = conn2.createStatement(); Statement stat2 = conn2.createStatement();
...@@ -213,7 +213,7 @@ public class TestServlet extends TestBase { ...@@ -213,7 +213,7 @@ public class TestServlet extends TestBase {
// listener must be stopped // listener must be stopped
try { try {
DriverManager.getConnection("jdbc:h2:tcp://localhost:8888/" + baseDir + "/servlet", getUser(), getPassword()); DriverManager.getConnection("jdbc:h2:tcp://localhost:8888/" + getBaseDir() + "/servlet", getUser(), getPassword());
fail(); fail();
} catch (SQLException e) { } catch (SQLException e) {
assertKnownException(e); assertKnownException(e);
...@@ -227,6 +227,8 @@ public class TestServlet extends TestBase { ...@@ -227,6 +227,8 @@ public class TestServlet extends TestBase {
assertKnownException(e); assertKnownException(e);
} }
deleteDb("servlet");
} }
} }
...@@ -16,6 +16,7 @@ import java.util.Random; ...@@ -16,6 +16,7 @@ import java.util.Random;
import org.h2.compress.LZFInputStream; import org.h2.compress.LZFInputStream;
import org.h2.compress.LZFOutputStream; import org.h2.compress.LZFOutputStream;
import org.h2.test.TestBase; import org.h2.test.TestBase;
import org.h2.util.IOUtils;
/** /**
* Tests the LZF stream. * Tests the LZF stream.
...@@ -52,7 +53,8 @@ public class TestStreams extends TestBase { ...@@ -52,7 +53,8 @@ public class TestStreams extends TestBase {
} }
private void testLZFStreamClose() throws IOException { private void testLZFStreamClose() throws IOException {
String fileName = getTestDir("") + "/temp"; String fileName = getBaseDir() + "/temp";
IOUtils.createDirs(fileName);
LZFOutputStream out = new LZFOutputStream(new FileOutputStream(fileName)); LZFOutputStream out = new LZFOutputStream(new FileOutputStream(fileName));
out.write("Hello".getBytes()); out.write("Hello".getBytes());
out.close(); out.close();
...@@ -61,6 +63,7 @@ public class TestStreams extends TestBase { ...@@ -61,6 +63,7 @@ public class TestStreams extends TestBase {
assertEquals(5, in.read(buff)); assertEquals(5, in.read(buff));
in.read(); in.read();
in.close(); in.close();
IOUtils.delete(getBaseDir() + "/temp");
} }
private void testLZFStreams() throws IOException { private void testLZFStreams() throws IOException {
......
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
package org.h2.test.unit; package org.h2.test.unit;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.PrintStream; import java.io.PrintStream;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
...@@ -28,6 +27,7 @@ import java.util.ArrayList; ...@@ -28,6 +27,7 @@ import java.util.ArrayList;
import java.util.Random; import java.util.Random;
import org.h2.constant.ErrorCode; import org.h2.constant.ErrorCode;
import org.h2.store.FileLister; import org.h2.store.FileLister;
import org.h2.store.fs.FileSystem;
import org.h2.test.TestBase; import org.h2.test.TestBase;
import org.h2.test.trace.Player; import org.h2.test.trace.Player;
import org.h2.tools.Backup; import org.h2.tools.Backup;
...@@ -81,6 +81,9 @@ public class TestTools extends TestBase { ...@@ -81,6 +81,9 @@ public class TestTools extends TestBase {
testBackupRestore(); testBackupRestore();
testRecover(); testRecover();
deleteDb("utils"); deleteDb("utils");
IOUtils.delete(getBaseDir() + "/b2.sql");
IOUtils.delete(getBaseDir() + "/b2.sql.txt");
IOUtils.delete(getBaseDir() + "/b2.zip");
} }
private void testSimpleResultSet() throws Exception { private void testSimpleResultSet() throws Exception {
...@@ -238,7 +241,7 @@ public class TestTools extends TestBase { ...@@ -238,7 +241,7 @@ public class TestTools extends TestBase {
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
stat.execute("create table test(c clob) as select space(10000) from dual"); stat.execute("create table test(c clob) as select space(10000) from dual");
conn.close(); conn.close();
DeleteDbFiles.execute(baseDir, "utils", true); DeleteDbFiles.execute(getBaseDir(), "utils", true);
conn = getConnection("utilsMore"); conn = getConnection("utilsMore");
stat = conn.createStatement(); stat = conn.createStatement();
ResultSet rs; ResultSet rs;
...@@ -246,6 +249,7 @@ public class TestTools extends TestBase { ...@@ -246,6 +249,7 @@ public class TestTools extends TestBase {
rs.next(); rs.next();
rs.getString(1); rs.getString(1);
conn.close(); conn.close();
deleteDb("utilsMore");
} }
private void testServerMain() throws SQLException { private void testServerMain() throws SQLException {
...@@ -335,7 +339,7 @@ public class TestTools extends TestBase { ...@@ -335,7 +339,7 @@ public class TestTools extends TestBase {
private void testConvertTraceFile() throws Exception { private void testConvertTraceFile() throws Exception {
deleteDb("toolsConvertTraceFile"); deleteDb("toolsConvertTraceFile");
org.h2.Driver.load(); org.h2.Driver.load();
String url = "jdbc:h2:" + baseDir + "/toolsConvertTraceFile"; String url = "jdbc:h2:" + getBaseDir() + "/toolsConvertTraceFile";
Connection conn = DriverManager.getConnection(url + ";TRACE_LEVEL_FILE=3", "sa", "sa"); Connection conn = DriverManager.getConnection(url + ";TRACE_LEVEL_FILE=3", "sa", "sa");
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
stat.execute("create table test(id int primary key, name varchar, amount decimal)"); stat.execute("create table test(id int primary key, name varchar, amount decimal)");
...@@ -360,27 +364,32 @@ public class TestTools extends TestBase { ...@@ -360,27 +364,32 @@ public class TestTools extends TestBase {
prep.executeUpdate(); prep.executeUpdate();
conn.close(); conn.close();
ConvertTraceFile.main("-traceFile", baseDir + "/toolsConvertTraceFile.trace.db", "-javaClass", baseDir + "/Test", "-script", baseDir + "/test.sql"); ConvertTraceFile.main("-traceFile", getBaseDir() + "/toolsConvertTraceFile.trace.db", "-javaClass", getBaseDir() + "/Test", "-script", getBaseDir() + "/test.sql");
new File(baseDir + "/Test.java").delete(); FileSystem fs = FileSystem.getInstance(getBaseDir());
fs.delete(getBaseDir() + "/Test.java");
File trace = new File(baseDir + "/toolsConvertTraceFile.trace.db"); String trace = getBaseDir() + "/toolsConvertTraceFile.trace.db";
assertTrue(trace.exists()); assertTrue(fs.exists(trace));
File newTrace = new File(baseDir + "/test.trace.db"); String newTrace = getBaseDir() + "/test.trace.db";
newTrace.delete(); fs.delete(newTrace);
assertFalse(newTrace.exists()); assertFalse(fs.exists(newTrace));
assertTrue(trace.renameTo(newTrace)); fs.rename(trace, newTrace);
deleteDb("toolsConvertTraceFile"); deleteDb("toolsConvertTraceFile");
Player.main(baseDir + "/test.trace.db"); Player.main(getBaseDir() + "/test.trace.db");
testTraceFile(url); testTraceFile(url);
deleteDb("toolsConvertTraceFile"); deleteDb("toolsConvertTraceFile");
RunScript.main("-url", url, "-user", "sa", "-script", baseDir + "/test.sql"); RunScript.main("-url", url, "-user", "sa", "-script", getBaseDir() + "/test.sql");
testTraceFile(url); testTraceFile(url);
deleteDb("toolsConvertTraceFile");
IOUtils.delete(getBaseDir() + "/toolsConvertTraceFile.h2.sql");
IOUtils.delete(getBaseDir() + "/test.sql");
} }
private void testTraceFile(String url) throws SQLException { private void testTraceFile(String url) throws SQLException {
Connection conn; Connection conn;
Recover.main("-removePassword", "-dir", baseDir, "-db", "toolsConvertTraceFile"); Recover.main("-removePassword", "-dir", getBaseDir(), "-db", "toolsConvertTraceFile");
conn = DriverManager.getConnection(url, "sa", ""); conn = DriverManager.getConnection(url, "sa", "");
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
ResultSet rs; ResultSet rs;
...@@ -408,13 +417,13 @@ public class TestTools extends TestBase { ...@@ -408,13 +417,13 @@ public class TestTools extends TestBase {
private void testRemove() throws SQLException { private void testRemove() throws SQLException {
deleteDb("toolsRemove"); deleteDb("toolsRemove");
org.h2.Driver.load(); org.h2.Driver.load();
String url = "jdbc:h2:" + baseDir + "/toolsRemove"; String url = "jdbc:h2:" + getBaseDir() + "/toolsRemove";
Connection conn = DriverManager.getConnection(url, "sa", "sa"); Connection conn = DriverManager.getConnection(url, "sa", "sa");
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
stat.execute("create table test(id int primary key, name varchar)"); stat.execute("create table test(id int primary key, name varchar)");
stat.execute("insert into test values(1, 'Hello')"); stat.execute("insert into test values(1, 'Hello')");
conn.close(); conn.close();
Recover.main("-dir", baseDir, "-db", "toolsRemove", "-removePassword"); Recover.main("-dir", getBaseDir(), "-db", "toolsRemove", "-removePassword");
conn = DriverManager.getConnection(url, "sa", ""); conn = DriverManager.getConnection(url, "sa", "");
stat = conn.createStatement(); stat = conn.createStatement();
ResultSet rs; ResultSet rs;
...@@ -423,6 +432,8 @@ public class TestTools extends TestBase { ...@@ -423,6 +432,8 @@ public class TestTools extends TestBase {
assertEquals(1, rs.getInt(1)); assertEquals(1, rs.getInt(1));
assertEquals("Hello", rs.getString(2)); assertEquals("Hello", rs.getString(2));
conn.close(); conn.close();
deleteDb("toolsRemove");
IOUtils.delete(getBaseDir() + "/toolsRemove.h2.sql");
} }
private void testRecover() throws SQLException { private void testRecover() throws SQLException {
...@@ -442,11 +453,11 @@ public class TestTools extends TestBase { ...@@ -442,11 +453,11 @@ public class TestTools extends TestBase {
String s1 = rs.getString(4); String s1 = rs.getString(4);
conn.close(); conn.close();
Recover.main("-dir", baseDir, "-db", "toolsRecover"); Recover.main("-dir", getBaseDir(), "-db", "toolsRecover");
// deleteDb would delete the .lob.db directory as well // deleteDb would delete the .lob.db directory as well
// deleteDb("toolsRecover"); // deleteDb("toolsRecover");
ArrayList<String> list = FileLister.getDatabaseFiles(baseDir, "toolsRecover", true); ArrayList<String> list = FileLister.getDatabaseFiles(getBaseDir(), "toolsRecover", true);
for (String fileName : list) { for (String fileName : list) {
if (!IOUtils.isDirectory(fileName)) { if (!IOUtils.isDirectory(fileName)) {
IOUtils.delete(fileName); IOUtils.delete(fileName);
...@@ -455,11 +466,8 @@ public class TestTools extends TestBase { ...@@ -455,11 +466,8 @@ public class TestTools extends TestBase {
conn = DriverManager.getConnection(url, "another", "another"); conn = DriverManager.getConnection(url, "another", "another");
stat = conn.createStatement(); stat = conn.createStatement();
String suffix = ".data.sql"; String suffix = ".h2.sql";
if (new File(baseDir + "/toolsRecover.h2.sql").exists()) { stat.execute("runscript from '" + getBaseDir() + "/toolsRecover" + suffix + "'");
suffix = ".h2.sql";
}
stat.execute("runscript from '" + baseDir + "/toolsRecover" + suffix + "'");
rs = stat.executeQuery("select * from \"test 2\""); rs = stat.executeQuery("select * from \"test 2\"");
assertFalse(rs.next()); assertFalse(rs.next());
rs = stat.executeQuery("select * from test"); rs = stat.executeQuery("select * from test");
...@@ -475,6 +483,10 @@ public class TestTools extends TestBase { ...@@ -475,6 +483,10 @@ public class TestTools extends TestBase {
assertEquals(s1, s2); assertEquals(s1, s2);
assertFalse(rs.next()); assertFalse(rs.next());
conn.close(); conn.close();
deleteDb("toolsRecover");
IOUtils.delete(getBaseDir() + "/toolsRecover.h2.sql");
String dir = getBaseDir() + "/toolsRecover.lobs.db";
FileSystem.getInstance(dir).deleteRecursive(dir, false);
} }
private void testManagementDb() throws SQLException { private void testManagementDb() throws SQLException {
...@@ -489,9 +501,9 @@ public class TestTools extends TestBase { ...@@ -489,9 +501,9 @@ public class TestTools extends TestBase {
private void testScriptRunscriptLob() throws Exception { private void testScriptRunscriptLob() throws Exception {
org.h2.Driver.load(); org.h2.Driver.load();
String url = "jdbc:h2:" + baseDir + "/utils"; String url = "jdbc:h2:" + getBaseDir() + "/utils";
String user = "sa", password = "abc"; String user = "sa", password = "abc";
String fileName = baseDir + "/b2.sql"; String fileName = getBaseDir() + "/b2.sql";
Connection conn = DriverManager.getConnection(url, user, password); Connection conn = DriverManager.getConnection(url, user, password);
conn.createStatement().execute("CREATE TABLE TEST(ID INT PRIMARY KEY, BDATA BLOB, CDATA CLOB)"); conn.createStatement().execute("CREATE TABLE TEST(ID INT PRIMARY KEY, BDATA BLOB, CDATA CLOB)");
PreparedStatement prep = conn.prepareStatement("INSERT INTO TEST VALUES(?, ?, ?)"); PreparedStatement prep = conn.prepareStatement("INSERT INTO TEST VALUES(?, ?, ?)");
...@@ -533,9 +545,9 @@ public class TestTools extends TestBase { ...@@ -533,9 +545,9 @@ public class TestTools extends TestBase {
conn.close(); conn.close();
Script.main("-url", url, "-user", user, "-password", password, "-script", fileName); Script.main("-url", url, "-user", user, "-password", password, "-script", fileName);
DeleteDbFiles.main("-dir", baseDir, "-db", "utils", "-quiet"); DeleteDbFiles.main("-dir", getBaseDir(), "-db", "utils", "-quiet");
RunScript.main("-url", url, "-user", user, "-password", password, "-script", fileName); RunScript.main("-url", url, "-user", user, "-password", password, "-script", fileName);
conn = DriverManager.getConnection("jdbc:h2:" + baseDir + "/utils", "sa", "abc"); conn = DriverManager.getConnection("jdbc:h2:" + getBaseDir() + "/utils", "sa", "abc");
} }
conn.close(); conn.close();
...@@ -543,10 +555,10 @@ public class TestTools extends TestBase { ...@@ -543,10 +555,10 @@ public class TestTools extends TestBase {
private void testScriptRunscript() throws SQLException { private void testScriptRunscript() throws SQLException {
org.h2.Driver.load(); org.h2.Driver.load();
String url = "jdbc:h2:" + baseDir + "/utils"; String url = "jdbc:h2:" + getBaseDir() + "/utils";
String user = "sa", password = "abc"; String user = "sa", password = "abc";
String fileName = baseDir + "/b2.sql"; String fileName = getBaseDir() + "/b2.sql";
DeleteDbFiles.main("-dir", baseDir, "-db", "utils", "-quiet"); DeleteDbFiles.main("-dir", getBaseDir(), "-db", "utils", "-quiet");
Connection conn = DriverManager.getConnection(url, user, password); Connection conn = DriverManager.getConnection(url, user, password);
conn.createStatement().execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR)"); conn.createStatement().execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR)");
conn.createStatement().execute("INSERT INTO TEST VALUES(1, 'Hello')"); conn.createStatement().execute("INSERT INTO TEST VALUES(1, 'Hello')");
...@@ -554,15 +566,15 @@ public class TestTools extends TestBase { ...@@ -554,15 +566,15 @@ public class TestTools extends TestBase {
Script.main("-url", url, "-user", user, "-password", password, "-script", fileName, "-options", Script.main("-url", url, "-user", user, "-password", password, "-script", fileName, "-options",
"nodata", "compression", "lzf", "cipher", "xtea", "password", "'123'"); "nodata", "compression", "lzf", "cipher", "xtea", "password", "'123'");
Script.main("-url", url, "-user", user, "-password", password, "-script", fileName + ".txt"); Script.main("-url", url, "-user", user, "-password", password, "-script", fileName + ".txt");
DeleteDbFiles.main("-dir", baseDir, "-db", "utils", "-quiet"); DeleteDbFiles.main("-dir", getBaseDir(), "-db", "utils", "-quiet");
RunScript.main("-url", url, "-user", user, "-password", password, "-script", fileName, RunScript.main("-url", url, "-user", user, "-password", password, "-script", fileName,
"-options", "compression", "lzf", "cipher", "xtea", "password", "'123'"); "-options", "compression", "lzf", "cipher", "xtea", "password", "'123'");
conn = DriverManager.getConnection("jdbc:h2:" + baseDir + "/utils", "sa", "abc"); conn = DriverManager.getConnection("jdbc:h2:" + getBaseDir() + "/utils", "sa", "abc");
ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM TEST"); ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM TEST");
assertFalse(rs.next()); assertFalse(rs.next());
conn.close(); conn.close();
DeleteDbFiles.main("-dir", baseDir, "-db", "utils", "-quiet"); DeleteDbFiles.main("-dir", getBaseDir(), "-db", "utils", "-quiet");
RunScript tool = new RunScript(); RunScript tool = new RunScript();
ByteArrayOutputStream buff = new ByteArrayOutputStream(); ByteArrayOutputStream buff = new ByteArrayOutputStream();
tool.setOut(new PrintStream(buff)); tool.setOut(new PrintStream(buff));
...@@ -573,58 +585,58 @@ public class TestTools extends TestBase { ...@@ -573,58 +585,58 @@ public class TestTools extends TestBase {
private void testBackupRestore() throws SQLException { private void testBackupRestore() throws SQLException {
org.h2.Driver.load(); org.h2.Driver.load();
String url = "jdbc:h2:" + baseDir + "/utils"; String url = "jdbc:h2:" + getBaseDir() + "/utils";
String user = "sa", password = "abc"; String user = "sa", password = "abc";
String fileName = baseDir + "/b2.zip"; String fileName = getBaseDir() + "/b2.zip";
DeleteDbFiles.main("-dir", baseDir, "-db", "utils", "-quiet"); DeleteDbFiles.main("-dir", getBaseDir(), "-db", "utils", "-quiet");
Connection conn = DriverManager.getConnection(url, user, password); Connection conn = DriverManager.getConnection(url, user, password);
conn.createStatement().execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR)"); conn.createStatement().execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR)");
conn.createStatement().execute("INSERT INTO TEST VALUES(1, 'Hello')"); conn.createStatement().execute("INSERT INTO TEST VALUES(1, 'Hello')");
conn.close(); conn.close();
Backup.main("-file", fileName, "-dir", baseDir, "-db", "utils", "-quiet"); Backup.main("-file", fileName, "-dir", getBaseDir(), "-db", "utils", "-quiet");
DeleteDbFiles.main("-dir", baseDir, "-db", "utils", "-quiet"); DeleteDbFiles.main("-dir", getBaseDir(), "-db", "utils", "-quiet");
Restore.main("-file", fileName, "-dir", baseDir, "-db", "utils", "-quiet"); Restore.main("-file", fileName, "-dir", getBaseDir(), "-db", "utils", "-quiet");
conn = DriverManager.getConnection("jdbc:h2:" + baseDir + "/utils", "sa", "abc"); conn = DriverManager.getConnection("jdbc:h2:" + getBaseDir() + "/utils", "sa", "abc");
ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM TEST"); ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM TEST");
assertTrue(rs.next()); assertTrue(rs.next());
assertFalse(rs.next()); assertFalse(rs.next());
try { try {
// must fail when the database is in use // must fail when the database is in use
Backup.main("-file", fileName, "-dir", baseDir, "-db", "utils"); Backup.main("-file", fileName, "-dir", getBaseDir(), "-db", "utils");
fail(); fail();
} catch (SQLException e) { } catch (SQLException e) {
assertKnownException(e); assertKnownException(e);
} }
conn.close(); conn.close();
DeleteDbFiles.main("-dir", baseDir, "-db", "utils", "-quiet"); DeleteDbFiles.main("-dir", getBaseDir(), "-db", "utils", "-quiet");
} }
private void testChangeFileEncryption() throws SQLException { private void testChangeFileEncryption() throws SQLException {
org.h2.Driver.load(); org.h2.Driver.load();
DeleteDbFiles.execute(baseDir, "utils", true); DeleteDbFiles.execute(getBaseDir(), "utils", true);
Connection conn = DriverManager.getConnection("jdbc:h2:" + Connection conn = DriverManager.getConnection("jdbc:h2:" +
baseDir + "/utils;CIPHER=XTEA", "sa", "abc 123"); getBaseDir() + "/utils;CIPHER=XTEA", "sa", "abc 123");
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, DATA CLOB) " + stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, DATA CLOB) " +
"AS SELECT X, SPACE(3000) FROM SYSTEM_RANGE(1, 300)"); "AS SELECT X, SPACE(3000) FROM SYSTEM_RANGE(1, 300)");
conn.close(); conn.close();
String[] args = { "-dir", baseDir, "-db", "utils", "-cipher", "XTEA", "-decrypt", "abc", "-quiet" }; String[] args = { "-dir", getBaseDir(), "-db", "utils", "-cipher", "XTEA", "-decrypt", "abc", "-quiet" };
ChangeFileEncryption.main(args); ChangeFileEncryption.main(args);
args = new String[] { "-dir", baseDir, "-db", "utils", "-cipher", "AES", "-encrypt", "def", "-quiet" }; args = new String[] { "-dir", getBaseDir(), "-db", "utils", "-cipher", "AES", "-encrypt", "def", "-quiet" };
ChangeFileEncryption.main(args); ChangeFileEncryption.main(args);
conn = DriverManager.getConnection("jdbc:h2:" + conn = DriverManager.getConnection("jdbc:h2:" +
baseDir + "/utils;CIPHER=AES", "sa", "def 123"); getBaseDir() + "/utils;CIPHER=AES", "sa", "def 123");
stat = conn.createStatement(); stat = conn.createStatement();
stat.execute("SELECT * FROM TEST"); stat.execute("SELECT * FROM TEST");
try { try {
args = new String[] { "-dir", baseDir, "-db", "utils", "-cipher", "AES", "-decrypt", "def", "-quiet" }; args = new String[] { "-dir", getBaseDir(), "-db", "utils", "-cipher", "AES", "-decrypt", "def", "-quiet" };
ChangeFileEncryption.main(args); ChangeFileEncryption.main(args);
fail(); fail();
} catch (SQLException e) { } catch (SQLException e) {
assertKnownException(e); assertKnownException(e);
} }
conn.close(); conn.close();
args = new String[] { "-dir", baseDir, "-db", "utils", "-quiet" }; args = new String[] { "-dir", getBaseDir(), "-db", "utils", "-quiet" };
DeleteDbFiles.main(args); DeleteDbFiles.main(args);
} }
...@@ -632,7 +644,7 @@ public class TestTools extends TestBase { ...@@ -632,7 +644,7 @@ public class TestTools extends TestBase {
Connection conn; Connection conn;
deleteDb("test"); deleteDb("test");
Server tcpServer = Server.createTcpServer( Server tcpServer = Server.createTcpServer(
"-baseDir", baseDir, "-baseDir", getBaseDir(),
"-tcpPort", "9192", "-tcpPort", "9192",
"-tcpAllowOthers").start(); "-tcpAllowOthers").start();
conn = DriverManager.getConnection("jdbc:h2:tcp://localhost:9192/test", "sa", ""); conn = DriverManager.getConnection("jdbc:h2:tcp://localhost:9192/test", "sa", "");
...@@ -641,7 +653,7 @@ public class TestTools extends TestBase { ...@@ -641,7 +653,7 @@ public class TestTools extends TestBase {
Server.createTcpServer( Server.createTcpServer(
"-ifExists", "-ifExists",
"-tcpPassword", "abc", "-tcpPassword", "abc",
"-baseDir", baseDir, "-baseDir", getBaseDir(),
"-tcpPort", "9192").start(); "-tcpPort", "9192").start();
try { try {
conn = DriverManager.getConnection("jdbc:h2:tcp://localhost:9192/test2", "sa", ""); conn = DriverManager.getConnection("jdbc:h2:tcp://localhost:9192/test2", "sa", "");
...@@ -676,7 +688,7 @@ public class TestTools extends TestBase { ...@@ -676,7 +688,7 @@ public class TestTools extends TestBase {
// Test filesystem prefix and escape from baseDir // Test filesystem prefix and escape from baseDir
deleteDb("testSplit"); deleteDb("testSplit");
server = Server.createTcpServer( server = Server.createTcpServer(
"-baseDir", baseDir, "-baseDir", getBaseDir(),
"-tcpPort", "9192", "-tcpPort", "9192",
"-tcpAllowOthers").start(); "-tcpAllowOthers").start();
conn = DriverManager.getConnection("jdbc:h2:tcp://localhost:9192/split:testSplit", "sa", ""); conn = DriverManager.getConnection("jdbc:h2:tcp://localhost:9192/split:testSplit", "sa", "");
......
...@@ -201,7 +201,7 @@ public class TestValueMemory extends TestBase implements DataHandler { ...@@ -201,7 +201,7 @@ public class TestValueMemory extends TestBase implements DataHandler {
} }
public String getDatabasePath() { public String getDatabasePath() {
return baseDir + "/valueMemory"; return getBaseDir() + "/valueMemory";
} }
public String getLobCompressionAlgorithm(int type) { public String getLobCompressionAlgorithm(int type) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论