KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: CreateTest.java,v 1.9 2005/06/19 02:01:05 oneovthafew Exp $
2
package org.hibernate.test.ops;
3
4 import java.util.ArrayList JavaDoc;
5 import java.util.Collection JavaDoc;
6
7 import junit.framework.Test;
8 import junit.framework.TestSuite;
9
10 import org.hibernate.PersistentObjectException;
11 import org.hibernate.Session;
12 import org.hibernate.Transaction;
13 import org.hibernate.cfg.Configuration;
14 import org.hibernate.cfg.Environment;
15 import org.hibernate.exception.ConstraintViolationException;
16 import org.hibernate.test.TestCase;
17
18 /**
19  * @author Gavin King
20  */

21 public class CreateTest extends TestCase {
22     
23     public CreateTest(String JavaDoc str) {
24         super(str);
25     }
26     
27     public void testCreateTree() {
28         
29         clearCounts();
30         
31         Session s = openSession();
32         Transaction tx = s.beginTransaction();
33         Node root = new Node("root");
34         Node child = new Node("child");
35         root.addChild(child);
36         s.persist(root);
37         tx.commit();
38         s.close();
39         
40         assertInsertCount(2);
41         assertUpdateCount(0);
42
43         s = openSession();
44         tx = s.beginTransaction();
45         System.out.println("getting");
46         root = (Node) s.get(Node.class, "root");
47         Node child2 = new Node("child2");
48         root.addChild(child2);
49         System.out.println("committing");
50         tx.commit();
51         s.close();
52         
53         assertInsertCount(3);
54         assertUpdateCount(0);
55     }
56     
57     public void testCreateTreeWithGeneratedId() {
58         
59         clearCounts();
60         
61         Session s = openSession();
62         Transaction tx = s.beginTransaction();
63         NumberedNode root = new NumberedNode("root");
64         NumberedNode child = new NumberedNode("child");
65         root.addChild(child);
66         s.persist(root);
67         tx.commit();
68         s.close();
69         
70         assertInsertCount(2);
71         assertUpdateCount(0);
72
73         s = openSession();
74         tx = s.beginTransaction();
75         root = (NumberedNode) s.get( NumberedNode.class, new Long JavaDoc( root.getId() ) );
76         NumberedNode child2 = new NumberedNode("child2");
77         root.addChild(child2);
78         tx.commit();
79         s.close();
80         
81         assertInsertCount(3);
82         assertUpdateCount(0);
83     }
84     
85     public void testCreateException() {
86         Session s = openSession();
87         Transaction tx = s.beginTransaction();
88         Node dupe = new Node("dupe");
89         s.persist(dupe);
90         s.persist(dupe);
91         tx.commit();
92         s.close();
93         
94         s = openSession();
95         tx = s.beginTransaction();
96         s.persist(dupe);
97         try {
98             tx.commit();
99             assertFalse(true);
100         }
101         catch (ConstraintViolationException cve) {
102             //verify that an exception is thrown!
103
}
104         tx.rollback();
105         s.close();
106         
107         Node nondupe = new Node("nondupe");
108         nondupe.addChild(dupe);
109         
110         s = openSession();
111         tx = s.beginTransaction();
112         s.persist(nondupe);
113         try {
114             tx.commit();
115             assertFalse(true);
116         }
117         catch (ConstraintViolationException cve) {
118             //verify that an exception is thrown!
119
}
120         tx.rollback();
121         s.close();
122     }
123     
124     public void testCreateExceptionWithGeneratedId() {
125         Session s = openSession();
126         Transaction tx = s.beginTransaction();
127         NumberedNode dupe = new NumberedNode("dupe");
128         s.persist(dupe);
129         s.persist(dupe);
130         tx.commit();
131         s.close();
132         
133         s = openSession();
134         tx = s.beginTransaction();
135         try {
136             s.persist(dupe);
137             assertFalse(true);
138         }
139         catch (PersistentObjectException poe) {
140             //verify that an exception is thrown!
141
}
142         tx.rollback();
143         s.close();
144         
145         NumberedNode nondupe = new NumberedNode("nondupe");
146         nondupe.addChild(dupe);
147         
148         s = openSession();
149         tx = s.beginTransaction();
150         try {
151             s.persist(nondupe);
152             assertFalse(true);
153         }
154         catch (PersistentObjectException poe) {
155             //verify that an exception is thrown!
156
}
157         tx.rollback();
158         s.close();
159     }
160
161     public void testBasic() throws Exception JavaDoc {
162         Session s;
163         Transaction tx;
164         s = openSession();
165         tx = s.beginTransaction();
166         Employer er = new Employer();
167         Employee ee = new Employee();
168         s.persist(ee);
169         Collection JavaDoc erColl = new ArrayList JavaDoc();
170         Collection JavaDoc eeColl = new ArrayList JavaDoc();
171         erColl.add(ee);
172         eeColl.add(er);
173         er.setEmployees(erColl);
174         ee.setEmployers(eeColl);
175         tx.commit();
176         s.close();
177
178         s = openSession();
179         tx = s.beginTransaction();
180         er = (Employer) s.load(Employer.class, er.getId() );
181         assertNotNull(er);
182         assertNotNull( er.getEmployees() );
183         assertEquals( 1, er.getEmployees().size() );
184         Employee eeFromDb = (Employee) er.getEmployees().iterator().next();
185         assertEquals( ee.getId(), eeFromDb.getId() );
186         tx.commit();
187         s.close();
188     }
189
190     private void clearCounts() {
191         getSessions().getStatistics().clear();
192     }
193     
194     private void assertInsertCount(int count) {
195         int inserts = (int) getSessions().getStatistics().getEntityInsertCount();
196         assertEquals(count, inserts);
197     }
198         
199     private void assertUpdateCount(int count) {
200         int updates = (int) getSessions().getStatistics().getEntityUpdateCount();
201         assertEquals(count, updates);
202     }
203         
204     protected void configure(Configuration cfg) {
205         cfg.setProperty(Environment.GENERATE_STATISTICS, "true");
206         cfg.setProperty(Environment.STATEMENT_BATCH_SIZE, "0");
207     }
208     
209     protected String JavaDoc[] getMappings() {
210         return new String JavaDoc[] {
211             "ops/Node.hbm.xml",
212             "ops/Employer.hbm.xml"
213         };
214     }
215
216     public static Test suite() {
217         return new TestSuite(CreateTest.class);
218     }
219
220     public String JavaDoc getCacheConcurrencyStrategy() {
221         return null;
222     }
223
224 }
225
226
Popular Tags