~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/cascade/MultiPathCascadeTest.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
 
//$Id: $
2
 
/*
3
 
 * Hibernate, Relational Persistence for Idiomatic Java
4
 
 *
5
 
 * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
6
 
 * indicated by the @author tags or express copyright attribution
7
 
 * statements applied by the authors.  All third-party contributions are
8
 
 * distributed under license by Red Hat Middleware LLC.
9
 
 *
10
 
 * This copyrighted material is made available to anyone wishing to use, modify,
11
 
 * copy, or redistribute it subject to the terms and conditions of the GNU
12
 
 * Lesser General Public License, as published by the Free Software Foundation.
13
 
 *
14
 
 * This program is distributed in the hope that it will be useful,
15
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16
 
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
17
 
 * for more details.
18
 
 *
19
 
 * You should have received a copy of the GNU Lesser General Public License
20
 
 * along with this distribution; if not, write to:
21
 
 * Free Software Foundation, Inc.
22
 
 * 51 Franklin Street, Fifth Floor
23
 
 * Boston, MA  02110-1301  USA
24
 
 *
25
 
 */
26
 
 
27
 
package org.hibernate.test.cascade;
28
 
 
29
 
import junit.framework.Test;
30
 
 
31
 
import org.hibernate.Session;
32
 
import org.hibernate.TransientObjectException;
33
 
import org.hibernate.junit.functional.FunctionalTestCase;
34
 
import org.hibernate.junit.functional.FunctionalTestClassTestSuite;
35
 
import org.hibernate.proxy.HibernateProxy;
36
 
 
37
 
/**
38
 
 * @author <a href="mailto:ovidiu@feodorov.com">Ovidiu Feodorov</a>
39
 
 * @author Gail Badner
40
 
 *
41
 
 */
42
 
 
43
 
public class MultiPathCascadeTest extends FunctionalTestCase {
44
 
 
45
 
        public MultiPathCascadeTest(String name) {
46
 
                super( name );
47
 
        }
48
 
 
49
 
        public String[] getMappings() {
50
 
                return new String[] {
51
 
                                "cascade/MultiPathCascade.hbm.xml"
52
 
                };
53
 
        }
54
 
 
55
 
        public static Test suite() {
56
 
                return new FunctionalTestClassTestSuite( MultiPathCascadeTest.class );
57
 
        }
58
 
 
59
 
        protected void cleanupTest() {
60
 
                Session s = openSession();
61
 
                s.beginTransaction();
62
 
                s.createQuery( "delete from A" );
63
 
                s.createQuery( "delete from G" );
64
 
                s.createQuery( "delete from H" );
65
 
        }
66
 
 
67
 
        public void testMultiPathMergeModifiedDetached() throws Exception
68
 
        {
69
 
                // persist a simple A in the database
70
 
 
71
 
                Session s = openSession();
72
 
                s.beginTransaction();
73
 
                A a = new A();
74
 
                a.setData( "Anna" );
75
 
                s.save( a );
76
 
                s.getTransaction().commit();
77
 
                s.close();
78
 
 
79
 
                // modify detached entity
80
 
                modifyEntity( a );
81
 
 
82
 
                s = openSession();
83
 
                s.beginTransaction();
84
 
                a = ( A ) s.merge( a );
85
 
                s.getTransaction().commit();
86
 
                s.close();
87
 
 
88
 
                verifyModifications( a.getId() );
89
 
        }
90
 
 
91
 
        public void testMultiPathMergeModifiedDetachedIntoProxy() throws Exception
92
 
        {
93
 
                // persist a simple A in the database
94
 
 
95
 
                Session s = openSession();
96
 
                s.beginTransaction();
97
 
                A a = new A();
98
 
                a.setData( "Anna" );
99
 
                s.save( a );
100
 
                s.getTransaction().commit();
101
 
                s.close();
102
 
 
103
 
                // modify detached entity
104
 
                modifyEntity( a );
105
 
 
106
 
                s = openSession();
107
 
                s.beginTransaction();
108
 
                A aLoaded = ( A ) s.load( A.class, new Long( a.getId() ) );
109
 
                assertTrue( aLoaded instanceof HibernateProxy );
110
 
                assertSame( aLoaded, s.merge( a ) );
111
 
                s.getTransaction().commit();
112
 
                s.close();
113
 
 
114
 
                verifyModifications( a.getId() );
115
 
        }
116
 
 
117
 
