~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/ops/SaveOrUpdateTest.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: SaveOrUpdateTest.java 10977 2006-12-12 23:28:04Z steve.ebersole@jboss.com $
2
 
package org.hibernate.test.ops;
3
 
 
4
 
import junit.framework.Test;
5
 
 
6
 
import org.hibernate.Hibernate;
7
 
import org.hibernate.Session;
8
 
import org.hibernate.Transaction;
9
 
import org.hibernate.junit.functional.FunctionalTestCase;
10
 
import org.hibernate.junit.functional.FunctionalTestClassTestSuite;
11
 
import org.hibernate.intercept.FieldInterceptionHelper;
12
 
import org.hibernate.cfg.Configuration;
13
 
import org.hibernate.cfg.Environment;
14
 
import org.hibernate.criterion.Projections;
15
 
 
16
 
/**
17
 
 * @author Gavin King
18
 
 */
19
 
public class SaveOrUpdateTest extends FunctionalTestCase {
20
 
 
21
 
        public SaveOrUpdateTest(String str) {
22
 
                super( str );
23
 
        }
24
 
 
25
 
        public void testSaveOrUpdateDeepTree() {
26
 
                clearCounts();
27
 
 
28
 
                Session s = openSession();
29
 
                Transaction tx = s.beginTransaction();
30
 
                Node root = new Node( "root" );
31
 
                Node child = new Node( "child" );
32
 
                Node grandchild = new Node( "grandchild" );
33
 
                root.addChild( child );
34
 
                child.addChild( grandchild );
35
 
                s.saveOrUpdate( root );
36
 
                tx.commit();
37
 
                s.close();
38
 
 
39
 
                assertInsertCount( 3 );
40
 
                assertUpdateCount( 0 );
41
 
                clearCounts();
42
 
 
43
 
                grandchild.setDescription( "the grand child" );
44
 
                Node grandchild2 = new Node( "grandchild2" );
45
 
                child.addChild( grandchild2 );
46
 
 
47
 
                s = openSession();
48
 
                tx = s.beginTransaction();
49
 
                s.saveOrUpdate( root );
50
 
                tx.commit();
51
 
                s.close();
52
 
 
53
 
                assertInsertCount( 1 );
54
 
                assertUpdateCount( 1 );
55
 
                clearCounts();
56
 
 
57
 
                Node child2 = new Node( "child2" );
58
 
                Node grandchild3 = new Node( "grandchild3" );
59
 
                child2.addChild( grandchild3 );
60
 
                root.addChild( child2 );
61
 
 
62
 
                s = openSession();
63
 
                tx = s.beginTransaction();
64
 
                s.saveOrUpdate( root );
65
 
                tx.commit();
66
 
                s.close();
67
 
 
68
 
                assertInsertCount( 2 );
69
 
                assertUpdateCount( 0 );
70
 
                clearCounts();
71
 
 
72
 
                s = openSession();
73
 
                tx = s.beginTransaction();
74
 
                s.delete( grandchild );
75
 
                s.delete( grandchild2 );
76
 
                s.delete( grandchild3 );
77
 
                s.delete( child );
78
 
                s.delete( child2 );
79
 
                s.delete( root );
80
 
                tx.commit();
81
 
                s.close();
82
 
        }
83
 
 
84
 
