summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorJames Ahlborn <jtahlborn@yahoo.com>2007-12-02 04:29:59 +0000
committerJames Ahlborn <jtahlborn@yahoo.com>2007-12-02 04:29:59 +0000
commitbca9319ef7c3fba0ee234d2ba8152de6fe0d1329 (patch)
tree2a8b859f6a325b1febeeb8e09367875f08d0d202 /test
parent5cd19292eb5178343233bd6e5ef2ce8367b443ff (diff)
downloadjackcess-bca9319ef7c3fba0ee234d2ba8152de6fe0d1329.tar.gz
jackcess-bca9319ef7c3fba0ee234d2ba8152de6fe0d1329.zip
implement range-based, index cursors
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@194 f203690c-595d-4dc9-a70b-905162fa7fd2
Diffstat (limited to 'test')
-rw-r--r--test/src/java/com/healthmarketscience/jackcess/CursorTest.java241
-rw-r--r--test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java10
2 files changed, 203 insertions, 48 deletions
diff --git a/test/src/java/com/healthmarketscience/jackcess/CursorTest.java b/test/src/java/com/healthmarketscience/jackcess/CursorTest.java
index f6b829d..6b67892 100644
--- a/test/src/java/com/healthmarketscience/jackcess/CursorTest.java
+++ b/test/src/java/com/healthmarketscience/jackcess/CursorTest.java
@@ -33,6 +33,17 @@ public class CursorTest extends TestCase {
}
return expectedRows;
}
+
+ private static List<Map<String,Object>> createTestTableData(
+ int startIdx,
+ int endIdx)
+ throws Exception
+ {
+ List<Map<String,Object>> expectedRows = createTestTableData();
+ expectedRows.subList(endIdx, expectedRows.size()).clear();
+ expectedRows.subList(0, startIdx).clear();
+ return expectedRows;
+ }
private static Database createTestTable() throws Exception {
Database db = create();
@@ -85,6 +96,17 @@ public class CursorTest extends TestCase {
return db;
}
+ private static Cursor createIndexSubRangeCursor(Table table,
+ Index idx,
+ int type)
+ throws Exception
+ {
+ return Cursor.createIndexCursor(
+ table, idx,
+ idx.constructIndexRow(3 - type), (type == 0),
+ idx.constructIndexRow(8 + type), (type == 0));
+ }
+
public void testRowId() throws Exception {
// test special cases
RowId rowId1 = new RowId(1, 2);
@@ -105,12 +127,17 @@ public class CursorTest extends TestCase {
Table table = db.getTable("test");
Cursor cursor = Cursor.createCursor(table);
- doTestSimple(table, cursor);
+ doTestSimple(table, cursor, null);
db.close();
}
- private void doTestSimple(Table table, Cursor cursor) throws Exception {
- List<Map<String,Object>> expectedRows = createTestTableData();
+ private void doTestSimple(Table table, Cursor cursor,
+ List<Map<String,Object>> expectedRows)
+ throws Exception
+ {
+ if(expectedRows == null) {
+ expectedRows = createTestTableData();
+ }
List<Map<String, Object>> foundRows =
new ArrayList<Map<String, Object>>();
@@ -125,13 +152,18 @@ public class CursorTest extends TestCase {
Table table = db.getTable("test");
Cursor cursor = Cursor.createCursor(table);
- doTestMove(table, cursor);
+ doTestMove(table, cursor, null);
db.close();
}
- private void doTestMove(Table table, Cursor cursor) throws Exception {
- List<Map<String,Object>> expectedRows = createTestTableData();
+ private void doTestMove(Table table, Cursor cursor,
+ List<Map<String,Object>> expectedRows)
+ throws Exception
+ {
+ if(expectedRows == null) {
+ expectedRows = createTestTableData();
+ }
expectedRows.subList(1, 4).clear();
List<Map<String, Object>> foundRows =
@@ -177,12 +209,13 @@ public class CursorTest extends TestCase {
Table table = db.getTable("test");
Cursor cursor = Cursor.createCursor(table);
- doTestSearch(table, cursor, null);
+ doTestSearch(table, cursor, null, 42, -13);
db.close();
}
- private void doTestSearch(Table table, Cursor cursor, Index index)
+ private void doTestSearch(Table table, Cursor cursor, Index index,
+ Integer... outOfRangeValues)
throws Exception
{
assertTrue(cursor.findRow(table.getColumn("id"), 3));
@@ -211,28 +244,38 @@ public class CursorTest extends TestCase {
"value", "data" + 7),
cursor.getCurrentRow());
- assertTrue(cursor.findRow(table.getColumn("value"), "data" + 2));
- assertEquals(createExpectedRow("id", 2,
- "value", "data" + 2),
+ assertTrue(cursor.findRow(table.getColumn("value"), "data" + 4));
+ assertEquals(createExpectedRow("id", 4,
+ "value", "data" + 4),
cursor.getCurrentRow());
+
+ for(Integer outOfRangeValue : outOfRangeValues) {
+ assertFalse(cursor.findRow(table.getColumn("id"),
+ outOfRangeValue));
+ assertFalse(cursor.findRow(table.getColumn("value"),
+ "data" + outOfRangeValue));
+ assertFalse(cursor.findRow(createExpectedRow(
+ "id", outOfRangeValue,
+ "value", "data" + outOfRangeValue)));
+ }
- assertEquals("data" + 9,
+ assertEquals("data" + 5,
Cursor.findValue(table,
table.getColumn("value"),
- table.getColumn("id"), 9));
- assertEquals(createExpectedRow("id", 9,
- "value", "data" + 9),
+ table.getColumn("id"), 5));
+ assertEquals(createExpectedRow("id", 5,
+ "value", "data" + 5),
Cursor.findRow(table,
- createExpectedRow("id", 9)));
+ createExpectedRow("id", 5)));
if(index != null) {
- assertEquals("data" + 9,
+ assertEquals("data" + 5,
Cursor.findValue(table, index,
table.getColumn("value"),
- table.getColumn("id"), 9));
- assertEquals(createExpectedRow("id", 9,
- "value", "data" + 9),
+ table.getColumn("id"), 5));
+ assertEquals(createExpectedRow("id", 5,
+ "value", "data" + 5),
Cursor.findRow(table, index,
- createExpectedRow("id", 9)));
+ createExpectedRow("id", 5)));
}
}
@@ -241,13 +284,18 @@ public class CursorTest extends TestCase {
Table table = db.getTable("test");
Cursor cursor = Cursor.createCursor(table);
- doTestReverse(table, cursor);
+ doTestReverse(table, cursor, null);
db.close();
}
- private void doTestReverse(Table table, Cursor cursor) throws Exception {
- List<Map<String,Object>> expectedRows = createTestTableData();
+ private void doTestReverse(Table table, Cursor cursor,
+ List<Map<String,Object>> expectedRows)
+ throws Exception
+ {
+ if(expectedRows == null) {
+ expectedRows = createTestTableData();
+ }
Collections.reverse(expectedRows);
List<Map<String, Object>> foundRows =
@@ -265,14 +313,15 @@ public class CursorTest extends TestCase {
Cursor cursor1 = Cursor.createCursor(table);
Cursor cursor2 = Cursor.createCursor(table);
- doTestLiveAddition(table, cursor1, cursor2);
+ doTestLiveAddition(table, cursor1, cursor2, 11);
db.close();
}
private void doTestLiveAddition(Table table,
Cursor cursor1,
- Cursor cursor2) throws Exception
+ Cursor cursor2,
+ Integer newRowNum) throws Exception
{
cursor1.moveNextRows(11);
cursor2.moveNextRows(11);
@@ -280,7 +329,6 @@ public class CursorTest extends TestCase {
assertTrue(cursor1.isAfterLast());
assertTrue(cursor2.isAfterLast());
- int newRowNum = 11;
table.addRow(newRowNum, "data" + newRowNum);
Map<String,Object> expectedRow =
createExpectedRow("id", newRowNum, "value", "data" + newRowNum);
@@ -306,7 +354,7 @@ public class CursorTest extends TestCase {
Cursor cursor2 = Cursor.createCursor(table);
Cursor cursor3 = Cursor.createCursor(table);
Cursor cursor4 = Cursor.createCursor(table);
- doTestLiveDeletion(table, cursor1, cursor2, cursor3, cursor4);
+ doTestLiveDeletion(table, cursor1, cursor2, cursor3, cursor4, 1);
db.close();
}
@@ -315,20 +363,23 @@ public class CursorTest extends TestCase {
Cursor cursor1,
Cursor cursor2,
Cursor cursor3,
- Cursor cursor4) throws Exception
+ Cursor cursor4,
+ int firstValue) throws Exception
{
- cursor1.moveNextRows(2);
- cursor2.moveNextRows(3);
- cursor3.moveNextRows(3);
- cursor4.moveNextRows(4);
+ assertEquals(2, cursor1.moveNextRows(2));
+ assertEquals(3, cursor2.moveNextRows(3));
+ assertEquals(3, cursor3.moveNextRows(3));
+ assertEquals(4, cursor4.moveNextRows(4));
Map<String,Object> expectedPrevRow =
- createExpectedRow("id", 1, "value", "data" + 1);
+ createExpectedRow("id", firstValue, "value", "data" + firstValue);
+ ++firstValue;
Map<String,Object> expectedDeletedRow =
- createExpectedRow("id", 2, "value", "data" + 2);
+ createExpectedRow("id", firstValue, "value", "data" + firstValue);
+ ++firstValue;
Map<String,Object> expectedNextRow =
- createExpectedRow("id", 3, "value", "data" + 3);
-
+ createExpectedRow("id", firstValue, "value", "data" + firstValue);
+
assertEquals(expectedDeletedRow, cursor2.getCurrentRow());
assertEquals(expectedDeletedRow, cursor3.getCurrentRow());
@@ -356,7 +407,7 @@ public class CursorTest extends TestCase {
assertTable(createUnorderedTestTableData(), table);
Cursor cursor = Cursor.createIndexCursor(table, idx);
- doTestSimple(table, cursor);
+ doTestSimple(table, cursor, null);
db.close();
}
@@ -367,7 +418,7 @@ public class CursorTest extends TestCase {
Table table = db.getTable("test");
Index idx = table.getIndexes().get(0);
Cursor cursor = Cursor.createIndexCursor(table, idx);
- doTestMove(table, cursor);
+ doTestMove(table, cursor, null);
db.close();
}
@@ -378,7 +429,7 @@ public class CursorTest extends TestCase {
Table table = db.getTable("test");
Index idx = table.getIndexes().get(0);
Cursor cursor = Cursor.createIndexCursor(table, idx);
- doTestReverse(table, cursor);
+ doTestReverse(table, cursor, null);
db.close();
}
@@ -389,7 +440,7 @@ public class CursorTest extends TestCase {
Table table = db.getTable("test");
Index idx = table.getIndexes().get(0);
Cursor cursor = Cursor.createIndexCursor(table, idx);
- doTestSearch(table, cursor, idx);
+ doTestSearch(table, cursor, idx, 42, -13);
db.close();
}
@@ -402,7 +453,7 @@ public class CursorTest extends TestCase {
Cursor cursor1 = Cursor.createIndexCursor(table, idx);
Cursor cursor2 = Cursor.createIndexCursor(table, idx);
- doTestLiveAddition(table, cursor1, cursor2);
+ doTestLiveAddition(table, cursor1, cursor2, 11);
db.close();
}
@@ -417,9 +468,113 @@ public class CursorTest extends TestCase {
Cursor cursor2 = Cursor.createIndexCursor(table, idx);
Cursor cursor3 = Cursor.createIndexCursor(table, idx);
Cursor cursor4 = Cursor.createIndexCursor(table, idx);
- doTestLiveDeletion(table, cursor1, cursor2, cursor3, cursor4);
+ doTestLiveDeletion(table, cursor1, cursor2, cursor3, cursor4, 1);
db.close();
}
+
+ public void testSimpleIndexSubRange() throws Exception {
+ for(int i = 0; i < 2; ++i) {
+ Database db = createTestIndexTable();
+
+ Table table = db.getTable("test");
+ Index idx = table.getIndexes().get(0);
+
+ Cursor cursor = createIndexSubRangeCursor(table, idx, i);
+
+ List<Map<String,Object>> expectedRows =
+ createTestTableData(3, 9);
+
+ doTestSimple(table, cursor, expectedRows);
+
+ db.close();
+ }
+ }
+
+ public void testMoveIndexSubRange() throws Exception {
+ for(int i = 0; i < 2; ++i) {
+ Database db = createTestIndexTable();
+
+ Table table = db.getTable("test");
+ Index idx = table.getIndexes().get(0);
+
+ Cursor cursor = createIndexSubRangeCursor(table, idx, i);
+
+ List<Map<String,Object>> expectedRows =
+ createTestTableData(3, 9);
+
+ doTestMove(table, cursor, expectedRows);
+
+ db.close();
+ }
+ }
+
+ public void testSearchIndexSubRange() throws Exception {
+ for(int i = 0; i < 2; ++i) {
+ Database db = createTestIndexTable();
+
+ Table table = db.getTable("test");
+ Index idx = table.getIndexes().get(0);
+
+ Cursor cursor = createIndexSubRangeCursor(table, idx, i);
+
+ doTestSearch(table, cursor, idx, 2, 9);
+
+ db.close();
+ }
+ }
+
+ public void testReverseIndexSubRange() throws Exception {
+ for(int i = 0; i < 2; ++i) {
+ Database db = createTestIndexTable();
+
+ Table table = db.getTable("test");
+ Index idx = table.getIndexes().get(0);
+
+ Cursor cursor = createIndexSubRangeCursor(table, idx, i);
+
+ List<Map<String,Object>> expectedRows =
+ createTestTableData(3, 9);
+
+ doTestReverse(table, cursor, expectedRows);
+
+ db.close();
+ }
+ }
+
+ public void testLiveAdditionIndexSubRange() throws Exception {
+ for(int i = 0; i < 2; ++i) {
+ Database db = createTestIndexTable();
+
+ Table table = db.getTable("test");
+ Index idx = table.getIndexes().get(0);
+
+ Cursor cursor1 = createIndexSubRangeCursor(table, idx, i);
+ Cursor cursor2 = createIndexSubRangeCursor(table, idx, i);
+
+ doTestLiveAddition(table, cursor1, cursor2, 8);
+
+ db.close();
+ }
+ }
+
+ public void testLiveDeletionIndexSubRange() throws Exception {
+ for(int i = 0; i < 2; ++i) {
+ Database db = createTestIndexTable();
+
+ 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);
+
+ doTestLiveDeletion(table, cursor1, cursor2, cursor3, cursor4, 4);
+
+ db.close();
+ }
+ }
+
}
diff --git a/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java b/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java
index a962c46..83930af 100644
--- a/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java
+++ b/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java
@@ -18,8 +18,8 @@ import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
-import java.util.HashMap;
import java.util.HashSet;
+import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -368,13 +368,13 @@ public class DatabaseTest extends TestCase {
public void testReadWithDeletedCols() throws Exception {
Table table = Database.open(new File("test/data/delColTest.mdb")).getTable("Table1");
- Map<String, Object> expectedRow0 = new HashMap<String, Object>();
+ 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 HashMap<String, Object>();
+ Map<String, Object> expectedRow1 = new LinkedHashMap<String, Object>();
expectedRow1.put("id", 3);
expectedRow1.put("id2", 5);
expectedRow1.put("data", "bar");
@@ -687,7 +687,7 @@ public class DatabaseTest extends TestCase {
Table t = db.getTable("test");
List<String> row = new ArrayList<String>();
- Map<String,Object> expectedRowData = new HashMap<String, Object>();
+ Map<String,Object> expectedRowData = new LinkedHashMap<String, Object>();
for(int i = 0; i < numColumns; ++i) {
String value = "" + i + " some row data";
row.add(value);
@@ -838,7 +838,7 @@ public class DatabaseTest extends TestCase {
}
static Map<String, Object> createExpectedRow(Object... rowElements) {
- Map<String, Object> row = new HashMap<String, Object>();
+ Map<String, Object> row = new LinkedHashMap<String, Object>();
for(int i = 0; i < rowElements.length; i += 2) {
row.put((String)rowElements[i],
rowElements[i + 1]);