Skip to content

Commit 87ddd73

Browse files
committed
HHH-9518 : Add tests for new warnings
(cherry picked from commit 5eb4c0b) HHH-9518 : Corrections to test to work pre-5.0
1 parent 0854a04 commit 87ddd73

File tree

1 file changed

+296
-0
lines changed

1 file changed

+296
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,296 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* Copyright (c) 2014, Red Hat Inc. or third-party contributors as
5+
* indicated by the @author tags or express copyright attribution
6+
* statements applied by the authors. All third-party contributions are
7+
* distributed under license by Red Hat Inc.
8+
*
9+
* This copyrighted material is made available to anyone wishing to use, modify,
10+
* copy, or redistribute it subject to the terms and conditions of the GNU
11+
* Lesser General Public License, as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16+
* for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this distribution; if not, write to:
20+
* Free Software Foundation, Inc.
21+
* 51 Franklin Street, Fifth Floor
22+
* Boston, MA 02110-1301 USA
23+
*/
24+
package org.hibernate.test.collection.multisession;
25+
26+
import java.util.HashSet;
27+
import java.util.Set;
28+
import javax.persistence.CascadeType;
29+
import javax.persistence.Entity;
30+
import javax.persistence.GeneratedValue;
31+
import javax.persistence.Id;
32+
import javax.persistence.JoinColumn;
33+
import javax.persistence.OneToMany;
34+
import javax.persistence.Table;
35+
36+
import org.junit.Test;
37+
38+
import org.hibernate.Session;
39+
import org.hibernate.collection.internal.AbstractPersistentCollection;
40+
import org.hibernate.collection.spi.PersistentCollection;
41+
import org.hibernate.engine.spi.CollectionEntry;
42+
import org.hibernate.engine.spi.SessionImplementor;
43+
import org.hibernate.internal.CoreLogging;
44+
import org.hibernate.internal.CoreMessageLogger;
45+
import org.hibernate.testing.TestForIssue;
46+
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
47+
48+
import static org.junit.Assert.assertFalse;
49+
import static org.junit.Assert.assertNotNull;
50+
import static org.junit.Assert.assertSame;
51+
import static org.junit.Assert.assertTrue;
52+
53+
/**
54+
* @author Gail Badner
55+
*/
56+
public class MultipleSessionCollectionWarningTest extends BaseCoreFunctionalTestCase {
57+
private static final CoreMessageLogger LOG = CoreLogging.messageLogger( AbstractPersistentCollection.class );
58+
59+
@Test
60+
@TestForIssue( jiraKey = "HHH-9518" )
61+
public void testSetCurrentSessionOverwritesNonConnectedSesssion() {
62+
Parent p = new Parent();
63+
Child c = new Child();
64+
p.children.add( c );
65+
66+
Session s1 = openSession();
67+
s1.getTransaction().begin();
68+
s1.saveOrUpdate( p );
69+
70+
// Now remove the collection from the PersistenceContext without unsetting its session
71+
// This should never be done in practice; it is done here only to test that the warning
72+
// gets logged. s1 will not function properly so the transaction will ultimately need
73+
// to be rolled-back.
74+
75+
CollectionEntry ce = (CollectionEntry) ( (SessionImplementor) s1 ).getPersistenceContext()
76+
.getCollectionEntries()
77+
.remove( p.children );
78+
assertNotNull( ce );
79+
80+
// the collection session should still be s1; the collection is no longer "connected" because its
81+
// CollectionEntry has been removed.
82+
assertSame( s1, ( (AbstractPersistentCollection) p.children ).getSession() );
83+
84+
Session s2 = openSession();
85+
s2.getTransaction().begin();
86+
87+
// The following should trigger warning because we're setting a new session when the collection already
88+
// has a non-null session (and the collection is not "connected" to that session);
89+
// Since s1 was not flushed, the collection role will not be known (no way to test that other than inspection).
90+
s2.saveOrUpdate( p );
91+
92+
// collection's session should be overwritten with s2
93+
assertSame( s2, ( (AbstractPersistentCollection) p.children ).getSession() );
94+
95+
s2.getTransaction().rollback();
96+
s2.close();
97+
98+
s1.getTransaction().rollback();
99+
s1.close();
100+
}
101+
102+
@Test
103+
@TestForIssue( jiraKey = "HHH-9518" )
104+
public void testSetCurrentSessionOverwritesNonConnectedSesssionFlushed() {
105+
Parent p = new Parent();
106+
Child c = new Child();
107+
p.children.add( c );
108+
109+
Session s1 = openSession();
110+
s1.getTransaction().begin();
111+
s1.saveOrUpdate( p );
112+
113+
// flush the session so that p.children will contain its role
114+
s1.flush();
115+
116+
// Now remove the collection from the PersistenceContext without unsetting its session
117+
// This should never be done in practice; it is done here only to test that the warning
118+
// gets logged. s1 will not function properly so the transaction will ultimately need
119+
// to be rolled-back.
120+
121+
CollectionEntry ce = (CollectionEntry) ( (SessionImplementor) s1 ).getPersistenceContext()
122+
.getCollectionEntries()
123+
.remove( p.children );
124+
assertNotNull( ce );
125+
126+
// the collection session should still be s1; the collection is no longer "connected" because its
127+
// CollectionEntry has been removed.
128+
assertSame( s1, ( (AbstractPersistentCollection) p.children ).getSession() );
129+
130+
Session s2 = openSession();
131+
s2.getTransaction().begin();
132+
133+
// The following should trigger warning because we're setting a new session when the collection already
134+
// has a non-null session (and the collection is not "connected" to that session);
135+
// The collection role and key should be included in the message (no way to test that other than inspection).
136+
s2.saveOrUpdate( p );
137+
138+
// collection's session should be overwritten with s2
139+
assertSame( s2, ( (AbstractPersistentCollection) p.children ).getSession() );
140+
141+
s2.getTransaction().rollback();
142+
s2.close();
143+
144+
s1.getTransaction().rollback();
145+
s1.close();
146+
}
147+
148+
@Test
149+
@TestForIssue( jiraKey = "HHH-9518" )
150+
public void testUnsetSessionCannotOverwriteNonConnectedSesssion() {
151+
Parent p = new Parent();
152+
Child c = new Child();
153+
p.children.add( c );
154+
155+
Session s1 = openSession();
156+
s1.getTransaction().begin();
157+
s1.saveOrUpdate( p );
158+
159+
// Now remove the collection from the PersistenceContext without unsetting its session
160+
// This should never be done in practice; it is done here only to test that the warning
161+
// gets logged. s1 will not function properly so the transaction will ultimately need
162+
// to be rolled-back.
163+
164+
CollectionEntry ce = (CollectionEntry) ( (SessionImplementor) s1 ).getPersistenceContext()
165+
.getCollectionEntries()
166+
.remove( p.children );
167+
assertNotNull( ce );
168+
169+
// the collection session should still be s1; the collection is no longer "connected" because its
170+
// CollectionEntry has been removed.
171+
assertSame( s1, ( (AbstractPersistentCollection) p.children ).getSession() );
172+
173+
Session s2 = openSession();
174+
s2.getTransaction().begin();
175+
176+
// The following should trigger warning because we're unsetting a different session.
177+
// We should not do this in practice; it is done here only to force the warning.
178+
// Since s1 was not flushed, the collection role will not be known (no way to test that).
179+
assertFalse( ( (PersistentCollection) p.children ).unsetSession( (SessionImplementor) s2 ) );
180+
181+
// collection's session should still be s1
182+
assertSame( s1, ( (AbstractPersistentCollection) p.children ).getSession() );
183+
184+
s2.getTransaction().rollback();
185+
s2.close();
186+
187+
s1.getTransaction().rollback();
188+
s1.close();
189+
}
190+
191+
@Test
192+
@TestForIssue( jiraKey = "HHH-9518" )
193+
public void testUnsetSessionCannotOverwriteConnectedSesssion() {
194+
Parent p = new Parent();
195+
Child c = new Child();
196+
p.children.add( c );
197+
198+
Session s1 = openSession();
199+
s1.getTransaction().begin();
200+
s1.saveOrUpdate( p );
201+
202+
// The collection is "connected" to s1 because it contains the CollectionEntry
203+
CollectionEntry ce = ( (SessionImplementor) s1 ).getPersistenceContext()
204+
.getCollectionEntry( (PersistentCollection) p.children );
205+
assertNotNull( ce );
206+
207+
// the collection session should be s1
208+
assertSame( s1, ( (AbstractPersistentCollection) p.children ).getSession() );
209+
210+
Session s2 = openSession();
211+
s2.getTransaction().begin();
212+
213+
// The following should trigger warning because we're unsetting a different session
214+
// We should not do this in practice; it is done here only to force the warning.
215+
// Since s1 was not flushed, the collection role will not be known (no way to test that).
216+
assertFalse( ( (PersistentCollection) p.children ).unsetSession( (SessionImplementor) s2 ) );
217+
218+
// collection's session should still be s1
219+
assertSame( s1, ( (AbstractPersistentCollection) p.children ).getSession() );
220+
221+
s2.getTransaction().rollback();
222+
s2.close();
223+
224+
s1.getTransaction().rollback();
225+
s1.close();
226+
}
227+
228+
@Test
229+
@TestForIssue( jiraKey = "HHH-9518" )
230+
public void testUnsetSessionCannotOverwriteConnectedSesssionFlushed() {
231+
Parent p = new Parent();
232+
Child c = new Child();
233+
p.children.add( c );
234+
235+
Session s1 = openSession();
236+
s1.getTransaction().begin();
237+
s1.saveOrUpdate( p );
238+
239+
// flush the session so that p.children will contain its role
240+
s1.flush();
241+
242+
// The collection is "connected" to s1 because it contains the CollectionEntry
243+
CollectionEntry ce = ( (SessionImplementor) s1 ).getPersistenceContext()
244+
.getCollectionEntry( (PersistentCollection) p.children );
245+
assertNotNull( ce );
246+
247+
// the collection session should be s1
248+
assertSame( s1, ( (AbstractPersistentCollection) p.children ).getSession() );
249+
250+
Session s2 = openSession();
251+
s2.getTransaction().begin();
252+
253+
// The following should trigger warning because we're unsetting a different session
254+
// We should not do this in practice; it is done here only to force the warning.
255+
// The collection role and key should be included in the message (no way to test that other than inspection).
256+
assertFalse( ( (PersistentCollection) p.children ).unsetSession( (SessionImplementor) s2 ) );
257+
258+
// collection's session should still be s1
259+
assertSame( s1, ( (AbstractPersistentCollection) p.children ).getSession() );
260+
261+
s2.getTransaction().rollback();
262+
s2.close();
263+
264+
s1.getTransaction().rollback();
265+
s1.close();
266+
}
267+
268+
@Override
269+
public Class<?>[] getAnnotatedClasses() {
270+
return new Class[] {
271+
Parent.class,
272+
Child.class
273+
};
274+
}
275+
276+
@Entity
277+
@Table(name="Parent")
278+
public static class Parent {
279+
@Id
280+
@GeneratedValue
281+
private Long id;
282+
283+
@OneToMany(cascade = CascadeType.ALL)
284+
@JoinColumn
285+
private Set<Child> children = new HashSet<Child>();
286+
}
287+
288+
@Entity
289+
@Table(name="Child")
290+
public static class Child {
291+
@Id
292+
@GeneratedValue
293+
private Long id;
294+
295+
}
296+
}

0 commit comments

Comments
 (0)