From 5ab3c60555a706a990ff2e48f48d78b449ed3c47 Mon Sep 17 00:00:00 2001 From: James Ahlborn Date: Sun, 24 Aug 2014 03:24:22 +0000 Subject: [PATCH] more test speedups git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@867 f203690c-595d-4dc9-a70b-905162fa7fd2 --- .../jackcess/BigIndexTest.java | 4 +-- .../jackcess/CursorTest.java | 10 ++++++ .../jackcess/DatabaseTest.java | 24 ++++++++++--- .../jackcess/IndexTest.java | 10 ++++++ .../jackcess/impl/CodecHandlerTest.java | 34 ++++++++++++------- 5 files changed, 62 insertions(+), 20 deletions(-) diff --git a/src/test/java/com/healthmarketscience/jackcess/BigIndexTest.java b/src/test/java/com/healthmarketscience/jackcess/BigIndexTest.java index 0485557..d0564f0 100644 --- a/src/test/java/com/healthmarketscience/jackcess/BigIndexTest.java +++ b/src/test/java/com/healthmarketscience/jackcess/BigIndexTest.java @@ -74,7 +74,7 @@ public class BigIndexTest extends TestCase { assertEquals(0, index.getIndexData().getEntryCount()); db.close(); - DatabaseTest._autoSync = false; + DatabaseTest.setTestAutoSync(false); try { String extraText = " some random text to fill out the index and make it fill up pages with lots of extra bytes so i will keep typing until i think that i probably have enough text in the index entry so that i do not need to add as many entries in order"; @@ -203,7 +203,7 @@ public class BigIndexTest extends TestCase { db.close(); } finally { - DatabaseTest._autoSync = Database.DEFAULT_AUTO_SYNC; + DatabaseTest.clearTestAutoSync(); } } } diff --git a/src/test/java/com/healthmarketscience/jackcess/CursorTest.java b/src/test/java/com/healthmarketscience/jackcess/CursorTest.java index cf55e2b..d8beff3 100644 --- a/src/test/java/com/healthmarketscience/jackcess/CursorTest.java +++ b/src/test/java/com/healthmarketscience/jackcess/CursorTest.java @@ -60,6 +60,16 @@ public class CursorTest extends TestCase { super(name); } + @Override + protected void setUp() { + DatabaseTest.setTestAutoSync(false); + } + + @Override + protected void tearDown() { + DatabaseTest.clearTestAutoSync(); + } + private static List> createTestTableData() throws Exception { diff --git a/src/test/java/com/healthmarketscience/jackcess/DatabaseTest.java b/src/test/java/com/healthmarketscience/jackcess/DatabaseTest.java index 3a45f39..e146aef 100644 --- a/src/test/java/com/healthmarketscience/jackcess/DatabaseTest.java +++ b/src/test/java/com/healthmarketscience/jackcess/DatabaseTest.java @@ -85,13 +85,27 @@ public class DatabaseTest extends TestCase public static final TimeZone TEST_TZ = TimeZone.getTimeZone("America/New_York"); - static boolean _autoSync = Database.DEFAULT_AUTO_SYNC; + private static final ThreadLocal _autoSync = + new ThreadLocal(); public DatabaseTest(String name) throws Exception { super(name); } + static void setTestAutoSync(boolean autoSync) { + _autoSync.set(autoSync); + } + + static void clearTestAutoSync() { + _autoSync.remove(); + } + + static boolean getTestAutoSync() { + Boolean autoSync = _autoSync.get(); + return ((autoSync != null) ? autoSync : Database.DEFAULT_AUTO_SYNC); + } + public static Database open(FileFormat fileFormat, File file) throws Exception { @@ -105,7 +119,7 @@ public class DatabaseTest extends TestCase FileChannel channel = (inMem ? MemFileChannel.newChannel(file, "r") : null); final Database db = new DatabaseBuilder(file).setReadOnly(true) - .setAutoSync(_autoSync).setChannel(channel).open(); + .setAutoSync(getTestAutoSync()).setChannel(channel).open(); assertEquals("Wrong JetFormat.", DatabaseImpl.getFileFormatDetails(fileFormat).getFormat(), ((DatabaseImpl)db).getFormat()); @@ -141,7 +155,7 @@ public class DatabaseTest extends TestCase { FileChannel channel = (inMem ? MemFileChannel.newChannel() : null); return new DatabaseBuilder(createTempFile(keep)).setFileFormat(fileFormat) - .setAutoSync(_autoSync).setChannel(channel).create(); + .setAutoSync(getTestAutoSync()).setChannel(channel).create(); } @@ -167,7 +181,7 @@ public class DatabaseTest extends TestCase { File tmp = createTempFile(keep); copyFile(file, tmp); - Database db = new DatabaseBuilder(tmp).setAutoSync(_autoSync).open(); + Database db = new DatabaseBuilder(tmp).setAutoSync(getTestAutoSync()).open(); assertEquals("Wrong JetFormat.", DatabaseImpl.getFileFormatDetails(fileFormat).getFormat(), ((DatabaseImpl)db).getFormat()); @@ -644,7 +658,7 @@ public class DatabaseTest extends TestCase assertTrue(!bogusFile.exists()); try { new DatabaseBuilder(bogusFile).setReadOnly(true). - setAutoSync(_autoSync).open(); + setAutoSync(getTestAutoSync()).open(); fail("FileNotFoundException should have been thrown"); } catch(FileNotFoundException e) { } diff --git a/src/test/java/com/healthmarketscience/jackcess/IndexTest.java b/src/test/java/com/healthmarketscience/jackcess/IndexTest.java index c2d058a..95cb2be 100644 --- a/src/test/java/com/healthmarketscience/jackcess/IndexTest.java +++ b/src/test/java/com/healthmarketscience/jackcess/IndexTest.java @@ -56,6 +56,16 @@ public class IndexTest extends TestCase { super(name); } + @Override + protected void setUp() { + DatabaseTest.setTestAutoSync(false); + } + + @Override + protected void tearDown() { + DatabaseTest.clearTestAutoSync(); + } + public void testByteOrder() throws Exception { byte b1 = (byte)0x00; byte b2 = (byte)0x01; diff --git a/src/test/java/com/healthmarketscience/jackcess/impl/CodecHandlerTest.java b/src/test/java/com/healthmarketscience/jackcess/impl/CodecHandlerTest.java index e680fd9..1db19b5 100644 --- a/src/test/java/com/healthmarketscience/jackcess/impl/CodecHandlerTest.java +++ b/src/test/java/com/healthmarketscience/jackcess/impl/CodecHandlerTest.java @@ -34,16 +34,13 @@ import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.charset.Charset; import java.util.Iterator; -import java.util.Map; import com.healthmarketscience.jackcess.ColumnBuilder; import com.healthmarketscience.jackcess.Cursor; -import com.healthmarketscience.jackcess.CursorBuilder; import com.healthmarketscience.jackcess.DataType; import com.healthmarketscience.jackcess.Database; import com.healthmarketscience.jackcess.DatabaseBuilder; import com.healthmarketscience.jackcess.DatabaseTest; -import com.healthmarketscience.jackcess.DatabaseTest; import com.healthmarketscience.jackcess.IndexBuilder; import com.healthmarketscience.jackcess.Row; import com.healthmarketscience.jackcess.Table; @@ -125,10 +122,16 @@ public class CodecHandlerTest extends TestCase private static void writeData(Table t1, Table t2, int start, int end) throws Exception { + Database db = t1.getDatabase(); + ((DatabaseImpl)db).getPageChannel().startWrite(); + try { for(int i = start; i < end; ++i) { t1.addRow(null, "rowdata-" + i + DatabaseTest.createString(100)); t2.addRow(null, "rowdata-" + i + DatabaseTest.createString(100)); } + } finally { + ((DatabaseImpl)db).getPageChannel().finishWrite(); + } Cursor c1 = t1.newCursor().setIndex(t1.getPrimaryKeyIndex()) .toCursor(); @@ -140,17 +143,22 @@ public class CodecHandlerTest extends TestCase int t1rows = 0; int t2rows = 0; - while(i1.hasNext() || i2.hasNext()) { - if(i1.hasNext()) { - checkRow(i1.next()); - i1.remove(); - ++t1rows; - } - if(i2.hasNext()) { - checkRow(i2.next()); - i2.remove(); - ++t2rows; + ((DatabaseImpl)db).getPageChannel().startWrite(); + try { + while(i1.hasNext() || i2.hasNext()) { + if(i1.hasNext()) { + checkRow(i1.next()); + i1.remove(); + ++t1rows; + } + if(i2.hasNext()) { + checkRow(i2.next()); + i2.remove(); + ++t2rows; + } } + } finally { + ((DatabaseImpl)db).getPageChannel().finishWrite(); } assertEquals(100, t1rows); -- 2.39.5