1   /*
2   Copyright (c) 2007 Health Market Science, Inc.
3   
4   This library is free software; you can redistribute it and/or
5   modify it under the terms of the GNU Lesser General Public
6   License as published by the Free Software Foundation; either
7   version 2.1 of the License, or (at your option) any later version.
8   
9   This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  Lesser General Public License for more details.
13  
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
17  USA
18  
19  You can contact Health Market Science at info@healthmarketscience.com
20  or at the following address:
21  
22  Health Market Science
23  2700 Horizon Drive
24  Suite 200
25  King of Prussia, PA 19406
26  */
27  
28  package com.healthmarketscience.jackcess;
29  
30  import junit.framework.TestCase;
31  
32  /**
33   * @author James Ahlborn
34   */
35  public class CursorBuilderTest extends TestCase {
36  
37    public CursorBuilderTest(String name) throws Exception {
38      super(name);
39    }
40  
41    private static void assertCursor(
42        Cursor expected, Cursor found)
43    {
44      assertSame(expected.getTable(), found.getTable());
45      assertSame(expected.getIndex(), found.getIndex());
46  
47      assertEquals(expected.getSavepoint().getCurrentPosition(),
48                   found.getSavepoint().getCurrentPosition());
49    }
50    
51    public void test() throws Exception
52    {
53      Database db = CursorTest.createTestIndexTable();
54  
55      Table table = db.getTable("test");
56      Index idx = table.getIndexes().get(0);
57  
58      Cursor expected = Cursor.createCursor(table);
59      
60      Cursor found = new CursorBuilder(table).toCursor();
61      assertCursor(expected, found);
62  
63      expected = Cursor.createIndexCursor(table, idx);
64      found = new CursorBuilder(table)
65        .setIndex(idx)
66        .toCursor();
67      assertCursor(expected, found);
68  
69      expected = Cursor.createIndexCursor(table, idx);
70      found = new CursorBuilder(table)
71        .setIndexByName("id")
72        .toCursor();
73      assertCursor(expected, found);
74  
75      try {
76        new CursorBuilder(table)
77          .setIndexByName("foo");
78        fail("IllegalArgumentException should have been thrown");
79      } catch(IllegalArgumentException ignored) {
80        // success
81      }
82      
83      expected = Cursor.createIndexCursor(table, idx);
84      found = new CursorBuilder(table)
85        .setIndexByColumns(table.getColumn("id"))
86        .toCursor();
87      assertCursor(expected, found);
88  
89      try {
90        new CursorBuilder(table)
91          .setIndexByColumns(table.getColumn("value"));
92        fail("IllegalArgumentException should have been thrown");
93      } catch(IllegalArgumentException ignored) {
94        // success
95      }
96      
97      try {
98        new CursorBuilder(table)
99          .setIndexByColumns(table.getColumn("id"), table.getColumn("value"));
100       fail("IllegalArgumentException should have been thrown");
101     } catch(IllegalArgumentException ignored) {
102       // success
103     }
104     
105     expected = Cursor.createCursor(table);
106     expected.beforeFirst();
107     found = new CursorBuilder(table)
108       .beforeFirst()
109       .toCursor();
110     assertCursor(expected, found);
111 
112     expected = Cursor.createCursor(table);
113     expected.afterLast();
114     found = new CursorBuilder(table)
115       .afterLast()
116       .toCursor();
117     assertCursor(expected, found);
118 
119     expected = Cursor.createCursor(table);
120     expected.moveNextRows(2);
121     Cursor.Savepoint sp = expected.getSavepoint();
122     found = new CursorBuilder(table)
123       .afterLast()
124       .restoreSavepoint(sp)
125       .toCursor();
126     assertCursor(expected, found);
127 
128     expected = Cursor.createIndexCursor(table, idx);
129     expected.moveNextRows(2);
130     sp = expected.getSavepoint();
131     found = new CursorBuilder(table)
132       .setIndex(idx)
133       .beforeFirst()
134       .restoreSavepoint(sp)
135       .toCursor();
136     assertCursor(expected, found);
137 
138     expected = Cursor.createIndexCursor(table, idx,
139                                         idx.constructIndexRowFromEntry(3),
140                                         null);
141     found = new CursorBuilder(table)
142       .setIndex(idx)
143       .setStartEntry(3)
144       .toCursor();
145     assertCursor(expected, found);
146 
147     expected = Cursor.createIndexCursor(table, idx,
148                                         idx.constructIndexRowFromEntry(3),
149                                         false,
150                                         idx.constructIndexRowFromEntry(7),
151                                         false);
152     found = new CursorBuilder(table)
153       .setIndex(idx)
154       .setStartEntry(3)
155       .setStartRowInclusive(false)
156       .setEndEntry(7)
157       .setEndRowInclusive(false)
158       .toCursor();
159     assertCursor(expected, found);
160 
161 
162     
163     db.close();
164   }
165   
166 }