        public void testMultiPathUpdateModifiedDetached() throws Exception
118
 
        {
119
 
                // persist a simple A in the database
120
 
 
121
 
                Session s = openSession();
122
 
                s.beginTransaction();
123
 
                A a = new A();
124
 
                a.setData( "Anna" );
125
 
                s.save( a );
126
 
                s.getTransaction().commit();
127
 
                s.close();
128
 
 
129
 
                // modify detached entity
130
 
                modifyEntity( a );
131
 
 
132
 
                s = openSession();
133
 
                s.beginTransaction();
134
 
                s.update( a );
135
 
                s.getTransaction().commit();
136
 
                s.close();
137
 
 
138
 
                verifyModifications( a.getId() );
139
 
        }
140
 
 
141
 
        public void testMultiPathGetAndModify() throws Exception
142
 
        {
143
 
                // persist a simple A in the database
144
 
 
145
 
                Session s = openSession();
146
 
                s.beginTransaction();
147
 
                A a = new A();
148
 
                a.setData( "Anna" );
149
 
                s.save( a );
150
 
                s.getTransaction().commit();
151
 
                s.close();
152
 
 
153
 
                s = openSession();
154
 
                s.beginTransaction();
155
 
                // retrieve the previously saved instance from the database, and update it
156
 
                a = ( A ) s.get( A.class, new Long( a.getId() ) );
157
 
                modifyEntity( a );
158
 
                s.getTransaction().commit();
159
 
                s.close();
160
 
 
161
 
                verifyModifications( a.getId() );
162
 
        }
163
 
 
164
 
        public void testMultiPathMergeNonCascadedTransientEntityInCollection() throws Exception
165
 
        {
166
 
                // persist a simple A in the database
167
 
 
168
 
                Session s = openSession();
169
 
                s.beginTransaction();
170
 
                A a = new A();
171
 
                a.setData( "Anna" );
172
 
                s.save( a );
173
 
                s.getTransaction().commit();
174
 
                s.close();
175
 
 
176
 
                // modify detached entity
177
 
                modifyEntity( a );
178
 
 
179
 
                s = openSession();
180
 
                s.beginTransaction();
181
 
                a = ( A ) s.merge( a );
182
 
                s.getTransaction().commit();
183
 
                s.close();
184
 
 
185
 
                verifyModifications( a.getId() );
186
 
 
187
 
                // add a new (transient) G to collection in h
188
 
                // there is no cascade from H to the collection, so this should fail when merged
189
 
                assertEquals( 1, a.getHs().size() );
190
 
                H h = ( H ) a.getHs().iterator().next();
191
 
                G gNew = new G();
192
 
                gNew.setData( "Gail" );
193
 
                gNew.getHs().add( h );
194
 
                h.getGs().add( gNew );
195
 
 
196
 
                s = openSession();
197
 
                s.beginTransaction();
198
 
                try {
199
 
                        s.merge( a );
200
 
                        s.merge( h );
201
 
                        fail( "should have thrown TransientObjectException" );
202
 
                }
203
 
                catch ( TransientObjectException ex ) {
204
 
                        // expected
205
 
                }
206
 
                finally {
207
 
                        s.getTransaction().rollback();
208
 
                }
209
 
                s.close();
210
 
        }
211
 
 
212
 
        public void testMultiPathMergeNonCascadedTransientEntityInOneToOne() throws Exception
213
 
        {
214
 
                // persist a simple A in the database
215
 
 
216
 
                Session s = openSession();
217
 
                s.beginTransaction();
218
 
                A a = new A();
219
 
                a.setData( "Anna" );
220
 
                s.save( a );
221
 
                s.getTransaction().commit();
222
 
                s.close();
223
 
 
224
 
                // modify detached entity
225
 
                modifyEntity( a );
226
 
 
227
 
                s = openSession();
228
 
                s.beginTransaction();
229
 
                a = ( A ) s.merge( a );
230
 
                s.getTransaction().commit();
231
 
                s.close();
232
 
 
233
 
                verifyModifications( a.getId() );
234
 
 
235
 
                // change the one-to-one association from g to be a new (transient) A
236
 
                // there is no cascade from G to A, so this should fail when merged
237
 
                G g = a.getG();
238
 
                a.setG( null );
239
 
                A aNew = new A();
240
 
                aNew.setData( "Alice" );
241
 
                g.setA( aNew );
242
 
                aNew.setG( g );
243
 
 
244
 
                s = openSession();
245
 
                s.beginTransaction();
246
 
                try {
247
 
                        s.merge( a );
248
 
                        s.merge( g );
249
 
                        fail( "should have thrown TransientObjectException" );
250
 
                }
251
 
                catch ( TransientObjectException ex ) {
252
 
                        // expected
253
 
                }
254
 
                finally {
255
 
                        s.getTransaction().rollback();
256
 
                }
257
 
                s.close();
258
 
        }
259
 
 
260
 
