提交 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 {
......
...@@ -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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论