001: package org.apache.ojb.broker;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005: import java.util.Iterator;
006:
007: import junit.framework.TestCase;
008: import org.apache.commons.lang.builder.ToStringBuilder;
009: import org.apache.ojb.broker.query.Criteria;
010: import org.apache.ojb.broker.query.Query;
011: import org.apache.ojb.broker.query.QueryFactory;
012:
013: /**
014: *
015: *
016: * @author <a href="mailto:om@ppi.de">Oliver Matz</a>
017: * @version $Id: AbstractExtentClassTest.java,v 1.7.2.1 2005/08/16 15:11:33 aclute Exp $
018: */
019: public class AbstractExtentClassTest extends TestCase {
020: private PersistenceBroker broker;
021:
022: public AbstractExtentClassTest(String name) {
023: super (name);
024: }
025:
026: public static void main(String args[]) {
027: String[] arr = { AbstractExtentClassTest.class.getName() };
028: junit.textui.TestRunner.main(arr);
029: }
030:
031: public void setUp() throws Exception {
032: broker = PersistenceBrokerFactory.defaultPersistenceBroker();
033: }
034:
035: public void tearDown() {
036: try {
037: broker.clearCache();
038: broker.close();
039: } catch (PersistenceBrokerException e) {
040: }
041: }
042:
043: /**
044: * Used data model:
045: * AbstractIF_X <-- AbstractIF_Y <-- ConcreteZ
046: * AbstractIF_X <-- AbstractClassX <-- AbstractClassY <-- ConcreteZZ
047: */
048: public void testStoreRetrieveQueryUsingInterface() throws Exception {
049: String name = "interface_test_" + System.currentTimeMillis();
050: broker.clearCache();
051: broker.beginTransaction();
052: // create new XContainer with ConcreteZ and ConreteZZ references
053: XContainer container = new XContainer();
054: container.addX(new ConcreteZ(name));
055: container.addX(new ConcreteZ(name));
056: // we a set additional field 'zzName' in ConcreteZZ
057: container.addX(new ConcreteZZ(name, "ZZ"));
058: container.addX(new ConcreteZZ(name, "ZZ"));
059: container.addX(new ConcreteZZ(name, "ZZ"));
060:
061: broker.store(container);
062: broker.commitTransaction();
063: broker.clearCache();
064:
065: Identity cont = new Identity(container, broker);
066: broker.beginTransaction();
067: XContainer retContainer = (XContainer) broker
068: .getObjectByIdentity(cont);
069: broker.commitTransaction();
070: Collection res = retContainer.getXReferences();
071: assertNotNull(res);
072: assertEquals(5, res.size());
073: boolean found = false;
074: for (Iterator iterator = res.iterator(); iterator.hasNext();) {
075: Object o = iterator.next();
076: if (o instanceof ConcreteZZ) {
077: ConcreteZZ zz = (ConcreteZZ) o;
078: assertNotNull(zz.getConcreteZZName());
079: assertEquals("ZZ", zz.getConcreteZZName());
080: found = true;
081: }
082: }
083: assertTrue("No ConcreteZZ instances be returned", found);
084:
085: broker.clearCache();
086:
087: // test query base interface
088: Criteria crit = new Criteria();
089: crit.addLike("name", name);
090: Query q = QueryFactory.newQuery(AbstractIF_X.class, crit);
091: Collection results = broker.getCollectionByQuery(q);
092: assertNotNull(results);
093: assertEquals(5, results.size());
094: found = false;
095: for (Iterator iterator = results.iterator(); iterator.hasNext();) {
096: Object o = iterator.next();
097: if (o instanceof ConcreteZZ) {
098: ConcreteZZ zz = (ConcreteZZ) o;
099: assertNotNull(zz.getConcreteZZName());
100: assertEquals("ZZ", zz.getConcreteZZName());
101: found = true;
102: }
103: }
104: assertTrue("No ConcreteZZ instances be returned", found);
105:
106: // test query abstract class
107: broker.clearCache();
108: crit = new Criteria();
109: crit.addLike("name", name);
110: q = QueryFactory.newQuery(AbstractClassX.class, crit);
111: results = broker.getCollectionByQuery(q);
112: assertNotNull(results);
113: assertEquals(3, results.size());
114: for (Iterator iterator = results.iterator(); iterator.hasNext();) {
115: Object o = iterator.next();
116: ConcreteZZ zz = (ConcreteZZ) o;
117: assertNotNull(zz.getConcreteZZName());
118: assertEquals("ZZ", zz.getConcreteZZName());
119: }
120:
121: // test query abstract class
122: broker.clearCache();
123: crit = new Criteria();
124: crit.addLike("name", name);
125: q = QueryFactory.newQuery(AbstractClassY.class, crit);
126: results = broker.getCollectionByQuery(q);
127: assertNotNull(results);
128: assertEquals(3, results.size());
129: for (Iterator iterator = results.iterator(); iterator.hasNext();) {
130: Object o = iterator.next();
131: ConcreteZZ zz = (ConcreteZZ) o;
132: assertNotNull(zz.getConcreteZZName());
133: assertEquals("ZZ", zz.getConcreteZZName());
134: }
135:
136: // test query extended interface
137: broker.clearCache();
138: crit = new Criteria();
139: crit.addLike("name", name);
140: q = QueryFactory.newQuery(AbstractIF_Y.class, crit);
141: results = broker.getCollectionByQuery(q);
142: assertNotNull(results);
143: assertEquals(2, results.size());
144:
145: // test query concrete class
146: broker.clearCache();
147: crit = new Criteria();
148: crit.addLike("name", name);
149: q = QueryFactory.newQuery(ConcreteZ.class, crit);
150: results = broker.getCollectionByQuery(q);
151: assertNotNull(results);
152: assertEquals(2, results.size());
153:
154: // test query concrete class
155: broker.clearCache();
156: crit = new Criteria();
157: crit.addLike("name", name);
158: q = QueryFactory.newQuery(ConcreteZZ.class, crit);
159: results = broker.getCollectionByQuery(q);
160: assertNotNull(results);
161: assertEquals(3, results.size());
162: for (Iterator iterator = results.iterator(); iterator.hasNext();) {
163: Object o = iterator.next();
164: ConcreteZZ zz = (ConcreteZZ) o;
165: assertNotNull(zz.getConcreteZZName());
166: assertEquals("ZZ", zz.getConcreteZZName());
167: }
168: }
169:
170: //******************************************************************
171: // inner classes / persistent objects
172: //******************************************************************
173:
174: public static abstract interface AbstractIF_X {
175: public int getId();
176:
177: public int getContainerId();
178:
179: public void setContainerId(int containerId);
180:
181: public void setName(String name);
182:
183: public String getName();
184: }
185:
186: public static abstract interface AbstractIF_Y extends AbstractIF_X {
187: }
188:
189: public static class ConcreteZ implements AbstractIF_Y {
190: private int containerId;
191: private int someValue;
192: private int id;
193: private String name;
194:
195: public ConcreteZ() {
196: }
197:
198: public ConcreteZ(String name) {
199: this .name = name;
200: }
201:
202: ConcreteZ(int no) {
203: someValue = no;
204: }
205:
206: public int getContainerId() {
207: return containerId;
208: }
209:
210: public void setContainerId(int containerId) {
211: this .containerId = containerId;
212: }
213:
214: public int getId() {
215: return id;
216: }
217:
218: public void setId(int id) {
219: this .id = id;
220: }
221:
222: public String getName() {
223: return name;
224: }
225:
226: public void setName(String name) {
227: this .name = name;
228: }
229:
230: public int getSomeValue() {
231: return someValue;
232: }
233:
234: public void setSomeValue(int someValue) {
235: this .someValue = someValue;
236: }
237:
238: public String toString() {
239: return (new ToStringBuilder(this )).append("id", id).append(
240: "someValue", someValue).toString();
241: }
242: }
243:
244: public static class XContainer {
245: private int id;
246: private Collection myXReferences;
247:
248: public XContainer() {
249: }
250:
251: public XContainer(int id) {
252: this .id = id;
253: }
254:
255: // make javabean conform
256: public Collection getMyXReferences() {
257: return myXReferences;
258: }
259:
260: public void setMyXReferences(Collection myXReferences) {
261: this .myXReferences = myXReferences;
262: }
263:
264: public void addX(AbstractIF_X someX) {
265: if (myXReferences == null)
266: myXReferences = new ArrayList();
267: myXReferences.add(someX);
268: }
269:
270: public Collection getXReferences() {
271: return myXReferences;
272: }
273:
274: public int getId() {
275: return id;
276: }
277:
278: public void setId(int id) {
279: this .id = id;
280: }
281:
282: public String toString() {
283: return (new ToStringBuilder(this )).append("id", id).append(
284: "myXReferences", myXReferences).toString();
285: }
286: }
287:
288: public static abstract class AbstractClassX implements AbstractIF_X {
289: private int containerId;
290: private String name;
291:
292: public AbstractClassX() {
293: }
294:
295: public AbstractClassX(String name) {
296: this .name = name;
297: }
298:
299: public int getContainerId() {
300: return containerId;
301: }
302:
303: public void setContainerId(int containerId) {
304: this .containerId = containerId;
305: }
306:
307: public String getName() {
308: return name;
309: }
310:
311: public void setName(String name) {
312: this .name = name;
313: }
314: }
315:
316: public static abstract class AbstractClassY extends AbstractClassX {
317: public AbstractClassY() {
318: }
319:
320: public AbstractClassY(String name) {
321: super (name);
322: }
323: }
324:
325: public static class ConcreteZZ extends AbstractClassY {
326:
327: private int someValue;
328: private int id;
329: private String concreteZZName;
330:
331: public ConcreteZZ() {
332: }
333:
334: public ConcreteZZ(String name, String zzName) {
335: super (name);
336: this .concreteZZName = zzName;
337: }
338:
339: public int getId() {
340: return id;
341: }
342:
343: public void setId(int id) {
344: this .id = id;
345: }
346:
347: public String getConcreteZZName() {
348: return concreteZZName;
349: }
350:
351: public void setConcreteZZName(String concreteZZName) {
352: this .concreteZZName = concreteZZName;
353: }
354:
355: public int getSomeValue() {
356: return someValue;
357: }
358:
359: public void setSomeValue(int someValue) {
360: this .someValue = someValue;
361: }
362:
363: public String toString() {
364: return (new ToStringBuilder(this )).append("id", id).append(
365: "someValue", someValue).toString();
366: }
367: }
368: }
|