~raginggoblin/infolog/infolog

« back to all changes in this revision

Viewing changes to InfologServer/lib/hibernate-distribution-3.3.2.GA/project/testsuite/src/test/java/org/hibernate/test/jpa/cascade/CascadeTest.java

  • Committer: Raging Goblin
  • Date: 2013-11-16 16:51:32 UTC
  • Revision ID: raging_goblin-20131116165132-weujnptzc88uy4ah
Mavenized the project, now using shared project InfologSync

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.hibernate.test.jpa.cascade;
2
 
 
3
 
import org.hibernate.test.jpa.AbstractJPATest;
4
 
import org.hibernate.Session;
5
 
import org.hibernate.TransientObjectException;
6
 
import org.hibernate.EntityMode;
7
 
import org.hibernate.HibernateException;
8
 
import org.hibernate.junit.functional.FunctionalTestClassTestSuite;
9
 
import org.hibernate.engine.Status;
10
 
import org.hibernate.persister.entity.EntityPersister;
11
 
import org.hibernate.event.FlushEntityEventListener;
12
 
import org.hibernate.event.FlushEntityEvent;
13
 
import org.hibernate.cfg.Configuration;
14
 
import org.slf4j.Logger;
15
 
import org.slf4j.LoggerFactory;
16
 
import junit.framework.Test;
17
 
 
18
 
/**
19
 
 * According to the JPA spec, persist()ing an entity should throw an exception
20
 
 * when said entity contains a reference to a transient entity through a mapped
21
 
 * association where that association is not marked for cascading the persist
22
 
 * operation.
23
 
 * <p/>
24
 
 * This test-case tests that requirement in the various association style
25
 
 * scenarios such as many-to-one, one-to-one, many-to-one (property-ref),
26
 
 * one-to-one (property-ref).  Additionally, it performs each of these tests
27
 
 * in both generated and assigned identifier usages...
28
 
 *
29
 
 * @author Steve Ebersole
30
 
 */
31
 
public class CascadeTest extends AbstractJPATest {
32
 
 
33
 
        public static final Logger log = LoggerFactory.getLogger( CascadeTest.class );
34
 
 
35
 
        public CascadeTest(String name) {
36
 
                super( name );
37
 
        }
38
 
 
39
 
        public String[] getMappings() {
40
 
                return new String[] { "jpa/cascade/ParentChild.hbm.xml" };
41
 
        }
42
 
 
43
 
        public static Test suite() {
44
 
                return new FunctionalTestClassTestSuite( CascadeTest.class );
45
 
        }
46
 
 
47
 
        public void testManyToOneGeneratedIdsOnSave() {
48
 
                // NOTES: Child defines a many-to-one back to its Parent.  This
49
 
                // association does not define persist cascading (which is natural;
50
 
                // a child should not be able to create its parent).
51
 
                try {
52
 
                        Session s = openSession();
53
 
                        s.beginTransaction();
54
 
                        Parent p = new Parent( "parent" );
55
 
                        Child c = new Child( "child" );
56
 
                        c.setParent( p );
57
 
                        s.save( c );
58
 
                        try {
59
 
                                s.getTransaction().commit();
60
 
                                fail( "expecting TransientObjectException on flush" );
61
 
                        }
62
 
                        catch( TransientObjectException e ) {
63
 
                                // expected result
64
 
                                log.trace( "handled expected exception", e );
65
 
                                s.getTransaction().rollback();
66
 
                        }
67
 
                        finally {
68
 
                                s.close();
69
 
                        }
70
 
                }
71
 
                finally {
72
 
                        cleanupData();
73
 
                }
74
 
        }
75
 
 
76
 
        public void testManyToOneGeneratedIds() {
77
 
                // NOTES: Child defines a many-to-one back to its Parent.  This
78
 
                // association does not define persist cascading (which is natural;
79
 
                // a child should not be able to create its parent).
80
 
                try {
81
 
                        Session s = openSession();
82
 
                        s.beginTransaction();
83
 
                        Parent p = new Parent( "parent" );
84
 
                        Child c = new Child( "child" );
85
 
                        c.setParent( p );
86
 
                        s.persist( c );
87
 
                        try {
88
 
                                s.getTransaction().commit();
89
 
                                fail( "expecting TransientObjectException on flush" );
90
 
                        }
91
 
                        catch( TransientObjectException e ) {
92
 
                                // expected result
93
 
                                log.trace( "handled expected exception", e );
94
 
                                s.getTransaction().rollback();
95
 
                        }
96
 
                        finally {
97
 
                                s.close();
98
 
                        }
99
 
                }
100
 
                finally {
101
 
                        cleanupData();
102
 
                }
103
 
        }
104
 
 
105
 
