001: package org.apache.ojb.broker;
002:
003: import java.util.Collection;
004: import java.util.Iterator;
005:
006: import junit.framework.TestCase;
007:
008: import org.apache.ojb.broker.query.Criteria;
009: import org.apache.ojb.broker.query.QueryByCriteria;
010: import org.apache.ojb.broker.query.QueryFactory;
011:
012: /**
013: * Test query against extent classes and abstract base class.
014: *
015: * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
016: * @version $Id: MultipleTableExtentAwareQueryTest.java,v 1.2.2.2 2005/08/17 13:54:37 aclute Exp $
017: */
018: public class MultipleTableExtentAwareQueryTest extends TestCase {
019: PersistenceBroker broker;
020:
021: public MultipleTableExtentAwareQueryTest(String s) {
022: super (s);
023: }
024:
025: public void setUp() {
026: broker = PersistenceBrokerFactory.defaultPersistenceBroker();
027: }
028:
029: protected void tearDown() throws Exception {
030: if (broker != null && !broker.isClosed())
031: broker.close();
032: }
033:
034: public static void main(String[] args) {
035: String[] arr = { MultipleTableExtentAwareQueryTest.class
036: .getName() };
037: junit.textui.TestRunner.main(arr);
038: }
039:
040: public void testQueryForBaseClass() {
041: int objCount = 5;
042: String name = "testQueryForBaseClass_"
043: + System.currentTimeMillis();
044: broker.beginTransaction();
045: for (int i = objCount - 1; i >= 0; i--) {
046: ExtentA a = new ExtentA();
047: a.setName(name);
048: ExtentB b = new ExtentB();
049: b.setName(name);
050: broker.store(a);
051: broker.store(b);
052: }
053: broker.commitTransaction();
054:
055: Criteria crit = new Criteria();
056: crit.addEqualTo("name", name);
057: QueryByCriteria query = QueryFactory.newQuery(BaseClass.class,
058: crit);
059: Collection result = broker.getCollectionByQuery(query);
060: assertNotNull(result);
061: assertEquals("Expect all objects extending 'BaseClass'",
062: 2 * objCount, result.size());
063:
064: int count = broker.getCount(query);
065: assertEquals("Expect all objects extending 'BaseClass'",
066: 2 * objCount, count);
067: }
068:
069: public void testQueryForExtentsOfAbstractClass() {
070: int objCount = 5;
071: String name = "testQueryForExtentsOfAbstractClass_"
072: + System.currentTimeMillis();
073: broker.beginTransaction();
074: for (int i = objCount - 1; i >= 0; i--) {
075: ExtentA a = new ExtentA();
076: a.setName(name);
077: ExtentB b = new ExtentB();
078: b.setName(name);
079: broker.store(a);
080: broker.store(b);
081: }
082: broker.commitTransaction();
083:
084: Criteria crit = new Criteria();
085: crit.addEqualTo("name", name);
086: QueryByCriteria query = QueryFactory.newQuery(ExtentA.class,
087: crit);
088: Collection result = broker.getCollectionByQuery(query);
089: assertNotNull(result);
090: assertEquals(
091: "Wrong number of objects, expect only classes of type 'ExtentA'",
092: objCount, result.size());
093: assertTrue("Expect only classes of type 'ExtentA'", result
094: .iterator().next() instanceof ExtentA);
095:
096: int count = broker.getCount(query);
097: assertNotNull(result);
098: assertEquals(
099: "Wrong number of objects, expect only classes of type 'ExtentA'",
100: objCount, count);
101: }
102:
103: public void testQueryForExtentsOfRealClass() {
104: int objCount = 5;
105: String name = "testQueryForExtentsOfRealClass_"
106: + System.currentTimeMillis();
107: broker.beginTransaction();
108: for (int i = objCount - 1; i >= 0; i--) {
109: ExtentB b = new ExtentB();
110: b.setName(name);
111: ExtentC c = new ExtentC();
112: c.setName(name);
113: ExtentD d = new ExtentD();
114: d.setName(name);
115: broker.store(b);
116: broker.store(c);
117: broker.store(d);
118: }
119: broker.commitTransaction();
120:
121: Criteria crit = new Criteria();
122: crit.addEqualTo("name", name);
123: QueryByCriteria query = QueryFactory.newQuery(ExtentD.class,
124: crit);
125: Collection result = broker.getCollectionByQuery(query);
126: assertNotNull(result);
127: assertEquals(
128: "Wrong number of objects, expect only classes of type 'ExtentD'",
129: objCount, result.size());
130: assertTrue("Expect only classes of type 'ExtentD'", result
131: .iterator().next() instanceof ExtentD);
132:
133: query = QueryFactory.newQuery(ExtentC.class, crit);
134: result = broker.getCollectionByQuery(query);
135: assertNotNull(result);
136: assertEquals(
137: "Wrong number of objects, expect only classes of type ExtentC/ExtentD",
138: objCount * 2, result.size());
139:
140: int counterB = 0;
141: for (Iterator iterator = result.iterator(); iterator.hasNext();) {
142: Object obj = (ExtentC) iterator.next();
143: if (obj instanceof ExtentB)
144: ++counterB;
145: }
146:
147: query = QueryFactory.newQuery(ExtentB.class, crit);
148: result = broker.getCollectionByQuery(query);
149: assertNotNull(result);
150: assertEquals(
151: "Wrong number of objects, expect only classes of type ExtentB/ExtentC/ExtentD",
152: objCount * 3, result.size());
153: counterB = 0;
154: int counterC = 0;
155: for (Iterator iterator = result.iterator(); iterator.hasNext();) {
156: Object obj = iterator.next();
157: if (obj instanceof ExtentB)
158: ++counterB;
159: if (obj instanceof ExtentC)
160: ++counterC;
161: }
162: assertEquals(15, counterB);
163: assertEquals(10, counterC);
164: int count = broker.getCount(query);
165: assertEquals(
166: "Wrong number of objects, expect only classes of type ExtentB/ExtentC/ExtentD",
167: objCount * 3, count);
168: }
169:
170: //********************************************************
171: // inner classes test objects
172: //********************************************************
173: public static class ExtentA extends BaseClass {
174: }
175:
176: public static class ExtentB extends BaseClass {
177: }
178:
179: public static class ExtentC extends ExtentB {
180: }
181:
182: public static class ExtentD extends ExtentC {
183: }
184:
185: public abstract static class BaseClass {
186: private int objId;
187: private String name;
188:
189: public int getObjId() {
190: return objId;
191: }
192:
193: public void setObjId(int objId) {
194: this .objId = objId;
195: }
196:
197: public String getName() {
198: return name;
199: }
200:
201: public void setName(String name) {
202: this.name = name;
203: }
204: }
205: }
|