KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > odmg > ScrollableQueryResultsTest


1 package org.apache.ojb.odmg;
2
3 import java.sql.Connection JavaDoc;
4 import java.sql.ResultSet JavaDoc;
5 import java.util.Collection JavaDoc;
6 import java.util.Iterator JavaDoc;
7
8 import org.apache.ojb.broker.PersistenceBroker;
9 import org.apache.ojb.broker.query.Query;
10 import org.apache.ojb.junit.ODMGTestCase;
11 import org.apache.ojb.odmg.oql.EnhancedOQLQuery;
12 import org.apache.ojb.odmg.shared.Person;
13 import org.apache.ojb.odmg.shared.PersonImpl;
14 import org.odmg.OQLQuery;
15 import org.odmg.QueryInvalidException;
16 import org.odmg.Transaction;
17
18 /**
19  * @author <a HREF="mailto:mattbaird@yahoo.com">Matthew Baird</a>
20  * @version $Id: ScrollableQueryResultsTest.java,v 1.18.4.1 2005/06/04 14:48:05 arminw Exp $
21  */

22 public class ScrollableQueryResultsTest extends ODMGTestCase
23 {
24     private static final int CONTROL_SIZE = 50;
25
26     public static void main(String JavaDoc[] args)
27     {
28         String JavaDoc[] arr = {ScrollableQueryResultsTest.class.getName()};
29         junit.textui.TestRunner.main(arr);
30     }
31
32     public ScrollableQueryResultsTest(String JavaDoc name)
33     {
34         super(name);
35     }
36
37     private void createData() throws Exception JavaDoc
38     {
39         Transaction tx = odmg.newTransaction();
40         tx.begin();
41         for(int i = 1; i <= CONTROL_SIZE; i++)
42         {
43             Person aPerson = new PersonImpl();
44             aPerson.setFirstname("firstname" + i);
45             aPerson.setLastname("lastname" + i);
46             database.makePersistent(aPerson);
47         }
48         tx.commit();
49     }
50
51     private void removeAllData() throws Exception JavaDoc
52     {
53         Transaction tx = odmg.newTransaction();
54         tx.begin();
55         OQLQuery query = odmg.newOQLQuery();
56         String JavaDoc sql = "select allPersons from " + Person.class.getName();
57         query.create(sql);
58         Collection JavaDoc allPersons = (Collection JavaDoc) query.execute();
59         Iterator JavaDoc it = allPersons.iterator();
60         while(it.hasNext())
61         {
62             database.deletePersistent(it.next());
63         }
64         tx.commit();
65     }
66
67     /**
68      * test getting all (make sure basic operation is still functional)
69      */

70     public void testGetAllUnrestricted() throws Exception JavaDoc
71     {
72         // 1. remove all data
73
removeAllData();
74         // 2. Insert a bunch of articles objects into the database
75

76         createData();
77
78         // 3. Get a list of some articles
79
Transaction tx = odmg.newTransaction();
80         tx.begin();
81
82         OQLQuery query = odmg.newOQLQuery();
83         String JavaDoc sql = "select allPersons from " + Person.class.getName();
84         query.create(sql);
85         Collection JavaDoc allPersons = (Collection JavaDoc) query.execute();
86         // Iterator over the restricted articles objects
87
Iterator JavaDoc it = allPersons.iterator();
88         int count = 0;
89         while(it.hasNext())
90         {
91             it.next();
92             count++;
93         }
94         tx.commit();
95
96         // check that we got the right amount back.
97
if(count != (CONTROL_SIZE))
98         {
99             fail("count not right, found <"
100                     + count
101                     + "> should have got <"
102                     + (CONTROL_SIZE)
103                     + "> This failure is expected if your driver doesn't support advanced JDBC operations.");
104         }
105     }
106
107     /**
108      * test starting at an index and ending at an index.
109      */

110
111     public void testGetSomeA() throws Exception JavaDoc
112     {
113         int start = 10;
114         int end = 15;
115         // 1. remove all data
116
removeAllData();
117         // 2. Insert a bunch of articles objects into the database
118
createData();
119         // 3. Get a list of some articles
120
Transaction tx = odmg.newTransaction();
121         tx.begin();
122         EnhancedOQLQuery query = odmg.newOQLQuery();
123         String JavaDoc sql = "select somePersons from " + Person.class.getName();
124         query.create(sql, start, end);
125         Collection JavaDoc somePersons = (Collection JavaDoc) query.execute();
126
127         // Iterator over the restricted articles objects
128
Iterator JavaDoc it = somePersons.iterator();
129         int count = 0;
130         while(it.hasNext())
131         {
132             it.next();
133             count++;
134         }
135         tx.commit();
136         // check that we got the right amount back.
137
if(count != (end - start + 1))
138         {
139             fail("count not right, found <"
140                     + count
141                     + "> should have got <"
142                     + (end - start)
143                     + "> This failure is expected if your driver doesn't support advanced JDBC operations.");
144         }
145     }
146
147     /**
148      * test start at beginning, and go to an end index.
149      */

150     public void testGetSomeB() throws Exception JavaDoc
151     {
152         int start = Query.NO_START_AT_INDEX;
153         int end = 15;
154         try
155         {
156             // 1. remove all data
157
removeAllData();
158             // 2. Insert a bunch of articles objects into the database
159
createData();
160
161             // 3. Get a list of some articles
162
Transaction tx = odmg.newTransaction();
163             tx.begin();
164
165             EnhancedOQLQuery query = odmg.newOQLQuery();
166             String JavaDoc sql = "select somePersons from " + Person.class.getName();
167             query.create(sql, start, end);
168             Collection JavaDoc somePersons = (Collection JavaDoc) query.execute();
169             // Iterator over the restricted articles objects
170
Iterator JavaDoc it = somePersons.iterator();
171             int count = 0;
172             while(it.hasNext())
173             {
174                 it.next();
175                 count++;
176             }
177             tx.commit();
178             // check that we got the right amount back.
179
if(count != end)
180             {
181                 fail("count not right, found <"
182                         + count
183                         + "> should have got <"
184                         + (end)
185                         + "> This failure is expected if your driver doesn't support advanced JDBC operations.");
186             }
187         }
188         catch(Throwable JavaDoc t)
189         {
190             t.printStackTrace(System.out);
191             fail("testGetSomeB: " + t.getMessage());
192         }
193     }
194
195     /**
196      * test starting at a specific place, and have no ending index
197      */

198     public void testGetSomeC() throws Exception JavaDoc
199     {
200         int start = 10;
201         int end = Query.NO_END_AT_INDEX;
202         // 1. remove all data
203
removeAllData();
204         // 2. Insert a bunch of articles objects into the database
205

206         createData();
207
208         // 3. Get a list of some articles
209
TransactionExt tx = (TransactionExt) odmg.newTransaction();
210         tx.begin();
211         PersistenceBroker broker = tx.getBroker();
212
213         Connection JavaDoc conn = broker.serviceConnectionManager().getConnection();
214         /**
215          * only execute this test if scrolling is supported.
216          */

217         if(!conn.getMetaData()
218                 .supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE))
219         {
220             tx.commit();
221             return;
222         }
223
224         EnhancedOQLQuery query = odmg.newOQLQuery();
225         String JavaDoc sql = "select somePersons from " + Person.class.getName();
226         query.create(sql, start, end);
227         Collection JavaDoc somePersons = (Collection JavaDoc) query.execute();
228         // Iterator over the restricted articles objects
229
Iterator JavaDoc it = somePersons.iterator();
230         int count = 0;
231         while(it.hasNext())
232         {
233             it.next();
234             count++;
235         }
236         tx.commit();
237         // check that we got the right amount back.
238
if(count != (CONTROL_SIZE - start + 1)) /* +1 because the last row is inclusive */
239         {
240             fail("count not right, found <"
241                     + count
242                     + "> should have got <"
243                     + (CONTROL_SIZE - start + 1)
244                     + "> This failure is expected if your driver doesn't support advanced JDBC operations.");
245         }
246     }
247
248     /**
249      * test the condition where start is after end.
250      */