        public void testManyToOneAssignedIds() {
106
 
                // NOTES: Child defines a many-to-one back to its Parent.  This
107
 
                // association does not define persist cascading (which is natural;
108
 
                // a child should not be able to create its parent).
109
 
                try {
110
 
                        Session s = openSession();
111
 
                        s.beginTransaction();
112
 
                        ParentAssigned p = new ParentAssigned( new Long( 1 ), "parent" );
113
 
                        ChildAssigned c = new ChildAssigned( new Long( 2 ), "child" );
114
 
                        c.setParent( p );
115
 
                        s.persist( c );
116
 
                        try {
117
 
                                s.getTransaction().commit();
118
 
                                fail( "expecting TransientObjectException on flush" );
119
 
                        }
120
 
                        catch( TransientObjectException e ) {
121
 
                                // expected result
122
 
                                log.trace( "handled expected exception", e );
123
 
                                s.getTransaction().rollback();
124
 
                        }
125
 
                        finally {
126
 
                                s.close();
127
 
                        }
128
 
                }
129
 
                finally {
130
 
                        cleanupData();
131
 
                }
132
 
        }
133
 
 
134
 
        public void testOneToOneGeneratedIds() {
135
 
                try {
136
 
                        Session s = openSession();
137
 
                        s.beginTransaction();
138
 
                        Parent p = new Parent( "parent" );
139
 
                        ParentInfo info = new ParentInfo( "xyz" );
140
 
                        p.setInfo( info );
141
 
                        info.setOwner( p );
142
 
                        s.persist( p );
143
 
                        try {
144
 
                                s.getTransaction().commit();
145
 
                                fail( "expecting TransientObjectException on flush" );
146
 
                        }
147
 
                        catch( TransientObjectException e ) {
148
 
                                // expected result
149
 
                                log.trace( "handled expected exception", e );
150
 
                                s.getTransaction().rollback();
151
 
                        }
152
 
                        finally {
153
 
                                s.close();
154
 
                        }
155
 
                }
156
 
                finally {
157
 
                        cleanupData();
158
 
                }
159
 
        }
160
 
 
161
 
        public void testOneToOneAssignedIds() {
162
 
                try {
163
 
                        Session s = openSession();
164
 
                        s.beginTransaction();
165
 
                        ParentAssigned p = new ParentAssigned( new Long( 1 ), "parent" );
166
 
                        ParentInfoAssigned info = new ParentInfoAssigned( "something secret" );
167
 
                        p.setInfo( info );
168
 
                        info.setOwner( p );
169
 
                        s.persist( p );
170
 
                        try {
171
 
                                s.getTransaction().commit();
172
 
                                fail( "expecting TransientObjectException on flush" );
173
 
                        }
174
 
                        catch( TransientObjectException e ) {
175
 
                                // expected result
176
 
                                log.trace( "handled expected exception", e );
177
 
                                s.getTransaction().rollback();
178
 
                        }
179
 
                        finally {
180
 
                                s.close();
181
 
                        }
182
 
                }
183
 
                finally {
184
 
                        cleanupData();
185
 
                }
186
 
        }
187
 
 
188
 
        public void testManyToOnePropertyRefGeneratedIds() {
189
 
                try {
190
 
                        Session s = openSession();
191
 
                        s.beginTransaction();
192
 
                        Parent p = new Parent( "parent" );
193
 
                        Other other = new Other();
194
 
                        other.setOwner( p );
195
 
                        s.persist( other );
196
 
                        try {
197
 
                                s.getTransaction().commit();
198
 
                                fail( "expecting TransientObjectException on flush" );
199
 
                        }
200
 
                        catch( TransientObjectException e ) {
201
 
                                // expected result
202
 
                                log.trace( "handled expected exception", e );
203
 
                                s.getTransaction().rollback();
204
 
                        }
205
 
                        finally {
206
 
                                s.close();
207
 
                        }
208
 
                }
209
 
                finally {
210
 
                        cleanupData();
211
 
                }
212
 
        }
213
 
 
214
 