        public void testSaveOrUpdateDeepTreeWithGeneratedId() {
85
 
                boolean instrumented = FieldInterceptionHelper.isInstrumented( new NumberedNode() );
86
 
                clearCounts();
87
 
 
88
 
                Session s = openSession();
89
 
                Transaction tx = s.beginTransaction();
90
 
                NumberedNode root = new NumberedNode( "root" );
91
 
                NumberedNode child = new NumberedNode( "child" );
92
 
                NumberedNode grandchild = new NumberedNode( "grandchild" );
93
 
                root.addChild( child );
94
 
                child.addChild( grandchild );
95
 
                s.saveOrUpdate( root );
96
 
                tx.commit();
97
 
                s.close();
98
 
 
99
 
                assertInsertCount( 3 );
100
 
                assertUpdateCount( 0 );
101
 
                clearCounts();
102
 
 
103
 
                child = ( NumberedNode ) root.getChildren().iterator().next();
104
 
                grandchild = ( NumberedNode ) child.getChildren().iterator().next();
105
 
                grandchild.setDescription( "the grand child" );
106
 
                NumberedNode grandchild2 = new NumberedNode( "grandchild2" );
107
 
                child.addChild( grandchild2 );
108
 
 
109
 
                s = openSession();
110
 
                tx = s.beginTransaction();
111
 
                s.saveOrUpdate( root );
112
 
                tx.commit();
113
 
                s.close();
114
 
 
115
 
                assertInsertCount( 1 );
116
 
                assertUpdateCount( instrumented ? 1 : 3 );
117
 
                clearCounts();
118
 
 
119
 
                NumberedNode child2 = new NumberedNode( "child2" );
120
 
                NumberedNode grandchild3 = new NumberedNode( "grandchild3" );
121
 
                child2.addChild( grandchild3 );
122
 
                root.addChild( child2 );
123
 
 
124
 
                s = openSession();
125
 
                tx = s.beginTransaction();
126
 
                s.saveOrUpdate( root );
127
 
                tx.commit();
128
 
                s.close();
129
 
 
130
 
                assertInsertCount( 2 );
131
 
                assertUpdateCount( instrumented ? 0 : 4 );
132
 
                clearCounts();
133
 
 
134
 
                s = openSession();
135
 
                tx = s.beginTransaction();
136
 
                s.createQuery( "delete from NumberedNode where name like 'grand%'" ).executeUpdate();
137
 
                s.createQuery( "delete from NumberedNode where name like 'child%'" ).executeUpdate();
138
 
                s.createQuery( "delete from NumberedNode" ).executeUpdate();
139
 
                tx.commit();
140
 
                s.close();
141
 
        }
142
 
 
143
 
        public void testSaveOrUpdateTree() {
144
 
                clearCounts();
145
 
 
146
 
                Session s = openSession();
147
 
                Transaction tx = s.beginTransaction();
148
 
                Node root = new Node( "root" );
149
 
                Node child = new Node( "child" );
150
 
                root.addChild( child );
151
 
                s.saveOrUpdate( root );
152
 
                tx.commit();
153
 
                s.close();
154
 
 
155
 
                assertInsertCount( 2 );
156
 
                clearCounts();
157
 
 
158
 
                root.setDescription( "The root node" );
159
 
                child.setDescription( "The child node" );
160
 
 
161
 
                Node secondChild = new Node( "second child" );
162
 
 
163
 
                root.addChild( secondChild );
164
 
 
165
 
                s = openSession();
166
 
                tx = s.beginTransaction();
167
 
                s.saveOrUpdate( root );
168
 
                tx.commit();
169
 
                s.close();
170
 
 
171
 
                assertInsertCount( 1 );
172
 
                assertUpdateCount( 2 );
173
 
 
174
 
                s = openSession();
175
 
                tx = s.beginTransaction();
176
 
                s.createQuery( "delete from Node where parent is not null" ).executeUpdate();
177
 
                s.createQuery( "delete from Node" ).executeUpdate();
178
 
                tx.commit();
179
 
                s.close();
180
 
        }
181
 
 
182
 
        public void testSaveOrUpdateTreeWithGeneratedId() {
183
 
                clearCounts();
184
 
 
185
 
                Session s = openSession();
186
 
                Transaction tx = s.beginTransaction();
187
 
                NumberedNode root = new NumberedNode( "root" );
188
 
                NumberedNode child = new NumberedNode( "child" );
189
 
                root.addChild( child );
190
 
                s.saveOrUpdate( root );
191
 
                tx.commit();
192
 
                s.close();
193
 
 
194
 
                assertInsertCount( 2 );
195
 
                clearCounts();
196
 
 
197
 
                root.setDescription( "The root node" );
198
 
                child.setDescription( "The child node" );
199
 
 
200
 
                NumberedNode secondChild = new NumberedNode( "second child" );
201
 
 
202
 
                root.addChild( secondChild );
203
 
 
204
 
                s = openSession();
205
 
                tx = s.beginTransaction();
206
 
                s.saveOrUpdate( root );
207
 
                tx.commit();
208
 
                s.close();
209
 
 
210
 
                assertInsertCount( 1 );
211
 
                assertUpdateCount( 2 );
212
 
 
213
 
                s = openSession();
214
 
                tx = s.beginTransaction();
215
 
                s.createQuery( "delete from NumberedNode where parent is not null" ).executeUpdate();
216
 
                s.createQuery( "delete from NumberedNode" ).executeUpdate();
217
 
                tx.commit();
218
 
                s.close();
219
 
        }
220
 
 
221
 