        public void testMultiPathMergeNonCascadedTransientEntityInManyToOne() throws Exception
261
 
        {
262
 
                // persist a simple A in the database
263
 
 
264
 
                Session s = openSession();
265
 
                s.beginTransaction();
266
 
                A a = new A();
267
 
                a.setData( "Anna" );
268
 
                s.save( a );
269
 
                s.getTransaction().commit();
270
 
                s.close();
271
 
 
272
 
                // modify detached entity
273
 
                modifyEntity( a );
274
 
 
275
 
                s = openSession();
276
 
                s.beginTransaction();
277
 
                a = ( A ) s.merge( a );
278
 
                s.getTransaction().commit();
279
 
                s.close();
280
 
 
281
 
                verifyModifications( a.getId() );
282
 
 
283
 
                // change the many-to-one association from h to be a new (transient) A
284
 
                // there is no cascade from H to A, so this should fail when merged
285
 
                assertEquals( 1, a.getHs().size() );
286
 
                H h = ( H ) a.getHs().iterator().next();
287
 
                a.getHs().remove( h );
288
 
                A aNew = new A();
289
 
                aNew.setData( "Alice" );
290
 
                aNew.addH( h );
291
 
 
292
 
                s = openSession();
293
 
                s.beginTransaction();
294
 
                try {
295
 
                        s.merge( a );
296
 
                        s.merge( h );
297
 
                        fail( "should have thrown TransientObjectException" );
298
 
                }
299
 
                catch ( TransientObjectException ex ) {
300
 
                        // expected
301
 
                }
302
 
                finally {
303
 
                        s.getTransaction().rollback();
304
 
                }
305
 
                s.close();
306
 
        }
307
 
 
308
 
        private void modifyEntity(A a) {
309
 
                // create a *circular* graph in detached entity
310
 
                a.setData("Anthony");
311
 
 
312
 
                G g = new G();
313
 
                g.setData( "Giovanni" );
314
 
 
315
 
                H h = new H();
316
 
                h.setData( "Hellen" );
317
 
 
318
 
                a.setG( g );
319
 
                g.setA( a );
320
 
 
321
 
                a.getHs().add( h );
322
 
                h.setA( a );
323
 
 
324
 
                g.getHs().add( h );
325
 
                h.getGs().add( g );
326
 
        }
327
 
 
328
 
        private void verifyModifications(long aId) {
329
 
                Session s = openSession();
330
 
                s.beginTransaction();
331
 
 
332
 
                // retrieve the A object and check it
333
 
                A a = ( A ) s.get( A.class, new Long( aId ) );
334
 
                assertEquals( aId, a.getId() );
335
 
                assertEquals( "Anthony", a.getData() );
336
 
                assertNotNull( a.getG() );
337
 
                assertNotNull( a.getHs() );
338
 
                assertEquals( 1, a.getHs().size() );
339
 
 
340
 
                G gFromA = a.getG();
341
 
                H hFromA = ( H ) a.getHs().iterator().next();
342
 
 
343
 
                // check the G object
344
 
                assertEquals( "Giovanni", gFromA.getData() );
345
 
                assertSame( a, gFromA.getA() );
346
 
                assertNotNull( gFromA.getHs() );
347
 
                assertEquals( a.getHs(), gFromA.getHs() );
348
 
                assertSame( hFromA, gFromA.getHs().iterator().next() );
349
 
 
350
 
                // check the H object
351
 
                assertEquals( "Hellen", hFromA.getData() );
352
 
                assertSame( a, hFromA.getA() );
353
 
                assertNotNull( hFromA.getGs() );
354
 
                assertEquals( 1, hFromA.getGs().size() );
355
 
                assertSame( gFromA, hFromA.getGs().iterator().next() );
356
 
 
357
 
                s.getTransaction().commit();
358
 
                s.close();
359
 
        }
360
 
 
361
 
}