1 package org.hibernate.ejb.test; 2 3 import java.util.HashMap ; 4 import java.util.HashSet ; 5 import javax.persistence.EntityManager; 6 import javax.persistence.EntityManagerFactory; 7 import javax.persistence.Persistence; 8 import javax.persistence.PersistenceContextType; 9 10 import org.hibernate.ejb.HibernateEntityManagerFactory; 11 import org.hibernate.ejb.test.pack.defaultpar.ApplicationServer; 12 import org.hibernate.ejb.test.pack.defaultpar.Mouse; 13 import org.hibernate.ejb.test.pack.defaultpar.Version; 14 import org.hibernate.ejb.test.pack.explodedpar.Carpet; 15 import org.hibernate.ejb.test.pack.explodedpar.Elephant; 16 import org.hibernate.ejb.test.pack.externaljar.Scooter; 17 import org.hibernate.ejb.test.pack.excludehbmpar.Caipirinha; 18 import org.hibernate.stat.Statistics; 19 20 22 25 public class PackagedEntityManagerTest extends TestCase { 26 27 public Class [] getAnnotatedClasses() { 28 return new Class []{ 29 }; 30 } 31 32 public void setUp() { 33 factory = Persistence.createEntityManagerFactory( "manager1", new HashMap () ); 34 } 35 36 public void testDefaultPar() throws Exception { 37 EntityManagerFactory emf = Persistence.createEntityManagerFactory( "defaultpar", new HashMap () ); 38 EntityManager em = emf.createEntityManager(); 39 ApplicationServer as = new ApplicationServer(); 40 as.setName( "JBoss AS" ); 41 Version v = new Version(); 42 v.setMajor( 4 ); 43 v.setMinor( 0 ); 44 v.setMicro( 3 ); 45 as.setVersion( v ); 46 Mouse mouse = new Mouse(); 47 mouse.setName( "mickey" ); 48 em.getTransaction().begin(); 49 em.persist( as ); 50 em.persist( mouse ); 51 assertEquals( 1, em.createNamedQuery( "allMouse" ).getResultList().size() ); 52 em.remove(mouse); 53 assertNotNull( as.getId() ); 54 em.remove(as); 55 em.getTransaction().commit(); 56 em.close(); 57 emf.close(); 58 } 59 60 public void testExplodedPar() throws Exception { 61 EntityManagerFactory emf = Persistence.createEntityManagerFactory( "explodedpar", new HashMap () ); 62 EntityManager em = emf.createEntityManager(); 63 org.hibernate.ejb.test.pack.explodedpar.Carpet carpet = new Carpet(); 64 Elephant el = new Elephant(); 65 el.setName( "Dumbo" ); 66 carpet.setCountry( "Turkey" ); 67 em.getTransaction().begin(); 68 em.persist( carpet ); 69 em.persist( el ); 70 assertEquals( 1, em.createNamedQuery("allCarpet").getResultList().size() ); 71 assertNotNull( carpet.getId() ); 72 em.remove( carpet); 73 em.getTransaction().commit(); 74 em.close(); 75 emf.close(); 76 } 77 78 79 public void testExcludeHbmPar() throws Exception { 80 EntityManagerFactory emf = Persistence.createEntityManagerFactory( "excludehbmpar", new HashMap () ); 81 EntityManager em = emf.createEntityManager(); 82 Caipirinha s = new Caipirinha( "Strong" ); 83 em.getTransaction().begin(); 84 em.persist( s ); 85 em.getTransaction().commit(); 86 87 em.getTransaction().begin(); 88 s = em.find( Caipirinha.class, s.getId() ); 89 em.remove(s); 90 em.getTransaction().commit(); 91 em.close(); 92 emf.close(); 93 } 94 95 public void testCfgXmlPar() throws Exception { 96 EntityManagerFactory emf = Persistence.createEntityManagerFactory( "cfgxmlpar", new HashMap () ); 97 EntityManager em = emf.createEntityManager(); 98 Item i = new Item(); 99 i.setDescr( "Blah" ); 100 i.setName( "factory" ); 101 em.getTransaction().begin(); 102 em.persist( i ); 103 em.getTransaction().commit(); 104 105 em.getTransaction().begin(); 106 i = em.find( Item.class, i.getName() ); 107 em.remove(i); 108 em.getTransaction().commit(); 109 em.close(); 110 emf.close(); 111 } 112 113 public void testEntityManager() { 114 115 Item item = new Item( "Mouse", "Micro$oft mouse" ); 116 117 EntityManager em = factory.createEntityManager(); 118 em.getTransaction().begin(); 119 em.persist( item ); 120 assertTrue( em.contains( item ) ); 121 em.getTransaction().commit(); 122 123 assertFalse( em.contains( item ) ); 124 125 em.getTransaction().begin(); 126 item = (Item) em.createQuery( "from Item where descr like 'M%'" ).getSingleResult(); 127 assertNotNull( item ); 128 assertEquals( "Micro$oft mouse", item.getDescr() ); 129 item.setDescr( "Micro$oft wireless mouse" ); 130 assertTrue( em.contains( item ) ); 131 em.getTransaction().commit(); 132 133 assertFalse( em.contains( item ) ); 134 135 em.getTransaction().begin(); 136 item = (Item) em.find( Item.class, "Mouse" ); 137 assertNotNull( item ); 138 em.getTransaction().commit(); 139 140 item = em.find( Item.class, "Mouse" ); 141 assertFalse( em.contains( item ) ); 142 143 item = (Item) em.createQuery( "from Item where descr like 'M%'" ).getSingleResult(); 144 assertNotNull( item ); 145 assertFalse( em.contains( item ) ); 146 147 em.getTransaction().begin(); 148 assertEquals("Explicit package in <class> should work", 1, em.createNamedQuery( "countItems" ).getSingleResult() ); 149 em.getTransaction().commit(); 150 151 em.getTransaction().begin(); 152 item = em.find( Item.class, "Mouse" ); 153 assertNotNull( item ); 154 assertTrue( em.contains( item ) ); 155 assertEquals( "Micro$oft wireless mouse", item.getDescr() ); 156 em.remove( item ); 157 em.getTransaction().commit(); 158 159 em.close(); 160 161 } 162 163 public void testExtendedEntityManager() { 164 165 Item item = new Item( "Mouse", "Micro$oft mouse" ); 166 167 EntityManager em = factory.createEntityManager( PersistenceContextType.EXTENDED ); 168 em.getTransaction().begin(); 169 em.persist( item ); 170 assertTrue( em.contains( item ) ); 171 em.getTransaction().commit(); 172 173 assertTrue( em.contains( item ) ); 174 175 em.getTransaction().begin(); 176 Item item1 = (Item) em.createQuery( "from Item where descr like 'M%'" ).getSingleResult(); 177 assertNotNull( item1 ); 178 assertSame( item, item1 ); 179 item.setDescr( "Micro$oft wireless mouse" ); 180 assertTrue( em.contains( item ) ); 181 em.getTransaction().commit(); 182 183 assertTrue( em.contains( item ) ); 184 185 em.getTransaction().begin(); 186 item1 = em.find( Item.class, "Mouse" ); 187 assertSame( item, item1 ); 188 em.getTransaction().commit(); 189 assertTrue( em.contains( item ) ); 190 191 item1 = em.find( Item.class, "Mouse" ); 192 assertSame( item, item1 ); 193 assertTrue( em.contains( item ) ); 194 195 item1 = (Item) em.createQuery( "from Item where descr like 'M%'" ).getSingleResult(); 196 assertNotNull( item1 ); 197 assertSame( item, item1 ); 198 assertTrue( em.contains( item ) ); 199 200 em.getTransaction().begin(); 201 assertTrue( em.contains( item ) ); 202 em.remove( item ); 203 em.getTransaction().commit(); 204 205 em.close(); 206 207 } 208 209 public void testConfiguration() throws Exception { 210 Item item = new Item( "Mouse", "Micro$oft mouse" ); 211 Distributor res = new Distributor(); 212 res.setName( "Bruce" ); 213 item.setDistributors( new HashSet <Distributor>() ); 214 item.getDistributors().add( res ); 215 Statistics stats = ( (HibernateEntityManagerFactory) factory ).getSessionFactory().getStatistics(); 216 stats.clear(); 217 stats.setStatisticsEnabled( true ); 218 219 EntityManager em = factory.createEntityManager(); 220 em.getTransaction().begin(); 221 222 em.persist( res ); 223 em.persist( item ); 224 assertTrue( em.contains( item ) ); 225 226 em.getTransaction().commit(); 227 em.close(); 228 229 assertEquals( 1, stats.getSecondLevelCachePutCount() ); 230 assertEquals( 0, stats.getSecondLevelCacheHitCount() ); 231 232 em = factory.createEntityManager(); 233 em.getTransaction().begin(); 234 Item second = em.find( Item.class, item.getName() ); 235 assertEquals( 1, second.getDistributors().size() ); 236 assertEquals( 1, stats.getSecondLevelCacheHitCount() ); 237 em.getTransaction().commit(); 238 em.close(); 239 240 em = factory.createEntityManager(); 241 em.getTransaction().begin(); 242 second = em.find( Item.class, item.getName() ); 243 assertEquals( 1, second.getDistributors().size() ); 244 assertEquals( 3, stats.getSecondLevelCacheHitCount() ); 245 for (Distributor distro : second.getDistributors() ) { 246 em.remove( distro ); 247 } 248 em.remove(second); 249 em.getTransaction().commit(); 250 em.close(); 251 252 stats.clear(); 253 stats.setStatisticsEnabled( false ); 254 } 255 256 public void testExternalJar() throws Exception { 257 EntityManager em = factory.createEntityManager(); 258 Scooter s = new Scooter(); 259 s.setModel( "Abadah" ); 260 s.setSpeed( new Long ( 85 ) ); 261 em.getTransaction().begin(); 262 em.persist( s ); 263 em.getTransaction().commit(); 264 em.close(); 265 em = factory.createEntityManager(); 266 em.getTransaction().begin(); 267 s = em.find( Scooter.class, s.getModel() ); 268 assertEquals( new Long ( 85 ), s.getSpeed() ); 269 em.remove(s); 270 em.getTransaction().commit(); 271 em.close(); 272 } 273 274 public PackagedEntityManagerTest() { 275 super(); 276 } 277 278 public PackagedEntityManagerTest(String arg0) { 279 super( arg0 ); 280 } 281 282 } 283 | Popular Tags |