        public void testSaveOrUpdateManaged() {
222
 
                Session s = openSession();
223
 
                Transaction tx = s.beginTransaction();
224
 
                NumberedNode root = new NumberedNode( "root" );
225
 
                s.saveOrUpdate( root );
226
 
                tx.commit();
227
 
 
228
 
                tx = s.beginTransaction();
229
 
                NumberedNode child = new NumberedNode( "child" );
230
 
                root.addChild( child );
231
 
                s.saveOrUpdate( root );
232
 
                assertFalse( s.contains( child ) );
233
 
                s.flush();
234
 
                assertTrue( s.contains( child ) );
235
 
                tx.commit();
236
 
 
237
 
                assertTrue( root.getChildren().contains( child ) );
238
 
                assertEquals( root.getChildren().size(), 1 );
239
 
 
240
 
                tx = s.beginTransaction();
241
 
                assertEquals(
242
 
                                s.createCriteria( NumberedNode.class )
243
 
                                                .setProjection( Projections.rowCount() )
244
 
                                                .uniqueResult(),
245
 
                        new Integer( 2 )
246
 
                );
247
 
                s.delete( root );
248
 
                s.delete( child );
249
 
                tx.commit();
250
 
                s.close();
251
 
        }
252
 
 
253
 
 
254
 
        public void testSaveOrUpdateGot() {
255
 
                boolean instrumented = FieldInterceptionHelper.isInstrumented( new NumberedNode() );
256
 
 
257
 
                Session s = openSession();
258
 
                Transaction tx = s.beginTransaction();
259
 
                NumberedNode root = new NumberedNode( "root" );
260
 
                s.saveOrUpdate( root );
261
 
                tx.commit();
262
 
                s.close();
263
 
 
264
 
                assertInsertCount( 1 );
265
 
                assertUpdateCount( 0 );
266
 
                clearCounts();
267
 
 
268
 
                s = openSession();
269
 
                tx = s.beginTransaction();
270
 
                s.saveOrUpdate( root );
271
 
                tx.commit();
272
 
                s.close();
273
 
 
274
 
                assertInsertCount( 0 );
275
 
                assertUpdateCount( instrumented ? 0 : 1 );
276
 
 
277
 
                s = openSession();
278
 
                tx = s.beginTransaction();
279
 
                root = ( NumberedNode ) s.get( NumberedNode.class, new Long( root.getId() ) );
280
 
                Hibernate.initialize( root.getChildren() );
281
 
                tx.commit();
282
 
                s.close();
283
 
 
284
 
                clearCounts();
285
 
 
286
 
                s = openSession();
287
 
                tx = s.beginTransaction();
288
 
                NumberedNode child = new NumberedNode( "child" );
289
 
                root.addChild( child );
290
 
                s.saveOrUpdate( root );
291
 
                assertTrue( s.contains( child ) );
292
 
                tx.commit();
293
 
 
294
 
                assertInsertCount( 1 );
295
 
                assertUpdateCount( instrumented ? 0 : 1 );
296
 
 
297
 
                tx = s.beginTransaction();
298
 
                assertEquals(
299
 
                                s.createCriteria( NumberedNode.class )
300
 
                                                .setProjection( Projections.rowCount() )
301
 
                                                .uniqueResult(),
302
 
                        new Integer( 2 )
303
 
                );
304
 
                s.delete( root );
305
 
                s.delete( child );
306
 
                tx.commit();
307
 
                s.close();
308
 
        }
309
 
 
310
 
        public void testSaveOrUpdateGotWithMutableProp() {
311
 
                Session s = openSession();
312
 
                Transaction tx = s.beginTransaction();
313
 
                Node root = new Node( "root" );
314
 
                s.saveOrUpdate( root );
315
 
                tx.commit();
316
 
                s.close();
317
 
 
318
 
                assertInsertCount( 1 );
319
 
                assertUpdateCount( 0 );
320
 
                clearCounts();
321
 
 
322
 
                s = openSession();
323
 
                tx = s.beginTransaction();
324
 
                s.saveOrUpdate( root );
325
 
                tx.commit();
326
 
                s.close();
327
 
 
328
 
                assertInsertCount( 0 );
329
 
                assertUpdateCount( 0 );
330
 
 
331
 
                s = openSession();
332
 
                tx = s.beginTransaction();
333
 
                root = ( Node ) s.get( Node.class, "root" );
334
 
                Hibernate.initialize( root.getChildren() );
335
 
                tx.commit();
336
 
                s.close();
337
 
 
338
 
                clearCounts();
339
 
 
340
 
                s = openSession();
341
 
                tx = s.beginTransaction();
342
 
                Node child = new Node( "child" );
343
 
                root.addChild( child );
344
 
                s.saveOrUpdate( root );
345
 
                assertTrue( s.contains( child ) );
346
 
                tx.commit();
347
 
 
348
 
                assertInsertCount( 1 );
349
 
                assertUpdateCount( 1 ); //note: will fail here if no second-level cache
350
 
 
351
 
                tx = s.beginTransaction();
352
 
                assertEquals(
353
 
                                s.createCriteria( Node.class )
354
 
                                                .setProjection( Projections.rowCount() )
355
 
                                                .uniqueResult(),
356
 
                        new Integer( 2 )
357
 
                );
358
 
                s.delete( root );
359
 
                s.delete( child );
360
 
                tx.commit();
361
 
                s.close();
362
 
        }
363
 
 
364
 
