1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.healthmarketscience.jackcess;
18
19 import junit.framework.TestCase;
20
21 import static com.healthmarketscience.jackcess.impl.JetFormatTest.*;
22 import com.healthmarketscience.jackcess.impl.IndexImpl;
23
24
25
26
27 public class CursorBuilderTest extends TestCase {
28
29 public CursorBuilderTest(String name) throws Exception {
30 super(name);
31 }
32
33 private static void assertCursor(
34 Cursor expected, Cursor found)
35 {
36 assertSame(expected.getTable(), found.getTable());
37 if(expected instanceof IndexCursor) {
38 assertSame(((IndexCursor)expected).getIndex(),
39 ((IndexCursor)found).getIndex());
40 }
41
42 assertEquals(expected.getSavepoint().getCurrentPosition(),
43 found.getSavepoint().getCurrentPosition());
44 }
45
46 public void test() throws Exception
47 {
48 for (final TestDB indexCursorDB : CursorTest.INDEX_CURSOR_DBS) {
49 Database db = CursorTest.createTestIndexTable(indexCursorDB);
50
51 Table table = db.getTable("test");
52 IndexImpl idx = (IndexImpl)table.getIndexes().get(0);
53
54 Cursor expected = CursorBuilder.createCursor(table);
55
56 Cursor found = table.newCursor().toCursor();
57 assertCursor(expected, found);
58
59 expected = CursorBuilder.createCursor(idx);
60 found = idx.newCursor().toCursor();
61 assertCursor(expected, found);
62
63 expected = CursorBuilder.createCursor(idx);
64 found = table.newCursor()
65 .setIndexByName("id")
66 .toCursor();
67 assertCursor(expected, found);
68
69 try {
70 table.newCursor()
71 .setIndexByName("foo");
72 fail("IllegalArgumentException should have been thrown");
73 } catch(IllegalArgumentException ignored) {
74
75 }
76
77 expected = CursorBuilder.createCursor(idx);
78 found = table.newCursor()
79 .setIndexByColumns(table.getColumn("id"))
80 .toCursor();
81 assertCursor(expected, found);
82
83 try {
84 table.newCursor()
85 .setIndexByColumns(table.getColumn("value"));
86 fail("IllegalArgumentException should have been thrown");
87 } catch(IllegalArgumentException ignored) {
88
89 }
90
91 try {
92 table.newCursor()
93 .setIndexByColumns(table.getColumn("id"), table.getColumn("value"));
94 fail("IllegalArgumentException should have been thrown");
95 } catch(IllegalArgumentException ignored) {
96
97 }
98
99 expected = CursorBuilder.createCursor(table);
100 expected.beforeFirst();
101 found = table.newCursor()
102 .beforeFirst()
103 .toCursor();
104 assertCursor(expected, found);
105
106 expected = CursorBuilder.createCursor(table);
107 expected.afterLast();
108 found = table.newCursor()
109 .afterLast()
110 .toCursor();
111 assertCursor(expected, found);
112
113 expected = CursorBuilder.createCursor(table);
114 expected.moveNextRows(2);
115 Cursor.Savepoint sp = expected.getSavepoint();
116 found = table.newCursor()
117 .afterLast()
118 .restoreSavepoint(sp)
119 .toCursor();
120 assertCursor(expected, found);
121
122 expected = CursorBuilder.createCursor(idx);
123 expected.moveNextRows(2);
124 sp = expected.getSavepoint();
125 found = idx.newCursor()
126 .beforeFirst()
127 .restoreSavepoint(sp)
128 .toCursor();
129 assertCursor(expected, found);
130
131 expected = CursorBuilder.createCursor(idx,
132 idx.constructIndexRowFromEntry(3),
133 null);
134 found = idx.newCursor()
135 .setStartEntry(3)
136 .toCursor();
137 assertCursor(expected, found);
138
139 expected = CursorBuilder.createCursor(idx,
140 idx.constructIndexRowFromEntry(3),
141 false,
142 idx.constructIndexRowFromEntry(7),
143 false);
144 found = idx.newCursor()
145 .setStartEntry(3)
146 .setStartRowInclusive(false)
147 .setEndEntry(7)
148 .setEndRowInclusive(false)
149 .toCursor();
150 assertCursor(expected, found);
151
152
153
154 db.close();
155 }
156 }
157
158 }