summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorJames Ahlborn <jtahlborn@yahoo.com>2008-03-11 00:49:42 +0000
committerJames Ahlborn <jtahlborn@yahoo.com>2008-03-11 00:49:42 +0000
commit5c1b068826d0e897b0ea3c4b77c2b8c08503f112 (patch)
tree02353204177fc2739df01bdfce3525d7f64948d4 /test
parentc7d0f58880d7abd80b8f572903f29636eb6c1181 (diff)
downloadjackcess-5c1b068826d0e897b0ea3c4b77c2b8c08503f112.tar.gz
jackcess-5c1b068826d0e897b0ea3c4b77c2b8c08503f112.zip
add unit tests (and fix some bugs) for ignoreNull and unique index handling
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@264 f203690c-595d-4dc9-a70b-905162fa7fd2
Diffstat (limited to 'test')
-rw-r--r--test/data/testIndexProperties.mdbbin0 -> 249856 bytes
-rw-r--r--test/src/java/com/healthmarketscience/jackcess/CursorTest.java7
-rw-r--r--test/src/java/com/healthmarketscience/jackcess/IndexCodesTest.java3
-rw-r--r--test/src/java/com/healthmarketscience/jackcess/IndexTest.java125
4 files changed, 115 insertions, 20 deletions
diff --git a/test/data/testIndexProperties.mdb b/test/data/testIndexProperties.mdb
new file mode 100644
index 0000000..bb1c158
--- /dev/null
+++ b/test/data/testIndexProperties.mdb
Binary files differ
diff --git a/test/src/java/com/healthmarketscience/jackcess/CursorTest.java b/test/src/java/com/healthmarketscience/jackcess/CursorTest.java
index 0aa9c70..531805a 100644
--- a/test/src/java/com/healthmarketscience/jackcess/CursorTest.java
+++ b/test/src/java/com/healthmarketscience/jackcess/CursorTest.java
@@ -105,12 +105,7 @@ public class CursorTest extends TestCase {
}
static Database createTestIndexTable() throws Exception {
- File srcFile = new File("test/data/indexCursorTest.mdb");
- File dbFile = File.createTempFile("databaseTest", ".mdb");
- dbFile.deleteOnExit();
- copyFile(srcFile, dbFile);
-
- Database db = Database.open(dbFile);
+ Database db = openCopy(new File("test/data/indexCursorTest.mdb"));
Table table = db.getTable("test");
diff --git a/test/src/java/com/healthmarketscience/jackcess/IndexCodesTest.java b/test/src/java/com/healthmarketscience/jackcess/IndexCodesTest.java
index 8e3ee2b..c057656 100644
--- a/test/src/java/com/healthmarketscience/jackcess/IndexCodesTest.java
+++ b/test/src/java/com/healthmarketscience/jackcess/IndexCodesTest.java
@@ -31,7 +31,6 @@ import java.io.File;
import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -376,7 +375,7 @@ public class IndexCodesTest extends TestCase {
return builder.toString();
}
- private static String entryToString(Cursor.Position curPos)
+ static String entryToString(Cursor.Position curPos)
throws Exception
{
Field eField = curPos.getClass().getDeclaredField("_entry");
diff --git a/test/src/java/com/healthmarketscience/jackcess/IndexTest.java b/test/src/java/com/healthmarketscience/jackcess/IndexTest.java
index 917fc81..bb3b7f1 100644
--- a/test/src/java/com/healthmarketscience/jackcess/IndexTest.java
+++ b/test/src/java/com/healthmarketscience/jackcess/IndexTest.java
@@ -28,6 +28,7 @@ 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;
import java.util.HashMap;
@@ -149,12 +150,7 @@ public class IndexTest extends TestCase {
db.close();
// copy to temp file and attemp to edit
- File testFile = File.createTempFile("databaseTest", ".mdb");
- testFile.deleteOnExit();
-
- copyFile(origFile, testFile);
-
- db = Database.open(testFile);
+ db = openCopy(origFile);
t = db.getTable("Table1");
try {
@@ -167,12 +163,7 @@ public class IndexTest extends TestCase {
}
public void testEntryDeletion() throws Exception {
- File srcFile = new File("test/data/test.mdb");
- File dbFile = File.createTempFile("databaseTest", ".mdb");
- dbFile.deleteOnExit();
- copyFile(srcFile, dbFile);
-
- Table table = Database.open(dbFile).getTable("Table1");
+ Table table = openCopy(new File("test/data/test.mdb")).getTable("Table1");
for(int i = 0; i < 10; ++i) {
table.addRow("foo" + i, "bar" + i, (byte)42 + i, (short)53 + i, 13 * i,
@@ -207,6 +198,116 @@ public class IndexTest extends TestCase {
}
}
+ public void testIgnoreNulls() throws Exception
+ {
+ Database db = openCopy(new File("test/data/testIndexProperties.mdb"));
+
+ doTestIgnoreNulls(db, "TableIgnoreNulls1");
+ doTestIgnoreNulls(db, "TableIgnoreNulls2");
+
+ db.close();
+ }
+
+ private void doTestIgnoreNulls(Database db, String tableName)
+ throws Exception
+ {
+ Table orig = db.getTable(tableName);
+ Index origI = orig.getIndex("DataIndex");
+ Table temp = db.getTable(tableName + "_temp");
+ Index tempI = temp.getIndex("DataIndex");
+
+ // copy from orig table to temp table
+ for(Map<String,Object> row : orig) {
+ temp.addRow(orig.asRow(row));
+ }
+
+ assertEquals(origI.getEntryCount(), tempI.getEntryCount());
+
+ Cursor origC = Cursor.createIndexCursor(orig, origI);
+ Cursor tempC = Cursor.createIndexCursor(temp, tempI);
+
+ while(true) {
+ boolean origHasNext = origC.moveToNextRow();
+ boolean tempHasNext = tempC.moveToNextRow();
+ assertTrue(origHasNext == tempHasNext);
+ if(!origHasNext) {
+ break;
+ }
+
+ Map<String,Object> origRow = origC.getCurrentRow();
+ Cursor.Position origCurPos = origC.getSavepoint().getCurrentPosition();
+ Map<String,Object> tempRow = tempC.getCurrentRow();
+ Cursor.Position tempCurPos = tempC.getSavepoint().getCurrentPosition();
+
+ assertEquals(origRow, tempRow);
+ assertEquals(IndexCodesTest.entryToString(origCurPos),
+ IndexCodesTest.entryToString(tempCurPos));
+ }
+ }
+
+ 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();
+ }
+
+ private void doTestUnique(Table t, Index index, int numValues,
+ Object... testData)
+ throws Exception
+ {
+ for(int i = 0; i < testData.length; i += (numValues + 1)) {
+ Object[] row = new Object[numValues + 1];
+ row[0] = "testRow" + i;
+ for(int j = 1; j < (numValues + 1); ++j) {
+ row[j] = testData[i + j - 1];
+ }
+ boolean expectedSuccess = (Boolean)testData[i + numValues];
+
+ IOException failure = null;
+ try {
+ index.addRow(row, new RowId(400 + i, 0));
+ } catch(IOException e) {
+ failure = e;
+ }
+ if(expectedSuccess) {
+ assertNull(failure);
+ } else {
+ assertTrue(failure != null);
+ assertTrue(failure.getMessage().contains("uniqueness"));
+ }
+ }
+ }
+
private void checkIndexColumns(Table table, String... idxInfo)
throws Exception
{