KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > ejb > test > EntityManagerTest


1 //$Id: EntityManagerTest.java,v 1.12 2005/07/29 22:04:28 epbernard Exp $
2
package org.hibernate.ejb.test;
3
4 import java.util.HashMap JavaDoc;
5 import java.util.HashSet JavaDoc;
6 import java.util.Map JavaDoc;
7 import javax.persistence.EntityManager;
8 import javax.persistence.PersistenceContextType;
9
10 import org.hibernate.ejb.HibernateEntityManagerFactory;
11 import org.hibernate.stat.Statistics;
12
13 /**
14  * @author Gavin King
15  */

16 public class EntityManagerTest extends TestCase {
17
18     public Class JavaDoc[] getAnnotatedClasses() {
19         return new Class JavaDoc[]{
20             Item.class,
21             Distributor.class,
22             Wallet.class
23         };
24     }
25
26     public Map JavaDoc<Class JavaDoc, String JavaDoc> getCachedClasses() {
27         Map JavaDoc<Class JavaDoc, String JavaDoc> result = new HashMap JavaDoc<Class JavaDoc, String JavaDoc>();
28         result.put( Item.class, "read-write" );
29         return result;
30     }
31
32     public Map JavaDoc<String JavaDoc, String JavaDoc> getCachedCollections() {
33         Map JavaDoc<String JavaDoc, String JavaDoc> result = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
34         result.put( Item.class.getName() + ".distributors", "read-write, RegionName" );
35         return result;
36     }
37
38
39     public void testEntityManager() {
40
41         Item item = new Item( "Mouse", "Micro$oft mouse" );
42
43         EntityManager em = factory.createEntityManager();
44         em.getTransaction().begin();
45         em.persist( item );
46         assertTrue( em.contains( item ) );
47         em.getTransaction().commit();
48
49         assertFalse( em.contains( item ) );
50
51         em.getTransaction().begin();
52         item = (Item) em.createQuery( "from Item where descr like 'M%'" ).getSingleResult();
53         assertNotNull( item );
54         assertEquals( "Micro$oft mouse", item.getDescr() );
55         item.setDescr( "Micro$oft wireless mouse" );
56         assertTrue( em.contains( item ) );
57         em.getTransaction().commit();
58
59         assertFalse( em.contains( item ) );
60
61         em.getTransaction().begin();
62         item = (Item) em.find( Item.class, "Mouse" );
63         assertNotNull( item );
64         em.getTransaction().commit();
65
66         item = em.find( Item.class, "Mouse" );
67         assertFalse( em.contains( item ) );
68
69         item = (Item) em.createQuery( "from Item where descr like 'M%'" ).getSingleResult();
70         assertNotNull( item );
71         assertFalse( em.contains( item ) );
72
73         em.getTransaction().begin();
74         item = em.find( Item.class, "Mouse" );
75         assertNotNull( item );
76         assertTrue( em.contains( item ) );
77         assertEquals( "Micro$oft wireless mouse", item.getDescr() );
78         em.remove( item );
79         em.remove( item );//second should be a no-op
80
em.getTransaction().commit();
81
82         em.close();
83
84     }
85
86     public void testExtendedEntityManager() {
87
88         Item item = new Item( "Mouse", "Micro$oft mouse" );
89
90         EntityManager em = factory.createEntityManager( PersistenceContextType.EXTENDED );
91         em.getTransaction().begin();
92         em.persist( item );
93         assertTrue( em.contains( item ) );
94         em.getTransaction().commit();
95
96         assertTrue( em.contains( item ) );
97
98         em.getTransaction().begin();
99         Item item1 = (Item) em.createQuery( "from Item where descr like 'M%'" ).getSingleResult();
100         assertNotNull( item1 );
101         assertSame( item, item1 );
102         item.setDescr( "Micro$oft wireless mouse" );
103         assertTrue( em.contains( item ) );
104         em.getTransaction().commit();
105
106         assertTrue( em.contains( item ) );
107
108         em.getTransaction().begin();
109         item1 = em.find( Item.class, "Mouse" );
110         assertSame( item, item1 );
111         em.getTransaction().commit();
112         assertTrue( em.contains( item ) );
113
114         item1 = em.find( Item.class, "Mouse" );
115         assertSame( item, item1 );
116         assertTrue( em.contains( item ) );
117
118         item1 = (Item) em.createQuery( "from Item where descr like 'M%'" ).getSingleResult();
119         assertNotNull( item1 );
120         assertSame( item, item1 );
121         assertTrue( em.contains( item ) );
122
123         em.getTransaction().begin();
124         assertTrue( em.contains( item ) );
125         em.remove( item );
126         em.getTransaction().commit();
127
128         em.close();
129
130     }
131
132     public void testConfiguration() throws Exception JavaDoc {
133         Item item = new Item( "Mouse", "Micro$oft mouse" );
134         Distributor res = new Distributor();
135         res.setName( "Bruce" );
136         item.setDistributors( new HashSet JavaDoc<Distributor>() );
137         item.getDistributors().add( res );
138         Statistics stats = ( (HibernateEntityManagerFactory) factory ).getSessionFactory().getStatistics();
139         stats.clear();
140         stats.setStatisticsEnabled( true );
141
142         EntityManager em = factory.createEntityManager();
143         em.getTransaction().begin();
144
145         em.persist( res );
146         em.persist( item );
147         assertTrue( em.contains( item ) );
148
149         em.getTransaction().commit();
150         em.close();
151
152         assertEquals( 1, stats.getSecondLevelCachePutCount() );
153         assertEquals( 0, stats.getSecondLevelCacheHitCount() );
154
155         em = factory.createEntityManager();
156         em.getTransaction().begin();
157         Item second = em.find( Item.class, item.getName() );
158         assertEquals( 1, second.getDistributors().size() );
159         assertEquals( 1, stats.getSecondLevelCacheHitCount() );
160         em.getTransaction().commit();
161         em.close();
162
163         em = factory.createEntityManager();
164         em.getTransaction().begin();
165         second = em.find( Item.class, item.getName() );
166         assertEquals( 1, second.getDistributors().size() );
167         assertEquals( 3, stats.getSecondLevelCacheHitCount() );
168         em.getTransaction().commit();
169         em.close();
170
171         stats.clear();
172         stats.setStatisticsEnabled( false );
173     }
174
175     public void testContains() throws Exception JavaDoc {
176         EntityManager em = factory.createEntityManager();
177         em.getTransaction().begin();
178         Integer JavaDoc nonManagedObject = new Integer JavaDoc( 4 );
179         try {
180             em.contains( nonManagedObject );
181             fail( "Should have raised an exception" );
182         }
183         catch (IllegalArgumentException JavaDoc iae) {
184             //success
185
if ( em.getTransaction() != null ) em.getTransaction().rollback();
186         }
187         finally {
188             em.close();
189         }
190         em = factory.createEntityManager();
191         em.getTransaction().begin();
192         Item item = new Item();
193         item.setDescr( "Mine" );
194         item.setName( "Juggy" );
195         em.persist( item );
196         em.getTransaction().commit();
197         em.getTransaction().begin();
198         item = em.getReference( Item.class, item.getName() );
199         assertTrue( em.contains( item ) );
200         em.getTransaction().commit();
201         em.close();
202     }
203
204     public void testPersistNoneGenerator() throws Exception JavaDoc {
205         EntityManager em = factory.createEntityManager();
206         em.getTransaction().begin();
207         Wallet w = new Wallet();
208         w.setBrand( "Lacoste" );
209         w.setModel( "Minimic" );
210         w.setSerial( "0100202002" );
211         em.persist( w );
212         em.getTransaction().commit();
213         em.getTransaction().begin();
214         assertEquals( w.getBrand(), em.find( Wallet.class, w.getSerial() ).getBrand() );
215         em.getTransaction().commit();
216         em.close();
217     }
218
219     //EJB-9
220
// public void testGet() throws Exception {
221
// EntityManager em = factory.createEntityManager();
222
// em.getTransaction().begin();
223
// Item item = (Item) em.get(Item.class, "nonexistentone");
224
// try {
225
// item.getDescr();
226
// em.getTransaction().commit();
227
// fail("Object with wrong id should have failed");
228
// }
229
// catch (EntityNotFoundException e) {
230
// //success
231
// if (em.getTransaction() != null) em.getTransaction().rollback();
232
// }
233
// finally {
234
// em.close();
235
// }
236
// }
237

238     public EntityManagerTest() {
239         super();
240     }
241
242     public EntityManagerTest(String JavaDoc arg0) {
243         super( arg0 );
244     }
245
246 }
247
Popular Tags