KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: MergeTest.java,v 1.2 2005/07/09 20:04:44 epbernard Exp $
2
package org.hibernate.ejb.test.ops;
3
4 import junit.framework.Test;
5 import junit.framework.TestSuite;
6 import org.hibernate.Session;
7 import org.hibernate.Transaction;
8 import org.hibernate.cfg.Configuration;
9 import org.hibernate.cfg.Environment;
10 import org.hibernate.ejb.test.EJB3TestCase;
11
12 /**
13  * @author Gavin King
14  */

15 public class MergeTest extends EJB3TestCase {
16
17     public MergeTest(String JavaDoc str) {
18         super( str );
19     }
20
21     public void testMergeTree() {
22
23         clearCounts();
24
25         Session s = openSession();
26         Transaction tx = s.beginTransaction();
27         Node root = new Node( "root" );
28         Node child = new Node( "child" );
29         root.addChild( child );
30         s.persist( root );
31         tx.commit();
32         s.close();
33
34         assertInsertCount( 2 );
35         clearCounts();
36
37         root.setDescription( "The root node" );
38         child.setDescription( "The child node" );
39
40         Node secondChild = new Node( "second child" );
41
42         root.addChild( secondChild );
43
44         s = openSession();
45         tx = s.beginTransaction();
46         s.merge( root );
47         tx.commit();
48         s.close();
49
50         assertInsertCount( 1 );
51         assertUpdateCount( 2 );
52
53     }
54
55     public void testMergeTreeWithGeneratedId() {
56
57         clearCounts();
58
59         Session s = openSession();
60         Transaction tx = s.beginTransaction();
61         NumberedNode root = new NumberedNode( "root" );
62         NumberedNode child = new NumberedNode( "child" );
63         root.addChild( child );
64         s.persist( root );
65         tx.commit();
66         s.close();
67
68         assertInsertCount( 2 );
69         clearCounts();
70
71         root.setDescription( "The root node" );
72         child.setDescription( "The child node" );
73
74         NumberedNode secondChild = new NumberedNode( "second child" );
75
76         root.addChild( secondChild );
77
78         s = openSession();
79         tx = s.beginTransaction();
80         s.merge( root );
81         tx.commit();
82         s.close();
83
84         assertInsertCount( 1 );
85         assertUpdateCount( 2 );
86
87     }
88
89     private void clearCounts() {
90         getSessions().getStatistics().clear();
91     }
92
93     private void assertInsertCount(int count) {
94         int inserts = (int) getSessions().getStatistics().getEntityInsertCount();
95         assertEquals( count, inserts );
96     }
97
98     private void assertUpdateCount(int count) {
99         int updates = (int) getSessions().getStatistics().getEntityUpdateCount();
100         assertEquals( count, updates );
101     }
102
103     protected void configure(Configuration cfg) {
104         super.configure( cfg );
105         cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
106         cfg.setProperty( Environment.STATEMENT_BATCH_SIZE, "0" );
107     }
108
109     protected String JavaDoc[] getMappings() {
110         return new String JavaDoc[]{"ops/Node.hbm.xml"};
111     }
112
113     public static Test suite() {
114         return new TestSuite( MergeTest.class );
115     }
116
117 }
118
119
Popular Tags