View Javadoc
1   /*
2   Copyright (c) 2007 Health Market Science, Inc.
3   
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7   
8       http://www.apache.org/licenses/LICENSE-2.0
9   
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
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   * @author James Ahlborn
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 = new CursorBuilder(table).toCursor();
57        assertCursor(expected, found);
58  
59        expected = CursorBuilder.createCursor(idx);
60        found = new CursorBuilder(table)
61          .setIndex(idx)
62          .toCursor();
63        assertCursor(expected, found);
64  
65        expected = CursorBuilder.createCursor(idx);
66        found = new CursorBuilder(table)
67          .setIndexByName("id")
68          .toCursor();
69        assertCursor(expected, found);
70  
71        try {
72          new CursorBuilder(table)
73            .setIndexByName("foo");
74          fail("IllegalArgumentException should have been thrown");
75        } catch(IllegalArgumentException ignored) {
76          // success
77        }
78  
79        expected = CursorBuilder.createCursor(idx);
80        found = new CursorBuilder(table)
81          .setIndexByColumns(table.getColumn("id"))
82          .toCursor();
83        assertCursor(expected, found);
84  
85        try {
86          new CursorBuilder(table)
87            .setIndexByColumns(table.getColumn("value"));
88          fail("IllegalArgumentException should have been thrown");
89        } catch(IllegalArgumentException ignored) {
90          // success
91        }
92  
93        try {
94          new CursorBuilder(table)
95            .setIndexByColumns(table.getColumn("id"), table.getColumn("value"));
96          fail("IllegalArgumentException should have been thrown");
97        } catch(IllegalArgumentException ignored) {
98          // success
99        }
100 
101       expected = CursorBuilder.createCursor(table);
102       expected.beforeFirst();
103       found = new CursorBuilder(table)
104         .beforeFirst()
105         .toCursor();
106       assertCursor(expected, found);
107 
108       expected = CursorBuilder.createCursor(table);
109       expected.afterLast();
110       found = new CursorBuilder(table)
111         .afterLast()
112         .toCursor();
113       assertCursor(expected, found);
114 
115       expected = CursorBuilder.createCursor(table);
116       expected.moveNextRows(2);
117       Cursor.Savepoint sp = expected.getSavepoint();
118       found = new CursorBuilder(table)
119         .afterLast()
120         .restoreSavepoint(sp)
121         .toCursor();
122       assertCursor(expected, found);
123 
124       expected = CursorBuilder.createCursor(idx);
125       expected.moveNextRows(2);
126       sp = expected.getSavepoint();
127       found = new CursorBuilder(table)
128         .setIndex(idx)
129         .beforeFirst()
130         .restoreSavepoint(sp)
131         .toCursor();
132       assertCursor(expected, found);
133 
134       expected = CursorBuilder.createCursor(idx,
135                                           idx.constructIndexRowFromEntry(3),
136                                           null);
137       found = new CursorBuilder(table)
138         .setIndex(idx)
139         .setStartEntry(3)
140         .toCursor();
141       assertCursor(expected, found);
142 
143       expected = CursorBuilder.createCursor(idx,
144                                           idx.constructIndexRowFromEntry(3),
145                                           false,
146                                           idx.constructIndexRowFromEntry(7),
147                                           false);
148       found = new CursorBuilder(table)
149         .setIndex(idx)
150         .setStartEntry(3)
151         .setStartRowInclusive(false)
152         .setEndEntry(7)
153         .setEndRowInclusive(false)
154         .toCursor();
155       assertCursor(expected, found);
156 
157 
158 
159       db.close();
160     }
161   }
162   
163 }