001: package org.apache.ojb.odmg;
002:
003: import java.sql.Connection;
004: import java.sql.ResultSet;
005: import java.util.Collection;
006: import java.util.Iterator;
007:
008: import org.apache.ojb.broker.PersistenceBroker;
009: import org.apache.ojb.broker.query.Query;
010: import org.apache.ojb.junit.ODMGTestCase;
011: import org.apache.ojb.odmg.oql.EnhancedOQLQuery;
012: import org.apache.ojb.odmg.shared.Person;
013: import org.apache.ojb.odmg.shared.PersonImpl;
014: import org.odmg.OQLQuery;
015: import org.odmg.QueryInvalidException;
016: import org.odmg.Transaction;
017:
018: /**
019: * @author <a href="mailto:mattbaird@yahoo.com">Matthew Baird</a>
020: * @version $Id: ScrollableQueryResultsTest.java,v 1.18.4.1 2005/06/04 14:48:05 arminw Exp $
021: */
022: public class ScrollableQueryResultsTest extends ODMGTestCase {
023: private static final int CONTROL_SIZE = 50;
024:
025: public static void main(String[] args) {
026: String[] arr = { ScrollableQueryResultsTest.class.getName() };
027: junit.textui.TestRunner.main(arr);
028: }
029:
030: public ScrollableQueryResultsTest(String name) {
031: super (name);
032: }
033:
034: private void createData() throws Exception {
035: Transaction tx = odmg.newTransaction();
036: tx.begin();
037: for (int i = 1; i <= CONTROL_SIZE; i++) {
038: Person aPerson = new PersonImpl();
039: aPerson.setFirstname("firstname" + i);
040: aPerson.setLastname("lastname" + i);
041: database.makePersistent(aPerson);
042: }
043: tx.commit();
044: }
045:
046: private void removeAllData() throws Exception {
047: Transaction tx = odmg.newTransaction();
048: tx.begin();
049: OQLQuery query = odmg.newOQLQuery();
050: String sql = "select allPersons from " + Person.class.getName();
051: query.create(sql);
052: Collection allPersons = (Collection) query.execute();
053: Iterator it = allPersons.iterator();
054: while (it.hasNext()) {
055: database.deletePersistent(it.next());
056: }
057: tx.commit();
058: }
059:
060: /**
061: * test getting all (make sure basic operation is still functional)
062: */
063: public void testGetAllUnrestricted() throws Exception {
064: // 1. remove all data
065: removeAllData();
066: // 2. Insert a bunch of articles objects into the database
067:
068: createData();
069:
070: // 3. Get a list of some articles
071: Transaction tx = odmg.newTransaction();
072: tx.begin();
073:
074: OQLQuery query = odmg.newOQLQuery();
075: String sql = "select allPersons from " + Person.class.getName();
076: query.create(sql);
077: Collection allPersons = (Collection) query.execute();
078: // Iterator over the restricted articles objects
079: Iterator it = allPersons.iterator();
080: int count = 0;
081: while (it.hasNext()) {
082: it.next();
083: count++;
084: }
085: tx.commit();
086:
087: // check that we got the right amount back.
088: if (count != (CONTROL_SIZE)) {
089: fail("count not right, found <"
090: + count
091: + "> should have got <"
092: + (CONTROL_SIZE)
093: + "> This failure is expected if your driver doesn't support advanced JDBC operations.");
094: }
095: }
096:
097: /**
098: * test starting at an index and ending at an index.
099: */
100:
101: public void testGetSomeA() throws Exception {
102: int start = 10;
103: int end = 15;
104: // 1. remove all data
105: removeAllData();
106: // 2. Insert a bunch of articles objects into the database
107: createData();
108: // 3. Get a list of some articles
109: Transaction tx = odmg.newTransaction();
110: tx.begin();
111: EnhancedOQLQuery query = odmg.newOQLQuery();
112: String sql = "select somePersons from "
113: + Person.class.getName();
114: query.create(sql, start, end);
115: Collection somePersons = (Collection) query.execute();
116:
117: // Iterator over the restricted articles objects
118: Iterator it = somePersons.iterator();
119: int count = 0;
120: while (it.hasNext()) {
121: it.next();
122: count++;
123: }
124: tx.commit();
125: // check that we got the right amount back.
126: if (count != (end - start + 1)) {
127: fail("count not right, found <"
128: + count
129: + "> should have got <"
130: + (end - start)
131: + "> This failure is expected if your driver doesn't support advanced JDBC operations.");
132: }
133: }
134:
135: /**
136: * test start at beginning, and go to an end index.
137: */
138: public void testGetSomeB() throws Exception {
139: int start = Query.NO_START_AT_INDEX;
140: int end = 15;
141: try {
142: // 1. remove all data
143: removeAllData();
144: // 2. Insert a bunch of articles objects into the database
145: createData();
146:
147: // 3. Get a list of some articles
148: Transaction tx = odmg.newTransaction();
149: tx.begin();
150:
151: EnhancedOQLQuery query = odmg.newOQLQuery();
152: String sql = "select somePersons from "
153: + Person.class.getName();
154: query.create(sql, start, end);
155: Collection somePersons = (Collection) query.execute();
156: // Iterator over the restricted articles objects
157: Iterator it = somePersons.iterator();
158: int count = 0;
159: while (it.hasNext()) {
160: it.next();
161: count++;
162: }
163: tx.commit();
164: // check that we got the right amount back.
165: if (count != end) {
166: fail("count not right, found <"
167: + count
168: + "> should have got <"
169: + (end)
170: + "> This failure is expected if your driver doesn't support advanced JDBC operations.");
171: }
172: } catch (Throwable t) {
173: t.printStackTrace(System.out);
174: fail("testGetSomeB: " + t.getMessage());
175: }
176: }
177:
178: /**
179: * test starting at a specific place, and have no ending index
180: */
181: public void testGetSomeC() throws Exception {
182: int start = 10;
183: int end = Query.NO_END_AT_INDEX;
184: // 1. remove all data
185: removeAllData();
186: // 2. Insert a bunch of articles objects into the database
187:
188: createData();
189:
190: // 3. Get a list of some articles
191: TransactionExt tx = (TransactionExt) odmg.newTransaction();
192: tx.begin();
193: PersistenceBroker broker = tx.getBroker();
194:
195: Connection conn = broker.serviceConnectionManager()
196: .getConnection();
197: /**
198: * only execute this test if scrolling is supported.
199: */
200: if (!conn.getMetaData().supportsResultSetType(
201: ResultSet.TYPE_SCROLL_INSENSITIVE)) {
202: tx.commit();
203: return;
204: }
205:
206: EnhancedOQLQuery query = odmg.newOQLQuery();
207: String sql = "select somePersons from "
208: + Person.class.getName();
209: query.create(sql, start, end);
210: Collection somePersons = (Collection) query.execute();
211: // Iterator over the restricted articles objects
212: Iterator it = somePersons.iterator();
213: int count = 0;
214: while (it.hasNext()) {
215: it.next();
216: count++;
217: }
218: tx.commit();
219: // check that we got the right amount back.
220: if (count != (CONTROL_SIZE - start + 1)) /* +1 because the last row is inclusive */
221: {
222: fail("count not right, found <"
223: + count
224: + "> should have got <"
225: + (CONTROL_SIZE - start + 1)
226: + "> This failure is expected if your driver doesn't support advanced JDBC operations.");
227: }
228: }
229:
230: /**
231: * test the condition where start is after end.
232: */
233: public void testGetSomeD() throws Exception {
234: int start = 10;
235: int end = 5;
236: Transaction tx = odmg.newTransaction();
237: try {
238: tx.begin();
239: EnhancedOQLQuery query = odmg.newOQLQuery();
240: String sql = "select somePersons from "
241: + Person.class.getName();
242: query.create(sql, start, end);
243: query.execute();
244: fail("should have thrown QueryInvalidException");
245: } catch (QueryInvalidException iqe) {
246: // we wait for this exception
247: assertTrue(true);
248: tx.abort();
249: }
250: }
251:
252: /**
253: * test condition where start and end are the same.
254: */
255: public void testGetSomeE() throws Exception {
256: int start = 10;
257: int end = 10;
258: Transaction tx = null;
259: try {
260: tx = odmg.newTransaction();
261: tx.begin();
262: EnhancedOQLQuery query = odmg.newOQLQuery();
263: String sql = "select somePersons from "
264: + Person.class.getName();
265: query.create(sql, start, end);
266: query.execute();
267: fail("should have thrown QueryInvalidException");
268: } catch (QueryInvalidException iqe) {
269: // we expect that exception
270: assertTrue(true);
271: } catch (Throwable t) {
272: t.printStackTrace(System.out);
273: fail("testGetSomeC: " + t.getMessage());
274: } finally {
275: if (tx != null) {
276: tx.abort();
277: }
278: }
279: }
280: }
|