~ubuntu-branches/ubuntu/wily/libhibernate3-java/wily-proposed

« back to all changes in this revision

Viewing changes to test/org/hibernate/test/ops/CreateTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2007-10-14 14:43:34 UTC
  • Revision ID: james.westby@ubuntu.com-20071014144334-eamc8i0q10gs1aro
Tags: upstream-3.2.5
ImportĀ upstreamĀ versionĀ 3.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//$Id: CreateTest.java 10976 2006-12-12 23:22:26Z steve.ebersole@jboss.com $
 
2
package org.hibernate.test.ops;
 
3
 
 
4
import java.util.ArrayList;
 
5
import java.util.Collection;
 
6
 
 
7
import junit.framework.Test;
 
8
 
 
9
import org.hibernate.PersistentObjectException;
 
10
import org.hibernate.Session;
 
11
import org.hibernate.Transaction;
 
12
import org.hibernate.junit.functional.FunctionalTestClassTestSuite;
 
13
import org.hibernate.exception.ConstraintViolationException;
 
14
 
 
15
/**
 
16
 * @author Gavin King
 
17
 */
 
18
public class CreateTest extends AbstractOperationTestCase {
 
19
 
 
20
        public CreateTest(String str) {
 
21
                super( str );
 
22
        }
 
23
 
 
24
        public static Test suite() {
 
25
                return new FunctionalTestClassTestSuite( CreateTest.class );
 
26
        }
 
27
 
 
28
        public void testNoUpdatesOnCreateVersionedWithCollection() {
 
29
                clearCounts();
 
30
 
 
31
                Session s = openSession();
 
32
                Transaction tx = s.beginTransaction();
 
33
                VersionedEntity root = new VersionedEntity( "root", "root" );
 
34
                VersionedEntity child = new VersionedEntity( "c1", "child-1" );
 
35
                root.getChildren().add( child );
 
36
                child.setParent( root );
 
37
                s.save(root);
 
38
                tx.commit();
 
39
                s.close();
 
40
 
 
41
                assertInsertCount( 2 );
 
42
                assertUpdateCount( 0 );
 
43
                assertDeleteCount( 0 );
 
44
 
 
45
                s = openSession();
 
46
                tx = s.beginTransaction();
 
47
                s.delete( root );
 
48
                tx.commit();
 
49
                s.close();
 
50
 
 
51
                assertUpdateCount( 0 );
 
52
                assertDeleteCount( 2 );
 
53
        }
 
54
 
 
55
        public void testCreateTree() {
 
56
 
 
57
                clearCounts();
 
58
 
 
59
                Session s = openSession();
 
60
                Transaction tx = s.beginTransaction();
 
61
                Node root = new Node("root");
 
62
                Node child = new Node("child");
 
63
                root.addChild(child);
 
64
                s.persist(root);
 
65
                tx.commit();
 
66
                s.close();
 
67
 
 
68
                assertInsertCount(2);
 
69
                assertUpdateCount(0);
 
70
 
 
71
                s = openSession();
 
72
                tx = s.beginTransaction();
 
73
                System.out.println("getting");
 
74
                root = (Node) s.get(Node.class, "root");
 
75
                Node child2 = new Node("child2");
 
76
                root.addChild(child2);
 
77
                System.out.println("committing");
 
78
                tx.commit();
 
79
                s.close();
 
80
 
 
81
                assertInsertCount(3);
 
82
                assertUpdateCount(0);
 
83
        }
 
84
 
 
85
        public void testCreateTreeWithGeneratedId() {
 
86
 
 
87
                clearCounts();
 
88
 
 
89
                Session s = openSession();
 
90
                Transaction tx = s.beginTransaction();
 
91
                NumberedNode root = new NumberedNode("root");
 
92
                NumberedNode child = new NumberedNode("child");
 
93
                root.addChild(child);
 
94
                s.persist(root);
 
95
                tx.commit();
 
96
                s.close();
 
97
 
 
98
                assertInsertCount(2);
 
99
                assertUpdateCount(0);
 
100
 
 
101
                s = openSession();
 
102
                tx = s.beginTransaction();
 
103
                root = (NumberedNode) s.get( NumberedNode.class, new Long( root.getId() ) );
 
104
                NumberedNode child2 = new NumberedNode("child2");
 
105
                root.addChild(child2);
 
106
                tx.commit();
 
107
                s.close();
 
108
 
 
109
                assertInsertCount(3);
 
110
                assertUpdateCount(0);
 
111
        }
 
112
 
 
113
        public void testCreateException() {
 
114
                Session s = openSession();
 
115
                Transaction tx = s.beginTransaction();
 
116
                Node dupe = new Node("dupe");
 
117
                s.persist(dupe);
 
118
                s.persist(dupe);
 
119
                tx.commit();
 
120
                s.close();
 
121
 
 
122
                s = openSession();
 
123
                tx = s.beginTransaction();
 
124
                s.persist(dupe);
 
125
                try {
 
126
                        tx.commit();
 
127
                        assertFalse(true);
 
128
                }
 
129
                catch (ConstraintViolationException cve) {
 
130
                        //verify that an exception is thrown!
 
131
                }
 
132
                tx.rollback();
 
133
                s.close();
 
134
 
 
135
                Node nondupe = new Node("nondupe");
 
136
                nondupe.addChild(dupe);
 
137
 
 
138
                s = openSession();
 
139
                tx = s.beginTransaction();
 
140
                s.persist(nondupe);
 
141
                try {
 
142
                        tx.commit();
 
143
                        assertFalse(true);
 
144
                }
 
145
                catch (ConstraintViolationException cve) {
 
146
                        //verify that an exception is thrown!
 
147
                }
 
148
                tx.rollback();
 
149
                s.close();
 
150
        }
 
151
 
 
152
        public void testCreateExceptionWithGeneratedId() {
 
153
                Session s = openSession();
 
154
                Transaction tx = s.beginTransaction();
 
155
                NumberedNode dupe = new NumberedNode("dupe");
 
156
                s.persist(dupe);
 
157
                s.persist(dupe);
 
158
                tx.commit();
 
159
                s.close();
 
160
 
 
161
                s = openSession();
 
162
                tx = s.beginTransaction();
 
163
                try {
 
164
                        s.persist(dupe);
 
165
                        assertFalse(true);
 
166
                }
 
167
                catch (PersistentObjectException poe) {
 
168
                        //verify that an exception is thrown!
 
169
                }
 
170
                tx.rollback();
 
171
                s.close();
 
172
 
 
173
                NumberedNode nondupe = new NumberedNode("nondupe");
 
174
                nondupe.addChild(dupe);
 
175
 
 
176
                s = openSession();
 
177
                tx = s.beginTransaction();
 
178
                try {
 
179
                        s.persist(nondupe);
 
180
                        assertFalse(true);
 
181
                }
 
182
                catch (PersistentObjectException poe) {
 
183
                        //verify that an exception is thrown!
 
184
                }
 
185
                tx.rollback();
 
186
                s.close();
 
187
        }
 
188
 
 
189
        public void testBasic() throws Exception {
 
190
                Session s;
 
191
                Transaction tx;
 
192
                s = openSession();
 
193
                tx = s.beginTransaction();
 
194
                Employer er = new Employer();
 
195
                Employee ee = new Employee();
 
196
                s.persist(ee);
 
197
                Collection erColl = new ArrayList();
 
198
                Collection eeColl = new ArrayList();
 
199
                erColl.add(ee);
 
200
                eeColl.add(er);
 
201
                er.setEmployees(erColl);
 
202
                ee.setEmployers(eeColl);
 
203
                tx.commit();
 
204
                s.close();
 
205
 
 
206
                s = openSession();
 
207
                tx = s.beginTransaction();
 
208
                er = (Employer) s.load(Employer.class, er.getId() );
 
209
                assertNotNull(er);
 
210
                assertNotNull( er.getEmployees() );
 
211
                assertEquals( 1, er.getEmployees().size() );
 
212
                Employee eeFromDb = (Employee) er.getEmployees().iterator().next();
 
213
                assertEquals( ee.getId(), eeFromDb.getId() );
 
214
                tx.commit();
 
215
                s.close();
 
216
        }
 
217
}
 
218