diff options
Diffstat (limited to 'test')
12 files changed, 2117 insertions, 1850 deletions
diff --git a/test/src/java/com/healthmarketscience/jackcess/BigIndexTest.java b/test/src/java/com/healthmarketscience/jackcess/BigIndexTest.java index 1ce4869..9a0615c 100644 --- a/test/src/java/com/healthmarketscience/jackcess/BigIndexTest.java +++ b/test/src/java/com/healthmarketscience/jackcess/BigIndexTest.java @@ -27,7 +27,6 @@ King of Prussia, PA 19406 package com.healthmarketscience.jackcess; -import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -36,6 +35,7 @@ import java.util.Random; import junit.framework.TestCase; import static com.healthmarketscience.jackcess.DatabaseTest.*; +import static com.healthmarketscience.jackcess.JetFormatTest.*; /** @@ -67,157 +67,160 @@ public class BigIndexTest extends TestCase { public void testComplexIndex() throws Exception { - // this file has an index with "compressed" entries and node pages - Database db = open(new File("test/data/compIndexTest.mdb")); - Table t = db.getTable("Table1"); - Index index = t.getIndex("CD_AGENTE"); - assertFalse(index.isInitialized()); - assertEquals(512, countRows(t)); - assertEquals(512, index.getEntryCount()); - db.close(); + for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.COMP_INDEX)) { + // this file has an index with "compressed" entries and node pages + Database db = open(testDB); + Table t = db.getTable("Table1"); + Index index = t.getIndex("CD_AGENTE"); + assertFalse(index.isInitialized()); + assertEquals(512, countRows(t)); + assertEquals(512, index.getEntryCount()); + db.close(); + } } public void testBigIndex() throws Exception { + for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.BIG_INDEX)) { // this file has an index with "compressed" entries and node pages - File origFile = new File("test/data/bigIndexTest.mdb"); - Database db = open(origFile); - Table t = db.getTable("Table1"); - Index index = t.getIndex("col1"); - assertFalse(index.isInitialized()); - assertEquals(0, countRows(t)); - assertEquals(0, index.getEntryCount()); - db.close(); - - DatabaseTest._autoSync = 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"; - - // copy to temp file and attempt to edit - db = openCopy(origFile); - t = db.getTable("Table1"); - index = t.getIndex("col1"); - - System.out.println("BigIndexTest: Index type: " + index.getClass()); - - // add 2,000 (pseudo) random entries to the table - Random rand = new Random(13L); - for(int i = 0; i < 2000; ++i) { - if((i == 850) || (i == 1850)) { - int end = i + 50; - List<Object[]> rows = new ArrayList<Object[]>(50); - for(; i < end; ++i) { + Database db = open(testDB); + Table t = db.getTable("Table1"); + Index index = t.getIndex("col1"); + assertFalse(index.isInitialized()); + assertEquals(0, countRows(t)); + assertEquals(0, index.getEntryCount()); + db.close(); + + DatabaseTest._autoSync = 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"; + + // copy to temp file and attempt to edit + db = openCopy(testDB); + t = db.getTable("Table1"); + index = t.getIndex("col1"); + + System.out.println("BigIndexTest: Index type: " + index.getClass()); + + // add 2,000 (pseudo) random entries to the table + Random rand = new Random(13L); + for(int i = 0; i < 2000; ++i) { + if((i == 850) || (i == 1850)) { + int end = i + 50; + List<Object[]> rows = new ArrayList<Object[]>(50); + for(; i < end; ++i) { + int nextInt = rand.nextInt(Integer.MAX_VALUE); + String nextVal = "" + nextInt + extraText; + if(((i + 1) % 333) == 0) { + nextVal = null; + } + rows.add(new Object[]{nextVal, + "this is some row data " + nextInt}); + } + t.addRows(rows); + --i; + } else { int nextInt = rand.nextInt(Integer.MAX_VALUE); String nextVal = "" + nextInt + extraText; if(((i + 1) % 333) == 0) { nextVal = null; } - rows.add(new Object[]{nextVal, - "this is some row data " + nextInt}); + t.addRow(nextVal, "this is some row data " + nextInt); } - t.addRows(rows); - --i; - } else { - int nextInt = rand.nextInt(Integer.MAX_VALUE); - String nextVal = "" + nextInt + extraText; - if(((i + 1) % 333) == 0) { - nextVal = null; + } + + ((BigIndex)index).validate(); + + db.flush(); + t = db.getTable("Table1"); + index = t.getIndex("col1"); + + // make sure all entries are there and correctly ordered + String firstValue = " "; + String prevValue = firstValue; + int rowCount = 0; + List<String> firstTwo = new ArrayList<String>(); + for(Map<String,Object> row : Cursor.createIndexCursor(t, index)) { + String origVal = (String)row.get("col1"); + String val = origVal; + if(val == null) { + val = firstValue; } - t.addRow(nextVal, "this is some row data " + nextInt); + assertTrue("" + prevValue + " <= " + val + " " + rowCount, + prevValue.compareTo(val) <= 0); + if(firstTwo.size() < 2) { + firstTwo.add(origVal); + } + prevValue = val; + ++rowCount; } - } - ((BigIndex)index).validate(); - - db.flush(); - t = db.getTable("Table1"); - index = t.getIndex("col1"); - - // make sure all entries are there and correctly ordered - String firstValue = " "; - String prevValue = firstValue; - int rowCount = 0; - List<String> firstTwo = new ArrayList<String>(); - for(Map<String,Object> row : Cursor.createIndexCursor(t, index)) { - String origVal = (String)row.get("col1"); - String val = origVal; - if(val == null) { - val = firstValue; + assertEquals(2000, rowCount); + + ((BigIndex)index).validate(); + + // delete an entry in the middle + Cursor cursor = Cursor.createIndexCursor(t, index); + for(int i = 0; i < (rowCount / 2); ++i) { + assertTrue(cursor.moveToNextRow()); } - assertTrue("" + prevValue + " <= " + val + " " + rowCount, - prevValue.compareTo(val) <= 0); - if(firstTwo.size() < 2) { - firstTwo.add(origVal); + cursor.deleteCurrentRow(); + --rowCount; + + // remove all but the first two entries (from the end) + cursor.afterLast(); + for(int i = 0; i < (rowCount - 2); ++i) { + assertTrue(cursor.moveToPreviousRow()); + cursor.deleteCurrentRow(); } - prevValue = val; - ++rowCount; - } - assertEquals(2000, rowCount); + ((BigIndex)index).validate(); - ((BigIndex)index).validate(); - - // delete an entry in the middle - Cursor cursor = Cursor.createIndexCursor(t, index); - for(int i = 0; i < (rowCount / 2); ++i) { - assertTrue(cursor.moveToNextRow()); - } - cursor.deleteCurrentRow(); - --rowCount; - - // remove all but the first two entries (from the end) - cursor.afterLast(); - for(int i = 0; i < (rowCount - 2); ++i) { - assertTrue(cursor.moveToPreviousRow()); - cursor.deleteCurrentRow(); - } + List<String> found = new ArrayList<String>(); + for(Map<String,Object> row : Cursor.createIndexCursor(t, index)) { + found.add((String)row.get("col1")); + } - ((BigIndex)index).validate(); - - List<String> found = new ArrayList<String>(); - for(Map<String,Object> row : Cursor.createIndexCursor(t, index)) { - found.add((String)row.get("col1")); - } + assertEquals(firstTwo, found); - assertEquals(firstTwo, found); + // remove remaining entries + cursor = Cursor.createCursor(t); + for(int i = 0; i < 2; ++i) { + assertTrue(cursor.moveToNextRow()); + cursor.deleteCurrentRow(); + } - // remove remaining entries - cursor = Cursor.createCursor(t); - for(int i = 0; i < 2; ++i) { - assertTrue(cursor.moveToNextRow()); - cursor.deleteCurrentRow(); - } + assertFalse(cursor.moveToNextRow()); + assertFalse(cursor.moveToPreviousRow()); + + ((BigIndex)index).validate(); + + // add 50 (pseudo) random entries to the table + rand = new Random(42L); + for(int i = 0; i < 50; ++i) { + int nextInt = rand.nextInt(Integer.MAX_VALUE); + String nextVal = "some prefix " + nextInt + extraText; + if(((i + 1) % 3333) == 0) { + nextVal = null; + } + t.addRow(nextVal, "this is some row data " + nextInt); + } + + ((BigIndex)index).validate(); - assertFalse(cursor.moveToNextRow()); - assertFalse(cursor.moveToPreviousRow()); - - ((BigIndex)index).validate(); - - // add 50 (pseudo) random entries to the table - rand = new Random(42L); - for(int i = 0; i < 50; ++i) { - int nextInt = rand.nextInt(Integer.MAX_VALUE); - String nextVal = "some prefix " + nextInt + extraText; - if(((i + 1) % 3333) == 0) { - nextVal = null; + cursor = Cursor.createIndexCursor(t, index); + while(cursor.moveToNextRow()) { + cursor.deleteCurrentRow(); } - t.addRow(nextVal, "this is some row data " + nextInt); - } - ((BigIndex)index).validate(); + ((BigIndex)index).validate(); - cursor = Cursor.createIndexCursor(t, index); - while(cursor.moveToNextRow()) { - cursor.deleteCurrentRow(); + db.close(); + + } finally { + DatabaseTest._autoSync = Database.DEFAULT_AUTO_SYNC; } - - ((BigIndex)index).validate(); - - db.close(); - - } finally { - DatabaseTest._autoSync = Database.DEFAULT_AUTO_SYNC; } } diff --git a/test/src/java/com/healthmarketscience/jackcess/CursorBuilderTest.java b/test/src/java/com/healthmarketscience/jackcess/CursorBuilderTest.java index 2788f65..260238d 100644 --- a/test/src/java/com/healthmarketscience/jackcess/CursorBuilderTest.java +++ b/test/src/java/com/healthmarketscience/jackcess/CursorBuilderTest.java @@ -29,6 +29,8 @@ package com.healthmarketscience.jackcess; import junit.framework.TestCase; +import static com.healthmarketscience.jackcess.JetFormatTest.*; + /** * @author James Ahlborn */ @@ -50,117 +52,119 @@ public class CursorBuilderTest extends TestCase { public void test() throws Exception { - Database db = CursorTest.createTestIndexTable(); - - Table table = db.getTable("test"); - Index idx = table.getIndexes().get(0); - - Cursor expected = Cursor.createCursor(table); - - Cursor found = new CursorBuilder(table).toCursor(); - assertCursor(expected, found); - - expected = Cursor.createIndexCursor(table, idx); - found = new CursorBuilder(table) - .setIndex(idx) - .toCursor(); - assertCursor(expected, found); - - expected = Cursor.createIndexCursor(table, idx); - found = new CursorBuilder(table) - .setIndexByName("id") - .toCursor(); - assertCursor(expected, found); - - try { - new CursorBuilder(table) - .setIndexByName("foo"); - fail("IllegalArgumentException should have been thrown"); - } catch(IllegalArgumentException ignored) { - // success - } - - expected = Cursor.createIndexCursor(table, idx); - found = new CursorBuilder(table) - .setIndexByColumns(table.getColumn("id")) - .toCursor(); - assertCursor(expected, found); - - try { - new CursorBuilder(table) - .setIndexByColumns(table.getColumn("value")); - fail("IllegalArgumentException should have been thrown"); - } catch(IllegalArgumentException ignored) { - // success - } - - try { - new CursorBuilder(table) - .setIndexByColumns(table.getColumn("id"), table.getColumn("value")); - fail("IllegalArgumentException should have been thrown"); - } catch(IllegalArgumentException ignored) { - // success + for (final TestDB indexCursorDB : CursorTest.INDEX_CURSOR_DBS) { + Database db = CursorTest.createTestIndexTable(indexCursorDB); + + Table table = db.getTable("test"); + Index idx = table.getIndexes().get(0); + + Cursor expected = Cursor.createCursor(table); + + Cursor found = new CursorBuilder(table).toCursor(); + assertCursor(expected, found); + + expected = Cursor.createIndexCursor(table, idx); + found = new CursorBuilder(table) + .setIndex(idx) + .toCursor(); + assertCursor(expected, found); + + expected = Cursor.createIndexCursor(table, idx); + found = new CursorBuilder(table) + .setIndexByName("id") + .toCursor(); + assertCursor(expected, found); + + try { + new CursorBuilder(table) + .setIndexByName("foo"); + fail("IllegalArgumentException should have been thrown"); + } catch(IllegalArgumentException ignored) { + // success + } + + expected = Cursor.createIndexCursor(table, idx); + found = new CursorBuilder(table) + .setIndexByColumns(table.getColumn("id")) + .toCursor(); + assertCursor(expected, found); + + try { + new CursorBuilder(table) + .setIndexByColumns(table.getColumn("value")); + fail("IllegalArgumentException should have been thrown"); + } catch(IllegalArgumentException ignored) { + // success + } + + try { + new CursorBuilder(table) + .setIndexByColumns(table.getColumn("id"), table.getColumn("value")); + fail("IllegalArgumentException should have been thrown"); + } catch(IllegalArgumentException ignored) { + // success + } + + expected = Cursor.createCursor(table); + expected.beforeFirst(); + found = new CursorBuilder(table) + .beforeFirst() + .toCursor(); + assertCursor(expected, found); + + expected = Cursor.createCursor(table); + expected.afterLast(); + found = new CursorBuilder(table) + .afterLast() + .toCursor(); + assertCursor(expected, found); + + expected = Cursor.createCursor(table); + expected.moveNextRows(2); + Cursor.Savepoint sp = expected.getSavepoint(); + found = new CursorBuilder(table) + .afterLast() + .restoreSavepoint(sp) + .toCursor(); + assertCursor(expected, found); + + expected = Cursor.createIndexCursor(table, idx); + expected.moveNextRows(2); + sp = expected.getSavepoint(); + found = new CursorBuilder(table) + .setIndex(idx) + .beforeFirst() + .restoreSavepoint(sp) + .toCursor(); + assertCursor(expected, found); + + expected = Cursor.createIndexCursor(table, idx, + idx.constructIndexRowFromEntry(3), + null); + found = new CursorBuilder(table) + .setIndex(idx) + .setStartEntry(3) + .toCursor(); + assertCursor(expected, found); + + expected = Cursor.createIndexCursor(table, idx, + idx.constructIndexRowFromEntry(3), + false, + idx.constructIndexRowFromEntry(7), + false); + found = new CursorBuilder(table) + .setIndex(idx) + .setStartEntry(3) + .setStartRowInclusive(false) + .setEndEntry(7) + .setEndRowInclusive(false) + .toCursor(); + assertCursor(expected, found); + + + + db.close(); } - - expected = Cursor.createCursor(table); - expected.beforeFirst(); - found = new CursorBuilder(table) - .beforeFirst() - .toCursor(); - assertCursor(expected, found); - - expected = Cursor.createCursor(table); - expected.afterLast(); - found = new CursorBuilder(table) - .afterLast() - .toCursor(); - assertCursor(expected, found); - - expected = Cursor.createCursor(table); - expected.moveNextRows(2); - Cursor.Savepoint sp = expected.getSavepoint(); - found = new CursorBuilder(table) - .afterLast() - .restoreSavepoint(sp) - .toCursor(); - assertCursor(expected, found); - - expected = Cursor.createIndexCursor(table, idx); - expected.moveNextRows(2); - sp = expected.getSavepoint(); - found = new CursorBuilder(table) - .setIndex(idx) - .beforeFirst() - .restoreSavepoint(sp) - .toCursor(); - assertCursor(expected, found); - - expected = Cursor.createIndexCursor(table, idx, - idx.constructIndexRowFromEntry(3), - null); - found = new CursorBuilder(table) - .setIndex(idx) - .setStartEntry(3) - .toCursor(); - assertCursor(expected, found); - - expected = Cursor.createIndexCursor(table, idx, - idx.constructIndexRowFromEntry(3), - false, - idx.constructIndexRowFromEntry(7), - false); - found = new CursorBuilder(table) - .setIndex(idx) - .setStartEntry(3) - .setStartRowInclusive(false) - .setEndEntry(7) - .setEndRowInclusive(false) - .toCursor(); - assertCursor(expected, found); - - - - db.close(); } } diff --git a/test/src/java/com/healthmarketscience/jackcess/CursorTest.java b/test/src/java/com/healthmarketscience/jackcess/CursorTest.java index 9cf15c9..63eab0c 100644 --- a/test/src/java/com/healthmarketscience/jackcess/CursorTest.java +++ b/test/src/java/com/healthmarketscience/jackcess/CursorTest.java @@ -27,7 +27,6 @@ King of Prussia, PA 19406 package com.healthmarketscience.jackcess; -import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -37,7 +36,9 @@ import java.util.TreeSet; import junit.framework.TestCase; +import static com.healthmarketscience.jackcess.Database.*; import static com.healthmarketscience.jackcess.DatabaseTest.*; +import static com.healthmarketscience.jackcess.JetFormatTest.*; /** * @author James Ahlborn @@ -70,8 +71,8 @@ public class CursorTest extends TestCase { return expectedRows; } - private static Database createTestTable() throws Exception { - Database db = create(); + private static Database createTestTable(final FileFormat fileFormat) throws Exception { + Database db = create(fileFormat); Table table = new TableBuilder("test") .addColumn(new ColumnBuilder("id", DataType.LONG).toColumn()) @@ -96,16 +97,18 @@ public class CursorTest extends TestCase { } return expectedRows; } - - static Database createTestIndexTable() throws Exception { - Database db = openCopy(new File("test/data/indexCursorTest.mdb")); + + static final TestDB[] INDEX_CURSOR_DBS = TestDB.getSupportedForBasename(Basename.INDEX_CURSOR); + + static Database createTestIndexTable(final TestDB indexCursorDB) throws Exception { + Database db = openCopy(indexCursorDB); Table table = db.getTable("test"); for(Map<String,Object> row : createUnorderedTestTableData()) { table.addRow(row.get("id"), row.get("value")); } - + return db; } @@ -139,16 +142,18 @@ public class CursorTest extends TestCase { } public void testSimple() throws Exception { - Database db = createTestTable(); + for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { + Database db = createTestTable(fileFormat); - Table table = db.getTable("test"); - Cursor cursor = Cursor.createCursor(table); - doTestSimple(table, cursor, null); - db.close(); + Table table = db.getTable("test"); + Cursor cursor = Cursor.createCursor(table); + doTestSimple(cursor, null); + db.close(); + } } - private void doTestSimple(Table table, Cursor cursor, - List<Map<String,Object>> expectedRows) + private void doTestSimple(Cursor cursor, + List<Map<String, Object>> expectedRows) throws Exception { if(expectedRows == null) { @@ -164,17 +169,19 @@ public class CursorTest extends TestCase { } public void testMove() throws Exception { - Database db = createTestTable(); + for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { + Database db = createTestTable(fileFormat); - Table table = db.getTable("test"); - Cursor cursor = Cursor.createCursor(table); - doTestMove(table, cursor, null); - - db.close(); + Table table = db.getTable("test"); + Cursor cursor = Cursor.createCursor(table); + doTestMove(cursor, null); + + db.close(); + } } - private void doTestMove(Table table, Cursor cursor, - List<Map<String,Object>> expectedRows) + private void doTestMove(Cursor cursor, + List<Map<String, Object>> expectedRows) throws Exception { if(expectedRows == null) { @@ -221,13 +228,15 @@ public class CursorTest extends TestCase { } public void testSearch() throws Exception { - Database db = createTestTable(); + for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { + Database db = createTestTable(fileFormat); - Table table = db.getTable("test"); - Cursor cursor = Cursor.createCursor(table); - doTestSearch(table, cursor, null, 42, -13); - - db.close(); + Table table = db.getTable("test"); + Cursor cursor = Cursor.createCursor(table); + doTestSearch(table, cursor, null, 42, -13); + + db.close(); + } } private void doTestSearch(Table table, Cursor cursor, Index index, @@ -303,17 +312,19 @@ public class CursorTest extends TestCase { } public void testReverse() throws Exception { - Database db = createTestTable(); + for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { + Database db = createTestTable(fileFormat); - Table table = db.getTable("test"); - Cursor cursor = Cursor.createCursor(table); - doTestReverse(table, cursor, null); + Table table = db.getTable("test"); + Cursor cursor = Cursor.createCursor(table); + doTestReverse(cursor, null); - db.close(); + db.close(); + } } - private void doTestReverse(Table table, Cursor cursor, - List<Map<String,Object>> expectedRows) + private void doTestReverse(Cursor cursor, + List<Map<String, Object>> expectedRows) throws Exception { if(expectedRows == null) { @@ -330,15 +341,17 @@ public class CursorTest extends TestCase { } public void testLiveAddition() throws Exception { - Database db = createTestTable(); + for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { + Database db = createTestTable(fileFormat); - Table table = db.getTable("test"); + Table table = db.getTable("test"); - Cursor cursor1 = Cursor.createCursor(table); - Cursor cursor2 = Cursor.createCursor(table); - doTestLiveAddition(table, cursor1, cursor2, 11); - - db.close(); + Cursor cursor1 = Cursor.createCursor(table); + Cursor cursor2 = Cursor.createCursor(table); + doTestLiveAddition(table, cursor1, cursor2, 11); + + db.close(); + } } private void doTestLiveAddition(Table table, @@ -369,25 +382,27 @@ public class CursorTest extends TestCase { public void testLiveDeletion() throws Exception { - Database db = createTestTable(); + for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { + Database db = createTestTable(fileFormat); - Table table = db.getTable("test"); + Table table = db.getTable("test"); - Cursor cursor1 = Cursor.createCursor(table); - Cursor cursor2 = Cursor.createCursor(table); - Cursor cursor3 = Cursor.createCursor(table); - Cursor cursor4 = Cursor.createCursor(table); - doTestLiveDeletion(table, cursor1, cursor2, cursor3, cursor4, 1); - - db.close(); + Cursor cursor1 = Cursor.createCursor(table); + Cursor cursor2 = Cursor.createCursor(table); + Cursor cursor3 = Cursor.createCursor(table); + Cursor cursor4 = Cursor.createCursor(table); + doTestLiveDeletion(cursor1, cursor2, cursor3, cursor4, 1); + + db.close(); + } } - private void doTestLiveDeletion(Table table, - Cursor cursor1, - Cursor cursor2, - Cursor cursor3, - Cursor cursor4, - int firstValue) throws Exception + private void doTestLiveDeletion( + Cursor cursor1, + Cursor cursor2, + Cursor cursor3, + Cursor cursor4, + int firstValue) throws Exception { assertEquals(2, cursor1.moveNextRows(2)); assertEquals(3, cursor2.moveNextRows(3)); @@ -460,220 +475,246 @@ public class CursorTest extends TestCase { } public void testSimpleIndex() throws Exception { - Database db = createTestIndexTable(); + for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { + Database db = createTestIndexTable(indexCursorDB); - Table table = db.getTable("test"); - Index idx = table.getIndexes().get(0); + Table table = db.getTable("test"); + Index idx = table.getIndexes().get(0); - assertTable(createUnorderedTestTableData(), table); + assertTable(createUnorderedTestTableData(), table); - Cursor cursor = Cursor.createIndexCursor(table, idx); - doTestSimple(table, cursor, null); + Cursor cursor = Cursor.createIndexCursor(table, idx); + doTestSimple(cursor, null); - db.close(); + db.close(); + } } public void testMoveIndex() throws Exception { - Database db = createTestIndexTable(); + for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { + Database db = createTestIndexTable(indexCursorDB); - Table table = db.getTable("test"); - Index idx = table.getIndexes().get(0); - Cursor cursor = Cursor.createIndexCursor(table, idx); - doTestMove(table, cursor, null); - - db.close(); + Table table = db.getTable("test"); + Index idx = table.getIndexes().get(0); + Cursor cursor = Cursor.createIndexCursor(table, idx); + doTestMove(cursor, null); + + db.close(); + } } public void testReverseIndex() throws Exception { - Database db = createTestIndexTable(); + for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { + Database db = createTestIndexTable(indexCursorDB); - Table table = db.getTable("test"); - Index idx = table.getIndexes().get(0); - Cursor cursor = Cursor.createIndexCursor(table, idx); - doTestReverse(table, cursor, null); + Table table = db.getTable("test"); + Index idx = table.getIndexes().get(0); + Cursor cursor = Cursor.createIndexCursor(table, idx); + doTestReverse(cursor, null); - db.close(); + db.close(); + } } public void testSearchIndex() throws Exception { - Database db = createTestIndexTable(); + for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { + Database db = createTestIndexTable(indexCursorDB); - Table table = db.getTable("test"); - Index idx = table.getIndexes().get(0); - Cursor cursor = Cursor.createIndexCursor(table, idx); - doTestSearch(table, cursor, idx, 42, -13); - - db.close(); + Table table = db.getTable("test"); + Index idx = table.getIndexes().get(0); + Cursor cursor = Cursor.createIndexCursor(table, idx); + doTestSearch(table, cursor, idx, 42, -13); + + db.close(); + } } public void testLiveAdditionIndex() throws Exception { - Database db = createTestIndexTable(); + for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { + Database db = createTestIndexTable(indexCursorDB); - Table table = db.getTable("test"); - Index idx = table.getIndexes().get(0); + Table table = db.getTable("test"); + Index idx = table.getIndexes().get(0); - Cursor cursor1 = Cursor.createIndexCursor(table, idx); - Cursor cursor2 = Cursor.createIndexCursor(table, idx); - doTestLiveAddition(table, cursor1, cursor2, 11); - - db.close(); + Cursor cursor1 = Cursor.createIndexCursor(table, idx); + Cursor cursor2 = Cursor.createIndexCursor(table, idx); + doTestLiveAddition(table, cursor1, cursor2, 11); + + db.close(); + } } public void testLiveDeletionIndex() throws Exception { - Database db = createTestIndexTable(); + for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { + Database db = createTestIndexTable(indexCursorDB); - Table table = db.getTable("test"); - Index idx = table.getIndexes().get(0); + Table table = db.getTable("test"); + Index idx = table.getIndexes().get(0); - Cursor cursor1 = Cursor.createIndexCursor(table, idx); - Cursor cursor2 = Cursor.createIndexCursor(table, idx); - Cursor cursor3 = Cursor.createIndexCursor(table, idx); - Cursor cursor4 = Cursor.createIndexCursor(table, idx); - doTestLiveDeletion(table, cursor1, cursor2, cursor3, cursor4, 1); - - db.close(); + Cursor cursor1 = Cursor.createIndexCursor(table, idx); + Cursor cursor2 = Cursor.createIndexCursor(table, idx); + Cursor cursor3 = Cursor.createIndexCursor(table, idx); + Cursor cursor4 = Cursor.createIndexCursor(table, idx); + doTestLiveDeletion(cursor1, cursor2, cursor3, cursor4, 1); + + db.close(); + } } public void testSimpleIndexSubRange() throws Exception { - for(int i = 0; i < 2; ++i) { - Database db = createTestIndexTable(); + for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { + for(int i = 0; i < 2; ++i) { + Database db = createTestIndexTable(indexCursorDB); - Table table = db.getTable("test"); - Index idx = table.getIndexes().get(0); + Table table = db.getTable("test"); + Index idx = table.getIndexes().get(0); - Cursor cursor = createIndexSubRangeCursor(table, idx, i); + Cursor cursor = createIndexSubRangeCursor(table, idx, i); - List<Map<String,Object>> expectedRows = - createTestTableData(3, 9); + List<Map<String,Object>> expectedRows = + createTestTableData(3, 9); - doTestSimple(table, cursor, expectedRows); - - db.close(); + doTestSimple(cursor, expectedRows); + + db.close(); + } } } public void testMoveIndexSubRange() throws Exception { - for(int i = 0; i < 2; ++i) { - Database db = createTestIndexTable(); + for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { + for(int i = 0; i < 2; ++i) { + Database db = createTestIndexTable(indexCursorDB); - Table table = db.getTable("test"); - Index idx = table.getIndexes().get(0); + Table table = db.getTable("test"); + Index idx = table.getIndexes().get(0); - Cursor cursor = createIndexSubRangeCursor(table, idx, i); + Cursor cursor = createIndexSubRangeCursor(table, idx, i); - List<Map<String,Object>> expectedRows = - createTestTableData(3, 9); + List<Map<String,Object>> expectedRows = + createTestTableData(3, 9); - doTestMove(table, cursor, expectedRows); - - db.close(); + doTestMove(cursor, expectedRows); + + db.close(); + } } } public void testSearchIndexSubRange() throws Exception { - for(int i = 0; i < 2; ++i) { - Database db = createTestIndexTable(); + for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { + for(int i = 0; i < 2; ++i) { + Database db = createTestIndexTable(indexCursorDB); - Table table = db.getTable("test"); - Index idx = table.getIndexes().get(0); + Table table = db.getTable("test"); + Index idx = table.getIndexes().get(0); - Cursor cursor = createIndexSubRangeCursor(table, idx, i); + Cursor cursor = createIndexSubRangeCursor(table, idx, i); - doTestSearch(table, cursor, idx, 2, 9); - - db.close(); + doTestSearch(table, cursor, idx, 2, 9); + + db.close(); + } } } public void testReverseIndexSubRange() throws Exception { - for(int i = 0; i < 2; ++i) { - Database db = createTestIndexTable(); + for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { + for(int i = 0; i < 2; ++i) { + Database db = createTestIndexTable(indexCursorDB); - Table table = db.getTable("test"); - Index idx = table.getIndexes().get(0); + Table table = db.getTable("test"); + Index idx = table.getIndexes().get(0); - Cursor cursor = createIndexSubRangeCursor(table, idx, i); + Cursor cursor = createIndexSubRangeCursor(table, idx, i); - List<Map<String,Object>> expectedRows = - createTestTableData(3, 9); + List<Map<String,Object>> expectedRows = + createTestTableData(3, 9); - doTestReverse(table, cursor, expectedRows); + doTestReverse(cursor, expectedRows); - db.close(); + db.close(); + } } } public void testLiveAdditionIndexSubRange() throws Exception { - for(int i = 0; i < 2; ++i) { - Database db = createTestIndexTable(); + for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { + for(int i = 0; i < 2; ++i) { + Database db = createTestIndexTable(indexCursorDB); - Table table = db.getTable("test"); - Index idx = table.getIndexes().get(0); + Table table = db.getTable("test"); + Index idx = table.getIndexes().get(0); - Cursor cursor1 = createIndexSubRangeCursor(table, idx, i); - Cursor cursor2 = createIndexSubRangeCursor(table, idx, i); + Cursor cursor1 = createIndexSubRangeCursor(table, idx, i); + Cursor cursor2 = createIndexSubRangeCursor(table, idx, i); - doTestLiveAddition(table, cursor1, cursor2, 8); - - db.close(); + doTestLiveAddition(table, cursor1, cursor2, 8); + + db.close(); + } } } public void testLiveDeletionIndexSubRange() throws Exception { - for(int i = 0; i < 2; ++i) { - Database db = createTestIndexTable(); + for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { + for(int i = 0; i < 2; ++i) { + Database db = createTestIndexTable(indexCursorDB); - Table table = db.getTable("test"); - Index idx = table.getIndexes().get(0); + Table table = db.getTable("test"); + Index idx = table.getIndexes().get(0); - Cursor cursor1 = createIndexSubRangeCursor(table, idx, i); - Cursor cursor2 = createIndexSubRangeCursor(table, idx, i); - Cursor cursor3 = createIndexSubRangeCursor(table, idx, i); - Cursor cursor4 = createIndexSubRangeCursor(table, idx, i); + Cursor cursor1 = createIndexSubRangeCursor(table, idx, i); + Cursor cursor2 = createIndexSubRangeCursor(table, idx, i); + Cursor cursor3 = createIndexSubRangeCursor(table, idx, i); + Cursor cursor4 = createIndexSubRangeCursor(table, idx, i); - doTestLiveDeletion(table, cursor1, cursor2, cursor3, cursor4, 4); + doTestLiveDeletion(cursor1, cursor2, cursor3, cursor4, 4); - db.close(); - } + db.close(); + } + } } public void testId() throws Exception { - Database db = createTestIndexTable(); + for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { + Database db = createTestIndexTable(indexCursorDB); - Table table = db.getTable("test"); - Index idx = table.getIndexes().get(0); + Table table = db.getTable("test"); + Index idx = table.getIndexes().get(0); - Cursor tCursor = Cursor.createCursor(table); - Cursor iCursor = Cursor.createIndexCursor(table, idx); + Cursor tCursor = Cursor.createCursor(table); + Cursor iCursor = Cursor.createIndexCursor(table, idx); - Cursor.Savepoint tSave = tCursor.getSavepoint(); - Cursor.Savepoint iSave = iCursor.getSavepoint(); + Cursor.Savepoint tSave = tCursor.getSavepoint(); + Cursor.Savepoint iSave = iCursor.getSavepoint(); - tCursor.restoreSavepoint(tSave); - iCursor.restoreSavepoint(iSave); + tCursor.restoreSavepoint(tSave); + iCursor.restoreSavepoint(iSave); - try { - tCursor.restoreSavepoint(iSave); - fail("IllegalArgumentException should have been thrown"); - } catch(IllegalArgumentException e) { - // success - } + try { + tCursor.restoreSavepoint(iSave); + fail("IllegalArgumentException should have been thrown"); + } catch(IllegalArgumentException e) { + // success + } - try { - iCursor.restoreSavepoint(tSave); - fail("IllegalArgumentException should have been thrown"); - } catch(IllegalArgumentException e) { - // success - } + try { + iCursor.restoreSavepoint(tSave); + fail("IllegalArgumentException should have been thrown"); + } catch(IllegalArgumentException e) { + // success + } - Cursor tCursor2 = Cursor.createCursor(table); - Cursor iCursor2 = Cursor.createIndexCursor(table, idx); + Cursor tCursor2 = Cursor.createCursor(table); + Cursor iCursor2 = Cursor.createIndexCursor(table, idx); - tCursor2.restoreSavepoint(tSave); - iCursor2.restoreSavepoint(iSave); + tCursor2.restoreSavepoint(tSave); + iCursor2.restoreSavepoint(iSave); - db.close(); + db.close(); + } } } diff --git a/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java b/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java index 46432e4..ba5e4c0 100644 --- a/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java +++ b/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java @@ -55,122 +55,133 @@ import java.util.UUID; import junit.framework.TestCase; +import static com.healthmarketscience.jackcess.Database.*; +import static com.healthmarketscience.jackcess.JetFormatTest.*; + /** * @author Tim McCune */ public class DatabaseTest extends TestCase { static boolean _autoSync = Database.DEFAULT_AUTO_SYNC; - + + public DatabaseTest(String name) throws Exception { super(name); } - - public static Database open() throws Exception { - return open(new File("test/data/test.mdb")); - } - - public static Database open(File file) throws Exception { - return Database.open(file, true, _autoSync); + + public static Database open(final TestDB testDB) throws Exception { + final Database db = Database.open(testDB.getFile(), true, _autoSync); + assertEquals("Wrong JetFormat.", testDB.getExpectedFormat(), db.getFormat()); + return db; } - - public static Database create() throws Exception { - return create(false); + + public static Database create(final Database.FileFormat fileFormat) throws Exception { + return create(fileFormat, false); } - public static Database create(boolean keep) throws Exception { - return Database.create(createTempFile(keep), _autoSync); + public static Database create(final Database.FileFormat fileFormat, boolean keep) throws Exception { + return Database.create(fileFormat, createTempFile(keep), _autoSync); } - public static Database openCopy(File srcFile) throws Exception { - return openCopy(srcFile, false); + + public static Database openCopy(final TestDB testDB) throws Exception { + return openCopy(testDB, false); } - - public static Database openCopy(File srcFile, boolean keep) - throws Exception + + + public static Database openCopy(final TestDB testDB, boolean keep) + throws Exception { File tmp = createTempFile(keep); - copyFile(srcFile, tmp); + copyFile(testDB.getFile(), tmp); return Database.open(tmp, false, _autoSync); } - + + public void testInvalidTableDefs() throws Exception { - Database db = create(); + for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { + Database db = create(fileFormat); + + try { + db.createTable("test", Collections.<Column>emptyList()); + fail("created table with no columns?"); + } catch(IllegalArgumentException e) { + // success + } - try { - db.createTable("test", Collections.<Column>emptyList()); - fail("created table with no columns?"); - } catch(IllegalArgumentException e) { - // success - } + try { + new TableBuilder("test") + .addColumn(new ColumnBuilder("A", DataType.TEXT).toColumn()) + .addColumn(new ColumnBuilder("a", DataType.MEMO).toColumn()) + .toTable(db); + fail("created table with duplicate column names?"); + } catch(IllegalArgumentException e) { + // success + } - try { - new TableBuilder("test") - .addColumn(new ColumnBuilder("A", DataType.TEXT).toColumn()) - .addColumn(new ColumnBuilder("a", DataType.MEMO).toColumn()) - .toTable(db); - fail("created table with duplicate column names?"); - } catch(IllegalArgumentException e) { - // success - } + try { + new TableBuilder("test") + .addColumn(new ColumnBuilder("A", DataType.TEXT) + .setLengthInUnits(352).toColumn()) + .toTable(db); + fail("created table with invalid column length?"); + } catch(IllegalArgumentException e) { + // success + } - try { - new TableBuilder("test") - .addColumn(new ColumnBuilder("A", DataType.TEXT) - .setLengthInUnits(352).toColumn()) - .toTable(db); - fail("created table with invalid column length?"); - } catch(IllegalArgumentException e) { - // success - } + try { + new TableBuilder("test") + .addColumn(new ColumnBuilder("A_" + createString(70), DataType.TEXT) + .toColumn()) + .toTable(db); + fail("created table with too long column name?"); + } catch(IllegalArgumentException e) { + // success + } - try { new TableBuilder("test") - .addColumn(new ColumnBuilder("A_" + createString(70), DataType.TEXT) - .toColumn()) + .addColumn(new ColumnBuilder("A", DataType.TEXT).toColumn()) .toTable(db); - fail("created table with too long column name?"); - } catch(IllegalArgumentException e) { - // success - } - new TableBuilder("test") - .addColumn(new ColumnBuilder("A", DataType.TEXT).toColumn()) - .toTable(db); - - try { - new TableBuilder("Test") - .addColumn(new ColumnBuilder("A", DataType.TEXT).toColumn()) - .toTable(db); - fail("create duplicate tables?"); - } catch(IllegalArgumentException e) { - // success + try { + new TableBuilder("Test") + .addColumn(new ColumnBuilder("A", DataType.TEXT).toColumn()) + .toTable(db); + fail("create duplicate tables?"); + } catch(IllegalArgumentException e) { + // success + } } - } public void testReadDeletedRows() throws Exception { - Table table = open(new File("test/data/delTest.mdb")).getTable("Table"); - int rows = 0; - while (table.getNextRow() != null) { - rows++; + for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.DEL)) { + Table table = open(testDB).getTable("Table"); + int rows = 0; + while (table.getNextRow() != null) { + rows++; + } + assertEquals(2, rows); } - assertEquals(2, rows); } public void testGetColumns() throws Exception { - List<Column> columns = open().getTable("Table1").getColumns(); - assertEquals(9, columns.size()); - checkColumn(columns, 0, "A", DataType.TEXT); - checkColumn(columns, 1, "B", DataType.TEXT); - checkColumn(columns, 2, "C", DataType.BYTE); - checkColumn(columns, 3, "D", DataType.INT); - checkColumn(columns, 4, "E", DataType.LONG); - checkColumn(columns, 5, "F", DataType.DOUBLE); - checkColumn(columns, 6, "G", DataType.SHORT_DATE_TIME); - checkColumn(columns, 7, "H", DataType.MONEY); - checkColumn(columns, 8, "I", DataType.BOOLEAN); + for (final TestDB testDB : SUPPORTED_DBS_TEST) { + + List<Column> columns = open(testDB).getTable("Table1").getColumns(); + assertEquals(9, columns.size()); + checkColumn(columns, 0, "A", DataType.TEXT); + checkColumn(columns, 1, "B", DataType.TEXT); + checkColumn(columns, 2, "C", DataType.BYTE); + checkColumn(columns, 3, "D", DataType.INT); + checkColumn(columns, 4, "E", DataType.LONG); + checkColumn(columns, 5, "F", DataType.DOUBLE); + checkColumn(columns, 6, "G", DataType.SHORT_DATE_TIME); + checkColumn(columns, 7, "H", DataType.MONEY); + checkColumn(columns, 8, "I", DataType.BOOLEAN); + } } static void checkColumn(List<Column> columns, int columnNumber, String name, @@ -183,285 +194,300 @@ public class DatabaseTest extends TestCase { } public void testGetNextRow() throws Exception { - Database db = open(); - assertEquals(4, db.getTableNames().size()); - Table table = db.getTable("Table1"); - - Map<String, Object> row = table.getNextRow(); - assertEquals("abcdefg", row.get("A")); - assertEquals("hijklmnop", row.get("B")); - assertEquals(new Byte((byte) 2), row.get("C")); - assertEquals(new Short((short) 222), row.get("D")); - assertEquals(new Integer(333333333), row.get("E")); - assertEquals(new Double(444.555d), row.get("F")); - Calendar cal = Calendar.getInstance(); - cal.setTime((Date) row.get("G")); - assertEquals(Calendar.SEPTEMBER, cal.get(Calendar.MONTH)); - assertEquals(21, cal.get(Calendar.DAY_OF_MONTH)); - assertEquals(1974, cal.get(Calendar.YEAR)); - assertEquals(0, cal.get(Calendar.HOUR_OF_DAY)); - assertEquals(0, cal.get(Calendar.MINUTE)); - assertEquals(0, cal.get(Calendar.SECOND)); - assertEquals(0, cal.get(Calendar.MILLISECOND)); - assertEquals(Boolean.TRUE, row.get("I")); - - row = table.getNextRow(); - assertEquals("a", row.get("A")); - assertEquals("b", row.get("B")); - assertEquals(new Byte((byte) 0), row.get("C")); - assertEquals(new Short((short) 0), row.get("D")); - assertEquals(new Integer(0), row.get("E")); - assertEquals(new Double(0d), row.get("F")); - cal = Calendar.getInstance(); - cal.setTime((Date) row.get("G")); - assertEquals(Calendar.DECEMBER, cal.get(Calendar.MONTH)); - assertEquals(12, cal.get(Calendar.DAY_OF_MONTH)); - assertEquals(1981, cal.get(Calendar.YEAR)); - assertEquals(0, cal.get(Calendar.HOUR_OF_DAY)); - assertEquals(0, cal.get(Calendar.MINUTE)); - assertEquals(0, cal.get(Calendar.SECOND)); - assertEquals(0, cal.get(Calendar.MILLISECOND)); - assertEquals(Boolean.FALSE, row.get("I")); + for (final TestDB testDB : SUPPORTED_DBS_TEST) { + Database db = open(testDB); + assertEquals(4, db.getTableNames().size()); + Table table = db.getTable("Table1"); + + Map<String, Object> row = table.getNextRow(); + assertEquals("testDB: " + testDB, "abcdefg", row.get("A")); // @todo currently fails w/ v2007 + assertEquals("hijklmnop", row.get("B")); + assertEquals(new Byte((byte) 2), row.get("C")); + assertEquals(new Short((short) 222), row.get("D")); + assertEquals(new Integer(333333333), row.get("E")); + assertEquals(new Double(444.555d), row.get("F")); + Calendar cal = Calendar.getInstance(); + cal.setTime((Date) row.get("G")); + assertEquals(Calendar.SEPTEMBER, cal.get(Calendar.MONTH)); + assertEquals(21, cal.get(Calendar.DAY_OF_MONTH)); + assertEquals(1974, cal.get(Calendar.YEAR)); + assertEquals(0, cal.get(Calendar.HOUR_OF_DAY)); + assertEquals(0, cal.get(Calendar.MINUTE)); + assertEquals(0, cal.get(Calendar.SECOND)); + assertEquals(0, cal.get(Calendar.MILLISECOND)); + assertEquals(Boolean.TRUE, row.get("I")); + + row = table.getNextRow(); + assertEquals("a", row.get("A")); + assertEquals("b", row.get("B")); + assertEquals(new Byte((byte) 0), row.get("C")); + assertEquals(new Short((short) 0), row.get("D")); + assertEquals(new Integer(0), row.get("E")); + assertEquals(new Double(0d), row.get("F")); + cal = Calendar.getInstance(); + cal.setTime((Date) row.get("G")); + assertEquals(Calendar.DECEMBER, cal.get(Calendar.MONTH)); + assertEquals(12, cal.get(Calendar.DAY_OF_MONTH)); + assertEquals(1981, cal.get(Calendar.YEAR)); + assertEquals(0, cal.get(Calendar.HOUR_OF_DAY)); + assertEquals(0, cal.get(Calendar.MINUTE)); + assertEquals(0, cal.get(Calendar.SECOND)); + assertEquals(0, cal.get(Calendar.MILLISECOND)); + assertEquals(Boolean.FALSE, row.get("I")); + } } public void testCreate() throws Exception { - Database db = create(); - assertEquals(0, db.getTableNames().size()); + for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { + Database db = create(fileFormat); + assertEquals(0, db.getTableNames().size()); + } } public void testWriteAndRead() throws Exception { - Database db = create(); - createTestTable(db); - Object[] row = createTestRow(); - row[3] = null; - Table table = db.getTable("Test"); - int count = 1000; - for (int i = 0; i < count; i++) { - table.addRow(row); - } - for (int i = 0; i < count; i++) { - Map<String, Object> readRow = table.getNextRow(); - assertEquals(row[0], readRow.get("A")); - assertEquals(row[1], readRow.get("B")); - assertEquals(row[2], readRow.get("C")); - assertEquals(row[3], readRow.get("D")); - assertEquals(row[4], readRow.get("E")); - assertEquals(row[5], readRow.get("F")); - assertEquals(row[6], readRow.get("G")); - assertEquals(row[7], readRow.get("H")); + for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { + Database db = create(fileFormat); + createTestTable(db); + Object[] row = createTestRow(); + row[3] = null; + Table table = db.getTable("Test"); + int count = 1000; + for (int i = 0; i < count; i++) { + table.addRow(row); + } + for (int i = 0; i < count; i++) { + Map<String, Object> readRow = table.getNextRow(); + assertEquals(row[0], readRow.get("A")); + assertEquals(row[1], readRow.get("B")); + assertEquals(row[2], readRow.get("C")); + assertEquals(row[3], readRow.get("D")); + assertEquals(row[4], readRow.get("E")); + assertEquals(row[5], readRow.get("F")); + assertEquals(row[6], readRow.get("G")); + assertEquals(row[7], readRow.get("H")); + } } } public void testWriteAndReadInBatch() throws Exception { - Database db = create(); - createTestTable(db); - int count = 1000; - List<Object[]> rows = new ArrayList<Object[]>(count); - Object[] row = createTestRow(); - for (int i = 0; i < count; i++) { - rows.add(row); - } - Table table = db.getTable("Test"); - table.addRows(rows); - for (int i = 0; i < count; i++) { - Map<String, Object> readRow = table.getNextRow(); - assertEquals(row[0], readRow.get("A")); - assertEquals(row[1], readRow.get("B")); - assertEquals(row[2], readRow.get("C")); - assertEquals(row[3], readRow.get("D")); - assertEquals(row[4], readRow.get("E")); - assertEquals(row[5], readRow.get("F")); - assertEquals(row[6], readRow.get("G")); - assertEquals(row[7], readRow.get("H")); + for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { + Database db = create(fileFormat); + createTestTable(db); + int count = 1000; + List<Object[]> rows = new ArrayList<Object[]>(count); + Object[] row = createTestRow(); + for (int i = 0; i < count; i++) { + rows.add(row); + } + Table table = db.getTable("Test"); + table.addRows(rows); + for (int i = 0; i < count; i++) { + Map<String, Object> readRow = table.getNextRow(); + assertEquals(row[0], readRow.get("A")); + assertEquals(row[1], readRow.get("B")); + assertEquals(row[2], readRow.get("C")); + assertEquals(row[3], readRow.get("D")); + assertEquals(row[4], readRow.get("E")); + assertEquals(row[5], readRow.get("F")); + assertEquals(row[6], readRow.get("G")); + assertEquals(row[7], readRow.get("H")); + } } } public void testDeleteCurrentRow() throws Exception { // make sure correct row is deleted - Database db = create(); - createTestTable(db); - Object[] row1 = createTestRow("Tim1"); - Object[] row2 = createTestRow("Tim2"); - Object[] row3 = createTestRow("Tim3"); - Table table = db.getTable("Test"); - table.addRows(Arrays.asList(row1, row2, row3)); - assertRowCount(3, table); - - table.reset(); - table.getNextRow(); - table.getNextRow(); - table.deleteCurrentRow(); - - table.reset(); - - Map<String, Object> outRow = table.getNextRow(); - assertEquals("Tim1", outRow.get("A")); - outRow = table.getNextRow(); - assertEquals("Tim3", outRow.get("A")); - assertRowCount(2, table); - - // test multi row delete/add - db = create(); - createTestTable(db); - Object[] row = createTestRow(); - table = db.getTable("Test"); - for (int i = 0; i < 10; i++) { - row[3] = i; - table.addRow(row); - } - row[3] = 1974; - assertRowCount(10, table); - table.reset(); - table.getNextRow(); - table.deleteCurrentRow(); - assertRowCount(9, table); - table.reset(); - table.getNextRow(); - table.deleteCurrentRow(); - assertRowCount(8, table); - table.reset(); - for (int i = 0; i < 8; i++) { + for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { + Database db = create(fileFormat); + createTestTable(db); + Object[] row1 = createTestRow("Tim1"); + Object[] row2 = createTestRow("Tim2"); + Object[] row3 = createTestRow("Tim3"); + Table table = db.getTable("Test"); + table.addRows(Arrays.asList(row1, row2, row3)); + assertRowCount(3, table); + + table.reset(); table.getNextRow(); - } - table.deleteCurrentRow(); - assertRowCount(7, table); - table.addRow(row); - assertRowCount(8, table); - table.reset(); - for (int i = 0; i < 3; i++) { table.getNextRow(); + table.deleteCurrentRow(); + + table.reset(); + + Map<String, Object> outRow = table.getNextRow(); + assertEquals("Tim1", outRow.get("A")); + outRow = table.getNextRow(); + assertEquals("Tim3", outRow.get("A")); + assertRowCount(2, table); + + // test multi row delete/add + db = create(fileFormat); + createTestTable(db); + Object[] row = createTestRow(); + table = db.getTable("Test"); + for (int i = 0; i < 10; i++) { + row[3] = i; + table.addRow(row); + } + row[3] = 1974; + assertRowCount(10, table); + table.reset(); + table.getNextRow(); + table.deleteCurrentRow(); + assertRowCount(9, table); + table.reset(); + table.getNextRow(); + table.deleteCurrentRow(); + assertRowCount(8, table); + table.reset(); + for (int i = 0; i < 8; i++) { + table.getNextRow(); + } + table.deleteCurrentRow(); + assertRowCount(7, table); + table.addRow(row); + assertRowCount(8, table); + table.reset(); + for (int i = 0; i < 3; i++) { + table.getNextRow(); + } + table.deleteCurrentRow(); + assertRowCount(7, table); + table.reset(); + assertEquals(2, table.getNextRow().get("D")); } - table.deleteCurrentRow(); - assertRowCount(7, table); - table.reset(); - assertEquals(2, table.getNextRow().get("D")); } public void testReadLongValue() throws Exception { - Database db = open(new File("test/data/test2.mdb")); - Table table = db.getTable("MSP_PROJECTS"); - Map<String, Object> row = table.getNextRow(); - assertEquals("Jon Iles this is a a vawesrasoih aksdkl fas dlkjflkasjd flkjaslkdjflkajlksj dfl lkasjdf lkjaskldfj lkas dlk lkjsjdfkl; aslkdf lkasjkldjf lka skldf lka sdkjfl;kasjd falksjdfljaslkdjf laskjdfk jalskjd flkj aslkdjflkjkjasljdflkjas jf;lkasjd fjkas dasdf asd fasdf asdf asdmhf lksaiyudfoi jasodfj902384jsdf9 aw90se fisajldkfj lkasj dlkfslkd jflksjadf as", row.get("PROJ_PROP_AUTHOR")); - assertEquals("T", row.get("PROJ_PROP_COMPANY")); - assertEquals("Standard", row.get("PROJ_INFO_CAL_NAME")); - assertEquals("Project1", row.get("PROJ_PROP_TITLE")); - byte[] foundBinaryData = (byte[])row.get("RESERVED_BINARY_DATA"); - byte[] expectedBinaryData = - toByteArray(new File("test/data/test2BinData.dat")); - assertTrue(Arrays.equals(expectedBinaryData, foundBinaryData)); + for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.TEST2)) { + Database db = open(testDB); + Table table = db.getTable("MSP_PROJECTS"); + Map<String, Object> row = table.getNextRow(); + assertEquals("Jon Iles this is a a vawesrasoih aksdkl fas dlkjflkasjd flkjaslkdjflkajlksj dfl lkasjdf lkjaskldfj lkas dlk lkjsjdfkl; aslkdf lkasjkldjf lka skldf lka sdkjfl;kasjd falksjdfljaslkdjf laskjdfk jalskjd flkj aslkdjflkjkjasljdflkjas jf;lkasjd fjkas dasdf asd fasdf asdf asdmhf lksaiyudfoi jasodfj902384jsdf9 aw90se fisajldkfj lkasj dlkfslkd jflksjadf as", row.get("PROJ_PROP_AUTHOR")); + assertEquals("T", row.get("PROJ_PROP_COMPANY")); + assertEquals("Standard", row.get("PROJ_INFO_CAL_NAME")); + assertEquals("Project1", row.get("PROJ_PROP_TITLE")); + byte[] foundBinaryData = (byte[])row.get("RESERVED_BINARY_DATA"); + byte[] expectedBinaryData = + toByteArray(new File("test/data/test2BinData.dat")); + assertTrue(Arrays.equals(expectedBinaryData, foundBinaryData)); + } } public void testWriteLongValue() throws Exception { - Database db = create(); + for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { + Database db = create(fileFormat); - Table table = - new TableBuilder("test") - .addColumn(new ColumnBuilder("A", DataType.TEXT).toColumn()) - .addColumn(new ColumnBuilder("B", DataType.MEMO).toColumn()) - .addColumn(new ColumnBuilder("C", DataType.OLE).toColumn()) - .toTable(db); + Table table = + new TableBuilder("test") + .addColumn(new ColumnBuilder("A", DataType.TEXT).toColumn()) + .addColumn(new ColumnBuilder("B", DataType.MEMO).toColumn()) + .addColumn(new ColumnBuilder("C", DataType.OLE).toColumn()) + .toTable(db); - String testStr = "This is a test"; - String longMemo = createString(2030); - byte[] oleValue = toByteArray(new File("test/data/test2BinData.dat")); - - - table.addRow(testStr, testStr, null); - table.addRow(testStr, longMemo, oleValue); + String testStr = "This is a test"; + String longMemo = createString(2030); + byte[] oleValue = toByteArray(new File("test/data/test2BinData.dat")); - table.reset(); - Map<String, Object> row = table.getNextRow(); + table.addRow(testStr, testStr, null); + table.addRow(testStr, longMemo, oleValue); - assertEquals(testStr, row.get("A")); - assertEquals(testStr, row.get("B")); - assertNull(row.get("C")); + table.reset(); - row = table.getNextRow(); - - assertEquals(testStr, row.get("A")); - assertEquals(longMemo, row.get("B")); - assertTrue(Arrays.equals(oleValue, (byte[])row.get("C"))); - + Map<String, Object> row = table.getNextRow(); + + assertEquals(testStr, row.get("A")); + assertEquals(testStr, row.get("B")); + assertNull(row.get("C")); + + row = table.getNextRow(); + + assertEquals(testStr, row.get("A")); + assertEquals(longMemo, row.get("B")); + assertTrue(Arrays.equals(oleValue, (byte[])row.get("C"))); + } } public void testManyMemos() throws Exception { - final int numColumns = 126; - Database db = create(); - TableBuilder bigTableBuilder = new TableBuilder("test"); - - for (int i = 0; i < numColumns; i++) - { - Column column = new ColumnBuilder("column_" + i, DataType.MEMO) - .toColumn(); - bigTableBuilder.addColumn(column); - } - - Table bigTable = bigTableBuilder.toTable(db); - - List<Object[]> expectedRows = new ArrayList<Object[]>(); - - for (int j = 0; j < 3; j++) - { - Object[] rowData = new String[numColumns]; - for (int i = 0; i < numColumns; i++) + for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { + Database db = create(fileFormat); + final int numColumns = 126; + TableBuilder bigTableBuilder = new TableBuilder("test"); + + for (int i = 0; i < numColumns; i++) { - rowData[i] = "v_" + i + ";" + (j + 999); - } - expectedRows.add(rowData); - bigTable.addRow(rowData); - } - - String extra1 = createString(100); - String extra2 = createString(2050); - - for (int j = 0; j < 1; j++) - { - Object[] rowData = new String[numColumns]; - for (int i = 0; i < numColumns; i++) + Column column = new ColumnBuilder("column_" + i, DataType.MEMO) + .toColumn(); + bigTableBuilder.addColumn(column); + } + + Table bigTable = bigTableBuilder.toTable(db); + + List<Object[]> expectedRows = new ArrayList<Object[]>(); + + for (int j = 0; j < 3; j++) { - rowData[i] = "v_" + i + ";" + (j + 999) + extra2; - } - expectedRows.add(rowData); - bigTable.addRow(rowData); - } - - for (int j = 0; j < 2; j++) - { - Object[] rowData = new String[numColumns]; - for (int i = 0; i < numColumns; i++) + Object[] rowData = new String[numColumns]; + for (int i = 0; i < numColumns; i++) + { + rowData[i] = "v_" + i + ";" + (j + 999); + } + expectedRows.add(rowData); + bigTable.addRow(rowData); + } + + String extra1 = createString(100); + String extra2 = createString(2050); + + for (int j = 0; j < 1; j++) { - String tmp = "v_" + i + ";" + (j + 999); - if((i % 3) == 0) { - tmp += extra1; - } else if((i % 7) == 0) { - tmp += extra2; + Object[] rowData = new String[numColumns]; + for (int i = 0; i < numColumns; i++) + { + rowData[i] = "v_" + i + ";" + (j + 999) + extra2; } - rowData[i] = tmp; - } - expectedRows.add(rowData); - bigTable.addRow(rowData); - } - - bigTable.reset(); - Iterator<Object[]> expIter = expectedRows.iterator(); - for(Map<?,?> row : bigTable) { - Object[] expectedRow = expIter.next(); - assertEquals(Arrays.asList(expectedRow), - new ArrayList<Object>(row.values())); - } + expectedRows.add(rowData); + bigTable.addRow(rowData); + } - db.close(); + for (int j = 0; j < 2; j++) + { + Object[] rowData = new String[numColumns]; + for (int i = 0; i < numColumns; i++) + { + String tmp = "v_" + i + ";" + (j + 999); + if((i % 3) == 0) { + tmp += extra1; + } else if((i % 7) == 0) { + tmp += extra2; + } + rowData[i] = tmp; + } + expectedRows.add(rowData); + bigTable.addRow(rowData); + } + + bigTable.reset(); + Iterator<Object[]> expIter = expectedRows.iterator(); + for(Map<?,?> row : bigTable) { + Object[] expectedRow = expIter.next(); + assertEquals(Arrays.asList(expectedRow), + new ArrayList<Object>(row.values())); + } + + db.close(); + } } public void testMissingFile() throws Exception { File bogusFile = new File("fooby-dooby.mdb"); assertTrue(!bogusFile.exists()); try { - open(bogusFile); + Database.open(bogusFile, true, _autoSync); fail("FileNotFoundException should have been thrown"); } catch(FileNotFoundException e) { } @@ -469,331 +495,352 @@ public class DatabaseTest extends TestCase { } public void testReadWithDeletedCols() throws Exception { - Table table = open(new File("test/data/delColTest.mdb")).getTable("Table1"); - - Map<String, Object> expectedRow0 = new LinkedHashMap<String, Object>(); - expectedRow0.put("id", 0); - expectedRow0.put("id2", 2); - expectedRow0.put("data", "foo"); - expectedRow0.put("data2", "foo2"); - - Map<String, Object> expectedRow1 = new LinkedHashMap<String, Object>(); - expectedRow1.put("id", 3); - expectedRow1.put("id2", 5); - expectedRow1.put("data", "bar"); - expectedRow1.put("data2", "bar2"); - - int rowNum = 0; - Map<String, Object> row = null; - while ((row = table.getNextRow()) != null) { - if(rowNum == 0) { - assertEquals(expectedRow0, row); - } else if(rowNum == 1) { - assertEquals(expectedRow1, row); - } else if(rowNum >= 2) { - fail("should only have 2 rows"); + for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.DEL_COL)) { + Table table = open(testDB).getTable("Table1"); + + Map<String, Object> expectedRow0 = new LinkedHashMap<String, Object>(); + expectedRow0.put("id", 0); + expectedRow0.put("id2", 2); + expectedRow0.put("data", "foo"); + expectedRow0.put("data2", "foo2"); + + Map<String, Object> expectedRow1 = new LinkedHashMap<String, Object>(); + expectedRow1.put("id", 3); + expectedRow1.put("id2", 5); + expectedRow1.put("data", "bar"); + expectedRow1.put("data2", "bar2"); + + int rowNum = 0; + Map<String, Object> row = null; + while ((row = table.getNextRow()) != null) { + if(rowNum == 0) { + assertEquals(expectedRow0, row); + } else if(rowNum == 1) { + assertEquals(expectedRow1, row); + } else if(rowNum >= 2) { + fail("should only have 2 rows"); + } + rowNum++; } - rowNum++; } } public void testCurrency() throws Exception { - Database db = create(); + for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { + Database db = create(fileFormat); - Table table = new TableBuilder("test") - .addColumn(new ColumnBuilder("A", DataType.MONEY).toColumn()) - .toTable(db); - - table.addRow(new BigDecimal("-2341234.03450")); - table.addRow(37L); - table.addRow("10000.45"); + Table table = new TableBuilder("test") + .addColumn(new ColumnBuilder("A", DataType.MONEY).toColumn()) + .toTable(db); - table.reset(); + table.addRow(new BigDecimal("-2341234.03450")); + table.addRow(37L); + table.addRow("10000.45"); - List<Object> foundValues = new ArrayList<Object>(); - Map<String, Object> row = null; - while((row = table.getNextRow()) != null) { - foundValues.add(row.get("A")); - } + table.reset(); - assertEquals(Arrays.asList( - new BigDecimal("-2341234.0345"), - new BigDecimal("37.0000"), - new BigDecimal("10000.4500")), - foundValues); + List<Object> foundValues = new ArrayList<Object>(); + Map<String, Object> row = null; + while((row = table.getNextRow()) != null) { + foundValues.add(row.get("A")); + } - try { - table.addRow(new BigDecimal("342523234145343543.3453")); - fail("IOException should have been thrown"); - } catch(IOException e) { - // ignored + assertEquals(Arrays.asList( + new BigDecimal("-2341234.0345"), + new BigDecimal("37.0000"), + new BigDecimal("10000.4500")), + foundValues); + + try { + table.addRow(new BigDecimal("342523234145343543.3453")); + fail("IOException should have been thrown"); + } catch(IOException e) { + // ignored + } } } public void testGUID() throws Exception { - Database db = create(); - - Table table = new TableBuilder("test") - .addColumn(new ColumnBuilder("A", DataType.GUID).toColumn()) - .toTable(db); + for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { + Database db = create(fileFormat); - table.addRow("{32A59F01-AA34-3E29-453F-4523453CD2E6}"); - table.addRow("{32a59f01-aa34-3e29-453f-4523453cd2e6}"); - table.addRow("{11111111-1111-1111-1111-111111111111}"); - table.addRow(" {FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF} "); - table.addRow(UUID.fromString("32a59f01-1234-3e29-4aaf-4523453cd2e6")); + Table table = new TableBuilder("test") + .addColumn(new ColumnBuilder("A", DataType.GUID).toColumn()) + .toTable(db); - table.reset(); + table.addRow("{32A59F01-AA34-3E29-453F-4523453CD2E6}"); + table.addRow("{32a59f01-aa34-3e29-453f-4523453cd2e6}"); + table.addRow("{11111111-1111-1111-1111-111111111111}"); + table.addRow(" {FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF} "); + table.addRow(UUID.fromString("32a59f01-1234-3e29-4aaf-4523453cd2e6")); - List<Object> foundValues = new ArrayList<Object>(); - Map<String, Object> row = null; - while((row = table.getNextRow()) != null) { - foundValues.add(row.get("A")); - } + table.reset(); - assertEquals(Arrays.asList( - "{32A59F01-AA34-3E29-453F-4523453CD2E6}", - "{32A59F01-AA34-3E29-453F-4523453CD2E6}", - "{11111111-1111-1111-1111-111111111111}", - "{FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF}", - "{32A59F01-1234-3E29-4AAF-4523453CD2E6}"), - foundValues); + List<Object> foundValues = new ArrayList<Object>(); + Map<String, Object> row = null; + while((row = table.getNextRow()) != null) { + foundValues.add(row.get("A")); + } - try { - table.addRow("3245234"); - fail("IOException should have been thrown"); - } catch(IOException e) { - // ignored + assertEquals(Arrays.asList( + "{32A59F01-AA34-3E29-453F-4523453CD2E6}", + "{32A59F01-AA34-3E29-453F-4523453CD2E6}", + "{11111111-1111-1111-1111-111111111111}", + "{FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF}", + "{32A59F01-1234-3E29-4AAF-4523453CD2E6}"), + foundValues); + + try { + table.addRow("3245234"); + fail("IOException should have been thrown"); + } catch(IOException e) { + // ignored + } } } public void testNumeric() throws Exception { - Database db = create(); + for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { + Database db = create(fileFormat); - Column col = new ColumnBuilder("A", DataType.NUMERIC) - .setScale(4).setPrecision(8).toColumn(); - assertTrue(col.isVariableLength()); - - Table table = new TableBuilder("test") - .addColumn(col) - .addColumn(new ColumnBuilder("B", DataType.NUMERIC) - .setScale(8).setPrecision(28).toColumn()) - .toTable(db); + Column col = new ColumnBuilder("A", DataType.NUMERIC) + .setScale(4).setPrecision(8).toColumn(); + assertTrue(col.isVariableLength()); - table.addRow(new BigDecimal("-1234.03450"), - new BigDecimal("23923434453436.36234219")); - table.addRow(37L, 37L); - table.addRow("1000.45", "-3452345321000"); + Table table = new TableBuilder("test") + .addColumn(col) + .addColumn(new ColumnBuilder("B", DataType.NUMERIC) + .setScale(8).setPrecision(28).toColumn()) + .toTable(db); - table.reset(); + table.addRow(new BigDecimal("-1234.03450"), + new BigDecimal("23923434453436.36234219")); + table.addRow(37L, 37L); + table.addRow("1000.45", "-3452345321000"); - List<Object> foundSmallValues = new ArrayList<Object>(); - List<Object> foundBigValues = new ArrayList<Object>(); - Map<String, Object> row = null; - while((row = table.getNextRow()) != null) { - foundSmallValues.add(row.get("A")); - foundBigValues.add(row.get("B")); - } + table.reset(); - assertEquals(Arrays.asList( - new BigDecimal("-1234.0345"), - new BigDecimal("37.0000"), - new BigDecimal("1000.4500")), - foundSmallValues); - assertEquals(Arrays.asList( - new BigDecimal("23923434453436.36234219"), - new BigDecimal("37.00000000"), - new BigDecimal("-3452345321000.00000000")), - foundBigValues); + List<Object> foundSmallValues = new ArrayList<Object>(); + List<Object> foundBigValues = new ArrayList<Object>(); + Map<String, Object> row = null; + while((row = table.getNextRow()) != null) { + foundSmallValues.add(row.get("A")); + foundBigValues.add(row.get("B")); + } - try { - table.addRow(new BigDecimal("3245234.234"), - new BigDecimal("3245234.234")); - fail("IOException should have been thrown"); - } catch(IOException e) { - // ignored + assertEquals(Arrays.asList( + new BigDecimal("-1234.0345"), + new BigDecimal("37.0000"), + new BigDecimal("1000.4500")), + foundSmallValues); + assertEquals(Arrays.asList( + new BigDecimal("23923434453436.36234219"), + new BigDecimal("37.00000000"), + new BigDecimal("-3452345321000.00000000")), + foundBigValues); + + try { + table.addRow(new BigDecimal("3245234.234"), + new BigDecimal("3245234.234")); + fail("IOException should have been thrown"); + } catch(IOException e) { + // ignored + } } } public void testFixedNumeric() throws Exception { - Database db = openCopy(new File("test/data/fixedNumericTest.mdb")); - Table t = db.getTable("test"); - - boolean first = true; - for(Column col : t.getColumns()) { - if(first) { - assertTrue(col.isVariableLength()); - assertEquals(DataType.MEMO, col.getType()); - first = false; - } else { - assertFalse(col.isVariableLength()); - assertEquals(DataType.NUMERIC, col.getType()); + for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.FIXED_NUMERIC)) { + Database db = openCopy(testDB); + Table t = db.getTable("test"); + + boolean first = true; + for(Column col : t.getColumns()) { + if(first) { + assertTrue(col.isVariableLength()); + assertEquals(DataType.MEMO, col.getType()); + first = false; + } else { + assertFalse(col.isVariableLength()); + assertEquals(DataType.NUMERIC, col.getType()); + } } - } - - Map<String, Object> row = t.getNextRow(); - assertEquals("some data", row.get("col1")); - assertEquals(new BigDecimal("1"), row.get("col2")); - assertEquals(new BigDecimal("0"), row.get("col3")); - assertEquals(new BigDecimal("0"), row.get("col4")); - assertEquals(new BigDecimal("4"), row.get("col5")); - assertEquals(new BigDecimal("-1"), row.get("col6")); - assertEquals(new BigDecimal("1"), row.get("col7")); - - Object[] tmpRow = new Object[]{ - "foo", new BigDecimal("1"), new BigDecimal(3), new BigDecimal("13"), - new BigDecimal("-17"), new BigDecimal("0"), new BigDecimal("8734")}; - t.addRow(tmpRow); - t.reset(); - - t.getNextRow(); - row = t.getNextRow(); - assertEquals(tmpRow[0], row.get("col1")); - assertEquals(tmpRow[1], row.get("col2")); - assertEquals(tmpRow[2], row.get("col3")); - assertEquals(tmpRow[3], row.get("col4")); - assertEquals(tmpRow[4], row.get("col5")); - assertEquals(tmpRow[5], row.get("col6")); - assertEquals(tmpRow[6], row.get("col7")); - - db.close(); + Map<String, Object> row = t.getNextRow(); + assertEquals("some data", row.get("col1")); + assertEquals(new BigDecimal("1"), row.get("col2")); + assertEquals(new BigDecimal("0"), row.get("col3")); + assertEquals(new BigDecimal("0"), row.get("col4")); + assertEquals(new BigDecimal("4"), row.get("col5")); + assertEquals(new BigDecimal("-1"), row.get("col6")); + assertEquals(new BigDecimal("1"), row.get("col7")); + + Object[] tmpRow = new Object[]{ + "foo", new BigDecimal("1"), new BigDecimal(3), new BigDecimal("13"), + new BigDecimal("-17"), new BigDecimal("0"), new BigDecimal("8734")}; + t.addRow(tmpRow); + t.reset(); + + t.getNextRow(); + row = t.getNextRow(); + assertEquals(tmpRow[0], row.get("col1")); + assertEquals(tmpRow[1], row.get("col2")); + assertEquals(tmpRow[2], row.get("col3")); + assertEquals(tmpRow[3], row.get("col4")); + assertEquals(tmpRow[4], row.get("col5")); + assertEquals(tmpRow[5], row.get("col6")); + assertEquals(tmpRow[6], row.get("col7")); + + db.close(); + } } public void testMultiPageTableDef() throws Exception { - List<Column> columns = open().getTable("Table2").getColumns(); - assertEquals(89, columns.size()); + for (final TestDB testDB : SUPPORTED_DBS_TEST) { + List<Column> columns = open(testDB).getTable("Table2").getColumns(); + assertEquals(89, columns.size()); + } } public void testOverflow() throws Exception { - Database mdb = open(new File("test/data/overflowTest.mdb")); - Table table = mdb.getTable("Table1"); + for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.OVERFLOW)) { + Database mdb = open(testDB); + Table table = mdb.getTable("Table1"); - // 7 rows, 3 and 5 are overflow - table.getNextRow(); - table.getNextRow(); + // 7 rows, 3 and 5 are overflow + table.getNextRow(); + table.getNextRow(); - Map<String, Object> row = table.getNextRow(); - assertEquals(Arrays.<Object>asList( - null, "row3col3", null, null, null, null, null, - "row3col9", null), - new ArrayList<Object>(row.values())); + Map<String, Object> row = table.getNextRow(); + assertEquals(Arrays.<Object>asList( + null, "row3col3", null, null, null, null, null, + "row3col9", null), + new ArrayList<Object>(row.values())); - table.getNextRow(); + table.getNextRow(); - row = table.getNextRow(); - assertEquals(Arrays.<Object>asList( - null, "row5col2", null, null, null, null, null, null, - null), - new ArrayList<Object>(row.values())); + row = table.getNextRow(); + assertEquals(Arrays.<Object>asList( + null, "row5col2", null, null, null, null, null, null, + null), + new ArrayList<Object>(row.values())); - table.reset(); - assertRowCount(7, table); - + table.reset(); + assertRowCount(7, table); + } } public void testLongValueAsMiddleColumn() throws Exception { + for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { + Database db = create(fileFormat); + Table newTable = new TableBuilder("NewTable") + .addColumn(new ColumnBuilder("a").setSQLType(Types.INTEGER).toColumn()) + .addColumn(new ColumnBuilder("b").setSQLType(Types.LONGVARCHAR).toColumn()) + .addColumn(new ColumnBuilder("c").setSQLType(Types.VARCHAR).toColumn()) + .toTable(db); - Database db = create(); - Table newTable = new TableBuilder("NewTable") - .addColumn(new ColumnBuilder("a").setSQLType(Types.INTEGER).toColumn()) - .addColumn(new ColumnBuilder("b").setSQLType(Types.LONGVARCHAR).toColumn()) - .addColumn(new ColumnBuilder("c").setSQLType(Types.VARCHAR).toColumn()) - .toTable(db); - - String lval = createString(2000); // "--2000 chars long text--"; - String tval = createString(40); // "--40chars long text--"; - newTable.addRow(new Integer(1), lval, tval); - - newTable = db.getTable("NewTable"); - Map<String, Object> readRow = newTable.getNextRow(); - assertEquals(new Integer(1), readRow.get("a")); - assertEquals(lval, readRow.get("b")); - assertEquals(tval, readRow.get("c")); + String lval = createString(2000); // "--2000 chars long text--"; + String tval = createString(40); // "--40chars long text--"; + newTable.addRow(new Integer(1), lval, tval); + newTable = db.getTable("NewTable"); + Map<String, Object> readRow = newTable.getNextRow(); + assertEquals(new Integer(1), readRow.get("a")); + assertEquals(lval, readRow.get("b")); + assertEquals(tval, readRow.get("c")); + } } public void testUsageMapPromotion() throws Exception { - Database db = openCopy(new File("test/data/testPromotion.mdb")); - Table t = db.getTable("jobDB1"); + for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.PROMOTION)) { + Database db = openCopy(testDB); + Table t = db.getTable("jobDB1"); - String lval = createString(255); // "--255 chars long text--"; + String lval = createString(255); // "--255 chars long text--"; - for(int i = 0; i < 1000; ++i) { - t.addRow(i, 13, 57, 47.0d, lval, lval, lval, lval, lval, lval); - } + for(int i = 0; i < 1000; ++i) { + t.addRow(i, 13, 57, 47.0d, lval, lval, lval, lval, lval, lval); // @todo Fails w/ V2007 + } - Set<Integer> ids = new HashSet<Integer>(); - for(Map<String,Object> row : t) { - ids.add((Integer)row.get("ID")); - } - assertEquals(1000, ids.size()); + Set<Integer> ids = new HashSet<Integer>(); + for(Map<String,Object> row : t) { + ids.add((Integer)row.get("ID")); + } + assertEquals(1000, ids.size()); - db.close(); + db.close(); + } } public void testLargeTableDef() throws Exception { - final int numColumns = 90; - Database db = create(); + for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { + Database db = create(fileFormat); - List<Column> columns = new ArrayList<Column>(); - List<String> colNames = new ArrayList<String>(); - for(int i = 0; i < numColumns; ++i) { - String colName = "MyColumnName" + i; - colNames.add(colName); - columns.add(new ColumnBuilder(colName, DataType.TEXT).toColumn()); - } + final int numColumns = 90; - db.createTable("test", columns); + List<Column> columns = new ArrayList<Column>(); + List<String> colNames = new ArrayList<String>(); + for(int i = 0; i < numColumns; ++i) { + String colName = "MyColumnName" + i; + colNames.add(colName); + columns.add(new ColumnBuilder(colName, DataType.TEXT).toColumn()); + } - Table t = db.getTable("test"); + db.createTable("test", columns); - List<String> row = new ArrayList<String>(); - Map<String,Object> expectedRowData = new LinkedHashMap<String, Object>(); - for(int i = 0; i < numColumns; ++i) { - String value = "" + i + " some row data"; - row.add(value); - expectedRowData.put(colNames.get(i), value); - } + Table t = db.getTable("test"); - t.addRow(row.toArray()); + List<String> row = new ArrayList<String>(); + Map<String,Object> expectedRowData = new LinkedHashMap<String, Object>(); + for(int i = 0; i < numColumns; ++i) { + String value = "" + i + " some row data"; + row.add(value); + expectedRowData.put(colNames.get(i), value); + } - t.reset(); - assertEquals(expectedRowData, t.getNextRow()); - - db.close(); + t.addRow(row.toArray()); + + t.reset(); + assertEquals(expectedRowData, t.getNextRow()); + + db.close(); + } } public void testAutoNumber() throws Exception { - Database db = create(); + for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { + Database db = create(fileFormat); - Table table = new TableBuilder("test") - .addColumn(new ColumnBuilder("a", DataType.LONG) - .setAutoNumber(true).toColumn()) - .addColumn(new ColumnBuilder("b", DataType.TEXT).toColumn()) - .toTable(db); + Table table = new TableBuilder("test") + .addColumn(new ColumnBuilder("a", DataType.LONG) + .setAutoNumber(true).toColumn()) + .addColumn(new ColumnBuilder("b", DataType.TEXT).toColumn()) + .toTable(db); - doTestAutoNumber(table); - - db.close(); + doTestAutoNumber(table); + + db.close(); + } } public void testAutoNumberPK() throws Exception { - Database db = openCopy(new File("test/data/test.mdb")); + for (final TestDB testDB : SUPPORTED_DBS_TEST) { + Database db = openCopy(testDB); - Table table = db.getTable("Table3"); + Table table = db.getTable("Table3"); - doTestAutoNumber(table); - - db.close(); + doTestAutoNumber(table); + + db.close(); + } } private void doTestAutoNumber(Table table) throws Exception @@ -831,69 +878,71 @@ public class DatabaseTest extends TestCase { } public void testWriteAndReadDate() throws Exception { - Database db = create(); + for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { + Database db = create(fileFormat); - Table table = new TableBuilder("test") - .addColumn(new ColumnBuilder("name", DataType.TEXT).toColumn()) - .addColumn(new ColumnBuilder("date", DataType.SHORT_DATE_TIME) - .toColumn()) - .toTable(db); - - // since jackcess does not really store millis, shave them off before - // storing the current date/time - long curTimeNoMillis = (System.currentTimeMillis() / 1000L); - curTimeNoMillis *= 1000L; - - DateFormat df = new SimpleDateFormat("yyyyMMdd HH:mm:ss"); - List<Date> dates = - new ArrayList<Date>( - Arrays.asList( - df.parse("19801231 00:00:00"), - df.parse("19930513 14:43:27"), - null, - df.parse("20210102 02:37:00"), - new Date(curTimeNoMillis))); - - Calendar c = Calendar.getInstance(); - for(int year = 1801; year < 2050; year +=3) { - for(int month = 0; month <= 12; ++month) { - for(int day = 1; day < 29; day += 3) { - c.clear(); - c.set(Calendar.YEAR, year); - c.set(Calendar.MONTH, month); - c.set(Calendar.DAY_OF_MONTH, day); - dates.add(c.getTime()); + Table table = new TableBuilder("test") + .addColumn(new ColumnBuilder("name", DataType.TEXT).toColumn()) + .addColumn(new ColumnBuilder("date", DataType.SHORT_DATE_TIME) + .toColumn()) + .toTable(db); + + // since jackcess does not really store millis, shave them off before + // storing the current date/time + long curTimeNoMillis = (System.currentTimeMillis() / 1000L); + curTimeNoMillis *= 1000L; + + DateFormat df = new SimpleDateFormat("yyyyMMdd HH:mm:ss"); + List<Date> dates = + new ArrayList<Date>( + Arrays.asList( + df.parse("19801231 00:00:00"), + df.parse("19930513 14:43:27"), + null, + df.parse("20210102 02:37:00"), + new Date(curTimeNoMillis))); + + Calendar c = Calendar.getInstance(); + for(int year = 1801; year < 2050; year +=3) { + for(int month = 0; month <= 12; ++month) { + for(int day = 1; day < 29; day += 3) { + c.clear(); + c.set(Calendar.YEAR, year); + c.set(Calendar.MONTH, month); + c.set(Calendar.DAY_OF_MONTH, day); + dates.add(c.getTime()); + } } } - } - for(Date d : dates) { - table.addRow("row " + d, d); - } + for(Date d : dates) { + table.addRow("row " + d, d); + } - List<Date> foundDates = new ArrayList<Date>(); - for(Map<String,Object> row : table) { - foundDates.add((Date)row.get("date")); - } + List<Date> foundDates = new ArrayList<Date>(); + for(Map<String,Object> row : table) { + foundDates.add((Date)row.get("date")); + } - assertEquals(dates.size(), foundDates.size()); - for(int i = 0; i < dates.size(); ++i) { - Date expected = dates.get(i); - Date found = foundDates.get(i); - if(expected == null) { - assertNull(found); - } else { - // there are some rounding issues due to dates being stored as - // doubles, but it results in a 1 millisecond difference, so i'm not - // going to worry about it - long expTime = expected.getTime(); - long foundTime = found.getTime(); - try { - assertTrue((expTime == foundTime) || - (Math.abs(expTime - foundTime) <= 1)); - } catch(Error e) { - System.err.println("Expected " + expTime + ", found " + foundTime); - throw e; + assertEquals(dates.size(), foundDates.size()); + for(int i = 0; i < dates.size(); ++i) { + Date expected = dates.get(i); + Date found = foundDates.get(i); + if(expected == null) { + assertNull(found); + } else { + // there are some rounding issues due to dates being stored as + // doubles, but it results in a 1 millisecond difference, so i'm not + // going to worry about it + long expTime = expected.getTime(); + long foundTime = found.getTime(); + try { + assertTrue((expTime == foundTime) || + (Math.abs(expTime - foundTime) <= 1)); + } catch(Error e) { + System.err.println("Expected " + expTime + ", found " + foundTime); + throw e; + } } } } @@ -901,128 +950,136 @@ public class DatabaseTest extends TestCase { public void testSystemTable() throws Exception { - Database db = create(); + for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { + Database db = create(fileFormat); - assertNotNull(db.getSystemTable("MSysAccessObjects")); - assertNotNull(db.getSystemTable("MSysObjects")); - assertNotNull(db.getSystemTable("MSysQueries")); - assertNotNull(db.getSystemTable("MSysACES")); - assertNotNull(db.getSystemTable("MSysRelationships")); + assertNotNull("file format: " + fileFormat + + "\nIs OK that v2003, v2007 template files have no \"MSysAccessObjects\" table?", + db.getSystemTable("MSysAccessObjects")); + assertNotNull(db.getSystemTable("MSysObjects")); + assertNotNull(db.getSystemTable("MSysQueries")); + assertNotNull(db.getSystemTable("MSysACES")); + assertNotNull(db.getSystemTable("MSysRelationships")); - assertNull(db.getSystemTable("MSysBogus")); + assertNull(db.getSystemTable("MSysBogus")); - db.close(); + db.close(); + } } public void testUpdateRow() throws Exception { - Database db = create(); - - Table t = new TableBuilder("test") - .addColumn(new ColumnBuilder("name", DataType.TEXT).toColumn()) - .addColumn(new ColumnBuilder("id", DataType.LONG) - .setAutoNumber(true).toColumn()) - .addColumn(new ColumnBuilder("data", DataType.TEXT) - .setLength(JetFormat.TEXT_FIELD_MAX_LENGTH).toColumn()) - .toTable(db); + for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { + Database db = create(fileFormat); + + Table t = new TableBuilder("test") + .addColumn(new ColumnBuilder("name", DataType.TEXT).toColumn()) + .addColumn(new ColumnBuilder("id", DataType.LONG) + .setAutoNumber(true).toColumn()) + .addColumn(new ColumnBuilder("data", DataType.TEXT) + .setLength(JetFormat.TEXT_FIELD_MAX_LENGTH).toColumn()) + .toTable(db); - for(int i = 0; i < 10; ++i) { - t.addRow("row" + i, Column.AUTO_NUMBER, "initial data"); - } + for(int i = 0; i < 10; ++i) { + t.addRow("row" + i, Column.AUTO_NUMBER, "initial data"); + } - Cursor c = Cursor.createCursor(t); - c.reset(); - c.moveNextRows(2); - Map<String,Object> row = c.getCurrentRow(); + Cursor c = Cursor.createCursor(t); + c.reset(); + c.moveNextRows(2); + Map<String,Object> row = c.getCurrentRow(); - assertEquals(createExpectedRow("name", "row1", - "id", 2, - "data", "initial data"), - row); + assertEquals(createExpectedRow("name", "row1", + "id", 2, + "data", "initial data"), + row); - c.updateCurrentRow(Column.KEEP_VALUE, Column.AUTO_NUMBER, "new data"); + c.updateCurrentRow(Column.KEEP_VALUE, Column.AUTO_NUMBER, "new data"); - c.moveNextRows(3); - row = c.getCurrentRow(); + c.moveNextRows(3); + row = c.getCurrentRow(); - assertEquals(createExpectedRow("name", "row4", - "id", 5, - "data", "initial data"), - row); + assertEquals(createExpectedRow("name", "row4", + "id", 5, + "data", "initial data"), + row); - c.updateCurrentRow(Column.KEEP_VALUE, Column.AUTO_NUMBER, "a larger amount of new data"); + c.updateCurrentRow(Column.KEEP_VALUE, Column.AUTO_NUMBER, "a larger amount of new data"); - c.reset(); - c.moveNextRows(2); - row = c.getCurrentRow(); + c.reset(); + c.moveNextRows(2); + row = c.getCurrentRow(); - assertEquals(createExpectedRow("name", "row1", - "id", 2, - "data", "new data"), - row); + assertEquals(createExpectedRow("name", "row1", + "id", 2, + "data", "new data"), + row); - c.moveNextRows(3); - row = c.getCurrentRow(); + c.moveNextRows(3); + row = c.getCurrentRow(); - assertEquals(createExpectedRow("name", "row4", - "id", 5, - "data", "a larger amount of new data"), - row); + assertEquals(createExpectedRow("name", "row4", + "id", 5, + "data", "a larger amount of new data"), + row); - t.reset(); - - String str = createString(100); - for(int i = 10; i < 50; ++i) { - t.addRow("row" + i, Column.AUTO_NUMBER, "big data_" + str); - } + t.reset(); + + String str = createString(100); + for(int i = 10; i < 50; ++i) { + t.addRow("row" + i, Column.AUTO_NUMBER, "big data_" + str); + } - c.reset(); - c.moveNextRows(9); - row = c.getCurrentRow(); + c.reset(); + c.moveNextRows(9); + row = c.getCurrentRow(); - assertEquals(createExpectedRow("name", "row8", - "id", 9, - "data", "initial data"), - row); + assertEquals(createExpectedRow("name", "row8", + "id", 9, + "data", "initial data"), + row); - String newText = "updated big data_" + createString(200); + String newText = "updated big data_" + createString(200); - c.setCurrentRowValue(t.getColumn("data"), newText); + c.setCurrentRowValue(t.getColumn("data"), newText); - c.reset(); - c.moveNextRows(9); - row = c.getCurrentRow(); + c.reset(); + c.moveNextRows(9); + row = c.getCurrentRow(); - assertEquals(createExpectedRow("name", "row8", - "id", 9, - "data", newText), - row); + assertEquals(createExpectedRow("name", "row8", + "id", 9, + "data", newText), + row); - db.close(); + db.close(); + } } public void testFixedText() throws Exception { - Database db = openCopy(new File("test/data/fixedTextTest.mdb")); + for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.FIXED_TEXT)) { + Database db = openCopy(testDB); - Table t = db.getTable("users"); - Column c = t.getColumn("c_flag_"); - assertEquals(DataType.TEXT, c.getType()); - assertEquals(false, c.isVariableLength()); - assertEquals(2, c.getLength()); + Table t = db.getTable("users"); + Column c = t.getColumn("c_flag_"); + assertEquals(DataType.TEXT, c.getType()); + assertEquals(false, c.isVariableLength()); + assertEquals(2, c.getLength()); - Map<String,Object> row = t.getNextRow(); - assertEquals("N", row.get("c_flag_")); + Map<String,Object> row = t.getNextRow(); + assertEquals("N", row.get("c_flag_")); - t.addRow(3, "testFixedText", "boo", "foo", "bob", 3, 5, 9, "Y", - new Date()); + t.addRow(3, "testFixedText", "boo", "foo", "bob", 3, 5, 9, "Y", + new Date()); - t.getNextRow(); - row = t.getNextRow(); - assertEquals("testFixedText", row.get("c_user_login")); - assertEquals("Y", row.get("c_flag_")); + t.getNextRow(); + row = t.getNextRow(); + assertEquals("testFixedText", row.get("c_user_login")); + assertEquals("Y", row.get("c_flag_")); - db.close(); + db.close(); + } } static Object[] createTestRow(String col1Val) { diff --git a/test/src/java/com/healthmarketscience/jackcess/ErrorHandlerTest.java b/test/src/java/com/healthmarketscience/jackcess/ErrorHandlerTest.java index 8b46a8a..488f6bd 100644 --- a/test/src/java/com/healthmarketscience/jackcess/ErrorHandlerTest.java +++ b/test/src/java/com/healthmarketscience/jackcess/ErrorHandlerTest.java @@ -35,6 +35,7 @@ import java.util.List; import junit.framework.TestCase; +import static com.healthmarketscience.jackcess.Database.*; import static com.healthmarketscience.jackcess.DatabaseTest.*; /** @@ -49,94 +50,96 @@ public class ErrorHandlerTest extends TestCase public void testErrorHandler() throws Exception { - Database db = create(); - - Table table = - new TableBuilder("test") - .addColumn(new ColumnBuilder("col", DataType.TEXT).toColumn()) - .addColumn(new ColumnBuilder("val", DataType.LONG).toColumn()) - .toTable(db); - - table.addRow("row1", 1); - table.addRow("row2", 2); - table.addRow("row3", 3); - - assertTable(createExpectedTable( - createExpectedRow("col", "row1", - "val", 1), - createExpectedRow("col", "row2", - "val", 2), - createExpectedRow("col", "row3", - "val", 3)), - table); - - - replaceColumn(table, "val"); - - table.reset(); - try { - table.getNextRow(); - fail("IOException should have been thrown"); - } catch(IOException e) { - // success - } + for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { + Database db = create(fileFormat); + + Table table = + new TableBuilder("test") + .addColumn(new ColumnBuilder("col", DataType.TEXT).toColumn()) + .addColumn(new ColumnBuilder("val", DataType.LONG).toColumn()) + .toTable(db); + + table.addRow("row1", 1); + table.addRow("row2", 2); + table.addRow("row3", 3); + + assertTable(createExpectedTable( + createExpectedRow("col", "row1", + "val", 1), + createExpectedRow("col", "row2", + "val", 2), + createExpectedRow("col", "row3", + "val", 3)), + table); + + + replaceColumn(table, "val"); + + table.reset(); + try { + table.getNextRow(); + fail("IOException should have been thrown"); + } catch(IOException e) { + // success + } - table.reset(); - table.setErrorHandler(new ReplacementErrorHandler()); - - assertTable(createExpectedTable( - createExpectedRow("col", "row1", - "val", null), - createExpectedRow("col", "row2", - "val", null), - createExpectedRow("col", "row3", - "val", null)), - table); - - Cursor c1 = Cursor.createCursor(table); - Cursor c2 = Cursor.createCursor(table); - Cursor c3 = Cursor.createCursor(table); - - c2.setErrorHandler(new DebugErrorHandler("#error")); - c3.setErrorHandler(Database.DEFAULT_ERROR_HANDLER); - - assertCursor(createExpectedTable( - createExpectedRow("col", "row1", - "val", null), - createExpectedRow("col", "row2", - "val", null), - createExpectedRow("col", "row3", - "val", null)), - c1); - - assertCursor(createExpectedTable( - createExpectedRow("col", "row1", - "val", "#error"), - createExpectedRow("col", "row2", - "val", "#error"), - createExpectedRow("col", "row3", - "val", "#error")), - c2); - - try { - c3.getNextRow(); - fail("IOException should have been thrown"); - } catch(IOException e) { - // success - } + table.reset(); + table.setErrorHandler(new ReplacementErrorHandler()); + + assertTable(createExpectedTable( + createExpectedRow("col", "row1", + "val", null), + createExpectedRow("col", "row2", + "val", null), + createExpectedRow("col", "row3", + "val", null)), + table); + + Cursor c1 = Cursor.createCursor(table); + Cursor c2 = Cursor.createCursor(table); + Cursor c3 = Cursor.createCursor(table); + + c2.setErrorHandler(new DebugErrorHandler("#error")); + c3.setErrorHandler(Database.DEFAULT_ERROR_HANDLER); + + assertCursor(createExpectedTable( + createExpectedRow("col", "row1", + "val", null), + createExpectedRow("col", "row2", + "val", null), + createExpectedRow("col", "row3", + "val", null)), + c1); + + assertCursor(createExpectedTable( + createExpectedRow("col", "row1", + "val", "#error"), + createExpectedRow("col", "row2", + "val", "#error"), + createExpectedRow("col", "row3", + "val", "#error")), + c2); + + try { + c3.getNextRow(); + fail("IOException should have been thrown"); + } catch(IOException e) { + // success + } - table.setErrorHandler(null); - c1.setErrorHandler(null); - c1.reset(); - try { - c1.getNextRow(); - fail("IOException should have been thrown"); - } catch(IOException e) { - // success - } + table.setErrorHandler(null); + c1.setErrorHandler(null); + c1.reset(); + try { + c1.getNextRow(); + fail("IOException should have been thrown"); + } catch(IOException e) { + // success + } - db.close(); + db.close(); + } } @SuppressWarnings("unchecked") diff --git a/test/src/java/com/healthmarketscience/jackcess/ImportTest.java b/test/src/java/com/healthmarketscience/jackcess/ImportTest.java index ad2c851..6403077 100644 --- a/test/src/java/com/healthmarketscience/jackcess/ImportTest.java +++ b/test/src/java/com/healthmarketscience/jackcess/ImportTest.java @@ -39,6 +39,7 @@ import java.util.List; import junit.framework.TestCase; +import static com.healthmarketscience.jackcess.Database.*; import static com.healthmarketscience.jackcess.DatabaseTest.*; /** @@ -53,75 +54,81 @@ public class ImportTest extends TestCase public void testImportFromFile() throws Exception { - Database db = create(); - db.importFile("test", new File("test/data/sample-input.tab"), "\\t"); + for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { + Database db = create(fileFormat); + db.importFile("test", new File("test/data/sample-input.tab"), "\\t"); + } } public void testImportFromFileWithOnlyHeaders() throws Exception { - Database db = create(); - db.importFile("test", new File("test/data/sample-input-only-headers.tab"), + for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { + Database db = create(fileFormat); + db.importFile("test", new File("test/data/sample-input-only-headers.tab"), "\\t"); + } } public void testCopySqlHeaders() throws Exception { - TestResultSet rs = new TestResultSet(); - - rs.addColumn(Types.INTEGER, "col1"); - rs.addColumn(Types.VARCHAR, "col2", 60, 0, 0); - rs.addColumn(Types.VARCHAR, "col3", 500, 0, 0); - rs.addColumn(Types.BINARY, "col4", 128, 0, 0); - rs.addColumn(Types.BINARY, "col5", 512, 0, 0); - rs.addColumn(Types.NUMERIC, "col6", 0, 7, 15); - rs.addColumn(Types.VARCHAR, "col7", Integer.MAX_VALUE, 0, 0); - - Database db = create(); - db.copyTable("Test1", (ResultSet)Proxy.newProxyInstance( - Thread.currentThread().getContextClassLoader(), - new Class[]{ResultSet.class}, - rs)); - - Table t = db.getTable("Test1"); - List<Column> columns = t.getColumns(); - assertEquals(7, columns.size()); - - Column c = columns.get(0); - assertEquals("col1", c.getName()); - assertEquals(DataType.LONG, c.getType()); - - c = columns.get(1); - assertEquals("col2", c.getName()); - assertEquals(DataType.TEXT, c.getType()); - assertEquals(120, c.getLength()); - - c = columns.get(2); - assertEquals("col3", c.getName()); - assertEquals(DataType.MEMO, c.getType()); - assertEquals(0, c.getLength()); - - c = columns.get(3); - assertEquals("col4", c.getName()); - assertEquals(DataType.BINARY, c.getType()); - assertEquals(128, c.getLength()); - - c = columns.get(4); - assertEquals("col5", c.getName()); - assertEquals(DataType.OLE, c.getType()); - assertEquals(0, c.getLength()); - - c = columns.get(5); - assertEquals("col6", c.getName()); - assertEquals(DataType.NUMERIC, c.getType()); - assertEquals(17, c.getLength()); - assertEquals(7, c.getScale()); - assertEquals(15, c.getPrecision()); - - c = columns.get(6); - assertEquals("col7", c.getName()); - assertEquals(DataType.MEMO, c.getType()); - assertEquals(0, c.getLength()); - + for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { + + TestResultSet rs = new TestResultSet(); + + rs.addColumn(Types.INTEGER, "col1"); + rs.addColumn(Types.VARCHAR, "col2", 60, 0, 0); + rs.addColumn(Types.VARCHAR, "col3", 500, 0, 0); + rs.addColumn(Types.BINARY, "col4", 128, 0, 0); + rs.addColumn(Types.BINARY, "col5", 512, 0, 0); + rs.addColumn(Types.NUMERIC, "col6", 0, 7, 15); + rs.addColumn(Types.VARCHAR, "col7", Integer.MAX_VALUE, 0, 0); + + Database db = create(fileFormat); + db.copyTable("Test1", (ResultSet)Proxy.newProxyInstance( + Thread.currentThread().getContextClassLoader(), + new Class[]{ResultSet.class}, + rs)); + + Table t = db.getTable("Test1"); + List<Column> columns = t.getColumns(); + assertEquals(7, columns.size()); + + Column c = columns.get(0); + assertEquals("col1", c.getName()); + assertEquals(DataType.LONG, c.getType()); + + c = columns.get(1); + assertEquals("col2", c.getName()); + assertEquals(DataType.TEXT, c.getType()); + assertEquals(120, c.getLength()); + + c = columns.get(2); + assertEquals("col3", c.getName()); + assertEquals(DataType.MEMO, c.getType()); + assertEquals(0, c.getLength()); + + c = columns.get(3); + assertEquals("col4", c.getName()); + assertEquals(DataType.BINARY, c.getType()); + assertEquals(128, c.getLength()); + + c = columns.get(4); + assertEquals("col5", c.getName()); + assertEquals(DataType.OLE, c.getType()); + assertEquals(0, c.getLength()); + + c = columns.get(5); + assertEquals("col6", c.getName()); + assertEquals(DataType.NUMERIC, c.getType()); + assertEquals(17, c.getLength()); + assertEquals(7, c.getScale()); + assertEquals(15, c.getPrecision()); + + c = columns.get(6); + assertEquals("col7", c.getName()); + assertEquals(DataType.MEMO, c.getType()); + assertEquals(0, c.getLength()); + } } diff --git a/test/src/java/com/healthmarketscience/jackcess/IndexCodesTest.java b/test/src/java/com/healthmarketscience/jackcess/IndexCodesTest.java index 2b9d666..217a566 100644 --- a/test/src/java/com/healthmarketscience/jackcess/IndexCodesTest.java +++ b/test/src/java/com/healthmarketscience/jackcess/IndexCodesTest.java @@ -27,7 +27,6 @@ King of Prussia, PA 19406 package com.healthmarketscience.jackcess; -import java.io.File; import java.lang.reflect.Field; import java.nio.ByteBuffer; import java.util.Arrays; @@ -40,6 +39,7 @@ import java.util.regex.Pattern; import junit.framework.TestCase; import static com.healthmarketscience.jackcess.DatabaseTest.*; +import static com.healthmarketscience.jackcess.JetFormatTest.*; /** @@ -66,19 +66,21 @@ public class IndexCodesTest extends TestCase { public void testIndexCodes() throws Exception { - Database db = open(new File("test/data/testIndexCodes.mdb")); + for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX_CODES)) { + Database db = open(testDB); - for(Table t : db) { - for(Index index : t.getIndexes()) { -// System.out.println("Checking " + t.getName() + "." + index.getName()); - checkIndexEntries(t, index); + for(Table t : db) { + for(Index index : t.getIndexes()) { + // System.out.println("Checking " + t.getName() + "." + index.getName()); + checkIndexEntries(testDB, t, index); + } } + + db.close(); } - - db.close(); } - private static void checkIndexEntries(Table t, Index index) throws Exception + private static void checkIndexEntries(final TestDB testDB, Table t, Index index) throws Exception { // index.initialize(); // System.out.println("Ind " + index); @@ -90,7 +92,7 @@ public class IndexCodesTest extends TestCase { Cursor.Position curPos = cursor.getSavepoint().getCurrentPosition(); boolean success = false; try { - findRow(t, index, row, curPos); + findRow(testDB, t, index, row, curPos); success = true; } finally { if(!success) { @@ -103,7 +105,7 @@ public class IndexCodesTest extends TestCase { } - private static void findRow(Table t, Index index, + private static void findRow(final TestDB testDB, Table t, Index index, Map<String,Object> expectedRow, Cursor.Position expectedPos) throws Exception @@ -123,8 +125,8 @@ public class IndexCodesTest extends TestCase { return; } } - fail("Could not find expected row " + expectedRow + " starting at " + - entryToString(startPos)); + fail("testDB: " + testDB + ";\nCould not find expected row " + expectedRow + " starting at " + + entryToString(startPos)); // @todo fails with v2007 on row2 } @@ -140,120 +142,134 @@ public class IndexCodesTest extends TestCase { public void x_testCreateIsoFile() throws Exception { - Database db = create(true); + for (final Database.FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { + Database db = create(fileFormat, true); - Table t = new TableBuilder("test") - .addColumn(new ColumnBuilder("row", DataType.TEXT).toColumn()) - .addColumn(new ColumnBuilder("data", DataType.TEXT).toColumn()) - .toTable(db); - - for(int i = 0; i < 256; ++i) { - String str = "AA" + ((char)i) + "AA"; - t.addRow("row" + i, str); - } + Table t = new TableBuilder("test") + .addColumn(new ColumnBuilder("row", DataType.TEXT).toColumn()) + .addColumn(new ColumnBuilder("data", DataType.TEXT).toColumn()) + .toTable(db); + + for(int i = 0; i < 256; ++i) { + String str = "AA" + ((char)i) + "AA"; + t.addRow("row" + i, str); + } - db.close(); + db.close(); + } } public void x_testCreateAltIsoFile() throws Exception { - Database db = openCopy(new File("/tmp/test_ind.mdb"), true); - - Table t = db.getTable("Table1"); + for (final TestDB testDB : TestDB.getSupportedForBasename(null)) { + // @todo Bank test dbFiles and create new TestDB here. + //Database db = openCopy(new File("/tmp/test_ind.mdb"), true); + Database db = openCopy(testDB, true); + + Table t = db.getTable("Table1"); + + for(int i = 0; i < 256; ++i) { + String str = "AA" + ((char)i) + "AA"; + t.addRow("row" + i, str, + (byte)42 + i, (short)53 + i, 13 * i, + (6.7d / i), null, null, true); + } - for(int i = 0; i < 256; ++i) { - String str = "AA" + ((char)i) + "AA"; - t.addRow("row" + i, str, - (byte)42 + i, (short)53 + i, 13 * i, - (6.7d / i), null, null, true); + db.close(); } - - db.close(); } public void x_testWriteAllCodesMdb() throws Exception { - Database db = create(true); - -// Table t = new TableBuilder("Table1") -// .addColumn(new ColumnBuilder("key", DataType.TEXT).toColumn()) -// .addColumn(new ColumnBuilder("data", DataType.TEXT).toColumn()) -// .toTable(db); - -// for(int i = 0; i <= 0xFFFF; ++i) { -// // skip non-char chars -// char c = (char)i; -// if(Character.isHighSurrogate(c) || Character.isLowSurrogate(c)) { -// continue; -// } -// String key = toUnicodeStr(c); -// String str = "AA" + c + "AA"; -// t.addRow(key, str); -// } - - Table t = new TableBuilder("Table5") - .addColumn(new ColumnBuilder("name", DataType.TEXT).toColumn()) - .addColumn(new ColumnBuilder("data", DataType.TEXT).toColumn()) - .toTable(db); - - char c = (char)0x3041; // crazy 7F 02 ... A0 - char c2 = (char)0x30A2; // crazy 7F 02 ... - char c3 = (char)0x2045; // inat 27 ... 1C - char c4 = (char)0x3043; // crazy 7F 03 ... A0 - char c5 = (char)0x3046; // crazy 7F 04 ... - char c6 = (char)0x30F6; // crazy 7F 0D ... A0 - char c7 = (char)0x3099; // unprint 03 - char c8 = (char)0x0041; // A - char c9 = (char)0x002D; // - (unprint) - char c10 = (char)0x20E1; // unprint F2 - char c11 = (char)0x309A; // unprint 04 - char c12 = (char)0x01C4; // (long extra) - char c13 = (char)0x005F; // _ (long inline) - char c14 = (char)0xFFFE; // removed - - char[] cs = new char[]{c7, c8, c3, c12, c13, c14, c, c2, c9}; - addCombos(t, 0, "", cs, 5); - -// t = new TableBuilder("Table2") -// .addColumn(new ColumnBuilder("data", DataType.TEXT).toColumn()) -// .toTable(db); - -// writeChars(0x0000, t); - -// t = new TableBuilder("Table3") -// .addColumn(new ColumnBuilder("data", DataType.TEXT).toColumn()) -// .toTable(db); - -// writeChars(0x0400, t); - - - db.close(); + for (final Database.FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { + Database db = create(fileFormat, true); + + // Table t = new TableBuilder("Table1") + // .addColumn(new ColumnBuilder("key", DataType.TEXT).toColumn()) + // .addColumn(new ColumnBuilder("data", DataType.TEXT).toColumn()) + // .toTable(db); + + // for(int i = 0; i <= 0xFFFF; ++i) { + // // skip non-char chars + // char c = (char)i; + // if(Character.isHighSurrogate(c) || Character.isLowSurrogate(c)) { + // continue; + // } + // String key = toUnicodeStr(c); + // String str = "AA" + c + "AA"; + // t.addRow(key, str); + // } + + Table t = new TableBuilder("Table5") + .addColumn(new ColumnBuilder("name", DataType.TEXT).toColumn()) + .addColumn(new ColumnBuilder("data", DataType.TEXT).toColumn()) + .toTable(db); + + char c = (char)0x3041; // crazy 7F 02 ... A0 + char c2 = (char)0x30A2; // crazy 7F 02 ... + char c3 = (char)0x2045; // inat 27 ... 1C + char c4 = (char)0x3043; // crazy 7F 03 ... A0 + char c5 = (char)0x3046; // crazy 7F 04 ... + char c6 = (char)0x30F6; // crazy 7F 0D ... A0 + char c7 = (char)0x3099; // unprint 03 + char c8 = (char)0x0041; // A + char c9 = (char)0x002D; // - (unprint) + char c10 = (char)0x20E1; // unprint F2 + char c11 = (char)0x309A; // unprint 04 + char c12 = (char)0x01C4; // (long extra) + char c13 = (char)0x005F; // _ (long inline) + char c14 = (char)0xFFFE; // removed + + char[] cs = new char[]{c7, c8, c3, c12, c13, c14, c, c2, c9}; + addCombos(t, 0, "", cs, 5); + + // t = new TableBuilder("Table2") + // .addColumn(new ColumnBuilder("data", DataType.TEXT).toColumn()) + // .toTable(db); + + // writeChars(0x0000, t); + + // t = new TableBuilder("Table3") + // .addColumn(new ColumnBuilder("data", DataType.TEXT).toColumn()) + // .toTable(db); + + // writeChars(0x0400, t); + + + db.close(); + } } public void x_testReadAllCodesMdb() throws Exception { -// Database db = openCopy(new File("/data2/jackcess_test/testAllIndexCodes.mdb")); -// Database db = openCopy(new File("/data2/jackcess_test/testAllIndexCodes_orig.mdb")); -// Database db = openCopy(new File("/data2/jackcess_test/testSomeMoreCodes.mdb")); - Database db = openCopy(new File("/data2/jackcess_test/testStillMoreCodes.mdb")); - Table t = db.getTable("Table5"); - - Index ind = t.getIndexes().iterator().next(); - ind.initialize(); - - System.out.println("Ind " + ind); + for (final TestDB testDB : TestDB.getSupportedForBasename(null)) { + + // Database db = openCopy(new File("/data2/jackcess_test/testAllIndexCodes.mdb")); + // Database db = openCopy(new File("/data2/jackcess_test/testAllIndexCodes_orig.mdb")); + // Database db = openCopy(new File("/data2/jackcess_test/testSomeMoreCodes.mdb")); + // @todo Bank test dbFiles and create new TestDB here. + //Database db = openCopy(new File("/data2/jackcess_test/testStillMoreCodes.mdb")); + Database db = openCopy(testDB); + + Table t = db.getTable("Table5"); + + Index ind = t.getIndexes().iterator().next(); + ind.initialize(); + + System.out.println("Ind " + ind); + + Cursor cursor = Cursor.createIndexCursor(t, ind); + while(cursor.moveToNextRow()) { + System.out.println("======="); + String entryStr = + entryToString(cursor.getSavepoint().getCurrentPosition()); + System.out.println("Entry Bytes: " + entryStr); + System.out.println("Value: " + cursor.getCurrentRow() + "; " + + toUnicodeStr(cursor.getCurrentRow().get("data"))); + } - Cursor cursor = Cursor.createIndexCursor(t, ind); - while(cursor.moveToNextRow()) { - System.out.println("======="); - String entryStr = - entryToString(cursor.getSavepoint().getCurrentPosition()); - System.out.println("Entry Bytes: " + entryStr); - System.out.println("Value: " + cursor.getCurrentRow() + "; " + - toUnicodeStr(cursor.getCurrentRow().get("data"))); + db.close(); } - - db.close(); } private int addCombos(Table t, int rowNum, String s, char[] cs, int len) @@ -285,201 +301,209 @@ public class IndexCodesTest extends TestCase { public void x_testReadIsoMdb() throws Exception { -// Database db = open(new File("/tmp/test_ind.mdb")); -// Database db = open(new File("/tmp/test_ind2.mdb")); - Database db = open(new File("/tmp/test_ind3.mdb")); -// Database db = open(new File("/tmp/test_ind4.mdb")); - - Table t = db.getTable("Table1"); - Index index = t.getIndex("B"); - index.initialize(); - System.out.println("Ind " + index); + for (final TestDB testDB : TestDB.getSupportedForBasename(null)) { + // Database db = open(new File("/tmp/test_ind.mdb")); + // Database db = open(new File("/tmp/test_ind2.mdb")); + // @todo Bank test dbFiles and create new TestDB here. + //Database db = open(new File("/tmp/test_ind3.mdb")); + Database db = open(testDB); + // Database db = open(new File("/tmp/test_ind4.mdb")); + + Table t = db.getTable("Table1"); + Index index = t.getIndex("B"); + index.initialize(); + System.out.println("Ind " + index); + + Cursor cursor = Cursor.createIndexCursor(t, index); + while(cursor.moveToNextRow()) { + System.out.println("======="); + System.out.println("Savepoint: " + cursor.getSavepoint()); + System.out.println("Value: " + cursor.getCurrentRow()); + } - Cursor cursor = Cursor.createIndexCursor(t, index); - while(cursor.moveToNextRow()) { - System.out.println("======="); - System.out.println("Savepoint: " + cursor.getSavepoint()); - System.out.println("Value: " + cursor.getCurrentRow()); + db.close(); } - - db.close(); } public void x_testReverseIsoMdb() throws Exception { - Database db = open(new File("/data2/jackcess_test/testAllIndexCodes3.mdb")); - - Table t = db.getTable("Table1"); - Index index = t.getIndexes().iterator().next(); - index.initialize(); - System.out.println("Ind " + index); - - Pattern inlinePat = Pattern.compile("7F 4A 4A (.*)4A 4A 01 00"); - Pattern unprintPat = Pattern.compile("01 01 01 80 (.+) 06 (.+) 00"); - Pattern unprint2Pat = Pattern.compile("4A 4A 4A 4A 01 02 (.+) 00"); - Pattern inatPat = Pattern.compile("7F 4A 4A (.*)4A 4A 01 02 02 (.+) 00"); - Pattern inat2Pat = Pattern.compile("7F 4A 4A (.*)4A 4A 01 (02 02 (.+))?01 01 (.*)FF 02 80 FF 80 00"); - - Map<Character,String[]> inlineCodes = new TreeMap<Character,String[]>(); - Map<Character,String[]> unprintCodes = new TreeMap<Character,String[]>(); - Map<Character,String[]> unprint2Codes = new TreeMap<Character,String[]>(); - Map<Character,String[]> inatInlineCodes = new TreeMap<Character,String[]>(); - Map<Character,String[]> inatExtraCodes = new TreeMap<Character,String[]>(); - Map<Character,String[]> inat2Codes = new TreeMap<Character,String[]>(); - Map<Character,String[]> inat2ExtraCodes = new TreeMap<Character,String[]>(); - Map<Character,String[]> inat2CrazyCodes = new TreeMap<Character,String[]>(); - - - Cursor cursor = Cursor.createIndexCursor(t, index); - while(cursor.moveToNextRow()) { -// System.out.println("======="); -// System.out.println("Savepoint: " + cursor.getSavepoint()); -// System.out.println("Value: " + cursor.getCurrentRow()); - Cursor.Savepoint savepoint = cursor.getSavepoint(); - String entryStr = entryToString(savepoint.getCurrentPosition()); + for (final TestDB testDB : TestDB.getSupportedForBasename(null)) { + // @todo Bank test dbFiles and create new TestDB here. + //Database db = open(new File("/data2/jackcess_test/testAllIndexCodes3.mdb")); + Database db = open(testDB); + + Table t = db.getTable("Table1"); + Index index = t.getIndexes().iterator().next(); + index.initialize(); + System.out.println("Ind " + index); + + Pattern inlinePat = Pattern.compile("7F 4A 4A (.*)4A 4A 01 00"); + Pattern unprintPat = Pattern.compile("01 01 01 80 (.+) 06 (.+) 00"); + Pattern unprint2Pat = Pattern.compile("4A 4A 4A 4A 01 02 (.+) 00"); + Pattern inatPat = Pattern.compile("7F 4A 4A (.*)4A 4A 01 02 02 (.+) 00"); + Pattern inat2Pat = Pattern.compile("7F 4A 4A (.*)4A 4A 01 (02 02 (.+))?01 01 (.*)FF 02 80 FF 80 00"); + + Map<Character,String[]> inlineCodes = new TreeMap<Character,String[]>(); + Map<Character,String[]> unprintCodes = new TreeMap<Character,String[]>(); + Map<Character,String[]> unprint2Codes = new TreeMap<Character,String[]>(); + Map<Character,String[]> inatInlineCodes = new TreeMap<Character,String[]>(); + Map<Character,String[]> inatExtraCodes = new TreeMap<Character,String[]>(); + Map<Character,String[]> inat2Codes = new TreeMap<Character,String[]>(); + Map<Character,String[]> inat2ExtraCodes = new TreeMap<Character,String[]>(); + Map<Character,String[]> inat2CrazyCodes = new TreeMap<Character,String[]>(); + + + Cursor cursor = Cursor.createIndexCursor(t, index); + while(cursor.moveToNextRow()) { + // System.out.println("======="); + // System.out.println("Savepoint: " + cursor.getSavepoint()); + // System.out.println("Value: " + cursor.getCurrentRow()); + Cursor.Savepoint savepoint = cursor.getSavepoint(); + String entryStr = entryToString(savepoint.getCurrentPosition()); + + Map<String,Object> row = cursor.getCurrentRow(); + String value = (String)row.get("data"); + String key = (String)row.get("key"); + char c = value.charAt(2); + System.out.println("======="); + System.out.println("RowId: " + + savepoint.getCurrentPosition().getRowId()); + System.out.println("Entry: " + entryStr); + // System.out.println("Row: " + row); + System.out.println("Value: (" + key + ")" + value); + System.out.println("Char: " + c + ", " + (int)c + ", " + + toUnicodeStr(c)); + + String type = null; + if(entryStr.endsWith("01 00")) { + + // handle inline codes + type = "INLINE"; + Matcher m = inlinePat.matcher(entryStr); + m.find(); + handleInlineEntry(m.group(1), c, inlineCodes); + + } else if(entryStr.contains("01 01 01 80")) { + + // handle most unprintable codes + type = "UNPRINTABLE"; + Matcher m = unprintPat.matcher(entryStr); + m.find(); + handleUnprintableEntry(m.group(2), c, unprintCodes); + + } else if(entryStr.contains("01 02 02") && + !entryStr.contains("FF 02 80 FF 80")) { + + // handle chars w/ symbols + type = "CHAR_WITH_SYMBOL"; + Matcher m = inatPat.matcher(entryStr); + m.find(); + handleInternationalEntry(m.group(1), m.group(2), c, + inatInlineCodes, inatExtraCodes); + + } else if(entryStr.contains("4A 4A 4A 4A 01 02")) { + + // handle chars w/ symbols + type = "UNPRINTABLE_2"; + Matcher m = unprint2Pat.matcher(entryStr); + m.find(); + handleUnprintable2Entry(m.group(1), c, unprint2Codes); + + } else if(entryStr.contains("FF 02 80 FF 80")) { + + type = "CRAZY_INAT"; + Matcher m = inat2Pat.matcher(entryStr); + m.find(); + handleInternational2Entry(m.group(1), m.group(3), m.group(4), c, + inat2Codes, inat2ExtraCodes, + inat2CrazyCodes); - Map<String,Object> row = cursor.getCurrentRow(); - String value = (String)row.get("data"); - String key = (String)row.get("key"); - char c = value.charAt(2); - System.out.println("======="); - System.out.println("RowId: " + - savepoint.getCurrentPosition().getRowId()); - System.out.println("Entry: " + entryStr); -// System.out.println("Row: " + row); - System.out.println("Value: (" + key + ")" + value); - System.out.println("Char: " + c + ", " + (int)c + ", " + - toUnicodeStr(c)); - - String type = null; - if(entryStr.endsWith("01 00")) { - - // handle inline codes - type = "INLINE"; - Matcher m = inlinePat.matcher(entryStr); - m.find(); - handleInlineEntry(m.group(1), c, inlineCodes); - - } else if(entryStr.contains("01 01 01 80")) { - - // handle most unprintable codes - type = "UNPRINTABLE"; - Matcher m = unprintPat.matcher(entryStr); - m.find(); - handleUnprintableEntry(m.group(2), c, unprintCodes); - - } else if(entryStr.contains("01 02 02") && - !entryStr.contains("FF 02 80 FF 80")) { - - // handle chars w/ symbols - type = "CHAR_WITH_SYMBOL"; - Matcher m = inatPat.matcher(entryStr); - m.find(); - handleInternationalEntry(m.group(1), m.group(2), c, - inatInlineCodes, inatExtraCodes); - - } else if(entryStr.contains("4A 4A 4A 4A 01 02")) { - - // handle chars w/ symbols - type = "UNPRINTABLE_2"; - Matcher m = unprint2Pat.matcher(entryStr); - m.find(); - handleUnprintable2Entry(m.group(1), c, unprint2Codes); - - } else if(entryStr.contains("FF 02 80 FF 80")) { - - type = "CRAZY_INAT"; - Matcher m = inat2Pat.matcher(entryStr); - m.find(); - handleInternational2Entry(m.group(1), m.group(3), m.group(4), c, - inat2Codes, inat2ExtraCodes, - inat2CrazyCodes); - - } else { - - throw new RuntimeException("unhandled " + entryStr); - } - - System.out.println("Type: " + type); - } + } else { - System.out.println("\n***CODES"); - for(int i = 0; i <= 0xFFFF; ++i) { + throw new RuntimeException("unhandled " + entryStr); + } - if(i == 256) { - System.out.println("\n***EXTENDED CODES"); + System.out.println("Type: " + type); } - // skip non-char chars - char c = (char)i; - if(Character.isHighSurrogate(c) || Character.isLowSurrogate(c)) { - continue; - } + System.out.println("\n***CODES"); + for(int i = 0; i <= 0xFFFF; ++i) { - if(c == (char)0xFFFE) { - // this gets replaced with FFFD, treat it the same - c = (char)0xFFFD; - } + if(i == 256) { + System.out.println("\n***EXTENDED CODES"); + } - Character cc = c; - String[] chars = inlineCodes.get(cc); - if(chars != null) { - if((chars.length == 1) && (chars[0].length() == 0)) { - System.out.println("X"); - } else { - System.out.println("S" + toByteString(chars)); + // skip non-char chars + char c = (char)i; + if(Character.isHighSurrogate(c) || Character.isLowSurrogate(c)) { + continue; } - continue; - } - chars = inatInlineCodes.get(cc); - if(chars != null) { - String[] extra = inatExtraCodes.get(cc); - System.out.println("I" + toByteString(chars) + "," + - toByteString(extra)); - continue; - } - - chars = unprintCodes.get(cc); - if(chars != null) { - System.out.println("U" + toByteString(chars)); - continue; - } + if(c == (char)0xFFFE) { + // this gets replaced with FFFD, treat it the same + c = (char)0xFFFD; + } - chars = unprint2Codes.get(cc); - if(chars != null) { - if(chars.length > 1) { - throw new RuntimeException("long unprint codes"); + Character cc = c; + String[] chars = inlineCodes.get(cc); + if(chars != null) { + if((chars.length == 1) && (chars[0].length() == 0)) { + System.out.println("X"); + } else { + System.out.println("S" + toByteString(chars)); + } + continue; + } + + chars = inatInlineCodes.get(cc); + if(chars != null) { + String[] extra = inatExtraCodes.get(cc); + System.out.println("I" + toByteString(chars) + "," + + toByteString(extra)); + continue; + } + + chars = unprintCodes.get(cc); + if(chars != null) { + System.out.println("U" + toByteString(chars)); + continue; } - int val = Integer.parseInt(chars[0], 16) - 2; - String valStr = ByteUtil.toHexString(new byte[]{(byte)val}).trim(); - System.out.println("P" + valStr); - continue; - } - chars = inat2Codes.get(cc); - if(chars != null) { - String [] crazyCodes = inat2CrazyCodes.get(cc); - String crazyCode = ""; - if(crazyCodes != null) { - if((crazyCodes.length != 1) || !"A0".equals(crazyCodes[0])) { - throw new RuntimeException("CC " + Arrays.asList(crazyCodes)); + chars = unprint2Codes.get(cc); + if(chars != null) { + if(chars.length > 1) { + throw new RuntimeException("long unprint codes"); } - crazyCode = "1"; + int val = Integer.parseInt(chars[0], 16) - 2; + String valStr = ByteUtil.toHexString(new byte[]{(byte)val}).trim(); + System.out.println("P" + valStr); + continue; } - String[] extra = inat2ExtraCodes.get(cc); - System.out.println("Z" + toByteString(chars) + "," + - toByteString(extra) + "," + - crazyCode); - continue; + chars = inat2Codes.get(cc); + if(chars != null) { + String [] crazyCodes = inat2CrazyCodes.get(cc); + String crazyCode = ""; + if(crazyCodes != null) { + if((crazyCodes.length != 1) || !"A0".equals(crazyCodes[0])) { + throw new RuntimeException("CC " + Arrays.asList(crazyCodes)); + } + crazyCode = "1"; + } + + String[] extra = inat2ExtraCodes.get(cc); + System.out.println("Z" + toByteString(chars) + "," + + toByteString(extra) + "," + + crazyCode); + continue; + } + + throw new RuntimeException("Unhandled char " + toUnicodeStr(c)); } + System.out.println("\n***END CODES"); - throw new RuntimeException("Unhandled char " + toUnicodeStr(c)); + db.close(); } - System.out.println("\n***END CODES"); - - db.close(); } private static String toByteString(String[] chars) diff --git a/test/src/java/com/healthmarketscience/jackcess/IndexTest.java b/test/src/java/com/healthmarketscience/jackcess/IndexTest.java index 4e547dd..4bb12e7 100644 --- a/test/src/java/com/healthmarketscience/jackcess/IndexTest.java +++ b/test/src/java/com/healthmarketscience/jackcess/IndexTest.java @@ -27,7 +27,6 @@ King of Prussia, PA 19406 package com.healthmarketscience.jackcess; -import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; @@ -40,6 +39,7 @@ import java.util.TreeSet; import junit.framework.TestCase; import static com.healthmarketscience.jackcess.DatabaseTest.*; +import static com.healthmarketscience.jackcess.JetFormatTest.*; /** * @author James Ahlborn @@ -84,131 +84,140 @@ public class IndexTest extends TestCase { } public void testPrimaryKey() throws Exception { - Table table = open().getTable("Table1"); - Map<String, Boolean> foundPKs = new HashMap<String, Boolean>(); - for(Index index : table.getIndexes()) { - foundPKs.put(index.getColumns().iterator().next().getName(), - index.isPrimaryKey()); + for (final TestDB testDB : SUPPORTED_DBS_TEST) { + Table table = open(testDB).getTable("Table1"); + Map<String, Boolean> foundPKs = new HashMap<String, Boolean>(); + for(Index index : table.getIndexes()) { + foundPKs.put(index.getColumns().iterator().next().getName(), + index.isPrimaryKey()); + } + Map<String, Boolean> expectedPKs = new HashMap<String, Boolean>(); + expectedPKs.put("A", Boolean.TRUE); + expectedPKs.put("B", Boolean.FALSE); + assertEquals(expectedPKs, foundPKs); } - Map<String, Boolean> expectedPKs = new HashMap<String, Boolean>(); - expectedPKs.put("A", Boolean.TRUE); - expectedPKs.put("B", Boolean.FALSE); - assertEquals(expectedPKs, foundPKs); } public void testIndexSlots() throws Exception { - Database mdb = open(new File("test/data/indexTest.mdb")); + for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX)) { + Database mdb = open(testDB); - Table table = mdb.getTable("Table1"); - for(Index idx : table.getIndexes()) { - idx.initialize(); - } - assertEquals(4, table.getIndexes().size()); - assertEquals(4, table.getIndexSlotCount()); - checkIndexColumns(table, - "id", "id", - "PrimaryKey", "id", - "Table2Table1", "otherfk1", - "Table3Table1", "otherfk2"); - - table = mdb.getTable("Table2"); - for(Index idx : table.getIndexes()) { - idx.initialize(); - } - assertEquals(2, table.getIndexes().size()); - assertEquals(3, table.getIndexSlotCount()); - checkIndexColumns(table, - "id", "id", - "PrimaryKey", "id"); - - table = mdb.getTable("Table3"); - for(Index idx : table.getIndexes()) { - idx.initialize(); + Table table = mdb.getTable("Table1"); + for(Index idx : table.getIndexes()) { + idx.initialize(); + } + assertEquals(4, table.getIndexes().size()); + assertEquals(4, table.getIndexSlotCount()); + checkIndexColumns(table, + "id", "id", + "PrimaryKey", "id", + "Table2Table1", "otherfk1", + "Table3Table1", "otherfk2"); + + table = mdb.getTable("Table2"); + for(Index idx : table.getIndexes()) { + idx.initialize(); + } + assertEquals(2, table.getIndexes().size()); + assertEquals(3, table.getIndexSlotCount()); + checkIndexColumns(table, + "id", "id", + "PrimaryKey", "id"); + + table = mdb.getTable("Table3"); + for(Index idx : table.getIndexes()) { + idx.initialize(); + } + assertEquals(2, table.getIndexes().size()); + assertEquals(3, table.getIndexSlotCount()); + checkIndexColumns(table, + "id", "id", + "PrimaryKey", "id"); } - assertEquals(2, table.getIndexes().size()); - assertEquals(3, table.getIndexSlotCount()); - checkIndexColumns(table, - "id", "id", - "PrimaryKey", "id"); } public void testComplexIndex() throws Exception { - // this file has an index with "compressed" entries and node pages - File origFile = new File("test/data/compIndexTest.mdb"); - Database db = open(origFile); - Table t = db.getTable("Table1"); - Index index = t.getIndexes().get(0); - assertFalse(index.isInitialized()); - assertEquals(512, countRows(t)); - assertEquals(512, index.getEntryCount()); - db.close(); - - // copy to temp file and attempt to edit - db = openCopy(origFile); - t = db.getTable("Table1"); - index = t.getIndexes().get(0); - - System.out.println("IndexTest: Index type: " + index.getClass()); - try { - t.addRow(99, "abc", "def"); - if(index instanceof SimpleIndex) { - // SimpleIndex doesn't support writing these indexes - fail("Should have thrown UnsupportedOperationException"); - } - } catch(UnsupportedOperationException e) { - // success - if(index instanceof BigIndex) { - throw e; + for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.COMP_INDEX)) { + // this file has an index with "compressed" entries and node pages + Database db = open(testDB); + Table t = db.getTable("Table1"); + Index index = t.getIndexes().get(0); + assertFalse(index.isInitialized()); + assertEquals(512, countRows(t)); + assertEquals(512, index.getEntryCount()); + db.close(); + + // copy to temp file and attempt to edit + db = openCopy(testDB); + t = db.getTable("Table1"); + index = t.getIndexes().get(0); + + System.out.println("IndexTest: Index type: " + index.getClass()); + try { + t.addRow(99, "abc", "def"); + if(index instanceof SimpleIndex) { + // SimpleIndex doesn't support writing these indexes + fail("Should have thrown UnsupportedOperationException"); + } + } catch(UnsupportedOperationException e) { + // success + if(index instanceof BigIndex) { + throw e; + } } } } public void testEntryDeletion() throws Exception { - Table table = openCopy(new File("test/data/test.mdb")).getTable("Table1"); + for (final TestDB testDB : SUPPORTED_DBS_TEST) { + Table table = openCopy(testDB).getTable("Table1"); - for(int i = 0; i < 10; ++i) { - table.addRow("foo" + i, "bar" + i, (byte)42 + i, (short)53 + i, 13 * i, - (6.7d / i), null, null, true); - } - table.reset(); - assertRowCount(12, table); + for(int i = 0; i < 10; ++i) { + table.addRow("foo" + i, "bar" + i, (byte)42 + i, (short)53 + i, 13 * i, + (6.7d / i), null, null, true); + } + table.reset(); + assertRowCount(12, table); - for(Index index : table.getIndexes()) { - assertEquals(12, index.getEntryCount()); - } + for(Index index : table.getIndexes()) { + assertEquals(12, index.getEntryCount()); + } - table.reset(); - table.getNextRow(); - table.getNextRow(); - table.deleteCurrentRow(); - table.getNextRow(); - table.deleteCurrentRow(); - table.getNextRow(); - table.getNextRow(); - table.deleteCurrentRow(); - table.getNextRow(); - table.getNextRow(); - table.getNextRow(); - table.deleteCurrentRow(); - - table.reset(); - assertRowCount(8, table); - - for(Index index : table.getIndexes()) { - assertEquals(8, index.getEntryCount()); + table.reset(); + table.getNextRow(); + table.getNextRow(); + table.deleteCurrentRow(); + table.getNextRow(); + table.deleteCurrentRow(); + table.getNextRow(); + table.getNextRow(); + table.deleteCurrentRow(); + table.getNextRow(); + table.getNextRow(); + table.getNextRow(); + table.deleteCurrentRow(); + + table.reset(); + assertRowCount(8, table); + + for(Index index : table.getIndexes()) { + assertEquals(8, index.getEntryCount()); + } } } public void testIgnoreNulls() throws Exception { - Database db = openCopy(new File("test/data/testIndexProperties.mdb")); + for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX_PROPERTIES)) { + Database db = openCopy(testDB); - doTestIgnoreNulls(db, "TableIgnoreNulls1"); - doTestIgnoreNulls(db, "TableIgnoreNulls2"); + doTestIgnoreNulls(db, "TableIgnoreNulls1"); + doTestIgnoreNulls(db, "TableIgnoreNulls2"); - db.close(); + db.close(); + } } private void doTestIgnoreNulls(Database db, String tableName) @@ -250,41 +259,43 @@ public class IndexTest extends TestCase { public void testUnique() throws Exception { - Database db = openCopy(new File("test/data/testIndexProperties.mdb")); - - Table t = db.getTable("TableUnique1_temp"); - Index index = t.getIndex("DataIndex"); - - doTestUnique(t, index, 1, - null, true, - "unique data", true, - null, true, - "more", false, - "stuff", false, - "unique data", false); - - t = db.getTable("TableUnique2_temp"); - index = t.getIndex("DataIndex"); - - doTestUnique(t, index, 2, - null, null, true, - "unique data", 42, true, - "unique data", null, true, - null, null, true, - "some", 42, true, - "more unique data", 13, true, - null, -4242, true, - "another row", -3462, false, - null, 49, false, - "more", null, false, - "unique data", 42, false, - "unique data", null, false, - null, -4242, false); - - db.close(); + for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX_PROPERTIES)) { + Database db = openCopy(testDB); + + Table t = db.getTable("TableUnique1_temp"); + Index index = t.getIndex("DataIndex"); + + doTestUnique(index, 1, + null, true, + "unique data", true, + null, true, + "more", false, + "stuff", false, + "unique data", false); + + t = db.getTable("TableUnique2_temp"); + index = t.getIndex("DataIndex"); + + doTestUnique(index, 2, + null, null, true, + "unique data", 42, true, + "unique data", null, true, + null, null, true, + "some", 42, true, + "more unique data", 13, true, + null, -4242, true, + "another row", -3462, false, + null, 49, false, + "more", null, false, + "unique data", 42, false, + "unique data", null, false, + null, -4242, false); + + db.close(); + } } - private void doTestUnique(Table t, Index index, int numValues, + private void doTestUnique(Index index, int numValues, Object... testData) throws Exception { @@ -312,70 +323,74 @@ public class IndexTest extends TestCase { } public void testUniqueEntryCount() throws Exception { - Database db = openCopy(new File("test/data/test.mdb")); - Table table = db.getTable("Table1"); - Index indA = table.getIndex("PrimaryKey"); - Index indB = table.getIndex("B"); + for (final TestDB testDB : SUPPORTED_DBS_TEST) { + Database db = openCopy(testDB); + Table table = db.getTable("Table1"); + Index indA = table.getIndex("PrimaryKey"); + Index indB = table.getIndex("B"); - assertEquals(2, indA.getUniqueEntryCount()); - assertEquals(2, indB.getUniqueEntryCount()); + assertEquals(2, indA.getUniqueEntryCount()); + assertEquals(2, indB.getUniqueEntryCount()); - List<String> bElems = Arrays.asList("bar", null, "baz", "argle", null, - "bazzle", "37", "bar", "bar", "BAZ"); - - for(int i = 0; i < 10; ++i) { - table.addRow("foo" + i, bElems.get(i), (byte)42 + i, (short)53 + i, - 13 * i, (6.7d / i), null, null, true); - } + List<String> bElems = Arrays.asList("bar", null, "baz", "argle", null, + "bazzle", "37", "bar", "bar", "BAZ"); - assertEquals(12, indA.getEntryCount()); - assertEquals(12, indB.getEntryCount()); - - assertEquals(12, indA.getUniqueEntryCount()); - assertEquals(8, indB.getUniqueEntryCount()); + for(int i = 0; i < 10; ++i) { + table.addRow("foo" + i, bElems.get(i), (byte)42 + i, (short)53 + i, + 13 * i, (6.7d / i), null, null, true); + } - table = null; - indA = null; - indB = null; + assertEquals(12, indA.getEntryCount()); + assertEquals(12, indB.getEntryCount()); - table = db.getTable("Table1"); - indA = table.getIndex("PrimaryKey"); - indB = table.getIndex("B"); - - assertEquals(12, indA.getEntryCount()); - assertEquals(12, indB.getEntryCount()); - - assertEquals(12, indA.getUniqueEntryCount()); - assertEquals(8, indB.getUniqueEntryCount()); - - Cursor c = Cursor.createCursor(table); - assertTrue(c.moveToNextRow()); - Map<String,Object> row = c.getCurrentRow(); - assertEquals("abcdefg", row.get("A")); - assertEquals("hijklmnop", row.get("B")); - c.deleteCurrentRow(); - - assertEquals(11, indA.getEntryCount()); - assertEquals(11, indB.getEntryCount()); - - assertEquals(12, indA.getUniqueEntryCount()); - assertEquals(8, indB.getUniqueEntryCount()); - - db.close(); + assertEquals(12, indA.getUniqueEntryCount()); + assertEquals(8, indB.getUniqueEntryCount()); + + table = null; + indA = null; + indB = null; + + table = db.getTable("Table1"); + indA = table.getIndex("PrimaryKey"); + indB = table.getIndex("B"); + + assertEquals(12, indA.getEntryCount()); + assertEquals(12, indB.getEntryCount()); + + assertEquals(12, indA.getUniqueEntryCount()); + assertEquals(8, indB.getUniqueEntryCount()); + + Cursor c = Cursor.createCursor(table); + assertTrue(c.moveToNextRow()); + Map<String,Object> row = c.getCurrentRow(); + assertEquals(testDB.toString(), "abcdefg", row.get("A")); // @todo Fails with v2007 + assertEquals("hijklmnop", row.get("B")); + c.deleteCurrentRow(); + + assertEquals(11, indA.getEntryCount()); + assertEquals(11, indB.getEntryCount()); + + assertEquals(12, indA.getUniqueEntryCount()); + assertEquals(8, indB.getUniqueEntryCount()); + + db.close(); + } } public void testReplId() throws Exception { - Database db = openCopy(new File("test/data/test.mdb")); - Table table = db.getTable("Table4"); + for (final TestDB testDB : SUPPORTED_DBS_TEST) { + Database db = openCopy(testDB); + Table table = db.getTable("Table4"); - for(int i = 0; i< 20; ++i) { - table.addRow("row" + i, Column.AUTO_NUMBER); - } + for(int i = 0; i< 20; ++i) { + table.addRow("row" + i, Column.AUTO_NUMBER); + } - assertEquals(20, table.getRowCount()); - - db.close(); + assertEquals(20, table.getRowCount()); + + db.close(); + } } private void checkIndexColumns(Table table, String... idxInfo) diff --git a/test/src/java/com/healthmarketscience/jackcess/JetFormatTest.java b/test/src/java/com/healthmarketscience/jackcess/JetFormatTest.java index ca46cdb..a25810e 100644 --- a/test/src/java/com/healthmarketscience/jackcess/JetFormatTest.java +++ b/test/src/java/com/healthmarketscience/jackcess/JetFormatTest.java @@ -11,12 +11,102 @@ import java.nio.channels.FileChannel; * Date: Mar 5, 2010 * Time: 12:44:21 PM */ -public final class JetFormatTest extends TestCase { +public final class JetFormatTest extends TestCase { + + private static final File DIR_TEST_DATA = new File("test/data"); + + /** + * Defines known valid db test file base names. + */ + public static enum Basename { + + BIG_INDEX("bigIndexTest"), + COMP_INDEX("compIndexTest"), + DEL_COL("delColTest"), + DEL("delTest"), + FIXED_NUMERIC("fixedNumericTest"), + FIXED_TEXT("fixedTextTest"), + INDEX_CURSOR("indexCursorTest"), + INDEX("indexTest"), + OVERFLOW("overflowTest"), + QUERY("queryTest"), + TEST("test"), + TEST2("test2"), + INDEX_CODES("testIndexCodes"), + INDEX_PROPERTIES("testIndexProperties"), + PROMOTION("testPromotion"), + ; + + private final String basename; + + Basename(final String fileBasename) { + basename = fileBasename; + } + + } + + /** Defines currently supported db file formats. */ + final static Database.FileFormat[] SUPPORTED_FILEFORMATS = new Database.FileFormat[] { + Database.FileFormat.V2000, + Database.FileFormat.V2003, + Database.FileFormat.V2007, + // @todo Uncomment these elements to run test other formats + }; + + /** + * Defines known valid test database files, and their jet format version. + */ + public static final class TestDB { + + private final File dbFile; + private final Database.FileFormat expectedFileFormat; + + private TestDB(final File databaseFile, final Database.FileFormat expectedDBFileFormat) { + + dbFile = databaseFile; + expectedFileFormat = expectedDBFileFormat; + } + + public final File getFile() { return dbFile; } + public final Database.FileFormat getExpectedFileFormat() { return expectedFileFormat; } + public final JetFormat getExpectedFormat() { return expectedFileFormat.getJetFormat(); } + + public final String toString() { + return "dbFile: " + dbFile.getAbsolutePath() + + "; expectedFileFormat: " + expectedFileFormat; + } + + public static TestDB[] getSupportedForBasename(final Basename basename) { + + final TestDB[] supportedTestDBs = new TestDB[SUPPORTED_FILEFORMATS.length]; + int i = 0; + for (final Database.FileFormat fileFormat: SUPPORTED_FILEFORMATS) { + supportedTestDBs[i++] = new TestDB( + getFileForBasename(basename, fileFormat), + fileFormat); + } + return supportedTestDBs; + } + + private static File getFileForBasename(Basename basename, Database.FileFormat fileFormat) { + + final String filenameExtension; + if (Database.FileFormat.V2007.equals(fileFormat)) { + filenameExtension = ".accdb"; + } else { + filenameExtension = ".mdb"; + } + + return new File(DIR_TEST_DATA, + fileFormat.name() + "/" + basename.basename + fileFormat.name() + filenameExtension); + } + } + + static final TestDB UNSUPPORTED_TEST_V1997 = new TestDB( + TestDB.getFileForBasename(Basename.TEST, Database.FileFormat.V1997), Database.FileFormat.V1997); + + static final TestDB[] SUPPORTED_DBS_TEST= TestDB.getSupportedForBasename(Basename.TEST); - static final File DB_1997 = new File("test/data/mdb97/test97.mdb"); - static final File DB_2000 = new File("test/data/test.mdb"); - static final File DB_2003 = new File("test/data/mdb2003/test2003.mdb"); - static final File DB_2007 = new File("test/data/accdb/test.accdb"); public void testGetFormat() throws Exception { try { @@ -26,21 +116,22 @@ public final class JetFormatTest extends TestCase { assertNull(e.getMessage()); } - checkJetFormat(DB_1997, JetFormat.VERSION_3); - checkJetFormat(DB_2000, JetFormat.VERSION_4); - checkJetFormat(DB_2003, JetFormat.VERSION_4); - checkJetFormat(DB_2007, JetFormat.VERSION_5); + checkJetFormat(UNSUPPORTED_TEST_V1997); + + for (final TestDB testDB : SUPPORTED_DBS_TEST) { + checkJetFormat(testDB); + } } - private static void checkJetFormat(final File dbFile, final JetFormat fmtExpected) + private static void checkJetFormat(final TestDB testDB) throws IOException { - final FileChannel channel = Database.openChannel(dbFile, false); + final FileChannel channel = Database.openChannel(testDB.dbFile, false); try { final JetFormat fmtActual = JetFormat.getFormat(channel); - assertEquals("Unexpected JetFormat for dbFile: " + dbFile.getAbsolutePath(), - fmtExpected, fmtActual); + assertEquals("Unexpected JetFormat for dbFile: " + testDB.dbFile.getAbsolutePath(), + testDB.expectedFileFormat.getJetFormat(), fmtActual); } finally { channel.close(); diff --git a/test/src/java/com/healthmarketscience/jackcess/RelationshipTest.java b/test/src/java/com/healthmarketscience/jackcess/RelationshipTest.java index 67ed711..a6f3c44 100644 --- a/test/src/java/com/healthmarketscience/jackcess/RelationshipTest.java +++ b/test/src/java/com/healthmarketscience/jackcess/RelationshipTest.java @@ -27,13 +27,13 @@ King of Prussia, PA 19406 package com.healthmarketscience.jackcess; -import java.io.File; import java.util.Arrays; import java.util.List; import junit.framework.TestCase; import static com.healthmarketscience.jackcess.DatabaseTest.*; +import static com.healthmarketscience.jackcess.JetFormatTest.*; /** * @author James Ahlborn @@ -45,50 +45,51 @@ public class RelationshipTest extends TestCase { } public void testSimple() throws Exception { - Database db = open(new File("test/data/indexTest.mdb")); - Table t1 = db.getTable("Table1"); - Table t2 = db.getTable("Table2"); - Table t3 = db.getTable("Table3"); - - List<Relationship> rels = db.getRelationships(t1, t2); - assertEquals(1, rels.size()); - Relationship rel = rels.get(0); - assertEquals("Table2Table1", rel.getName()); - assertEquals(t2, rel.getFromTable()); - assertEquals(Arrays.asList(t2.getColumn("id")), - rel.getFromColumns()); - assertEquals(t1, rel.getToTable()); - assertEquals(Arrays.asList(t1.getColumn("otherfk1")), - rel.getToColumns()); - assertTrue(rel.hasReferentialIntegrity()); - assertEquals(0, rel.getFlags()); - assertSameRelationships(rels, db.getRelationships(t2, t1)); - - rels = db.getRelationships(t2, t3); - assertTrue(db.getRelationships(t2, t3).isEmpty()); - assertSameRelationships(rels, db.getRelationships(t3, t2)); - - rels = db.getRelationships(t1, t3); - assertEquals(1, rels.size()); - rel = rels.get(0); - assertEquals("Table3Table1", rel.getName()); - assertEquals(t3, rel.getFromTable()); - assertEquals(Arrays.asList(t3.getColumn("id")), - rel.getFromColumns()); - assertEquals(t1, rel.getToTable()); - assertEquals(Arrays.asList(t1.getColumn("otherfk2")), - rel.getToColumns()); - assertTrue(rel.hasReferentialIntegrity()); - assertEquals(0, rel.getFlags()); - assertSameRelationships(rels, db.getRelationships(t3, t1)); - - try { - db.getRelationships(t1, t1); - fail("IllegalArgumentException should have been thrown"); - } catch(IllegalArgumentException ignored) { - // success + for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX)) { + Database db = open(testDB); + Table t1 = db.getTable("Table1"); + Table t2 = db.getTable("Table2"); + Table t3 = db.getTable("Table3"); + + List<Relationship> rels = db.getRelationships(t1, t2); + assertEquals(1, rels.size()); + Relationship rel = rels.get(0); + assertEquals("Table2Table1", rel.getName()); + assertEquals(t2, rel.getFromTable()); + assertEquals(Arrays.asList(t2.getColumn("id")), + rel.getFromColumns()); + assertEquals(t1, rel.getToTable()); + assertEquals(Arrays.asList(t1.getColumn("otherfk1")), + rel.getToColumns()); + assertTrue(rel.hasReferentialIntegrity()); + assertEquals(0, rel.getFlags()); + assertSameRelationships(rels, db.getRelationships(t2, t1)); + + rels = db.getRelationships(t2, t3); + assertTrue(db.getRelationships(t2, t3).isEmpty()); + assertSameRelationships(rels, db.getRelationships(t3, t2)); + + rels = db.getRelationships(t1, t3); + assertEquals(1, rels.size()); + rel = rels.get(0); + assertEquals("Table3Table1", rel.getName()); + assertEquals(t3, rel.getFromTable()); + assertEquals(Arrays.asList(t3.getColumn("id")), + rel.getFromColumns()); + assertEquals(t1, rel.getToTable()); + assertEquals(Arrays.asList(t1.getColumn("otherfk2")), + rel.getToColumns()); + assertTrue(rel.hasReferentialIntegrity()); + assertEquals(0, rel.getFlags()); + assertSameRelationships(rels, db.getRelationships(t3, t1)); + + try { + db.getRelationships(t1, t1); + fail("IllegalArgumentException should have been thrown"); + } catch(IllegalArgumentException ignored) { + // success + } } - } private void assertSameRelationships( diff --git a/test/src/java/com/healthmarketscience/jackcess/UsageMapTest.java b/test/src/java/com/healthmarketscience/jackcess/UsageMapTest.java index 47937b5..85b0630 100644 --- a/test/src/java/com/healthmarketscience/jackcess/UsageMapTest.java +++ b/test/src/java/com/healthmarketscience/jackcess/UsageMapTest.java @@ -5,6 +5,8 @@ import junit.framework.TestCase; import java.io.File; import java.io.IOException; +import static com.healthmarketscience.jackcess.JetFormatTest.*; + /** * @author Dan Rollo * Date: Mar 5, 2010 @@ -14,14 +16,30 @@ public final class UsageMapTest extends TestCase { public void testRead() throws Exception { try { - Database.open(JetFormatTest.DB_1997); + Database.open(UNSUPPORTED_TEST_V1997.getFile()); fail("mdb v97 usage map unsupported"); } catch (IOException e) { assertEquals(UsageMap.MSG_PREFIX_UNRECOGNIZED_MAP + 2, e.getMessage()); } - checkUsageMapRead(JetFormatTest.DB_2000, 743, 767); - checkUsageMapRead(JetFormatTest.DB_2007, 42, 511); + for (final TestDB testDB : SUPPORTED_DBS_TEST) { + final int expectedFirstPage; + final int expectedLastPage; + final Database.FileFormat expectedFileFormat = testDB.getExpectedFileFormat(); + if (Database.FileFormat.V2000.equals(expectedFileFormat)) { + expectedFirstPage = 743; + expectedLastPage = 767; + } else if (Database.FileFormat.V2003.equals(expectedFileFormat)) { + expectedFirstPage = 16; + expectedLastPage = 799; + } else if (Database.FileFormat.V2007.equals(expectedFileFormat)) { + expectedFirstPage = 42; + expectedLastPage = 511; + } else { + throw new IllegalAccessException("Unknown file format: " + expectedFileFormat); + } + checkUsageMapRead(testDB.getFile(), expectedFirstPage, expectedLastPage); + } } private static void checkUsageMapRead(final File dbFile, @@ -30,8 +48,8 @@ public final class UsageMapTest extends TestCase { final Database db = Database.open(dbFile); final UsageMap usageMap = UsageMap.read(db, - 1, //PageChannel.PAGE_GLOBAL_USAGE_MAP, - 0, //PageChannel.ROW_GLOBAL_USAGE_MAP, + PageChannel.PAGE_GLOBAL_USAGE_MAP, + PageChannel.ROW_GLOBAL_USAGE_MAP, true); assertEquals("Unexpected FirstPageNumber.", expectedFirstPage, usageMap.getFirstPageNumber()); assertEquals("Unexpected LastPageNumber.", expectedLastPage, usageMap.getLastPageNumber()); diff --git a/test/src/java/com/healthmarketscience/jackcess/query/QueryTest.java b/test/src/java/com/healthmarketscience/jackcess/query/QueryTest.java index 751fbb4..0ba3272 100644 --- a/test/src/java/com/healthmarketscience/jackcess/query/QueryTest.java +++ b/test/src/java/com/healthmarketscience/jackcess/query/QueryTest.java @@ -27,7 +27,6 @@ King of Prussia, PA 19406 package com.healthmarketscience.jackcess.query; -import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -45,6 +44,8 @@ import org.apache.commons.lang.StringUtils; import static org.apache.commons.lang.SystemUtils.LINE_SEPARATOR; import static com.healthmarketscience.jackcess.query.QueryFormat.*; +import static com.healthmarketscience.jackcess.JetFormatTest.*; + /** * @author James Ahlborn @@ -199,68 +200,70 @@ public class QueryTest extends TestCase public void testReadQueries() throws Exception { - Map<String,String> expectedQueries = new HashMap<String,String>(); - expectedQueries.put( - "SelectQuery", multiline( - "SELECT DISTINCT Table1.*, Table2.col1, Table2.col2, Table3.col3", - "FROM (Table1 LEFT JOIN Table3 ON Table1.col1 = Table3.col1) INNER JOIN Table2 ON (Table3.col1 = Table2.col1) AND (Table3.col1 = Table2.col2)", - "WHERE (((Table2.col2)=\"foo\" Or (Table2.col2) In (\"buzz\",\"bazz\")))", - "ORDER BY Table2.col1;")); - expectedQueries.put( - "DeleteQuery", multiline( - "DELETE Table1.col1, Table1.col2, Table1.col3", - "FROM Table1", - "WHERE (((Table1.col1)>\"blah\"));")); - expectedQueries.put( - "AppendQuery",multiline( - "INSERT INTO Table3", - "SELECT [Table1].[col2], [Table2].[col2], [Table2].[col3]", - "FROM Table3, Table1 INNER JOIN Table2 ON [Table1].[col1]=[Table2].[col1];")); - expectedQueries.put( - "UpdateQuery",multiline( - "PARAMETERS User Name Text;", - "UPDATE Table1", - "SET Table1.col1 = \"foo\", Table1.col2 = [Table2].[col3], [[Table2]].[[col1]] = [User Name]", - "WHERE ((([Table2].[col1]) Is Not Null));")); - expectedQueries.put( - "MakeTableQuery",multiline( - "SELECT Max(Table2.col1) AS MaxOfcol1, Table2.col2, Table3.col2 INTO Table4", - "FROM (Table2 INNER JOIN Table1 ON Table2.col1 = Table1.col2) RIGHT JOIN Table3 ON Table1.col2 = Table3.col3", - "GROUP BY Table2.col2, Table3.col2", - "HAVING (((Max(Table2.col1))=\"buzz\") AND ((Table2.col2)<>\"blah\"));")); - expectedQueries.put( - "CrosstabQuery", multiline( - "TRANSFORM Count([Table2].[col2]) AS CountOfcol2", - "SELECT Table2_1.col1, [Table2].[col3], Avg(Table2_1.col2) AS AvgOfcol2", - "FROM (Table1 INNER JOIN Table2 ON [Table1].[col1]=[Table2].[col1]) INNER JOIN Table2 AS Table2_1 ON [Table2].[col1]=Table2_1.col3", - "WHERE ((([Table1].[col1])>\"10\") And ((Table2_1.col1) Is Not Null) And ((Avg(Table2_1.col2))>\"10\"))", - "GROUP BY Table2_1.col1, [Table2].[col3]", - "ORDER BY [Table2].[col3]", - "PIVOT [Table1].[col1];")); - expectedQueries.put( - "UnionQuery", multiline( - "Select Table1.col1, Table1.col2", - "where Table1.col1 = \"foo\"", - "UNION", - "Select Table2.col1, Table2.col2", - "UNION ALL Select Table3.col1, Table3.col2", - "where Table3.col3 > \"blah\";")); - expectedQueries.put( - "PassthroughQuery", multiline( - "ALTER TABLE Table4 DROP COLUMN col5;\0")); - expectedQueries.put( - "DataDefinitionQuery", multiline( - "CREATE TABLE Table5 (col1 CHAR, col2 CHAR);\0")); - - Database db = DatabaseTest.open(new File("test/data/queryTest.mdb")); - - for(Query q : db.getQueries()) { - assertEquals(expectedQueries.remove(q.getName()), q.toSQLString()); - } + for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.QUERY)) { + Map<String,String> expectedQueries = new HashMap<String,String>(); + expectedQueries.put( + "SelectQuery", multiline( + "SELECT DISTINCT Table1.*, Table2.col1, Table2.col2, Table3.col3", + "FROM (Table1 LEFT JOIN Table3 ON Table1.col1 = Table3.col1) INNER JOIN Table2 ON (Table3.col1 = Table2.col1) AND (Table3.col1 = Table2.col2)", + "WHERE (((Table2.col2)=\"foo\" Or (Table2.col2) In (\"buzz\",\"bazz\")))", + "ORDER BY Table2.col1;")); + expectedQueries.put( + "DeleteQuery", multiline( + "DELETE Table1.col1, Table1.col2, Table1.col3", + "FROM Table1", + "WHERE (((Table1.col1)>\"blah\"));")); + expectedQueries.put( + "AppendQuery",multiline( + "INSERT INTO Table3", + "SELECT [Table1].[col2], [Table2].[col2], [Table2].[col3]", + "FROM Table3, Table1 INNER JOIN Table2 ON [Table1].[col1]=[Table2].[col1];")); + expectedQueries.put( + "UpdateQuery",multiline( + "PARAMETERS User Name Text;", + "UPDATE Table1", + "SET Table1.col1 = \"foo\", Table1.col2 = [Table2].[col3], [[Table2]].[[col1]] = [User Name]", + "WHERE ((([Table2].[col1]) Is Not Null));")); + expectedQueries.put( + "MakeTableQuery",multiline( + "SELECT Max(Table2.col1) AS MaxOfcol1, Table2.col2, Table3.col2 INTO Table4", + "FROM (Table2 INNER JOIN Table1 ON Table2.col1 = Table1.col2) RIGHT JOIN Table3 ON Table1.col2 = Table3.col3", + "GROUP BY Table2.col2, Table3.col2", + "HAVING (((Max(Table2.col1))=\"buzz\") AND ((Table2.col2)<>\"blah\"));")); + expectedQueries.put( + "CrosstabQuery", multiline( + "TRANSFORM Count([Table2].[col2]) AS CountOfcol2", + "SELECT Table2_1.col1, [Table2].[col3], Avg(Table2_1.col2) AS AvgOfcol2", + "FROM (Table1 INNER JOIN Table2 ON [Table1].[col1]=[Table2].[col1]) INNER JOIN Table2 AS Table2_1 ON [Table2].[col1]=Table2_1.col3", + "WHERE ((([Table1].[col1])>\"10\") And ((Table2_1.col1) Is Not Null) And ((Avg(Table2_1.col2))>\"10\"))", + "GROUP BY Table2_1.col1, [Table2].[col3]", + "ORDER BY [Table2].[col3]", + "PIVOT [Table1].[col1];")); + expectedQueries.put( + "UnionQuery", multiline( + "Select Table1.col1, Table1.col2", + "where Table1.col1 = \"foo\"", + "UNION", + "Select Table2.col1, Table2.col2", + "UNION ALL Select Table3.col1, Table3.col2", + "where Table3.col3 > \"blah\";")); + expectedQueries.put( + "PassthroughQuery", multiline( + "ALTER TABLE Table4 DROP COLUMN col5;\0")); + expectedQueries.put( + "DataDefinitionQuery", multiline( + "CREATE TABLE Table5 (col1 CHAR, col2 CHAR);\0")); + + Database db = DatabaseTest.open(testDB); + + for(Query q : db.getQueries()) { + assertEquals(expectedQueries.remove(q.getName()), q.toSQLString()); + } - assertTrue(expectedQueries.isEmpty()); + assertTrue(expectedQueries.isEmpty()); - db.close(); + db.close(); + } } private void doTestColumns(SelectQuery query) throws Exception |