1 package org.apache.ojb.broker; 2 3 import java.util.Iterator ; 4 import java.util.Collection ; 5 6 import org.apache.ojb.broker.query.Criteria; 7 import org.apache.ojb.broker.query.Query; 8 import org.apache.ojb.broker.query.QueryFactory; 9 import org.apache.ojb.broker.metadata.ClassDescriptor; 10 import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor; 11 import org.apache.ojb.junit.PBTestCase; 12 13 17 public class BidirectionalAssociationTest extends PBTestCase 18 { 19 public static void main(String [] args) 20 { 21 String [] arr = {BidirectionalAssociationTest.class.getName()}; 22 junit.textui.TestRunner.main(arr); 23 } 24 25 public BidirectionalAssociationTest(String name) 26 { 27 super(name); 28 } 29 30 public void setUp() throws Exception 31 { 32 super.setUp(); 33 } 34 35 public void tearDown() throws Exception 36 { 37 super.tearDown(); 38 } 39 40 public void testAutoRefreshTrue() 41 { 42 String pkSuffix = "_" + System.currentTimeMillis(); 43 ObjectReferenceDescriptor ord_A = null; 44 ObjectReferenceDescriptor ord_B = null; 45 ClassDescriptor cld_A = broker.getClassDescriptor(BidirectionalAssociationObjectA.class); 46 ord_A = cld_A.getObjectReferenceDescriptorByName("relatedB"); 47 ClassDescriptor cld_B = broker.getClassDescriptor(BidirectionalAssociationObjectB.class); 48 ord_B = cld_B.getObjectReferenceDescriptorByName("relatedA"); 49 boolean oldA = ord_A.isRefresh(); 50 boolean oldB = ord_B.isRefresh(); 51 try 52 { 53 ord_A.setRefresh(true); 54 ord_B.setRefresh(true); 55 createWithUpdate(pkSuffix); 56 Criteria crit = new Criteria(); 57 crit.addLike("pk", "%" + pkSuffix); 58 Query query = QueryFactory.newQuery(BidirectionalAssociationObjectB.class, crit); 59 Collection result = broker.getCollectionByQuery(query); 60 assertEquals(1, result.size()); 61 } 62 finally 63 { 64 if(ord_A != null) ord_A.setRefresh(oldA); 65 if(ord_B != null) ord_B.setRefresh(oldB); 66 } 67 } 68 69 public void testCreateDelete() 70 { 71 String pkSuffix = "_" + System.currentTimeMillis(); 72 createWithUpdate(pkSuffix); 73 deleteAllA(); 74 deleteAllB(); 75 } 76 77 private void createWithUpdate(String pkSuffix) 78 { 79 broker.beginTransaction(); 80 BidirectionalAssociationObjectA a = new BidirectionalAssociationObjectA(); 81 a.setPk("A" + pkSuffix); 82 BidirectionalAssociationObjectB b = new BidirectionalAssociationObjectB(); 83 b.setPk("B" + pkSuffix); 84 broker.store(a); 85 broker.store(b); 86 89 b.setRelatedA(a); 90 a.setRelatedB(b); 91 94 broker.store(a); 95 broker.store(b); 96 broker.commitTransaction(); 97 } 98 99 public void testGetA() throws Exception 100 { 101 String pkSuffix = "_" + System.currentTimeMillis(); 102 createWithUpdate(pkSuffix); 103 104 Criteria crit = new Criteria(); 105 Query q; 106 Iterator iter; 107 q = QueryFactory.newQuery(BidirectionalAssociationObjectA.class, crit); 108 iter = broker.getIteratorByQuery(q); 109 BidirectionalAssociationObjectA temp = null; 110 while (iter.hasNext()) 111 { 112 temp = (BidirectionalAssociationObjectA) iter.next(); 113 if (temp.getRelatedB() == null) 114 { 115 fail("relatedB not found"); 116 } 117 } 118 119 deleteAllA(); 120 deleteAllB(); 121 } 122 123 public void testGetB() throws Exception 124 { 125 String pkSuffix = "_" + System.currentTimeMillis(); 126 createWithUpdate(pkSuffix); 127 128 Criteria crit = new Criteria(); 129 Query q; 130 Iterator iter; 131 q = QueryFactory.newQuery(BidirectionalAssociationObjectB.class, crit); 132 iter = broker.getIteratorByQuery(q); 133 BidirectionalAssociationObjectB temp = null; 134 while (iter.hasNext()) 135 { 136 temp = (BidirectionalAssociationObjectB) iter.next(); 137 if (temp.getRelatedA() == null) 138 { 139 fail("relatedA not found"); 140 } 141 } 142 143 deleteAllA(); 144 deleteAllB(); 145 } 146 147 public void testDeleteA() 148 { 149 String pkSuffix = "_" + System.currentTimeMillis(); 150 createWithUpdate(pkSuffix); 151 deleteAllA(); 152 deleteAllB(); 153 } 154 155 public void testDeleteB() 156 { 157 String pkSuffix = "_" + System.currentTimeMillis(); 158 createWithUpdate(pkSuffix); 159 deleteAllB(); 160 deleteAllA(); 161 } 162 163 private void deleteAllA() 164 { 165 Criteria crit = new Criteria(); 166 Query q; 167 Iterator iter; 168 q = QueryFactory.newQuery(BidirectionalAssociationObjectA.class, crit); 169 iter = broker.getIteratorByQuery(q); 170 BidirectionalAssociationObjectA temp = null; 171 broker.beginTransaction(); 172 while (iter.hasNext()) 173 { 174 temp = (BidirectionalAssociationObjectA) iter.next(); 175 BidirectionalAssociationObjectB b = temp.getRelatedB(); 176 if (b != null) 177 { 178 b.setRelatedA(null); 179 broker.store(b); 180 } 181 broker.delete(temp); 182 } 183 broker.commitTransaction(); 184 } 185 186 private void deleteAllB() 187 { 188 Criteria crit = new Criteria(); 189 Query q; 190 Iterator iter; 191 q = QueryFactory.newQuery(BidirectionalAssociationObjectB.class, crit); 192 iter = broker.getIteratorByQuery(q); 193 BidirectionalAssociationObjectB temp = null; 194 broker.beginTransaction(); 195 while (iter.hasNext()) 196 { 197 temp = (BidirectionalAssociationObjectB) iter.next(); 198 BidirectionalAssociationObjectA a = temp.getRelatedA(); 199 if (a != null) 200 { 201 a.setRelatedB(null); 202 broker.store(a); 203 } 204 broker.delete(temp); 205 } 206 broker.commitTransaction(); 207 } 208 } 209 | Popular Tags |