1 package org.apache.ojb.jdo; 2 3 17 18 import java.util.Iterator ; 19 import java.util.NoSuchElementException ; 20 21 import javax.jdo.Extent; 22 import javax.jdo.PersistenceManager; 23 import javax.jdo.Transaction; 24 25 import junit.framework.TestCase; 26 27 import org.apache.ojb.otm.Person; 28 29 public class TestPersistenceManager extends TestCase 30 { 31 32 private PersistenceManagerFactoryImpl factory = new PersistenceManagerFactoryImpl(); 33 34 public void testLoadExtent() throws Exception 35 { 36 Person article = new Person(); 37 PersistenceManager pm = factory.getPersistenceManager(); 38 Transaction tx = pm.currentTransaction(); 39 tx.begin(); 40 pm.makePersistent(article); 41 tx.commit(); 42 43 tx.begin(); 44 Extent extent = pm.getExtent(Person.class, true); 45 Iterator itty = extent.iterator(); 46 assertTrue(itty.hasNext()); 47 tx.commit(); 48 pm.close(); 49 } 50 51 public void testIteratorClosedWhenOutsideTx() throws Exception 52 { 53 Person article = new Person(); 54 PersistenceManager pm = factory.getPersistenceManager(); 55 Transaction tx = pm.currentTransaction(); 56 tx.begin(); 57 pm.makePersistent(article); 58 tx.commit(); 59 60 tx.begin(); 61 Extent extent = pm.getExtent(Person.class, true); 62 Iterator itty = extent.iterator(); 63 tx.commit(); 64 try 65 { 66 itty.next(); 67 fail("Should have thrown exception"); 68 } 69 catch (NoSuchElementException e) 70 { 71 assertTrue("Flow of control will pass through here", true); 72 } 73 pm.close(); 74 } 75 76 public void testVerifyCannotQueryExtentWithoutSubclasses() 77 { 78 PersistenceManager pm = factory.getPersistenceManager(); 79 pm.currentTransaction().begin(); 80 try 81 { 82 pm.getExtent(Person.class, false); 83 fail("Not supposed to be supported!"); 84 } 85 catch (UnsupportedOperationException e) 86 { 87 assertTrue("Flow goes through here", true); 88 } 89 pm.currentTransaction().commit(); 90 pm.close(); 91 } 92 93 public void testQueryClassCast() 94 { 95 PersistenceManager pm = factory.getPersistenceManager(); 96 pm.currentTransaction().begin(); 97 try 98 { 99 pm.newQuery(new Object ()); 100 fail("Should not be able to pass incorrect argument to newQuery(Object)"); 101 } 102 catch (IllegalArgumentException e) 103 { 104 assertTrue("Flow goes through here", true); 105 } 106 pm.currentTransaction().commit(); 107 pm.close(); 108 } 109 } 110 | Popular Tags |