        public void testManyToOnePropertyRefAssignedIds() {
215
 
                try {
216
 
                        Session s = openSession();
217
 
                        s.beginTransaction();
218
 
                        ParentAssigned p = new ParentAssigned( new Long( 1 ), "parent" );
219
 
                        OtherAssigned other = new OtherAssigned( new Long( 2 ) );
220
 
                        other.setOwner( p );
221
 
                        s.persist( other );
222
 
                        try {
223
 
                                s.getTransaction().commit();
224
 
                                fail( "expecting TransientObjectException on flush" );
225
 
                        }
226
 
                        catch( TransientObjectException e ) {
227
 
                                // expected result
228
 
                                log.trace( "handled expected exception", e );
229
 
                                s.getTransaction().rollback();
230
 
                        }
231
 
                        finally {
232
 
                                s.close();
233
 
                        }
234
 
                }
235
 
                finally {
236
 
                        cleanupData();
237
 
                }
238
 
        }
239
 
 
240
 
        public void testOneToOnePropertyRefGeneratedIds() {
241
 
                try {
242
 
                        Session s = openSession();
243
 
                        s.beginTransaction();
244
 
                        Child c2 = new Child( "c2" );
245
 
                        ChildInfo info = new ChildInfo( "blah blah blah" );
246
 
                        c2.setInfo( info );
247
 
                        info.setOwner( c2 );
248
 
                        s.persist( c2 );
249
 
                        try {
250
 
                                s.getTransaction().commit();
251
 
                                fail( "expecting TransientObjectException on flush" );
252
 
                        }
253
 
                        catch( TransientObjectException e ) {
254
 
                                // expected result
255
 
                                log.trace( "handled expected exception : " + e );
256
 
                                s.getTransaction().rollback();
257
 
                        }
258
 
                        finally {
259
 
                                s.close();
260
 
                        }
261
 
                }
262
 
                finally {
263
 
                        cleanupData();
264
 
                }
265
 
        }
266
 
 
267
 
        public void testOneToOnePropertyRefAssignedIds() {
268
 
                try {
269
 
                        Session s = openSession();
270
 
                        s.beginTransaction();
271
 
                        ChildAssigned c2 = new ChildAssigned( new Long( 3 ), "c3" );
272
 
                        ChildInfoAssigned info = new ChildInfoAssigned( new Long( 4 ), "blah blah blah" );
273
 
                        c2.setInfo( info );
274
 
                        info.setOwner( c2 );
275
 
                        s.persist( c2 );
276
 
                        try {
277
 
                                s.getTransaction().commit();
278
 
                                fail( "expecting TransientObjectException on flush" );
279
 
                        }
280
 
                        catch( TransientObjectException e ) {
281
 
                                // expected result
282
 
                                log.trace( "handled expected exception : " + e );
283
 
                                s.getTransaction().rollback();
284
 
                        }
285
 
                        finally {
286
 
                                s.close();
287
 
                        }
288
 
                }
289
 
                finally {
290
 
                        cleanupData();
291
 
                }
292
 
        }
293
 
 
294
 
 
295
 
        private void cleanupData() {
296
 
                Session s = null;
297
 
                try {
298
 
                        s = openSession();
299
 
                        s.beginTransaction();
300
 
                        s.createQuery( "delete ChildInfoAssigned" ).executeUpdate();
301
 
                        s.createQuery( "delete ChildAssigned" ).executeUpdate();
302
 
                        s.createQuery( "delete ParentAssigned" ).executeUpdate();
303
 
                        s.createQuery( "delete ChildInfoAssigned" ).executeUpdate();
304
 
                        s.createQuery( "delete ChildAssigned" ).executeUpdate();
305
 
                        s.createQuery( "delete ParentAssigned" ).executeUpdate();
306
 
                        s.getTransaction().commit();
307
 
                }
308
 
                catch( Throwable t ) {
309
 
                        log.warn( "unable to cleanup test data [" + fullTestName() + "] : " + t );
310
 
                }
311
 
                finally {
312
 
                        if ( s != null ) {
313
 
                                try {
314
 
                                        s.close();
315
 
                                }
316
 
                                catch( Throwable ignore ) {
317
 
                                }
318
 
                        }
319
 
                }
320
 
        }
321
 
}