1 package org.apache.ojb.odmg; 2 3 import java.util.List ; 4 5 import org.apache.ojb.broker.Identity; 6 import org.apache.ojb.junit.ODMGTestCase; 7 import org.apache.ojb.odmg.shared.Person; 8 import org.apache.ojb.odmg.shared.PersonImpl; 9 import org.apache.ojb.odmg.shared.Site; 10 import org.odmg.Implementation; 11 import org.odmg.OQLQuery; 12 import org.odmg.Transaction; 13 import org.odmg.TransactionNotInProgressException; 14 15 16 22 public class UserTestCases extends ODMGTestCase 23 { 24 public static void main(String [] args) 25 { 26 String [] arr = {UserTestCases.class.getName()}; 27 junit.textui.TestRunner.main(arr); 28 } 29 30 35 public void testDuplicateInsertion() throws Exception 36 { 37 String name = "testDuplicateInsertion_" + System.currentTimeMillis(); 38 String nameNew = "testDuplicateInsertion_New_" + System.currentTimeMillis(); 39 40 42 newSite(odmg, name, 2, 1); 45 46 try 51 { 52 newSite(odmg, name, 3, 2); 53 assertTrue("We should get a SqlException 'Violation of unique index'", false); 54 } 55 catch(Exception e) 56 { 57 assertTrue(true); 59 } 60 61 try 65 { 66 newSite(odmg, nameNew, 1, 2); 67 assertTrue(true); 68 } 69 catch(Exception e) 70 { 71 e.printStackTrace(); 72 assertTrue("This exception should not happend: " + e.getMessage(), false); 73 throw e; 74 } 75 } 77 78 private void newSite(Implementation odmg, String name, int year, int semester) throws Exception 79 { 80 Transaction tx = null; 81 tx = odmg.newTransaction(); 82 Site site = null; 83 tx.begin(); 84 85 site = new Site(); 86 site.setName(name); 87 site.setYear(new Integer (year)); 88 site.setSemester(new Integer (semester)); 89 90 tx.lock(site, Transaction.WRITE); 91 tx.commit(); 92 } 93 94 public void testSimpleQueryDelete() throws Exception 95 { 96 String name = "testSimpleQueryDelete - " + System.currentTimeMillis(); 97 98 Site site = new Site(); 99 site.setName(name); 100 Transaction tx = odmg.newTransaction(); 101 tx.begin(); 102 tx.lock(site, Transaction.WRITE); 103 tx.commit(); 104 105 OQLQuery query = odmg.newOQLQuery(); 106 query.create("select sites from " + Site.class.getName() + " where name=$1"); 107 query.bind(name); 108 tx.begin(); 109 List result = (List ) query.execute(); 110 if(result.size() == 0) 111 { 112 fail("Stored object not found"); 113 } 114 tx.commit(); 115 116 tx.begin(); 117 database.deletePersistent(site); 118 tx.commit(); 119 120 query = odmg.newOQLQuery(); 121 query.create("select sites from " + Site.class.getName() + " where name=$1"); 122 query.bind(name); 123 tx.begin(); 124 List result2 = (List ) query.execute(); 125 if(result2.size() > 0) 126 { 127 fail("We should not found deleted objects"); 128 } 129 tx.commit(); 130 } 131 132 159 public void testImplicitLocking() throws Exception 160 { 161 String name = "testImplicitLocking - " + System.currentTimeMillis(); 162 String queryString = "select sites from " + Site.class.getName() + " where name = $1"; 163 164 165 Site site = new Site(); 166 site.setName(name); 167 168 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 169 tx.setImplicitLocking(false); 172 tx.begin(); 173 database.makePersistent(site); 174 tx.commit(); 175 176 177 OQLQuery query = odmg.newOQLQuery(); 178 query.create(queryString); 179 query.bind(name); 180 181 tx.begin(); 182 List result = (List ) query.execute(); 183 assertEquals(1, result.size()); 184 site = (Site) result.get(0); 185 assertNotNull(site); 186 assertNull(site.getYear()); 187 tx.lock(site, Transaction.WRITE); 188 site.setYear(new Integer (2003)); 189 tx.commit(); 190 191 192 query = odmg.newOQLQuery(); 193 query.create(queryString); 194 query.bind(name); 195 tx.begin(); 196 tx.getBroker().clearCache(); 197 result = (List ) query.execute(); 198 assertEquals(1, result.size()); 199 site = (Site) result.get(0); 200 assertNotNull(site); 201 assertNotNull("year should not be null", site.getYear()); 202 tx.commit(); 203 } 204 205 208 public void testStoreRetrieveSameTxn() throws Exception 209 { 210 String name = "testStoreRetrieveSameTxn_" + System.currentTimeMillis(); 211 Person mum = new PersonImpl(); 212 mum.setFirstname(name); 213 214 TransactionExt txn = (TransactionExt) odmg.newTransaction(); 215 txn.begin(); 216 txn.lock(mum, Transaction.WRITE); 217 txn.commit(); 219 220 txn.begin(); 221 txn.getBroker().clearCache(); 222 Identity mumId = txn.getBroker().serviceIdentity().buildIdentity(mum); 223 Person mum2 = (Person) txn.getBroker().getObjectByIdentity(mumId); 224 txn.commit(); 226 assertNotNull(mum2); 227 assertEquals(name, mum2.getFirstname()); 228 } 229 230 public void testRetrieveNonExistent() 231 { 232 try 233 { 234 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 235 tx.begin(); 236 Identity id = tx.getBroker().serviceIdentity().buildIdentity(PersonImpl.class, new Integer (-1)); 238 tx.getBroker().getObjectByIdentity(id); 239 tx.abort(); 240 } 241 catch(Exception exc) 242 { 243 exc.printStackTrace(); 244 fail("caught unexpected exception: " + exc.toString()); 245 } 246 } 247 248 251 public void testRetrieveOutsideTxn() 252 { 253 try 254 { 255 Identity id = new Identity(Person.class, Person.class, new Integer []{new Integer (-1)}); 257 TransactionImpl txn = (TransactionImpl) odmg.newTransaction(); 258 try 259 { 260 txn.getObjectByIdentity(id); 261 fail("expected TransactionNotInProgressException not thrown"); 262 } 263 catch(TransactionNotInProgressException exc) 264 { 265 } 267 } 268 catch(Exception exc) 269 { 270 exc.printStackTrace(); 271 fail("caught unexpected exception: " + exc.toString()); 272 } 273 } 274 } 275 | Popular Tags |