        public void testEvictThenSaveOrUpdate() {
365
 
                Session s = openSession();
366
 
                s.getTransaction().begin();
367
 
                Node parent = new Node( "1:parent" );
368
 
                Node child = new Node( "2:child" );
369
 
                Node grandchild = new Node( "3:grandchild" );
370
 
                parent.addChild( child );
371
 
                child.addChild( grandchild );
372
 
                s.saveOrUpdate( parent );
373
 
                s.getTransaction().commit();
374
 
                s.close();
375
 
 
376
 
                Session s1 = openSession();
377
 
                s1.getTransaction().begin();
378
 
                child = ( Node ) s1.load( Node.class, "2:child" );
379
 
                assertTrue( s1.contains( child ) );
380
 
                assertFalse( Hibernate.isInitialized( child ) );
381
 
                assertTrue( s1.contains( child.getParent() ) );
382
 
                assertTrue( Hibernate.isInitialized( child ) );
383
 
                assertFalse( Hibernate.isInitialized( child.getChildren() ) );
384
 
                assertFalse( Hibernate.isInitialized( child.getParent() ) );
385
 
                assertTrue( s1.contains( child ) );
386
 
                s1.evict( child );
387
 
                assertFalse( s1.contains( child ) );
388
 
                assertTrue( s1.contains( child.getParent() ) );
389
 
 
390
 
                Session s2 = openSession();
391
 
                s2.getTransaction().begin();
392
 
                s2.saveOrUpdate( child );
393
 
                assertTrue( s2.contains( child ) );
394
 
                assertFalse( s1.contains( child ) );
395
 
                assertTrue( s2.contains( child.getParent() ) );
396
 
                assertFalse( s1.contains( child.getParent() ) );
397
 
                assertFalse( Hibernate.isInitialized( child.getChildren() ) );
398
 
                assertFalse( Hibernate.isInitialized( child.getParent() ) );
399
 
                assertEquals( 1, child.getChildren().size() );
400
 
                assertEquals( "1:parent", child.getParent().getName() );
401
 
                assertTrue( Hibernate.isInitialized( child.getChildren() ) );
402
 
                assertFalse( Hibernate.isInitialized( child.getParent() ) );
403
 
                assertNull( child.getParent().getDescription() );
404
 
                assertTrue( Hibernate.isInitialized( child.getParent() ) );
405
 
 
406
 
                s1.getTransaction().commit();
407
 
                s2.getTransaction().commit();
408
 
                s1.close();
409
 
                s2.close();
410
 
 
411
 
                s = openSession();
412
 
                s.beginTransaction();
413
 
                s.delete( s.get( Node.class, "3:grandchild" ) );
414
 
                s.delete( s.get( Node.class, "2:child" ) );
415
 
                s.delete( s.get( Node.class, "1:parent" ) );
416
 
                s.getTransaction().commit();
417
 
                s.close();
418
 
        }
419
 
 
420
 
        private void clearCounts() {
421
 
                getSessions().getStatistics().clear();
422
 
        }
423
 
 
424
 
        private void assertInsertCount(int count) {
425
 
                int inserts = ( int ) getSessions().getStatistics().getEntityInsertCount();
426
 
                assertEquals( count, inserts );
427
 
        }
428
 
 
429
 
        private void assertUpdateCount(int count) {
430
 
                int updates = ( int ) getSessions().getStatistics().getEntityUpdateCount();
431
 
                assertEquals( count, updates );
432
 
        }
433
 
 
434
 
        public void configure(Configuration cfg) {
435
 
                cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
436
 
                cfg.setProperty( Environment.STATEMENT_BATCH_SIZE, "0" );
437
 
        }
438
 
 
439
 
        public String[] getMappings() {
440
 
                return new String[] {"ops/Node.hbm.xml"};
441
 
        }
442
 
 
443
 
        public static Test suite() {
444
 
                return new FunctionalTestClassTestSuite( SaveOrUpdateTest.class );
445
 
        }
446
 
 
447
 
}
448