1 package org.apache.ojb.odmg; 2 3 17 18 import java.util.Collection ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 22 import org.apache.ojb.junit.ODMGTestCase; 23 import org.apache.ojb.odmg.shared.PersonImpl; 24 import org.apache.ojb.odmg.shared.TestClassA; 25 import org.apache.ojb.odmg.shared.TestClassB; 26 import org.odmg.OQLQuery; 27 import org.odmg.Transaction; 28 29 55 public class OQLOrOnForeignKeyTest extends ODMGTestCase 56 { 57 public static void main(String [] args) 58 { 59 String [] arr = {OQLOrOnForeignKeyTest.class.getName()}; 60 junit.textui.TestRunner.main(arr); 61 } 62 63 public OQLOrOnForeignKeyTest(String name) 64 { 65 super(name); 66 } 67 68 private void deleteData(Class target) 69 throws Exception 70 { 71 Transaction tx = odmg.newTransaction(); 72 tx.begin(); 73 OQLQuery query = odmg.newOQLQuery(); 74 query.create("select allStuff from " + target.getName()); 75 Collection allTargets = (Collection ) query.execute(); 76 Iterator it = allTargets.iterator(); 77 while (it.hasNext()) 78 { 79 database.deletePersistent(it.next()); 80 } 81 tx.commit(); 82 } 83 84 89 public void testOrReferenceOnSameTable() throws Exception 90 { 91 deleteData(PersonImpl.class); 92 93 PersonImpl jimmy = new PersonImpl(); 94 PersonImpl joe = new PersonImpl(); 95 PersonImpl father = new PersonImpl(); 96 PersonImpl mother = new PersonImpl(); 97 OQLQuery query; 98 List persons; 99 100 mother.setFirstname("mom"); 101 father.setFirstname("dad"); 102 103 jimmy.setMother(mother); 104 jimmy.setFirstname("jimmy"); 105 106 joe.setFather(father); 107 joe.setFirstname("joe"); 108 109 Transaction tx = odmg.newTransaction(); 110 tx.begin(); 111 database.makePersistent(father); 112 database.makePersistent(mother); 113 database.makePersistent(jimmy); 114 database.makePersistent(joe); 115 tx.commit(); 116 117 tx = odmg.newTransaction(); 119 tx.begin(); 120 query = odmg.newOQLQuery(); 121 query.create("select person from " + PersonImpl.class.getName() + 122 " where (mother.id=$1 or father.id=$2)"); 123 query.bind(new Integer (mother.getId())); 124 query.bind(new Integer (father.getId())); 125 persons = (List ) query.execute(); 126 assertEquals(2, persons.size()); 127 tx.commit(); 128 129 tx = odmg.newTransaction(); 131 tx.begin(); 132 query = odmg.newOQLQuery(); 133 query.create("select person from " + PersonImpl.class.getName() + 134 " where (mother.firstname=$1 or father.firstname=$2)"); 135 query.bind("mom"); 136 query.bind("dad"); 137 persons = (List ) query.execute(); 138 assertEquals(2, persons.size()); 139 tx.commit(); 140 } 141 142 public void testOrReferenceOnDifferentTables() throws Exception 143 { 144 deleteData(TestClassA.class); 145 deleteData(TestClassB.class); 146 147 TestClassA a1 = new TestClassA(); 148 TestClassA a2 = new TestClassA(); 149 150 TestClassB b1 = new TestClassB(); 151 TestClassB b2 = new TestClassB(); 152 a1.setB(b1); 153 a2.setB(b2); 154 155 Transaction tx = odmg.newTransaction(); 156 tx.begin(); 157 database.makePersistent(a1); 158 database.makePersistent(a2); 159 database.makePersistent(b1); 160 database.makePersistent(b2); 161 tx.commit(); 162 tx = odmg.newTransaction(); 163 tx.begin(); 164 OQLQuery query = odmg.newOQLQuery(); 166 query.create("select a from " + TestClassA.class.getName()); 167 List As = (List ) query.execute(); 168 Iterator asIterator = As.iterator(); 169 TestClassA temp = null; 170 171 temp = (TestClassA) asIterator.next(); 172 String bID1 = temp.getB().getOid(); 173 temp = (TestClassA) asIterator.next(); 174 String bID2 = temp.getB().getOid(); 175 176 query = odmg.newOQLQuery(); 177 query.create("select a from " + TestClassA.class.getName() + 178 " where (b.oid=$1 or b.oid=$2)"); 179 query.bind(bID1); 180 query.bind(bID2); 181 As = (List ) query.execute(); 182 assertTrue(As.size() == 2); 183 tx.commit(); 184 } 185 } 186 | Popular Tags |