1 package org.apache.ojb.broker; 2 3 import java.util.Collection ; 4 import java.util.Iterator ; 5 6 import junit.framework.TestCase; 7 8 import org.apache.ojb.broker.query.Criteria; 9 import org.apache.ojb.broker.query.QueryByCriteria; 10 import org.apache.ojb.broker.query.QueryFactory; 11 12 18 public class MultipleTableExtentAwareQueryTest extends TestCase 19 { 20 PersistenceBroker broker; 21 22 public MultipleTableExtentAwareQueryTest(String s) 23 { 24 super(s); 25 } 26 27 public void setUp() 28 { 29 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 30 } 31 32 protected void tearDown() throws Exception 33 { 34 if(broker != null && !broker.isClosed()) broker.close(); 35 } 36 37 public static void main(String [] args) 38 { 39 String [] arr = {MultipleTableExtentAwareQueryTest.class.getName()}; 40 junit.textui.TestRunner.main(arr); 41 } 42 43 public void testQueryForBaseClass() 44 { 45 int objCount = 5; 46 String name = "testQueryForBaseClass_"+System.currentTimeMillis(); 47 broker.beginTransaction(); 48 for (int i = objCount-1; i >= 0; i--) 49 { 50 ExtentA a = new ExtentA(); 51 a.setName(name); 52 ExtentB b = new ExtentB(); 53 b.setName(name); 54 broker.store(a); 55 broker.store(b); 56 } 57 broker.commitTransaction(); 58 59 Criteria crit = new Criteria(); 60 crit.addEqualTo("name", name); 61 QueryByCriteria query = QueryFactory.newQuery(BaseClass.class, crit); 62 Collection result = broker.getCollectionByQuery(query); 63 assertNotNull(result); 64 assertEquals("Expect all objects extending 'BaseClass'", 2*objCount, result.size()); 65 66 int count = broker.getCount(query); 67 assertEquals("Expect all objects extending 'BaseClass'", 2*objCount, count); 68 } 69 70 public void testQueryForExtentsOfAbstractClass() 71 { 72 int objCount = 5; 73 String name = "testQueryForExtentsOfAbstractClass_"+System.currentTimeMillis(); 74 broker.beginTransaction(); 75 for (int i = objCount-1; i >= 0; i--) 76 { 77 ExtentA a = new ExtentA(); 78 a.setName(name); 79 ExtentB b = new ExtentB(); 80 b.setName(name); 81 broker.store(a); 82 broker.store(b); 83 } 84 broker.commitTransaction(); 85 86 Criteria crit = new Criteria(); 87 crit.addEqualTo("name", name); 88 QueryByCriteria query = QueryFactory.newQuery(ExtentA.class, crit); 89 Collection result = broker.getCollectionByQuery(query); 90 assertNotNull(result); 91 assertEquals("Wrong number of objects, expect only classes of type 'ExtentA'", objCount, result.size()); 92 assertTrue("Expect only classes of type 'ExtentA'", result.iterator().next() instanceof ExtentA); 93 94 int count = broker.getCount(query); 95 assertNotNull(result); 96 assertEquals("Wrong number of objects, expect only classes of type 'ExtentA'", objCount, count); 97 } 98 99 public void testQueryForExtentsOfRealClass() 100 { 101 int objCount = 5; 102 String name = "testQueryForExtentsOfRealClass_"+System.currentTimeMillis(); 103 broker.beginTransaction(); 104 for (int i = objCount-1; i >= 0; i--) 105 { 106 ExtentB b = new ExtentB(); 107 b.setName(name); 108 ExtentC c = new ExtentC(); 109 c.setName(name); 110 ExtentD d = new ExtentD(); 111 d.setName(name); 112 broker.store(b); 113 broker.store(c); 114 broker.store(d); 115 } 116 broker.commitTransaction(); 117 118 Criteria crit = new Criteria(); 119 crit.addEqualTo("name", name); 120 QueryByCriteria query = QueryFactory.newQuery(ExtentD.class, crit); 121 Collection result = broker.getCollectionByQuery(query); 122 assertNotNull(result); 123 assertEquals("Wrong number of objects, expect only classes of type 'ExtentD'", objCount, result.size()); 124 assertTrue("Expect only classes of type 'ExtentD'", result.iterator().next() instanceof ExtentD); 125 126 query = QueryFactory.newQuery(ExtentC.class, crit); 127 result = broker.getCollectionByQuery(query); 128 assertNotNull(result); 129 assertEquals("Wrong number of objects, expect only classes of type ExtentC/ExtentD", 130 objCount*2, result.size()); 131 132 int counterB = 0; 133 for (Iterator iterator = result.iterator(); iterator.hasNext();) 134 { 135 Object obj = (ExtentC) iterator.next(); 136 if(obj instanceof ExtentB) ++counterB; 137 } 138 139 query = QueryFactory.newQuery(ExtentB.class, crit); 140 result = broker.getCollectionByQuery(query); 141 assertNotNull(result); 142 assertEquals("Wrong number of objects, expect only classes of type ExtentB/ExtentC/ExtentD", 143 objCount*3, result.size()); 144 counterB = 0; 145 int counterC = 0; 146 for (Iterator iterator = result.iterator(); iterator.hasNext();) 147 { 148 Object obj = iterator.next(); 149 if(obj instanceof ExtentB) ++counterB; 150 if(obj instanceof ExtentC) ++counterC; 151 } 152 assertEquals(15, counterB); 153 assertEquals(10, counterC); 154 int count = broker.getCount(query); 155 assertEquals("Wrong number of objects, expect only classes of type ExtentB/ExtentC/ExtentD", 156 objCount*3, count); 157 } 158 159 160 161 public static class ExtentA extends BaseClass 165 { 166 } 167 168 public static class ExtentB extends BaseClass 169 { 170 } 171 172 public static class ExtentC extends ExtentB 173 { 174 } 175 176 public static class ExtentD extends ExtentC 177 { 178 } 179 180 public abstract static class BaseClass 181 { 182 private int objId; 183 private String name; 184 185 public int getObjId() 186 { 187 return objId; 188 } 189 190 public void setObjId(int objId) 191 { 192 this.objId = objId; 193 } 194 195 public String getName() 196 { 197 return name; 198 } 199 200 public void setName(String name) 201 { 202 this.name = name; 203 } 204 } 205 } 206 | Popular Tags |