~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/collection/set/PersistentSetTest.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.collection.set;
2
 
 
3
 
import java.util.HashSet;
4
 
 
5
 
import junit.framework.Test;
6
 
 
7
 
import org.hibernate.Session;
8
 
import org.hibernate.cfg.Configuration;
9
 
import org.hibernate.cfg.Environment;
10
 
import org.hibernate.collection.PersistentSet;
11
 
import org.hibernate.junit.functional.FunctionalTestCase;
12
 
import org.hibernate.junit.functional.FunctionalTestClassTestSuite;
13
 
import org.hibernate.stat.CollectionStatistics;
14
 
 
15
 
/**
16
 
 * todo: describe PersistentSetTest
17
 
 *
18
 
 * @author Steve Ebersole
19
 
 */
20
 
public class PersistentSetTest extends FunctionalTestCase {
21
 
        public PersistentSetTest(String name) {
22
 
                super( name );
23
 
        }
24
 
 
25
 
        public String[] getMappings() {
26
 
                return new String[] { "collection/set/Mappings.hbm.xml" };
27
 
        }
28
 
 
29
 
        public static Test suite() {
30
 
                return new FunctionalTestClassTestSuite( PersistentSetTest.class );
31
 
        }
32
 
 
33
 
        public void configure(Configuration cfg) {
34
 
                super.configure( cfg );
35
 
                cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
36
 
        }
37
 
 
38
 
        public void testWriteMethodDirtying() {
39
 
                Parent parent = new Parent( "p1" );
40
 
                Child child = new Child( "c1" );
41
 
                parent.getChildren().add( child );
42
 
                child.setParent( parent );
43
 
                Child otherChild = new Child( "c2" );
44
 
 
45
 
                Session session = openSession();
46
 
                session.beginTransaction();
47
 
                session.save( parent );
48
 
                session.flush();
49
 
                // at this point, the set on parent has now been replaced with a PersistentSet...
50
 
                PersistentSet children = ( PersistentSet ) parent.getChildren();
51
 
 
52
 
                assertFalse( children.add( child ) );
53
 
                assertFalse( children.isDirty() );
54
 
 
55
 
                assertFalse( children.remove( otherChild ) );
56
 
                assertFalse( children.isDirty() );
57
 
 
58
 
                HashSet otherSet = new HashSet();
59
 
                otherSet.add( child );
60
 
                assertFalse( children.addAll( otherSet ) );
61
 
                assertFalse( children.isDirty() );
62
 
 
63
 
                assertFalse( children.retainAll( otherSet ) );
64
 
                assertFalse( children.isDirty() );
65
 
 
66
 
                otherSet = new HashSet();
67
 
                otherSet.add( otherChild );
68
 
                assertFalse( children.removeAll( otherSet ) );
69
 
                assertFalse( children.isDirty() );
70
 
 
71
 
                assertTrue( children.retainAll( otherSet ));
72
 
                assertTrue( children.isDirty() );
73
 
                assertTrue( children.isEmpty() );
74
 
 
75
 
                children.clear();
76
 
                session.delete( child );
77
 
                assertTrue( children.isDirty() );
78
 
 
79
 
                session.flush();
80
 
 
81
 
                children.clear();
82
 
                assertFalse( children.isDirty() );
83
 
 
84
 
                session.delete( parent );
85
 
                session.getTransaction().commit();
86
 
                session.close();
87
 
        }
88
 
 
89
 
        public void testCollectionMerging() {
90
 
                Session session = openSession();
91
 
                session.beginTransaction();
92
 
                Parent parent = new Parent( "p1" );
93
 
                Child child = new Child( "c1" );
94
 
                parent.getChildren().add( child );
95
 
                child.setParent( parent );
96
 
                session.save( parent );
97
 
                session.getTransaction().commit();
98
 
                session.close();
99
 
 
100
 
                CollectionStatistics stats =  sfi().getStatistics().getCollectionStatistics( Parent.class.getName() + ".children" );
101
 
                long recreateCount = stats.getRecreateCount();
102
 
                long updateCount = stats.getUpdateCount();
103
 
 
104
 
                session = openSession();
105
 
                session.beginTransaction();
106
 
                parent = ( Parent ) session.merge( parent );
107
 
                session.getTransaction().commit();
108
 
                session.close();
109
 
 
110
 
                assertEquals( 1, parent.getChildren().size() );
111
 
                assertEquals( recreateCount, stats.getRecreateCount() );
112
 
                assertEquals( updateCount, stats.getUpdateCount() );
113
 
 
114
 
                session = openSession();
115
 
                session.beginTransaction();
116
 
                parent = ( Parent ) session.get( Parent.class, "p1" );
117
 
                assertEquals( 1, parent.getChildren().size() );
118
 
                session.delete( parent );
119
 
                session.getTransaction().commit();
120
 
                session.close();
121
 
        }
122
 
 
123
 
        public void testCollectiondirtyChecking() {
124
 
                Session session = openSession();
125
 
                session.beginTransaction();
126
 
                Parent parent = new Parent( "p1" );
127
 
                Child child = new Child( "c1" );
128
 
                parent.getChildren().add( child );
129
 
                child.setParent( parent );
130
 
                session.save( parent );
131
 
                session.getTransaction().commit();
132
 
                session.close();
133
 
 
134
 
                CollectionStatistics stats =  sfi().getStatistics().getCollectionStatistics( Parent.class.getName() + ".children" );
135
 
                long recreateCount = stats.getRecreateCount();
136
 
                long updateCount = stats.getUpdateCount();
137
 
 
138
 
                session = openSession();
139
 
                session.beginTransaction();
140
 
                parent = ( Parent ) session.get( Parent.class, "p1" );
141
 
                assertEquals( 1, parent.getChildren().size() );
142
 
                session.getTransaction().commit();
143
 
                session.close();
144
 
 
145
 
                assertEquals( 1, parent.getChildren().size() );
146
 
                assertEquals( recreateCount, stats.getRecreateCount() );
147
 
                assertEquals( updateCount, stats.getUpdateCount() );
148
 
 
149
 
                session = openSession();
150
 
                session.beginTransaction();
151
 
                assertEquals( 1, parent.getChildren().size() );
152
 
                session.delete( parent );
153
 
                session.getTransaction().commit();
154
 
                session.close();
155
 
        }
156
 
 
157
 