251     public void testGetSomeD() throws Exception JavaDoc
252     {
253         int start = 10;
254         int end = 5;
255         Transaction tx = odmg.newTransaction();
256         try
257         {
258             tx.begin();
259             EnhancedOQLQuery query = odmg.newOQLQuery();
260             String JavaDoc sql = "select somePersons from " + Person.class.getName();
261             query.create(sql, start, end);
262             query.execute();
263             fail("should have thrown QueryInvalidException");
264         }
265         catch(QueryInvalidException iqe)
266         {
267             // we wait for this exception
268
assertTrue(true);
269             tx.abort();
270         }
271     }
272
273     /**
274      * test condition where start and end are the same.
275      */

276     public void testGetSomeE() throws Exception JavaDoc
277     {
278         int start = 10;
279         int end = 10;
280         Transaction tx = null;
281         try
282         {
283             tx = odmg.newTransaction();
284             tx.begin();
285             EnhancedOQLQuery query = odmg.newOQLQuery();
286             String JavaDoc sql = "select somePersons from " + Person.class.getName();
287             query.create(sql, start, end);
288             query.execute();
289             fail("should have thrown QueryInvalidException");
290         }
291         catch(QueryInvalidException iqe)
292         {
293             // we expect that exception
294
assertTrue(true);
295         }
296         catch(Throwable JavaDoc t)
297         {
298             t.printStackTrace(System.out);
299             fail("testGetSomeC: " + t.getMessage());
300         }
301         finally
302         {
303             if(tx != null)
304             {
305                 tx.abort();
306             }
307         }
308     }
309 }
310
Popular Tags