Skip to content

Commit cff4cb4

Browse files
committed
HHH-9985 - bytecode enhancement - add testcase, and for HHH-10017 as well
1 parent c622d39 commit cff4cb4

File tree

10 files changed

+476
-19
lines changed

10 files changed

+476
-19
lines changed

hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/DecompileUtils.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ public static void decompileDumpedClass(String workingDir, String className) {
102102
assertTrue( methodNames.contains( EnhancerConstants.TRACKER_GET_NAME ) );
103103
assertTrue( methodNames.contains( EnhancerConstants.TRACKER_CLEAR_NAME ) );
104104
assertTrue( methodNames.contains( EnhancerConstants.TRACKER_HAS_CHANGED_NAME ) );
105+
assertTrue( methodNames.contains( EnhancerConstants.TRACKER_SUSPEND_NAME ) );
106+
assertTrue( methodNames.contains( EnhancerConstants.TRACKER_COLLECTION_GET_NAME ) );
105107
}
106108
if ( interfaceNames.contains( CompositeTracker.class.getName() ) ) {
107109
assertTrue( fieldNames.contains( EnhancerConstants.TRACKER_COMPOSITE_FIELD_NAME ) );

hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/EnhancerTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
import org.hibernate.test.bytecode.enhancement.lazy.LazyBasicFieldNotInitializedTestTask;
2424
import org.hibernate.test.bytecode.enhancement.lazy.LazyLoadingIntegrationTestTask;
2525
import org.hibernate.test.bytecode.enhancement.lazy.LazyLoadingTestTask;
26+
import org.hibernate.test.bytecode.enhancement.lazy.basic.LazyBasicFieldAccessTestTask;
27+
import org.hibernate.test.bytecode.enhancement.lazy.basic.LazyBasicPropertyAccessTestTask;
28+
import org.hibernate.test.bytecode.enhancement.merge.CompositeMergeTestTask;
2629
import org.junit.Test;
2730

2831
/**
@@ -51,8 +54,17 @@ public void testAssociation() {
5154
public void testLazy() {
5255
EnhancerTestUtils.runEnhancerTestTask( LazyLoadingTestTask.class );
5356
EnhancerTestUtils.runEnhancerTestTask( LazyLoadingIntegrationTestTask.class );
57+
58+
EnhancerTestUtils.runEnhancerTestTask( LazyBasicPropertyAccessTestTask.class );
59+
EnhancerTestUtils.runEnhancerTestTask( LazyBasicFieldAccessTestTask.class );
5460
}
5561

62+
@Test
63+
public void testMerge() {
64+
EnhancerTestUtils.runEnhancerTestTask( CompositeMergeTestTask.class );
65+
}
66+
67+
5668
@Test
5769
public void testFieldAccess() {
5870
EnhancerTestUtils.runEnhancerTestTask( FieldAccessEnhancementTestTask.class );

hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/EnhancerTestUtils.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,10 @@
99
import java.io.BufferedInputStream;
1010
import java.io.ByteArrayInputStream;
1111
import java.io.File;
12+
import java.io.FileOutputStream;
1213
import java.io.IOException;
1314
import java.io.InputStream;
1415
import java.lang.reflect.Field;
15-
import java.nio.file.Files;
16-
import java.nio.file.Path;
17-
import java.nio.file.Paths;
18-
import java.nio.file.StandardOpenOption;
1916
import java.util.Arrays;
2017

2118
import javassist.ClassPool;
@@ -150,9 +147,12 @@ public Class<?> loadClass(String name) throws ClassNotFoundException {
150147

151148
final byte[] enhanced = new Enhancer( enhancementContext ).enhance( name, original );
152149

153-
Path debugOutput = Paths.get( workingDir + File.separator + name.replace( '.', '/' ) + ".class" );
154-
Files.createDirectories( debugOutput.getParent() );
155-
Files.write( debugOutput, enhanced, StandardOpenOption.CREATE );
150+
File f = new File( workingDir + File.separator + name.replace( ".", File.separator ) + ".class" );
151+
f.getParentFile().mkdirs();
152+
f.createNewFile();
153+
FileOutputStream out = new FileOutputStream( f );
154+
out.write( enhanced );
155+
out.close();
156156

157157
return defineClass( name, enhanced, 0, enhanced.length );
158158
}

hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/dirty/DirtyTrackingTestTask.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,19 @@ public void execute() {
3030
SimpleEntity entity = new SimpleEntity();
3131

3232
// Basic single field
33-
entity.getId();
33+
entity.getSomeNumber();
3434
EnhancerTestUtils.checkDirtyTracking( entity );
35-
entity.setId( 1l );
36-
EnhancerTestUtils.checkDirtyTracking( entity, "id" );
35+
entity.setSomeNumber( 1l );
36+
EnhancerTestUtils.checkDirtyTracking( entity, "someNumber" );
3737
EnhancerTestUtils.clearDirtyTracking( entity );
38-
entity.setId( entity.getId() );
38+
entity.setSomeNumber( entity.getSomeNumber() );
3939
EnhancerTestUtils.checkDirtyTracking( entity );
4040

41-
// Basic multi-field
41+
// Basic multi-field (Id properties are not flagged as dirty)
4242
entity.setId( 2l );
4343
entity.setActive( !entity.isActive() );
4444
entity.setSomeNumber( 193L );
45-
EnhancerTestUtils.checkDirtyTracking( entity, "id", "active", "someNumber" );
45+
EnhancerTestUtils.checkDirtyTracking( entity, "active", "someNumber" );
4646
EnhancerTestUtils.clearDirtyTracking( entity );
4747

4848
// Setting the same value should not make it dirty
@@ -70,7 +70,7 @@ public void execute() {
7070
Address address = new Address();
7171
entity.setAddress( address );
7272
address.setCity( "Arendal" );
73-
EnhancerTestUtils.checkDirtyTracking( entity, "address", "address.city" );
73+
EnhancerTestUtils.checkDirtyTracking( entity, "address" );
7474
EnhancerTestUtils.clearDirtyTracking( entity );
7575

7676
// make sure that new composite instances are cleared
@@ -82,7 +82,7 @@ public void execute() {
8282
Country country = new Country();
8383
address2.setCountry( country );
8484
country.setName( "Norway" );
85-
EnhancerTestUtils.checkDirtyTracking( entity, "address", "address.country", "address.country.name" );
85+
EnhancerTestUtils.checkDirtyTracking( entity, "address", "address.country" );
8686

8787
}
8888

hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazy/LazyLoadingIntegrationTestTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public void execute() {
7575
loadedChildren.remove( loadedChild );
7676
loadedParent.setChildren( loadedChildren );
7777

78-
EnhancerTestUtils.checkDirtyTracking( loadedParent );
78+
EnhancerTestUtils.checkDirtyTracking( loadedParent, "children" );
7979
Assert.assertNull( loadedChild.parent );
8080

8181
s.getTransaction().commit();

hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazy/Parent.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,12 @@
2222
@Entity
2323
public class Parent {
2424

25-
@Id
26-
@GeneratedValue(strategy = GenerationType.AUTO)
2725
Long id;
2826

29-
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
3027
List<Child> children;
3128

29+
@Id
30+
@GeneratedValue(strategy = GenerationType.AUTO)
3231
public Long getId() {
3332
return id;
3433
}
@@ -37,6 +36,7 @@ public void setId(Long id) {
3736
this.id = id;
3837
}
3938

39+
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
4040
public List<Child> getChildren() {
4141
return children;
4242
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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.test.bytecode.enhancement.lazy.basic;
8+
9+
10+
import javax.persistence.Basic;
11+
import javax.persistence.FetchType;
12+
import javax.persistence.GeneratedValue;
13+
import javax.persistence.Id;
14+
15+
import org.hibernate.Hibernate;
16+
import org.hibernate.Session;
17+
import org.hibernate.cfg.Configuration;
18+
import org.hibernate.cfg.Environment;
19+
20+
import org.hibernate.test.bytecode.enhancement.AbstractEnhancerTestTask;
21+
import org.hibernate.test.bytecode.enhancement.EnhancerTestUtils;
22+
import org.junit.Assert;
23+
24+
/**
25+
* @author Gail Badner
26+
*/
27+
public class LazyBasicFieldAccessTestTask extends AbstractEnhancerTestTask {
28+
29+
private Long entityId;
30+
31+
public Class<?>[] getAnnotatedClasses() {
32+
return new Class<?>[] {Entity.class};
33+
}
34+
35+
public void prepare() {
36+
Configuration cfg = new Configuration();
37+
cfg.setProperty( Environment.ENABLE_LAZY_LOAD_NO_TRANS, "true" );
38+
cfg.setProperty( Environment.USE_SECOND_LEVEL_CACHE, "false" );
39+
super.prepare( cfg );
40+
41+
Session s = getFactory().openSession();
42+
s.beginTransaction();
43+
44+
Entity entity = new Entity();
45+
entity.setDescription( "desc" );
46+
s.persist( entity );
47+
entityId = entity.getId();
48+
49+
s.getTransaction().commit();
50+
s.clear();
51+
s.close();
52+
}
53+
54+
public void execute() {
55+
Session s = getFactory().openSession();
56+
s.beginTransaction();
57+
58+
Entity entity = s.get( Entity.class, entityId );
59+
60+
Assert.assertFalse( Hibernate.isPropertyInitialized( entity, "description" ) );
61+
EnhancerTestUtils.checkDirtyTracking( entity );
62+
63+
Assert.assertEquals( "desc", entity.getDescription() );
64+
Assert.assertTrue( Hibernate.isPropertyInitialized( entity, "description" ) );
65+
66+
s.getTransaction().commit();
67+
s.close();
68+
69+
s = getFactory().openSession();
70+
s.beginTransaction();
71+
entity.setDescription( "desc1" );
72+
s.update( entity );
73+
74+
//Assert.assertFalse( Hibernate.isPropertyInitialized( entity, "description" ) );
75+
EnhancerTestUtils.checkDirtyTracking( entity, "description" );
76+
77+
Assert.assertEquals( "desc1", entity.getDescription() );
78+
Assert.assertTrue( Hibernate.isPropertyInitialized( entity, "description" ) );
79+
80+
s.getTransaction().commit();
81+
s.close();
82+
83+
s = getFactory().openSession();
84+
s.beginTransaction();
85+
entity = s.get( Entity.class, entityId );
86+
Assert.assertEquals( "desc1", entity.getDescription() );
87+
s.getTransaction().commit();
88+
s.close();
89+
90+
s = getFactory().openSession();
91+
s.beginTransaction();
92+
entity.setDescription( "desc2" );
93+
entity = (Entity) s.merge( entity );
94+
95+
//Assert.assertFalse( Hibernate.isPropertyInitialized( entity, "description" ) );
96+
EnhancerTestUtils.checkDirtyTracking( entity, "description" );
97+
98+
Assert.assertEquals( "desc2", entity.getDescription() );
99+
Assert.assertTrue( Hibernate.isPropertyInitialized( entity, "description" ) );
100+
101+
s.getTransaction().commit();
102+
s.close();
103+
104+
s = getFactory().openSession();
105+
s.beginTransaction();
106+
entity = s.get( Entity.class, entityId );
107+
Assert.assertEquals( "desc2", entity.getDescription() );
108+
s.getTransaction().commit();
109+
s.close();
110+
}
111+
112+
protected void cleanup() {
113+
}
114+
115+
@javax.persistence.Entity
116+
public static class Entity {
117+
private Long id;
118+
119+
private String description;
120+
121+
@Id
122+
@GeneratedValue
123+
public Long getId() {
124+
return id;
125+
}
126+
127+
public void setId(Long id) {
128+
this.id = id;
129+
}
130+
131+
@Basic(fetch = FetchType.LAZY)
132+
public String getDescription() {
133+
return description;
134+
}
135+
136+
public void setDescription(String description) {
137+
this.description = description;
138+
}
139+
}
140+
141+
142+
}

0 commit comments

Comments
 (0)