        public void testCompositeElementWriteMethodDirtying() {
158
 
                Container container = new Container( "p1" );
159
 
                Container.Content c1 = new Container.Content( "c1" );
160
 
                container.getContents().add( c1 );
161
 
                Container.Content c2 = new Container.Content( "c2" );
162
 
 
163
 
                Session session = openSession();
164
 
                session.beginTransaction();
165
 
                session.save( container );
166
 
                session.flush();
167
 
                // at this point, the set on container has now been replaced with a PersistentSet...
168
 
                PersistentSet children = ( PersistentSet ) container.getContents();
169
 
 
170
 
                assertFalse( children.add( c1 ) );
171
 
                assertFalse( children.isDirty() );
172
 
 
173
 
                assertFalse( children.remove( c2 ) );
174
 
                assertFalse( children.isDirty() );
175
 
 
176
 
                HashSet otherSet = new HashSet();
177
 
                otherSet.add( c1 );
178
 
                assertFalse( children.addAll( otherSet ) );
179
 
                assertFalse( children.isDirty() );
180
 
 
181
 
                assertFalse( children.retainAll( otherSet ) );
182
 
                assertFalse( children.isDirty() );
183
 
 
184
 
                otherSet = new HashSet();
185
 
                otherSet.add( c2 );
186
 
                assertFalse( children.removeAll( otherSet ) );
187
 
                assertFalse( children.isDirty() );
188
 
 
189
 
                assertTrue( children.retainAll( otherSet ));
190
 
                assertTrue( children.isDirty() );
191
 
                assertTrue( children.isEmpty() );
192
 
 
193
 
                children.clear();
194
 
                assertTrue( children.isDirty() );
195
 
 
196
 
                session.flush();
197
 
 
198
 
                children.clear();
199
 
                assertFalse( children.isDirty() );
200
 
 
201
 
                session.delete( container );
202
 
                session.getTransaction().commit();
203
 
                session.close();
204
 
        }
205
 
 
206
 
        public void testCompositeElementMergingFailureExpected() {
207
 
                // HHH-2485
208
 
                Session session = openSession();
209
 
                session.beginTransaction();
210
 
                Container container = new Container( "p1" );
211
 
                Container.Content c1 = new Container.Content( "c1" );
212
 
                container.getContents().add( c1 );
213
 
                session.save( container );
214
 
                session.getTransaction().commit();
215
 
                session.close();
216
 
 
217
 
                CollectionStatistics stats =  sfi().getStatistics().getCollectionStatistics( Container.class.getName() + ".contents" );
218
 
                long recreateCount = stats.getRecreateCount();
219
 
                long updateCount = stats.getUpdateCount();
220
 
 
221
 
                container.setName( "another name" );
222
 
 
223
 
                session = openSession();
224
 
                session.beginTransaction();
225
 
                container = ( Container ) session.merge( container );
226
 
                session.getTransaction().commit();
227
 
                session.close();
228
 
 
229
 
                assertEquals( 1, container.getContents().size() );
230
 
                assertEquals( recreateCount, stats.getRecreateCount() );
231
 
                assertEquals( updateCount, stats.getUpdateCount() );
232
 
 
233
 
                session = openSession();
234
 
                session.beginTransaction();
235
 
                container = ( Container ) session.get( Container.class, container.getId() );
236
 
                assertEquals( 1, container.getContents().size() );
237
 
                session.delete( container );
238
 
                session.getTransaction().commit();
239
 
                session.close();
240
 
        }
241
 
 
242
 
        public void testCompositeElementCollectionDirtyCheckingFailureExpected() {
243
 
                // HHH-2485
244
 
                Session session = openSession();
245
 
                session.beginTransaction();
246
 
                Container container = new Container( "p1" );
247
 
                Container.Content c1 = new Container.Content( "c1" );
248
 
                container.getContents().add( c1 );
249
 
                session.save( container );
250
 
                session.getTransaction().commit();
251
 
                session.close();
252
 
 
253
 
                CollectionStatistics stats =  sfi().getStatistics().getCollectionStatistics( Container.class.getName() + ".contents" );
254
 
                long recreateCount = stats.getRecreateCount();
255
 
                long updateCount = stats.getUpdateCount();
256
 
 
257
 
                session = openSession();
258
 
                session.beginTransaction();
259
 
                container = ( Container ) session.get( Container.class, container.getId() );
260
 
                assertEquals( 1, container.getContents().size() );
261
 
                session.getTransaction().commit();
262
 
                session.close();
263
 
 
264
 
                assertEquals( 1, container.getContents().size() );
265
 
                assertEquals( recreateCount, stats.getRecreateCount() );
266
 
                assertEquals( updateCount, stats.getUpdateCount() );
267
 
 
268
 
                session = openSession();
269
 
                session.beginTransaction();
270
 
                container = ( Container ) session.get( Container.class, container.getId() );
271
 
                assertEquals( 1, container.getContents().size() );
272
 
                session.delete( container );
273
 
                session.getTransaction().commit();
274
 
                session.close();
275
 
        }
276
 
}