]> source.dussan.org Git - jackcess.git/commitdiff
test cursorbuilder
authorJames Ahlborn <jtahlborn@yahoo.com>
Wed, 5 Dec 2007 18:58:27 +0000 (18:58 +0000)
committerJames Ahlborn <jtahlborn@yahoo.com>
Wed, 5 Dec 2007 18:58:27 +0000 (18:58 +0000)
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@199 f203690c-595d-4dc9-a70b-905162fa7fd2

src/java/com/healthmarketscience/jackcess/Cursor.java
src/java/com/healthmarketscience/jackcess/CursorBuilder.java
src/java/com/healthmarketscience/jackcess/Table.java
test/src/java/com/healthmarketscience/jackcess/CursorBuilderTest.java [new file with mode: 0644]
test/src/java/com/healthmarketscience/jackcess/CursorTest.java

index 4578a413b2d0942f02ab515db0c06022117df45d..6ca0c84efe495f5245ae845e68d4ec2540625e02 100644 (file)
@@ -265,6 +265,10 @@ public abstract class Cursor implements Iterable<Map<String, Object>>
   public Table getTable() {
     return _table;
   }
+
+  public Index getIndex() {
+    return null;
+  }
   
   public JetFormat getFormat() {
     return getTable().getFormat();
@@ -1065,6 +1069,11 @@ public abstract class Cursor implements Iterable<Map<String, Object>>
       _entryCursor = entryCursor;
     }
 
+    @Override
+    public Index getIndex() {
+      return _entryCursor.getIndex();
+    }
+    
     @Override
     protected IndexDirHandler getDirHandler(boolean moveForward) {
       return (moveForward ? _forwardDirHandler : _reverseDirHandler);
index 224dc152a5480471750906aac4c018bb6cd90ea0..32e622018a3b1e9a794359c7976c758d1b0cc5ce 100644 (file)
@@ -105,19 +105,7 @@ public class CursorBuilder {
    *         with the given name
    */
   public CursorBuilder setIndexByName(String indexName) {
-    boolean found = false;
-    for(Index index : _table.getIndexes()) {
-      if(ObjectUtils.equals(indexName, index.getName())) {
-        _index = index;
-        found = true;
-        break;
-      }
-    }
-    if(!found) {
-      throw new IllegalArgumentException("Index with name " + indexName +
-                                         " does not exist in table " + _table);
-    }
-    return this;
+    return setIndex(_table.getIndex(indexName));
   }
 
   /**
index d83c4b81d3c68fef000bb218cdf017c224866683..cfc7fcff57bec5cbf5290cc0e283b2cc7c5da98a 100644 (file)
@@ -248,6 +248,19 @@ public class Table
     return Collections.unmodifiableList(_indexes);
   }
 
+  /**
+   * @return the index with the given name
+   */
+  public Index getIndex(String name) {
+    for(Index index : _indexes) {
+      if(index.getName().equals(name)) {
+        return index;
+      }
+    }
+    throw new IllegalArgumentException("Index with name " + name +
+                                       " does not exist on this table");
+  }
+    
   /**
    * Only called by unit tests
    */
diff --git a/test/src/java/com/healthmarketscience/jackcess/CursorBuilderTest.java b/test/src/java/com/healthmarketscience/jackcess/CursorBuilderTest.java
new file mode 100644 (file)
index 0000000..2788f65
--- /dev/null
@@ -0,0 +1,166 @@
+/*
+Copyright (c) 2007 Health Market Science, Inc.
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Lesser General Public
+License as published by the Free Software Foundation; either
+version 2.1 of the License, or (at your option) any later version.
+
+This library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public
+License along with this library; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
+USA
+
+You can contact Health Market Science at info@healthmarketscience.com
+or at the following address:
+
+Health Market Science
+2700 Horizon Drive
+Suite 200
+King of Prussia, PA 19406
+*/
+
+package com.healthmarketscience.jackcess;
+
+import junit.framework.TestCase;
+
+/**
+ * @author James Ahlborn
+ */
+public class CursorBuilderTest extends TestCase {
+
+  public CursorBuilderTest(String name) throws Exception {
+    super(name);
+  }
+
+  private static void assertCursor(
+      Cursor expected, Cursor found)
+  {
+    assertSame(expected.getTable(), found.getTable());
+    assertSame(expected.getIndex(), found.getIndex());
+
+    assertEquals(expected.getSavepoint().getCurrentPosition(),
+                 found.getSavepoint().getCurrentPosition());
+  }
+  
+  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
+    }
+    
+    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();
+  }
+  
+}
index 87210ff5109c37a8638751630823583709193c1c..0aa9c707adb2b1ff5bac7a23e42de5a4b2164856 100644 (file)
@@ -104,7 +104,7 @@ public class CursorTest extends TestCase {
     return expectedRows;
   }  
   
-  private static Database createTestIndexTable() throws Exception {
+  static Database createTestIndexTable() throws Exception {
     File srcFile = new File("test/data/indexCursorTest.mdb");
     File dbFile = File.createTempFile("databaseTest", ".mdb");
     dbFile.deleteOnExit();