Skip to content

Commit 0b6ea75

Browse files
committed
HHH-7573 : Re-work test case to be similar to org.hibernate.test.instrument tests
1 parent 8653c31 commit 0b6ea75

File tree

8 files changed

+447
-108
lines changed

8 files changed

+447
-108
lines changed

hibernate-entitymanager/hibernate-entitymanager.gradle

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,3 @@ task testJar(type: Jar, dependsOn: testClasses) {
112112
artifacts {
113113
tests testJar
114114
}
115-
116-
task instrument(dependsOn: compileTestJava) << {
117-
ant.taskdef(name: 'hibInstrument',
118-
classname: 'org.hibernate.tool.instrument.javassist.InstrumentTask',
119-
classpath: configurations.testCompile.asPath)
120-
ant.hibInstrument(verbose: 'true'){
121-
fileset(dir:"$buildDir/classes/test/org/hibernate/jpa/test/callbacks/"){
122-
include(name: "EntityWithLazyProperty.class")
123-
}
124-
}
125-
println("hibernate instrumentation")
126-
}
127-
128-
test.dependsOn instrument

hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/callbacks/CallbacksTest.java

Lines changed: 1 addition & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,11 @@
1313

1414
import org.junit.Test;
1515

16-
import org.hibernate.Hibernate;
17-
import org.hibernate.internal.util.collections.ArrayHelper;
1816
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
1917
import org.hibernate.jpa.test.Cat;
2018
import org.hibernate.jpa.test.Kitten;
2119

2220
import org.hibernate.testing.FailureExpected;
23-
import org.hibernate.testing.TestForIssue;
2421

2522
import static org.junit.Assert.assertEquals;
2623
import static org.junit.Assert.assertFalse;
@@ -254,96 +251,7 @@ public Class[] getAnnotatedClasses() {
254251
RemoteControl.class,
255252
Rythm.class,
256253
Plant.class,
257-
Kitten.class,
258-
EntityWithLazyProperty.class
254+
Kitten.class
259255
};
260256
}
261-
262-
/**
263-
* Test for HHH-7573.
264-
* Load some test data into an entity which has a lazy property and a @PreUpdate callback, then reload and update a
265-
* non lazy field which will trigger the PreUpdate lifecycle callback.
266-
* @throws Exception
267-
*/
268-
@Test
269-
@TestForIssue( jiraKey = "HHH-7573" )
270-
public void testJpaFlushEntityEventListener() throws Exception {
271-
EntityWithLazyProperty entity;
272-
EntityManager em = getOrCreateEntityManager();
273-
274-
byte[] testArray = new byte[]{0x2A};
275-
276-
//persist the test entity.
277-
em.getTransaction().begin();
278-
entity = new EntityWithLazyProperty();
279-
entity.setSomeField("TEST");
280-
entity.setLazyData(testArray);
281-
em.persist(entity);
282-
em.getTransaction().commit();
283-
em.close();
284-
285-
checkLazyField(entity, em, testArray);
286-
287-
/**
288-
* Set a non lazy field, therefore the lazyData field will be LazyPropertyInitializer.UNFETCHED_PROPERTY
289-
* for both state and newState so the field should not change. This should no longer cause a ClassCastException.
290-
*/
291-
em = getOrCreateEntityManager();
292-
em.getTransaction().begin();
293-
entity = em.find(EntityWithLazyProperty.class, entity.getId());
294-
entity.setSomeField("TEST1");
295-
assertFalse( Hibernate.isPropertyInitialized( entity, "lazyData" ) );
296-
em.getTransaction().commit();
297-
assertFalse( Hibernate.isPropertyInitialized( entity, "lazyData" ) );
298-
em.close();
299-
300-
checkLazyField(entity, em, testArray);
301-
302-
/**
303-
* Set the updateLazyFieldInPreUpdate flag so that the lazy field is updated from within the
304-
* PreUpdate annotated callback method. So state == LazyPropertyInitializer.UNFETCHED_PROPERTY and
305-
* newState == EntityWithLazyProperty.PRE_UPDATE_VALUE. This should no longer cause a ClassCastException.
306-
*/
307-
em = getOrCreateEntityManager();
308-
em.getTransaction().begin();
309-
entity = em.find(EntityWithLazyProperty.class, entity.getId());
310-
entity.setUpdateLazyFieldInPreUpdate(true);
311-
entity.setSomeField("TEST2");
312-
assertFalse( Hibernate.isPropertyInitialized( entity, "lazyData" ) );
313-
em.getTransaction().commit();
314-
assertTrue( Hibernate.isPropertyInitialized( entity, "lazyData" ) );
315-
em.close();
316-
317-
checkLazyField(entity, em, EntityWithLazyProperty.PRE_UPDATE_VALUE);
318-
319-
/**
320-
* Set the updateLazyFieldInPreUpdate flag so that the lazy field is updated from within the
321-
* PreUpdate annotated callback method and also set the lazyData field directly to testArray1. When we reload we
322-
* should get EntityWithLazyProperty.PRE_UPDATE_VALUE.
323-
*/
324-
em = getOrCreateEntityManager();
325-
em.getTransaction().begin();
326-
entity = em.find(EntityWithLazyProperty.class, entity.getId());
327-
entity.setUpdateLazyFieldInPreUpdate(true);
328-
assertFalse( Hibernate.isPropertyInitialized( entity, "lazyData" ) );
329-
entity.setLazyData(testArray);
330-
assertTrue( Hibernate.isPropertyInitialized( entity, "lazyData" ) );
331-
entity.setSomeField("TEST3");
332-
em.getTransaction().commit();
333-
em.close();
334-
335-
checkLazyField( entity, em, EntityWithLazyProperty.PRE_UPDATE_VALUE);
336-
}
337-
338-
private void checkLazyField(EntityWithLazyProperty entity, EntityManager em, byte[] expected) {
339-
// reload the entity and check the lazy value matches what we expect.
340-
em = getOrCreateEntityManager();
341-
em.getTransaction().begin();
342-
entity = em.find(EntityWithLazyProperty.class, entity.getId());
343-
assertFalse( Hibernate.isPropertyInitialized( entity, "lazyData") );
344-
assertTrue( ArrayHelper.isEquals( expected, entity.getLazyData() ) );
345-
assertTrue( Hibernate.isPropertyInitialized( entity, "lazyData" ) );
346-
em.getTransaction().commit();
347-
em.close();
348-
}
349257
}
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.jpa.test.instrument.cases;
8+
9+
import java.net.URL;
10+
import java.util.ArrayList;
11+
import java.util.Arrays;
12+
import java.util.Collection;
13+
import java.util.List;
14+
import java.util.Map;
15+
import java.util.Properties;
16+
import javax.persistence.EntityManager;
17+
import javax.persistence.SharedCacheMode;
18+
import javax.persistence.ValidationMode;
19+
import javax.persistence.spi.PersistenceUnitTransactionType;
20+
21+
import org.hibernate.bytecode.spi.InstrumentedClassLoader;
22+
import org.hibernate.cfg.Environment;
23+
import org.hibernate.dialect.Dialect;
24+
import org.hibernate.jpa.AvailableSettings;
25+
import org.hibernate.jpa.HibernateEntityManagerFactory;
26+
import org.hibernate.jpa.HibernatePersistenceProvider;
27+
import org.hibernate.jpa.boot.spi.Bootstrap;
28+
import org.hibernate.jpa.boot.spi.PersistenceUnitDescriptor;
29+
30+
/**
31+
* @author Steve Ebersole
32+
* @author Gail Badner
33+
*/
34+
public abstract class AbstractExecutable implements Executable {
35+
private static final Dialect dialect = Dialect.getDialect();
36+
private HibernateEntityManagerFactory entityManagerFactory;
37+
private EntityManager em;
38+
39+
@Override
40+
public final void prepare() {
41+
// make sure we pick up the TCCL, and make sure its the isolated CL...
42+
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
43+
if ( classLoader == null ) {
44+
throw new RuntimeException( "Isolated ClassLoader not yet set as TCCL" );
45+
}
46+
if ( !InstrumentedClassLoader.class.isInstance( classLoader ) ) {
47+
throw new RuntimeException( "Isolated ClassLoader not yet set as TCCL" );
48+
}
49+
50+
entityManagerFactory = Bootstrap.getEntityManagerFactoryBuilder(
51+
buildPersistenceUnitDescriptor( getClass().getSimpleName() ),
52+
buildSettings(),
53+
classLoader
54+
).build().unwrap( HibernateEntityManagerFactory.class );
55+
}
56+
57+
@Override
58+
public final void complete() {
59+
try {
60+
cleanup();
61+
}
62+
finally {
63+
if ( em != null && em.isOpen() ) {
64+
em.close();
65+
}
66+
em = null;
67+
entityManagerFactory.close();
68+
entityManagerFactory = null;
69+
}
70+
}
71+
72+
protected EntityManager getOrCreateEntityManager() {
73+
if ( em == null || !em.isOpen() ) {
74+
em = entityManagerFactory.createEntityManager();
75+
}
76+
return em;
77+
}
78+
79+
protected void cleanup() {
80+
}
81+
82+
private Map buildSettings() {
83+
Map<Object, Object> settings = Environment.getProperties();
84+
ArrayList<Class> classes = new ArrayList<Class>();
85+
classes.addAll( Arrays.asList( getAnnotatedClasses() ) );
86+
settings.put( AvailableSettings.LOADED_CLASSES, classes );
87+
settings.put( org.hibernate.cfg.AvailableSettings.HBM2DDL_AUTO, "create-drop" );
88+
settings.put( org.hibernate.cfg.AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS, "true" );
89+
settings.put( org.hibernate.cfg.AvailableSettings.DIALECT, dialect.getClass().getName() );
90+
return settings;
91+
}
92+
93+
private PersistenceUnitDescriptor buildPersistenceUnitDescriptor(final String puName) {
94+
return new PersistenceUnitDescriptor() {
95+
private final String name = puName;
96+
97+
@Override public URL getPersistenceUnitRootUrl() {
98+
return null;
99+
}
100+
101+
@Override
102+
public String getName() {
103+
return name;
104+
}
105+
106+
@Override
107+
public String getProviderClassName() {
108+
return HibernatePersistenceProvider.class.getName();
109+
}
110+
111+
@Override
112+
public boolean isUseQuotedIdentifiers() {
113+
return false;
114+
}
115+
116+
@Override
117+
public boolean isExcludeUnlistedClasses() {
118+
return false;
119+
}
120+
121+
@Override
122+
public PersistenceUnitTransactionType getTransactionType() {
123+
return null;
124+
}
125+
126+
@Override
127+
public ValidationMode getValidationMode() {
128+
return null;
129+
}
130+
131+
@Override
132+
public SharedCacheMode getSharedCacheMode() {
133+
return null;
134+
}
135+
136+
@Override
137+
public List<String> getManagedClassNames() {
138+
return null;
139+
}
140+
141+
@Override
142+
public List<String> getMappingFileNames() {
143+
return null;
144+
}
145+
146+
@Override
147+
public List<URL> getJarFileUrls() {
148+
return null;
149+
}
150+
151+
@Override
152+
public Object getNonJtaDataSource() {
153+
return null;
154+
}
155+
156+
@Override
157+
public Object getJtaDataSource() {
158+
return null;
159+
}
160+
161+
@Override
162+
public Properties getProperties() {
163+
return null;
164+
}
165+
166+
@Override
167+
public ClassLoader getClassLoader() {
168+
return null;
169+
}
170+
171+
@Override
172+
public ClassLoader getTempClassLoader() {
173+
return null;
174+
}
175+
176+
@Override
177+
public void pushClassTransformer(Collection<String> entityClassNames) {
178+
}
179+
};
180+
}
181+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.jpa.test.instrument.cases;
8+
9+
import java.util.Map;
10+
11+
/**
12+
* @author Steve Ebersole
13+
*/
14+
public interface Executable {
15+
public void prepare();
16+
public void execute() throws Exception;
17+
public void complete();
18+
public Class[] getAnnotatedClasses();
19+
}

0 commit comments

Comments
 (0)