1 package org.apache.ojb.odmg; 2 3 import org.apache.ojb.broker.Contract; 4 import org.apache.ojb.broker.Identity; 5 import org.apache.ojb.junit.ODMGTestCase; 6 import org.apache.ojb.odmg.shared.DetailFKinPK; 7 import org.apache.ojb.odmg.shared.DetailFKnoPK; 8 import org.apache.ojb.odmg.shared.Master; 9 import org.odmg.Implementation; 10 import org.odmg.OQLQuery; 11 import org.odmg.Transaction; 12 13 import java.util.Collection ; 14 import java.util.Iterator ; 15 import java.util.Vector ; 16 import java.util.List ; 17 18 21 public class RITest extends ODMGTestCase 22 { 23 public static void main(String [] args) 24 { 25 String [] arr = {RITest.class.getName()}; 26 junit.textui.TestRunner.main(arr); 27 } 28 29 public RITest(String name) 30 { 31 super(name); 32 } 33 34 38 public void testStoreFKnoPK() throws Exception 39 { 40 long timestamp = System.currentTimeMillis(); 41 Transaction tx = odmg.newTransaction(); 42 tx.begin(); 43 Master master_1 = populatedMasterFKnoPK(tx, 5, timestamp); 44 Master master_2 = populatedMasterFKnoPK(tx, 5, timestamp); 45 46 database.makePersistent(master_1); 47 database.makePersistent(master_2); 48 tx.commit(); 49 50 OQLQuery query = odmg.newOQLQuery(); 52 query.create("select masters from " + Master.class.getName() + " where masterText like $1"); 53 query.bind("%" + timestamp); 54 List allMasters = (List ) query.execute(); 55 assertEquals("We should found master objects", 2, allMasters.size()); 56 Master lookup_1 = (Master) allMasters.get(0); 57 58 Collection col_in = lookup_1.getCollDetailFKinPK(); 59 Collection col_no = lookup_1.getCollDetailFKnoPK(); 60 assertEquals("Should found none " + DetailFKinPK.class.getName() + " objects", 0, col_in.size()); 61 assertEquals("Should found " + DetailFKnoPK.class.getName() + " objects", 5, col_no.size()); 62 } 63 64 68 public void testStoreFKinPK() throws Exception 69 { 70 final long timestamp = System.currentTimeMillis(); 71 Transaction tx = odmg.newTransaction(); 72 tx.begin(); 73 74 Master master_1 = populatedMasterFKinPK(tx, 5, timestamp); 75 Master master_2 = populatedMasterFKinPK(tx, 5, timestamp); 76 77 database.makePersistent(master_1); 78 database.makePersistent(master_2); 79 tx.commit(); 80 81 OQLQuery query = odmg.newOQLQuery(); 83 query.create("select masters from " + Master.class.getName() + " where masterText like $1"); 84 query.bind("%" + timestamp); 85 List allMasters = (List ) query.execute(); 86 assertEquals("We should found master objects", 2, allMasters.size()); 87 Master lookup_1 = (Master) allMasters.get(0); 88 Collection col_in = lookup_1.getCollDetailFKinPK(); 89 Collection col_no = lookup_1.getCollDetailFKnoPK(); 90 assertEquals("Should found none " + DetailFKnoPK.class.getName() + " objects", 0, col_no.size()); 91 assertEquals("Should found " + DetailFKinPK.class.getName() + " objects", 5, col_in.size()); 92 } 93 94 private Master populatedMasterFKnoPK(Transaction tx, int countDetailObjects, long timestamp) 95 throws Exception 96 { 97 Master master = new Master(); 98 master.masterText = "Master_timestamp_" + timestamp; 99 master.collDetailFKnoPK = new Vector (); 100 new Identity(master, ((HasBroker) tx).getBroker()); 101 for (int i = 0; i < countDetailObjects; i++) 102 { 103 DetailFKnoPK aDetail = new DetailFKnoPK(); 104 aDetail.detailText = "DetailFK*no*PK count " + i + ", associate master " + master.masterId + " timestamp_" + timestamp; 105 aDetail.master = master; 106 master.collDetailFKnoPK.add(aDetail); 107 } 108 return master; 109 } 110 111 private Master populatedMasterFKinPK(Transaction tx, int countDetailObjects, long timestamp) 112 throws Exception 113 { 114 Master master = new Master(); 115 master.masterText = "Master_timestamp_" + timestamp; 116 master.collDetailFKinPK = new Vector (); 117 new Identity(master, ((HasBroker) tx).getBroker()); 118 for (int i = 0; i < countDetailObjects; i++) 119 { 120 DetailFKinPK aDetail = new DetailFKinPK(); 121 aDetail.detailText = "DetailFKinPK count " + i + ", associate master " + master.masterId + " timestamp_" + timestamp; 122 aDetail.master = master; 123 master.collDetailFKinPK.add(aDetail); 124 } 125 return master; 126 } 127 128 public void testDelete() throws Exception 129 { 130 long timestamp = System.currentTimeMillis(); 131 Transaction tx = odmg.newTransaction(); 133 tx.begin(); 134 Master master_1 = populatedMasterFKinPK(tx, 7, timestamp); 137 Master master_2 = populatedMasterFKinPK(tx, 6, timestamp); 138 Master master_3 = populatedMasterFKnoPK(tx, 7, timestamp); 139 Master master_4 = populatedMasterFKnoPK(tx, 6, timestamp); 140 141 tx.lock(master_1, Transaction.WRITE); 142 tx.lock(master_2, Transaction.WRITE); 143 tx.lock(master_3, Transaction.WRITE); 144 tx.lock(master_4, Transaction.WRITE); 145 tx.commit(); 146 147 tx.begin(); 148 OQLQuery query = odmg.newOQLQuery(); 149 query.create("select masters from " + Master.class.getName() + " where masterText like $1"); 150 query.bind("%" + timestamp); 151 List allMasters = (List ) query.execute(); 152 153 Iterator it = allMasters.iterator(); 155 int counter = 0; 156 while (it.hasNext()) 157 { 158 ++counter; 159 Master aMaster = (Master) it.next(); 160 Iterator it2 = aMaster.collDetailFKinPK.iterator(); 161 while (it2.hasNext()) 162 database.deletePersistent(it2.next()); 163 it2 = aMaster.collDetailFKnoPK.iterator(); 164 while (it2.hasNext()) 165 database.deletePersistent(it2.next()); 166 database.deletePersistent(aMaster); 167 } 168 tx.commit(); 169 assertEquals("Wrong count of Master objects found", 4, counter); 170 171 query = odmg.newOQLQuery(); 172 query.create("select masters from " + Master.class.getName() + " where masterText like $1"); 173 query.bind("%" + timestamp); 174 allMasters = (List ) query.execute(); 175 assertEquals("Delete of Master objects failed", 0, allMasters.size()); 176 } 177 178 public void testInsertAfterDelete() throws Exception 179 { 180 final String contractPk = "The Contract_" + System.currentTimeMillis(); 181 Contract obj1 = new Contract(); 182 Contract obj2 = new Contract(); 183 184 obj1.setPk(contractPk); 185 obj1.setContractValue2(1); 186 187 obj2.setPk(contractPk); 188 obj2.setContractValue2(2); 189 190 Transaction tx = odmg.newTransaction(); 192 tx.begin(); 193 200 database.makePersistent(obj1); 201 database.deletePersistent(obj2); 202 database.makePersistent(obj1); 203 214 tx.commit(); 215 Collection result = getContract(contractPk, odmg); 216 assertEquals("We should found exact one contract", 1, result.size()); 217 Contract newObj = (Contract) result.iterator().next(); 218 assertNotNull("Object not found", newObj); 219 assertEquals(1, newObj.getContractValue2()); 220 221 tx.begin(); 223 database.deletePersistent(newObj); 224 database.makePersistent(obj2); 225 tx.commit(); 226 assertEquals(2, obj2.getContractValue2()); 227 228 result = getContract(contractPk, odmg); 229 assertEquals("We should found exact one contract", 1, result.size()); 230 newObj = (Contract) result.iterator().next(); 231 assertNotNull("Object not found", newObj); 232 assertEquals(2, newObj.getContractValue2()); 233 234 tx.begin(); 236 database.deletePersistent(obj1); 237 tx.commit(); 238 239 result = getContract(contractPk, odmg); 240 assertEquals(0, result.size()); 241 } 242 243 private Collection getContract(String pk, Implementation odmg) 244 throws Exception 245 { 246 OQLQuery query = odmg.newOQLQuery(); 247 query.create("select c from " + Contract.class.getName() + " where pk like $1"); 248 query.bind(pk); 249 return (Collection ) query.execute(); 250 } 251 } 252 | Popular Tags |