001: package org.apache.ojb.broker;
002:
003: import java.util.Iterator;
004:
005: import junit.framework.TestCase;
006: import org.apache.ojb.broker.query.*;
007: import org.apache.ojb.broker.accesslayer.RsIterator;
008:
009: /**
010: * Test case for the RsIterator
011: *
012: * @author <a href="mailto:rongallagher@bellsouth.net">Ron Gallagher<a>
013: * @version $Id: $
014: */
015: public class RsIteratorTest extends TestCase {
016: private PersistenceBroker broker;
017:
018: public static void main(String[] args) {
019: String[] arr = { RsIteratorTest.class.getName() };
020: junit.textui.TestRunner.main(arr);
021: }
022:
023: public RsIteratorTest(String name) {
024: super (name);
025: }
026:
027: public void setUp() {
028: broker = PersistenceBrokerFactory.defaultPersistenceBroker();
029: }
030:
031: public void tearDown() {
032: if (broker != null) {
033: broker.close();
034: }
035: }
036:
037: public void testRsIterator() throws Exception {
038: String name = "testRsIterator_" + System.currentTimeMillis();
039: prepareTest(name);
040:
041: Criteria criteria = new Criteria();
042: criteria.addLike("name", name + "*");
043: Query query = new QueryByCriteria(
044: ObjectRepository.Component.class, criteria);
045:
046: Iterator it = broker.getIteratorByQuery(query);
047: int k = 0;
048: while (it.hasNext()) {
049: it.next();
050: k++;
051: }
052: assertEquals("Wrong number of items found", 2, k);
053: }
054:
055: /**
056: * Test RsIterator cleanup on PB.commitTransaction()
057: */
058: public void testRsIteratorAutomaticCleanupCheck_1()
059: throws Exception {
060: String name = "testRsIteratorAutomaticCleanupCheck_1_"
061: + System.currentTimeMillis();
062: prepareTest(name);
063:
064: Criteria criteria = new Criteria();
065: criteria.addLike("name", name + "*");
066: Query query = new QueryByCriteria(
067: ObjectRepository.Component.class, criteria);
068:
069: Iterator it = broker.getIteratorByQuery(query);
070: it.hasNext();
071: broker.beginTransaction();
072: broker.commitTransaction();
073: /*
074: if tx was commited we invalidate RsIterator instance
075: */
076: try {
077: it.next();
078: fail("We expect RsIterator has released resources on pb.commit..");
079: } catch (RsIterator.ResourceClosedException e) {
080: assertTrue(true);
081: }
082:
083: it = broker.getIteratorByQuery(query);
084: it.hasNext();
085: it.next();
086: broker.beginTransaction();
087: broker.commitTransaction();
088: /*
089: if tx was commited we invalidate RsIterator instance
090: */
091: try {
092: it.hasNext();
093: it.next();
094: fail("We expect RsIterator has released resources on pb.commit..");
095: } catch (RsIterator.ResourceClosedException e) {
096: assertTrue(true);
097: }
098: }
099:
100: /**
101: * Test RsIterator cleanup on PB.abortTransaction()
102: */
103: public void testRsIteratorAutomaticCleanupCheck_2()
104: throws Exception {
105: String name = "testRsIteratorAutomaticCleanupCheck_"
106: + System.currentTimeMillis();
107: prepareTest(name);
108:
109: Criteria criteria = new Criteria();
110: criteria.addLike("name", name + "*");
111: Query query = new QueryByCriteria(
112: ObjectRepository.Component.class, criteria);
113:
114: Iterator it = broker.getIteratorByQuery(query);
115: it.hasNext();
116: broker.beginTransaction();
117: broker.abortTransaction();
118: /*
119: if tx was aborted we invalidate RsIterator instance
120: */
121: try {
122: it.next();
123: fail("We expect RsIterator has released resources on pb.commit..");
124: } catch (RsIterator.ResourceClosedException e) {
125: assertTrue(true);
126: }
127:
128: it = broker.getIteratorByQuery(query);
129: it.hasNext();
130: it.next();
131: broker.beginTransaction();
132: broker.abortTransaction();
133: /*
134: if tx was aborted we invalidate RsIterator instance
135: */
136: try {
137: it.hasNext();
138: it.next();
139: fail("We expect RsIterator has released resources on pb.commit..");
140: } catch (RsIterator.ResourceClosedException e) {
141: assertTrue(true);
142: }
143: }
144:
145: /**
146: * Test RsIterator cleanup on PB.close()
147: */
148: public void testRsIteratorAutomaticCleanupCheck_3()
149: throws Exception {
150: String name = "testRsIteratorAutomaticCleanupCheck_"
151: + System.currentTimeMillis();
152: prepareTest(name);
153:
154: Criteria criteria = new Criteria();
155: criteria.addLike("name", name + "*");
156: Query query = new QueryByCriteria(
157: ObjectRepository.Component.class, criteria);
158:
159: Iterator it = broker.getIteratorByQuery(query);
160: broker.close();
161: /*
162: if was closed we invalidate RsIterator instance
163: */
164: try {
165: if (it.hasNext())
166: it.next();
167: fail("We expect RsIterator has released resources on pb.commit..");
168: } catch (RsIterator.ResourceClosedException e) {
169: assertTrue(true);
170: }
171: }
172:
173: /**
174: * Test RsIterator cleanup on PB.abortTransaction()
175: */
176: public void testRsIteratorUserCleanup_1() throws Exception {
177: String name = "testRsIteratorAutomaticCleanupCheck_"
178: + System.currentTimeMillis();
179: prepareTest(name);
180:
181: Criteria criteria = new Criteria();
182: criteria.addLike("name", name + "*");
183: Query query = new QueryByCriteria(
184: ObjectRepository.Component.class, criteria);
185:
186: Iterator it = broker.getIteratorByQuery(query);
187:
188: /*
189: TODO: After integration of setAutoRelease into OJBIterator and changes
190: in PB interface getIteratorXXX methods we don't need these casts any longer
191: */
192: if (!(it instanceof RsIterator)) {
193: // skip test
194: return;
195: }
196:
197: // TODO: Remove this cast one day
198: ((RsIterator) it).setAutoRelease(false);
199:
200: it.hasNext();
201: broker.beginTransaction();
202: broker.abortTransaction();
203: /*
204: if tx was aborted we invalidate RsIterator instance
205: */
206: try {
207: it.next();
208: fail("We expect RsIterator has released resources on pb.commit..");
209: } catch (RsIterator.ResourceClosedException e) {
210: assertTrue(true);
211: }
212:
213: it = broker.getIteratorByQuery(query);
214: // TODO: Remove this cast one day
215: ((RsIterator) it).setAutoRelease(false);
216:
217: it.hasNext();
218: it.next();
219: broker.beginTransaction();
220: broker.abortTransaction();
221: /*
222: if tx was aborted we invalidate RsIterator instance
223: */
224: try {
225: it.hasNext();
226: it.next();
227: fail("We expect RsIterator has released resources on pb.commit..");
228: } catch (RsIterator.ResourceClosedException e) {
229: assertTrue(true);
230: }
231: }
232:
233: /**
234: * Test RsIterator cleanup on PB.abortTransaction()
235: */
236: public void testRsIteratorUserCleanup_2() throws Exception {
237: String name = "testRsIteratorAutomaticCleanupCheck_"
238: + System.currentTimeMillis();
239: prepareTest(name);
240:
241: Criteria criteria = new Criteria();
242: criteria.addLike("name", name + "*");
243: Query query = new QueryByCriteria(
244: ObjectRepository.Component.class, criteria);
245:
246: Iterator it = broker.getIteratorByQuery(query);
247:
248: /*
249: TODO: After integration of setAutoRelease into OJBIterator and changes
250: in PB interface getIteratorXXX methods we don't need these casts any longer
251: */
252: if (!(it instanceof RsIterator)) {
253: // skip test
254: return;
255: }
256:
257: // TODO: Remove this cast one day
258: ((RsIterator) it).setAutoRelease(false);
259:
260: while (it.hasNext()) {
261: ObjectRepository.Component c = (ObjectRepository.Component) it
262: .next();
263: assertNotNull(c.getId());
264: }
265:
266: try {
267: ((RsIterator) it).relative(1);
268: } catch (RsIterator.ResourceClosedException e) {
269: fail("RsIterator should not close resources by itself");
270: } catch (PersistenceBrokerException ignore) {
271: }
272:
273: // TODO: Remove this cast one day
274: ((RsIterator) it).releaseDbResources();
275: }
276:
277: private void prepareTest(String objectName) {
278: ObjectRepository.Component c1 = new ObjectRepository.Component();
279: c1.setName(objectName + "_1");
280: ObjectRepository.Component c2 = new ObjectRepository.Component();
281: c2.setName(objectName + "_2");
282:
283: broker.beginTransaction();
284: broker.store(c1);
285: broker.store(c2);
286: broker.commitTransaction();
287: }
288:
289: /**
290: * Test retrieving data via the rsIterator
291: * test by Ron Gallagher
292: */
293: public void testInternUsedRsIterator() throws Exception {
294: // Build the query
295: Criteria criteria = new Criteria();
296: criteria.addEqualTo("id", new Integer(1));
297: Query query = new QueryByCriteria(Person.class, criteria);
298: // Run the query.
299: Person person = (Person) broker.getObjectByQuery(query);
300: assertNotNull("Person with id 1 was not found", person);
301: }
302:
303: }
|