1 5 6 package org.apache.ojb.odmg; 7 8 import java.util.ArrayList ; 9 import java.util.Arrays ; 10 import java.util.Collection ; 11 import java.util.List ; 12 13 import org.apache.ojb.junit.ODMGTestCase; 14 import org.apache.ojb.odmg.shared.Person; 15 import org.apache.ojb.odmg.shared.PersonImpl; 16 import org.apache.commons.lang.ArrayUtils; 17 import org.apache.commons.collections.ListUtils; 18 import org.odmg.OQLQuery; 19 import org.odmg.Transaction; 20 21 27 public class PersonWithArrayTest extends ODMGTestCase 28 { 29 public static void main(String [] args) 30 { 31 String [] arr = {PersonWithArrayTest.class.getName()}; 32 junit.textui.TestRunner.main(arr); 33 } 34 35 39 public void testStoreDeleteThreePersons_1() throws Exception 40 { 41 String postfix = "_" + System.currentTimeMillis(); 42 String firstnameFather = "Father" + postfix; 43 String firstnameChild_1 = "Child_One" + postfix; 44 String firstnameChild_2 = "Child_Two" + postfix; 45 String lastname = "testStoreThreePersons_1_" + postfix; 46 47 Person father = createPerson(firstnameFather, lastname, null, null); 48 Person child_1 = createPerson(firstnameChild_1, lastname, null, null); 49 Person child_2 = createPerson(firstnameChild_2, lastname, null, null); 50 51 Person[] children = new Person[]{child_1, child_2}; 52 father.setChildren(children); 53 child_1.setFather(father); 54 child_2.setFather(father); 55 56 59 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 60 tx.begin(); 61 tx.lock(father, Transaction.WRITE); 62 tx.commit(); 63 64 tx.begin(); 65 ((TransactionImpl) tx).getBroker().clearCache(); 67 OQLQuery qry = odmg.newOQLQuery(); 68 qry.create("select a from " + PersonImpl.class.getName() + " where firstname=$1"); 69 qry.bind(firstnameFather); 70 Collection result = (Collection ) qry.execute(); 71 72 assertEquals("Exactly one element in result set", 1, result.size()); 73 Person returnedFather = (Person) result.iterator().next(); 74 assertTrue("not same", returnedFather != father); 76 Person[] returnedChildren = returnedFather.getChildren(); 77 assertNotNull(returnedChildren); 78 assertEquals(2, returnedChildren.length); 79 Person child = returnedChildren[0]; 80 Person lookupFather = child.getFather(); 81 assertNotNull(lookupFather); 82 assertEquals(returnedFather.getFirstname(), lookupFather.getFirstname()); 83 assertEquals( 85 "children's names are equal", 86 Arrays.asList(getFirstNames(returnedChildren)), 87 Arrays.asList(getFirstNames(children))); 88 tx.commit(); 89 90 94 tx.begin(); 95 database.deletePersistent(returnedFather); 96 database.deletePersistent(returnedFather.getChildren()[0]); 97 database.deletePersistent(returnedFather.getChildren()[1]); 98 99 tx.commit(); 100 101 qry = odmg.newOQLQuery(); 102 qry.create("select a from " + PersonImpl.class.getName() + " where firstname=$1"); 103 qry.bind(firstnameFather); 104 result = (Collection ) qry.execute(); 105 assertEquals(0, result.size()); 106 107 qry = odmg.newOQLQuery(); 108 qry.create("select a from " + PersonImpl.class.getName() + " where firstname=$1"); 109 qry.bind(firstnameChild_1); 110 result = (Collection ) qry.execute(); 111 assertEquals(0, result.size()); 113 } 114 115 119 public void testStoreDeleteThreePersons_2() throws Exception 120 { 121 String postfix = "_" + System.currentTimeMillis(); 122 String firstnameFather = "Father" + postfix; 123 String firstnameChild_1 = "Child_One" + postfix; 124 String firstnameChild_2 = "Child_Two" + postfix; 125 String lastname = "testStoreThreePersons_2_" + postfix; 126 127 Person father = createPerson(firstnameFather, lastname, null, null); 128 Person child_1 = createPerson(firstnameChild_1, lastname, null, null); 129 Person child_2 = createPerson(firstnameChild_2, lastname, null, null); 130 131 Person[] children = new Person[]{child_1, child_2}; 132 father.setChildren(children); 133 child_1.setFather(father); 134 child_2.setFather(father); 135 136 139 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 140 tx.begin(); 141 tx.lock(father, Transaction.WRITE); 142 tx.lock(child_2, Transaction.WRITE); 143 tx.lock(child_1, Transaction.WRITE); 144 145 tx.commit(); 146 assertEquals(2, father.getChildren().length); 147 148 tx.begin(); 149 ((TransactionImpl) tx).getBroker().clearCache(); 151 OQLQuery qry = odmg.newOQLQuery(); 152 qry.create("select a from " + PersonImpl.class.getName() + " where firstname=$1"); 153 qry.bind(firstnameFather); 154 Collection result = (Collection ) qry.execute(); 155 156 assertEquals("Exactly one element in result set", 1, result.size()); 157 Person returnedFather = (Person) result.iterator().next(); 158 assertTrue("not same", returnedFather != father); 160 Person[] returnedChildren = returnedFather.getChildren(); 161 assertNotNull(returnedChildren); 162 assertEquals(2, father.getChildren().length); 164 assertEquals(2, returnedChildren.length); 165 Person child = returnedChildren[0]; 166 Person lookupFather = child.getFather(); 167 assertNotNull(lookupFather); 168 assertEquals(returnedFather.getFirstname(), lookupFather.getFirstname()); 169 assertEquals( 171 "children's names are equal", 172 Arrays.asList(getFirstNames(returnedChildren)), 173 Arrays.asList(getFirstNames(children))); 174 tx.commit(); 176 177 180 tx.begin(); 181 186 tx.setCascadingDelete(PersonImpl.class, "children", false); 187 database.deletePersistent(returnedFather); 188 tx.commit(); 189 190 qry = odmg.newOQLQuery(); 191 qry.create("select a from " + PersonImpl.class.getName() + " where firstname=$1"); 192 qry.bind(firstnameFather); 193 result = (Collection ) qry.execute(); 194 assertEquals("Exactly one element in result set", 0, result.size()); 195 196 qry = odmg.newOQLQuery(); 197 qry.create("select a from " + PersonImpl.class.getName() + " where lastname=$1"); 198 qry.bind(lastname); 199 result = (Collection ) qry.execute(); 200 assertEquals("Expected the two children objects of deleted main object", 2, result.size()); 201 } 202 203 207 public void testStoreDeleteThreePersons_3() throws Exception 208 { 209 String postfix = "_" + System.currentTimeMillis(); 210 String firstnameFather = "Father" + postfix; 211 String firstnameChild_1 = "Child_One" + postfix; 212 String firstnameChild_2 = "Child_Two" + postfix; 213 String lastname = "testStoreThreePersons_3" + postfix; 214 215 Person father = createPerson(firstnameFather, lastname, null, null); 216 Person child_1 = createPerson(firstnameChild_1, lastname, null, null); 217 Person child_2 = createPerson(firstnameChild_2, lastname, null, null); 218 219 Person[] children = new Person[]{child_1, child_2}; 220 father.setChildren(children); 221 child_1.setFather(father); 222 child_2.setFather(father); 223 224 230 Transaction tx = odmg.newTransaction(); 231 tx.begin(); 232 tx.lock(child_1, Transaction.WRITE); 233 tx.lock(child_2, Transaction.WRITE); 234 tx.lock(father, Transaction.WRITE); 235 tx.commit(); 236 237 tx = odmg.newTransaction(); 238 tx.begin(); 239 ((TransactionImpl) tx).getBroker().clearCache(); 241 242 OQLQuery qry = odmg.newOQLQuery(); 243 qry.create("select a from " + PersonImpl.class.getName() + " where lastname=$1"); 244 qry.bind(lastname); 245 Collection result = (Collection ) qry.execute(); 246 assertEquals(3, new ArrayList (result).size()); 247 248 249 qry = odmg.newOQLQuery(); 250 qry.create("select a from " + PersonImpl.class.getName() + " where firstname=$1"); 251 qry.bind(firstnameFather); 252 result = (Collection ) qry.execute(); 253 tx.commit(); 254 assertEquals("Exactly one element in result set", 1, result.size()); 255 256 tx.begin(); 257 Person returnedFather = (Person) result.iterator().next(); 258 assertTrue("not same instance expected", returnedFather != father); 260 Person[] returnedChildren = returnedFather.getChildren(); 261 assertNotNull(returnedChildren); 262 assertEquals(2, returnedChildren.length); 263 Person child = returnedChildren[0]; 264 Person lookupFather = child.getFather(); 265 assertNotNull(lookupFather); 266 assertEquals(returnedFather.getFirstname(), lookupFather.getFirstname()); 267 List list = ListUtils.intersection(Arrays.asList(getFirstNames(returnedChildren)), Arrays.asList(getFirstNames(children))); 275 assertEquals(2, list.size()); 276 tx.commit(); 278 279 284 tx.begin(); 285 database.deletePersistent(returnedFather); 286 database.deletePersistent(returnedFather.getChildren()[0]); 287 database.deletePersistent(returnedFather.getChildren()[1]); 288 tx.commit(); 289 290 qry = odmg.newOQLQuery(); 291 qry.create("select a from " + PersonImpl.class.getName() + " where firstname=$1"); 292 qry.bind(firstnameFather); 293 result = (Collection ) qry.execute(); 294 assertEquals("Exactly one element in result set", 0, result.size()); 295 296 qry = odmg.newOQLQuery(); 297 qry.create("select a from " + PersonImpl.class.getName() + " where firstname=$1"); 298 qry.bind(firstnameChild_1); 299 result = (Collection ) qry.execute(); 300 assertEquals("Exactly one element in result set", 0, result.size()); 302 } 303 304 private Person createPerson(String firstname, String lastname, Person father, Person mother) 305 { 306 Person p = new PersonImpl(); 307 p.setFirstname(firstname); 308 p.setLastname(lastname); 309 p.setFather(father); 310 p.setMother(mother); 311 return p; 313 } 314 315 private static String [] getFirstNames(Person[] persons) 316 { 317 int length = persons == null ? 0 : persons.length; 318 String [] ret = new String [length]; 319 for (int i = 0; i < ret.length; i++) 320 { 321 ret[i] = persons[i].getFirstname(); 322 } 323 return ret; 324 } 325 } 326 | Popular Tags |