KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: MergeTest.java,v 1.12 2005/07/16 22:28:30 oneovthafew Exp $
2
package org.hibernate.test.ops;
3
4 import java.util.ArrayList JavaDoc;
5
6 import junit.framework.Test;
7 import junit.framework.TestSuite;
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 MergeTest extends TestCase {
21     
22     public MergeTest(String JavaDoc str) {
23         super(str);
24     }
25     
26     public void testMergeDeepTree() {
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.merge(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.merge(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.merge(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 testMergeDeepTreeWithGeneratedId() {
88         
89         clearCounts();
90         
91         Session s = openSession();
92         Transaction tx = s.beginTransaction();
93         NumberedNode root = new NumberedNode("root");
94         NumberedNode child = new NumberedNode("child");
95         NumberedNode grandchild = new NumberedNode("grandchild");
96         root.addChild(child);
97         child.addChild(grandchild);
98         root = (NumberedNode) s.merge(root);
99         tx.commit();
100         s.close();
101         
102         assertInsertCount(3);
103         assertUpdateCount(0);
104         clearCounts();
105         
106         child = (NumberedNode) root.getChildren().iterator().next();
107         grandchild = (NumberedNode) child.getChildren().iterator().next();
108         grandchild.setDescription("the grand child");
109         NumberedNode grandchild2 = new NumberedNode("grandchild2");
110         child.addChild( grandchild2 );
111
112         s = openSession();
113         tx = s.beginTransaction();
114         root = (NumberedNode) s.merge(root);
115         tx.commit();
116         s.close();
117         
118         assertInsertCount(1);
119         assertUpdateCount(1);
120         clearCounts();
121         
122         NumberedNode child2 = new NumberedNode("child2");
123         NumberedNode grandchild3 = new NumberedNode("grandchild3");
124         child2.addChild( grandchild3 );
125         root.addChild(child2);
126         
127         s = openSession();
128         tx = s.beginTransaction();
129         root = (NumberedNode) s.merge(root);
130         tx.commit();
131         s.close();
132         
133         assertInsertCount(2);
134         assertUpdateCount(0);
135         clearCounts();
136         
137         s = openSession();
138         tx = s.beginTransaction();
139         s.createQuery("delete from NumberedNode where name like 'grand%'").executeUpdate();
140         s.createQuery("delete from NumberedNode where name like 'child%'").executeUpdate();
141         s.createQuery("delete from NumberedNode").executeUpdate();
142         tx.commit();
143         s.close();
144     
145     }
146     
147     public void testMergeTree() {
148         
149         clearCounts();
150         
151         Session s = openSession();
152         Transaction tx = s.beginTransaction();
153         Node root = new Node("root");
154         Node child = new Node("child");
155         root.addChild(child);
156         s.persist(root);
157         tx.commit();
158         s.close();
159         
160         assertInsertCount(2);
161         clearCounts();
162         
163         root.setDescription("The root node");
164         child.setDescription("The child node");
165         
166         Node secondChild = new Node("second child");
167         
168         root.addChild(secondChild);
169         
170         s = openSession();
171         tx = s.beginTransaction();
172         s.merge(root);
173         tx.commit();
174         s.close();
175         
176         assertInsertCount(1);
177         assertUpdateCount(2);
178         
179         s = openSession();
180         tx = s.beginTransaction();
181         s.createQuery("delete from Node where parent is not null").executeUpdate();
182         s.createQuery("delete from Node").executeUpdate();
183         tx.commit();
184         s.close();
185         
186     }
187         
188     public void testMergeTreeWithGeneratedId() {
189         
190         clearCounts();
191         
192         Session s = openSession();
193         Transaction tx = s.beginTransaction();
194         NumberedNode root = new NumberedNode("root");
195         NumberedNode child = new NumberedNode("child");
196         root.addChild(child);
197         s.persist(root);
198         tx.commit();
199         s.close();
200         
201         assertInsertCount(2);
202         clearCounts();
203         
204         root.setDescription("The root node");
205         child.setDescription("The child node");
206         
207         NumberedNode secondChild = new NumberedNode("second child");
208         
209         root.addChild(secondChild);
210         
211         s = openSession();
212         tx = s.beginTransaction();
213         s.merge(root);
214         tx.commit();
215         s.close();
216         
217         assertInsertCount(1);
218         assertUpdateCount(2);
219         
220         s = openSession();
221         tx = s.beginTransaction();
222         s.createQuery("delete from NumberedNode where parent is not null").executeUpdate();
223         s.createQuery("delete from NumberedNode").executeUpdate();
224         tx.commit();
225         s.close();
226
227     }
228         
229     public void testMergeManaged() {
230         
231         Session s = openSession();
232         Transaction tx = s.beginTransaction();
233         NumberedNode root = new NumberedNode("root");
234         s.persist(root);
235         tx.commit();
236         
237         clearCounts();
238         
239         tx = s.beginTransaction();
240         NumberedNode child = new NumberedNode("child");
241         root.addChild(child);
242         assertSame( root, s.merge(root) );
243         Object JavaDoc mergedChild = root.getChildren().iterator().next();
244         assertNotSame( mergedChild, child );
245         assertTrue( s.contains(mergedChild) );
246         assertFalse( s.contains(child) );
247         assertEquals( root.getChildren().size(), 1 );
248         assertTrue( root.getChildren().contains(mergedChild) );
249         //assertNotSame( mergedChild, s.merge(child) ); //yucky :(
250
tx.commit();
251         
252         assertInsertCount(1);
253         assertUpdateCount(0);
254         
255         assertEquals( root.getChildren().size(), 1 );
256         assertTrue( root.getChildren().contains(mergedChild) );
257         
258         tx = s.beginTransaction();
259         assertEquals(
260             s.createCriteria(NumberedNode.class)
261                 .setProjection(Projections.rowCount())
262                 .uniqueResult(),
263             new Integer JavaDoc(2)
264         );
265         s.delete(root);
266         s.delete(mergedChild);
267         tx.commit();
268         s.close();
269         
270     }
271     
272     public void testRecursiveMergeTransient() {
273         Session s = openSession();
274         Transaction tx = s.beginTransaction();
275         Employer jboss = new Employer();
276         Employee gavin = new Employee();
277         jboss.setEmployees( new ArrayList JavaDoc() );
278         jboss.getEmployees().add(gavin);
279         s.merge(jboss);
280         s.flush();
281         jboss = (Employer) s.createQuery("from Employer e join fetch e.employees").uniqueResult();
282         assertTrue( Hibernate.isInitialized( jboss.getEmployees() ) );
283         assertEquals( 1, jboss.getEmployees().size() );
284         tx.commit();
285         s.close();
286     }
287     
288     private void clearCounts() {
289         getSessions().getStatistics().clear();
290     }
291     
292     private void assertInsertCount(int count) {
293         int inserts = (int) getSessions().getStatistics().getEntityInsertCount();
294         assertEquals(count, inserts);
295     }
296         
297     private void assertUpdateCount(int count) {
298         int updates = (int) getSessions().getStatistics().getEntityUpdateCount();
299         assertEquals(count, updates);
300     }
301         
302     protected void configure(Configuration cfg) {
303         cfg.setProperty(Environment.GENERATE_STATISTICS, "true");
304         cfg.setProperty(Environment.STATEMENT_BATCH_SIZE, "0");
305     }
306     
307     protected String JavaDoc[] getMappings() {
308         return new String JavaDoc[] { "ops/Node.hbm.xml", "ops/Employer.hbm.xml" };
309     }
310
311     public static Test suite() {
312         return new TestSuite(MergeTest.class);
313     }
314
315 }
316
317
Popular Tags