KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > ops > SaveOrUpdateTest


1 //$Id: SaveOrUpdateTest.java,v 1.2 2005/06/20 03:19:34 oneovthafew Exp $
2
package org.hibernate.test.ops;
3
4 import junit.framework.Test;
5 import junit.framework.TestSuite;
6
7 import net.sf.cglib.transform.impl.InterceptFieldEnabled;
8
9 import org.hibernate.Hibernate;
10 import org.hibernate.Session;
11 import org.hibernate.Transaction;
12 import org.hibernate.cfg.Configuration;
13 import org.hibernate.cfg.Environment;
14 import org.hibernate.criterion.Projections;
15 import org.hibernate.test.TestCase;
16
17 /**
18  * @author Gavin King
19  */

20 public class SaveOrUpdateTest extends TestCase {
21     
22     public SaveOrUpdateTest(String JavaDoc str) {
23         super(str);
24     }
25     
26     public void testSaveOrUpdateDeepTree() {
27         
28         clearCounts();
29         
30         Session s = openSession();
31         Transaction tx = s.beginTransaction();
32         Node root = new Node("root");
33         Node child = new Node("child");
34         Node grandchild = new Node("grandchild");
35         root.addChild(child);
36         child.addChild(grandchild);
37         s.saveOrUpdate(root);
38         tx.commit();
39         s.close();
40         
41         assertInsertCount(3);
42         assertUpdateCount(0);
43         clearCounts();
44         
45         grandchild.setDescription("the grand child");
46         Node grandchild2 = new Node("grandchild2");
47         child.addChild( grandchild2 );
48
49         s = openSession();
50         tx = s.beginTransaction();
51         s.saveOrUpdate(root);
52         tx.commit();
53         s.close();
54         
55         assertInsertCount(1);
56         assertUpdateCount(1);
57         clearCounts();
58         
59         Node child2 = new Node("child2");
60         Node grandchild3 = new Node("grandchild3");
61         child2.addChild( grandchild3 );
62         root.addChild(child2);
63         
64         s = openSession();
65         tx = s.beginTransaction();
66         s.saveOrUpdate(root);
67         tx.commit();
68         s.close();
69         
70         assertInsertCount(2);
71         assertUpdateCount(0);
72         clearCounts();
73         
74         s = openSession();
75         tx = s.beginTransaction();
76         s.delete(grandchild);
77         s.delete(grandchild2);
78         s.delete(grandchild3);
79         s.delete(child);
80         s.delete(child2);
81         s.delete(root);
82         tx.commit();
83         s.close();
84     
85     }
86     
87     public void testSaveOrUpdateDeepTreeWithGeneratedId() {
88         
89         boolean instrumented = new NumberedNode() instanceof InterceptFieldEnabled;
90
91         clearCounts();
92         
93         Session s = openSession();
94         Transaction tx = s.beginTransaction();
95         NumberedNode root = new NumberedNode("root");
96         NumberedNode child = new NumberedNode("child");
97         NumberedNode grandchild = new NumberedNode("grandchild");
98         root.addChild(child);
99         child.addChild(grandchild);
100         s.saveOrUpdate(root);
101         tx.commit();
102         s.close();
103         
104         assertInsertCount(3);
105         assertUpdateCount(0);
106         clearCounts();
107         
108         child = (NumberedNode) root.getChildren().iterator().next();
109         grandchild = (NumberedNode) child.getChildren().iterator().next();
110         grandchild.setDescription("the grand child");
111         NumberedNode grandchild2 = new NumberedNode("grandchild2");
112         child.addChild( grandchild2 );
113
114         s = openSession();
115         tx = s.beginTransaction();
116         s.saveOrUpdate(root);
117         tx.commit();
118         s.close();
119         
120         assertInsertCount(1);
121         assertUpdateCount(instrumented ? 1 : 3);
122         clearCounts();
123         
124         NumberedNode child2 = new NumberedNode("child2");
125         NumberedNode grandchild3 = new NumberedNode("grandchild3");
126         child2.addChild( grandchild3 );
127         root.addChild(child2);
128         
129         s = openSession();
130         tx = s.beginTransaction();
131         s.saveOrUpdate(root);
132         tx.commit();
133         s.close();
134         
135         assertInsertCount(2);
136         assertUpdateCount(instrumented ? 0 : 4);
137         clearCounts();
138         
139         s = openSession();
140         tx = s.beginTransaction();
141         s.createQuery("delete from NumberedNode where name like 'grand%'").executeUpdate();
142         s.createQuery("delete from NumberedNode where name like 'child%'").executeUpdate();
143         s.createQuery("delete from NumberedNode").executeUpdate();
144         tx.commit();
145         s.close();
146     
147     }
148     
149     public void testSaveOrUpdateTree() {
150         
151         clearCounts();
152         
153         Session s = openSession();
154         Transaction tx = s.beginTransaction();
155         Node root = new Node("root");
156         Node child = new Node("child");
157         root.addChild(child);
158         s.saveOrUpdate(root);
159         tx.commit();
160         s.close();
161         
162         assertInsertCount(2);
163         clearCounts();
164         
165         root.setDescription("The root node");
166         child.setDescription("The child node");
167         
168         Node secondChild = new Node("second child");
169         
170         root.addChild(secondChild);
171         
172         s = openSession();
173         tx = s.beginTransaction();
174         s.saveOrUpdate(root);
175         tx.commit();
176         s.close();
177         
178         assertInsertCount(1);
179         assertUpdateCount(2);
180         
181         s = openSession();
182         tx = s.beginTransaction();
183         s.createQuery("delete from Node where parent is not null").executeUpdate();
184         s.createQuery("delete from Node").executeUpdate();
185         tx.commit();
186         s.close();
187         
188     }
189         
190     public void testSaveOrUpdateTreeWithGeneratedId() {
191         
192         clearCounts();
193         
194         Session s = openSession();
195         Transaction tx = s.beginTransaction();
196         NumberedNode root = new NumberedNode("root");
197         NumberedNode child = new NumberedNode("child");
198         root.addChild(child);
199         s.saveOrUpdate(root);
200         tx.commit();
201         s.close();
202         
203         assertInsertCount(2);
204         clearCounts();
205         
206         root.setDescription("The root node");
207         child.setDescription("The child node");
208         
209         NumberedNode secondChild = new NumberedNode("second child");
210         
211         root.addChild(secondChild);
212         
213         s = openSession();
214         tx = s.beginTransaction();
215         s.saveOrUpdate(root);
216         tx.commit();
217         s.close();
218         
219         assertInsertCount(1);
220         assertUpdateCount(2);
221         
222         s = openSession();
223         tx = s.beginTransaction();
224         s.createQuery("delete from NumberedNode where parent is not null").executeUpdate();
225         s.createQuery("delete from NumberedNode").executeUpdate();
226         tx.commit();
227         s.close();
228
229     }
230         
231     public void testSaveOrUpdateManaged() {
232         
233         Session s = openSession();
234         Transaction tx = s.beginTransaction();
235         NumberedNode root = new NumberedNode("root");
236         s.saveOrUpdate(root);
237         tx.commit();
238         
239         tx = s.beginTransaction();
240         NumberedNode child = new NumberedNode("child");
241         root.addChild(child);
242         s.saveOrUpdate(root);
243         assertFalse( s.contains(child) );
244         s.flush();
245         assertTrue( s.contains(child) );
246         tx.commit();
247         
248         assertTrue( root.getChildren().contains(child) );
249         assertEquals( root.getChildren().size(), 1 );
250         
251         tx = s.beginTransaction();
252         assertEquals(
253             s.createCriteria(NumberedNode.class)
254                 .setProjection(Projections.rowCount())
255                 .uniqueResult(),
256             new Integer JavaDoc(2)
257         );
258         s.delete(root);
259         s.delete(child);
260         tx.commit();
261         s.close();
262         
263     }
264     
265     
266     public void testSaveOrUpdateGot() {
267         
268         boolean instrumented = new NumberedNode() instanceof InterceptFieldEnabled;
269
270         Session s = openSession();
271         Transaction tx = s.beginTransaction();
272         NumberedNode root = new NumberedNode("root");
273         s.saveOrUpdate(root);
274         tx.commit();
275         s.close();
276         
277         assertInsertCount(1);
278         assertUpdateCount(0);
279         clearCounts();
280         
281         s = openSession();
282         tx = s.beginTransaction();
283         s.saveOrUpdate(root);
284         tx.commit();
285         s.close();
286         
287         assertInsertCount(0);
288         assertUpdateCount( instrumented ? 0 : 1 );
289         
290         s = openSession();
291         tx = s.beginTransaction();
292         root = (NumberedNode) s.get( NumberedNode.class, new Long JavaDoc(root.getId()) );
293         Hibernate.initialize( root.getChildren() );
294         tx.commit();
295         s.close();
296         
297         clearCounts();
298         
299         s = openSession();
300         tx = s.beginTransaction();
301         NumberedNode child = new NumberedNode("child");
302         root.addChild(child);
303         s.saveOrUpdate(root);
304         assertTrue( s.contains(child) );
305         tx.commit();
306         
307         assertInsertCount(1);
308         assertUpdateCount( instrumented ? 0 : 1 );
309         
310         tx = s.beginTransaction();
311         assertEquals(
312             s.createCriteria(NumberedNode.class)
313                 .setProjection(Projections.rowCount())
314                 .uniqueResult(),
315             new Integer JavaDoc(2)
316         );
317         s.delete(root);
318         s.delete(child);
319         tx.commit();
320         s.close();
321         
322     }
323     
324     public void testSaveOrUpdateGotWithMutableProp() {
325         
326         Session s = openSession();
327         Transaction tx = s.beginTransaction();
328         Node root = new Node("root");
329         s.saveOrUpdate(root);
330         tx.commit();
331         s.close();
332         
333         assertInsertCount(1);
334         assertUpdateCount(0);
335         clearCounts();
336         
337         s = openSession();
338         tx = s.beginTransaction();
339         s.saveOrUpdate(root);
340         tx.commit();
341         s.close();
342         
343         assertInsertCount(0);
344         assertUpdateCount(0);
345         
346         s = openSession();
347         tx = s.beginTransaction();
348         root = (Node) s.get( Node.class, "root" );
349         Hibernate.initialize( root.getChildren() );
350         tx.commit();
351         s.close();
352         
353         clearCounts();
354         
355         s = openSession();
356         tx = s.beginTransaction();
357         Node child = new Node("child");
358         root.addChild(child);
359         s.saveOrUpdate(root);
360         assertTrue( s.contains(child) );
361         tx.commit();
362         
363         assertInsertCount(1);
364         assertUpdateCount(1); //note: will fail here if no second-level cache
365

366         tx = s.beginTransaction();
367         assertEquals(
368             s.createCriteria(Node.class)
369                 .setProjection(Projections.rowCount())
370                 .uniqueResult(),
371             new Integer JavaDoc(2)
372         );
373         s.delete(root);
374         s.delete(child);
375         tx.commit();
376         s.close();
377         
378     }
379     
380     private void clearCounts() {
381         getSessions().getStatistics().clear();
382     }
383     
384     private void assertInsertCount(int count) {
385         int inserts = (int) getSessions().getStatistics().getEntityInsertCount();
386         assertEquals(count, inserts);
387     }
388         
389     private void assertUpdateCount(int count) {
390         int updates = (int) getSessions().getStatistics().getEntityUpdateCount();
391         assertEquals(count, updates);
392     }
393         
394     protected void configure(Configuration cfg) {
395         cfg.setProperty(Environment.GENERATE_STATISTICS, "true");
396         cfg.setProperty(Environment.STATEMENT_BATCH_SIZE, "0");
397     }
398     
399     protected String JavaDoc[] getMappings() {
400         return new String JavaDoc[] { "ops/Node.hbm.xml" };
401     }
402
403     public static Test suite() {
404         return new TestSuite(SaveOrUpdateTest.class);
405     }
406
407 }
408
409
Popular Tags