diff options
author | James Ahlborn <jtahlborn@yahoo.com> | 2007-11-20 21:03:11 +0000 |
---|---|---|
committer | James Ahlborn <jtahlborn@yahoo.com> | 2007-11-20 21:03:11 +0000 |
commit | e39f2d1d3d4d752a276daff7710a9d91a590930b (patch) | |
tree | 7326649b9b93101db2aed2eb4ee9e1e6d4d8497a /test/src/java | |
parent | b77f5f9e97ac1e27368c3b32e8cfa9a9d2f77014 (diff) | |
download | jackcess-e39f2d1d3d4d752a276daff7710a9d91a590930b.tar.gz jackcess-e39f2d1d3d4d752a276daff7710a9d91a590930b.zip |
Move table iteration out of Table and into Cursor. First stage in
offering more complicated table access.
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@178 f203690c-595d-4dc9-a70b-905162fa7fd2
Diffstat (limited to 'test/src/java')
-rw-r--r-- | test/src/java/com/healthmarketscience/jackcess/CursorTest.java | 118 | ||||
-rw-r--r-- | test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java | 6 |
2 files changed, 121 insertions, 3 deletions
diff --git a/test/src/java/com/healthmarketscience/jackcess/CursorTest.java b/test/src/java/com/healthmarketscience/jackcess/CursorTest.java new file mode 100644 index 0000000..9e6c187 --- /dev/null +++ b/test/src/java/com/healthmarketscience/jackcess/CursorTest.java @@ -0,0 +1,118 @@ +// Copyright (c) 2007 Health Market Science, Inc. + +package com.healthmarketscience.jackcess; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import junit.framework.TestCase; + +import static com.healthmarketscience.jackcess.DatabaseTest.*; + +/** + * @author James Ahlborn + */ +public class CursorTest extends TestCase { + + public CursorTest(String name) throws Exception { + super(name); + } + + private static List<Map<String,Object>> createTestTableData() + throws Exception + { + List<Map<String,Object>> expectedRows = + new ArrayList<Map<String,Object>>(); + for(int i = 0; i < 10; ++i) { + expectedRows.add(createExpectedRow("id", i, "value", "data" + i)); + } + return expectedRows; + } + + private static Database createTestTable() throws Exception { + Database db = create(); + + List<Column> columns = new ArrayList<Column>(); + Column col = new Column(); + col.setName("id"); + col.setType(DataType.LONG); + columns.add(col); + col = new Column(); + col.setName("value"); + col.setType(DataType.TEXT); + columns.add(col); + db.createTable("test", columns); + + Table table = db.getTable("test"); + for(int i = 0; i < 10; ++i) { + table.addRow(i, "data" + i); + } + + return db; + } + + public void testSimple() throws Exception { + Database db = createTestTable(); + + Table table = db.getTable("test"); + List<Map<String,Object>> expectedRows = createTestTableData(); + + Cursor cursor = Cursor.createCursor(table); + List<Map<String, Object>> foundRows = + new ArrayList<Map<String, Object>>(); + for(Map<String, Object> row : cursor) { + foundRows.add(row); + } + assertEquals(expectedRows, foundRows); + + db.close(); + } + + public void testSkip() throws Exception { + Database db = createTestTable(); + + Table table = db.getTable("test"); + List<Map<String,Object>> expectedRows = createTestTableData(); + expectedRows.subList(1, 4).clear(); + + Cursor cursor = Cursor.createCursor(table); + List<Map<String, Object>> foundRows = + new ArrayList<Map<String, Object>>(); + foundRows.add(cursor.getNextRow()); + assertEquals(3, cursor.skipRows(3)); + while(cursor.moveToNextRow()) { + foundRows.add(cursor.getCurrentRow()); + } + assertEquals(expectedRows, foundRows); + + assertEquals(0, cursor.skipRows(3)); + + db.close(); + } + + public void testSearch() throws Exception { + Database db = createTestTable(); + + Table table = db.getTable("test"); + List<Map<String,Object>> expectedRows = createTestTableData(); + + Cursor cursor = Cursor.createCursor(table); + + assertTrue(cursor.moveToRow(table.getColumn("id"), 3)); + assertEquals(createExpectedRow("id", 3, + "value", "data" + 3), + cursor.getCurrentRow()); + + assertTrue(cursor.moveToRow(createExpectedRow( + "id", 6, + "value", "data" + 6))); + assertEquals(createExpectedRow("id", 6, + "value", "data" + 6), + cursor.getCurrentRow()); + + db.close(); + } + + +} diff --git a/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java b/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java index 5235ef2..a962c46 100644 --- a/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java +++ b/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java @@ -821,7 +821,7 @@ public class DatabaseTest extends TestCase { static int countRows(Table table) throws Exception { int rtn = 0; - for(Map<String, Object> row : table) { + for(Map<String, Object> row : Cursor.createCursor(table)) { rtn++; } return rtn; @@ -831,7 +831,7 @@ public class DatabaseTest extends TestCase { { List<Map<String, Object>> foundTable = new ArrayList<Map<String, Object>>(); - for(Map<String, Object> row : table) { + for(Map<String, Object> row : Cursor.createCursor(table)) { foundTable.add(row); } assertEquals(expectedTable, foundTable); @@ -873,7 +873,7 @@ public class DatabaseTest extends TestCase { colNames.add(col.getName()); } writer.println("COLUMNS: " + colNames); - for(Map<String, Object> row : table) { + for(Map<String, Object> row : Cursor.createCursor(table)) { // make byte[] printable for(Map.Entry<String, Object> entry : row.entrySet()) { |