Skip to content

Commit 5a8fc3e

Browse files
barreirosebersole
authored andcommitted
HHH-10055 - Add test case (enhanced version of org.hibernate.test.ondemandload.LazyLoadingTest)
(cherry picked from commit d0b0d7e)
1 parent 5a034f9 commit 5a8fc3e

File tree

7 files changed

+531
-0
lines changed

7 files changed

+531
-0
lines changed

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
@@ -27,6 +27,9 @@
2727
import org.hibernate.test.bytecode.enhancement.lazy.basic.LazyBasicPropertyAccessTestTask;
2828
import org.hibernate.test.bytecode.enhancement.merge.CompositeMergeTestTask;
2929
import org.hibernate.test.bytecode.enhancement.pk.EmbeddedPKTestTask;
30+
import org.hibernate.test.bytecode.enhancement.ondemandload.LazyCollectionWithClearedSessionTestTask;
31+
import org.hibernate.test.bytecode.enhancement.ondemandload.LazyCollectionWithClosedSessionTestTask;
32+
import org.hibernate.test.bytecode.enhancement.ondemandload.LazyEntityLoadingWithClosedSessionTestTask;
3033
import org.junit.Test;
3134

3235
/**
@@ -60,6 +63,15 @@ public void testLazy() {
6063
EnhancerTestUtils.runEnhancerTestTask( LazyBasicFieldAccessTestTask.class );
6164
}
6265

66+
@Test
67+
@TestForIssue( jiraKey = "HHH-10055" )
68+
@FailureExpected( jiraKey = "HHH-10055" )
69+
public void testOnDemand() {
70+
EnhancerTestUtils.runEnhancerTestTask( LazyCollectionWithClearedSessionTestTask.class );
71+
EnhancerTestUtils.runEnhancerTestTask( LazyCollectionWithClosedSessionTestTask.class );
72+
EnhancerTestUtils.runEnhancerTestTask( LazyEntityLoadingWithClosedSessionTestTask.class );
73+
}
74+
6375
@Test
6476
public void testMerge() {
6577
EnhancerTestUtils.runEnhancerTestTask( CompositeMergeTestTask.class );
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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.ondemandload;
8+
9+
import java.math.BigDecimal;
10+
import javax.persistence.Entity;
11+
import javax.persistence.GeneratedValue;
12+
import javax.persistence.Id;
13+
import javax.persistence.JoinColumn;
14+
import javax.persistence.ManyToOne;
15+
16+
import org.hibernate.annotations.GenericGenerator;
17+
18+
@Entity
19+
public class Inventory {
20+
private int id = -1;
21+
private Store store;
22+
private Product product;
23+
private Long quantity;
24+
private BigDecimal storePrice;
25+
26+
public Inventory() {
27+
}
28+
29+
public Inventory(Store store, Product product) {
30+
this.store = store;
31+
this.product = product;
32+
}
33+
34+
@Id
35+
@GeneratedValue
36+
@GenericGenerator( name = "increment", strategy = "increment" )
37+
public int getId() {
38+
return id;
39+
}
40+
41+
public void setId(int id) {
42+
this.id = id;
43+
}
44+
45+
@ManyToOne
46+
@JoinColumn( name = "store_id" )
47+
public Store getStore() {
48+
return store;
49+
}
50+
51+
public Inventory setStore(Store store) {
52+
this.store = store;
53+
return this;
54+
}
55+
56+
@ManyToOne
57+
@JoinColumn( name = "prod_id" )
58+
public Product getProduct() {
59+
return product;
60+
}
61+
62+
public Inventory setProduct(Product product) {
63+
this.product = product;
64+
return this;
65+
}
66+
67+
public Long getQuantity() {
68+
return quantity;
69+
}
70+
71+
public Inventory setQuantity(Long quantity) {
72+
this.quantity = quantity;
73+
return this;
74+
}
75+
76+
public BigDecimal getStorePrice() {
77+
return storePrice;
78+
}
79+
80+
public Inventory setStorePrice(BigDecimal storePrice) {
81+
this.storePrice = storePrice;
82+
return this;
83+
}
84+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
8+
package org.hibernate.test.bytecode.enhancement.ondemandload;
9+
10+
import java.math.BigDecimal;
11+
12+
import org.hibernate.Hibernate;
13+
import org.hibernate.Session;
14+
import org.hibernate.cfg.Configuration;
15+
import org.hibernate.cfg.Environment;
16+
17+
import org.hibernate.test.bytecode.enhancement.AbstractEnhancerTestTask;
18+
19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertFalse;
21+
import static org.junit.Assert.assertNotNull;
22+
import static org.junit.Assert.assertTrue;
23+
24+
public class LazyCollectionWithClearedSessionTestTask extends AbstractEnhancerTestTask {
25+
26+
public void prepare() {
27+
Configuration cfg = new Configuration();
28+
cfg.setProperty( Environment.ENABLE_LAZY_LOAD_NO_TRANS, "true" );
29+
cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
30+
cfg.setProperty( Environment.USE_SECOND_LEVEL_CACHE, "false" );
31+
super.prepare( cfg );
32+
33+
Session s = getFactory().openSession();
34+
s.beginTransaction();
35+
36+
Store store = new Store( 1 ).setName( "Acme Super Outlet" );
37+
s.persist( store );
38+
39+
Product product = new Product( "007" ).setName( "widget" ).setDescription( "FooBar" );
40+
s.persist( product );
41+
42+
store.addInventoryProduct( product ).setQuantity( 10L ).setStorePrice( new BigDecimal( 500 ) );
43+
44+
s.getTransaction().commit();
45+
s.close();
46+
}
47+
48+
public void cleanup() {
49+
}
50+
51+
public void execute() {
52+
getFactory().getStatistics().clear();
53+
54+
Session s = getFactory().openSession();
55+
s.beginTransaction();
56+
57+
// first load the store, making sure collection is not initialized
58+
Store store = s.get( Store.class, 1 );
59+
assertNotNull( store );
60+
assertFalse( Hibernate.isInitialized( store.getInventories() ) );
61+
62+
assertEquals( 1, getFactory().getStatistics().getSessionOpenCount() );
63+
assertEquals( 0, getFactory().getStatistics().getSessionCloseCount() );
64+
65+
// then clear session and try to initialize collection
66+
s.clear();
67+
store.getInventories().size();
68+
assertTrue( Hibernate.isInitialized( store.getInventories() ) );
69+
70+
assertEquals( 2, getFactory().getStatistics().getSessionOpenCount() );
71+
assertEquals( 1, getFactory().getStatistics().getSessionCloseCount() );
72+
73+
s.clear();
74+
store = s.get( Store.class, 1 );
75+
assertNotNull( store );
76+
assertFalse( Hibernate.isInitialized( store.getInventories() ) );
77+
78+
assertEquals( 2, getFactory().getStatistics().getSessionOpenCount() );
79+
assertEquals( 1, getFactory().getStatistics().getSessionCloseCount() );
80+
81+
s.clear();
82+
store.getInventories().iterator();
83+
assertTrue( Hibernate.isInitialized( store.getInventories() ) );
84+
85+
assertEquals( 3, getFactory().getStatistics().getSessionOpenCount() );
86+
assertEquals( 2, getFactory().getStatistics().getSessionCloseCount() );
87+
88+
s.getTransaction().commit();
89+
s.close();
90+
}
91+
92+
protected void configure(Configuration cfg) {
93+
}
94+
95+
public Class[] getAnnotatedClasses() {
96+
return new Class[] {
97+
Store.class,
98+
Inventory.class,
99+
Product.class
100+
};
101+
}
102+
103+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
8+
package org.hibernate.test.bytecode.enhancement.ondemandload;
9+
10+
import java.math.BigDecimal;
11+
12+
import org.hibernate.Hibernate;
13+
import org.hibernate.Session;
14+
import org.hibernate.cfg.Configuration;
15+
import org.hibernate.cfg.Environment;
16+
17+
import org.hibernate.test.bytecode.enhancement.AbstractEnhancerTestTask;
18+
19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertFalse;
21+
import static org.junit.Assert.assertNotNull;
22+
import static org.junit.Assert.assertTrue;
23+
24+
public class LazyCollectionWithClosedSessionTestTask extends AbstractEnhancerTestTask {
25+
26+
public void prepare() {
27+
Configuration cfg = new Configuration();
28+
cfg.setProperty( Environment.ENABLE_LAZY_LOAD_NO_TRANS, "true" );
29+
cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
30+
cfg.setProperty( Environment.USE_SECOND_LEVEL_CACHE, "false" );
31+
super.prepare( cfg );
32+
33+
34+
Session s = getFactory().openSession();
35+
s.beginTransaction();
36+
37+
Store store = new Store( 1 ).setName( "Acme Super Outlet" );
38+
s.persist( store );
39+
40+
Product product = new Product( "007" ).setName( "widget" ).setDescription( "FooBar" );
41+
s.persist( product );
42+
43+
store.addInventoryProduct( product ).setQuantity( 10L ).setStorePrice( new BigDecimal( 500 ) );
44+
45+
s.getTransaction().commit();
46+
s.close();
47+
}
48+
49+
public void cleanup() {
50+
}
51+
52+
public void execute() {
53+
getFactory().getStatistics().clear();
54+
55+
Session s = getFactory().openSession();
56+
s.beginTransaction();
57+
58+
// first load the store, making sure collection is not initialized
59+
Store store = s.get( Store.class, 1 );
60+
assertNotNull( store );
61+
assertFalse( Hibernate.isInitialized( store.getInventories() ) );
62+
63+
assertEquals( 1, getFactory().getStatistics().getSessionOpenCount() );
64+
assertEquals( 0, getFactory().getStatistics().getSessionCloseCount() );
65+
66+
// close the session and try to initialize collection
67+
s.getTransaction().commit();
68+
s.close();
69+
70+
assertEquals( 1, getFactory().getStatistics().getSessionOpenCount() );
71+
assertEquals( 1, getFactory().getStatistics().getSessionCloseCount() );
72+
73+
store.getInventories().size();
74+
assertTrue( Hibernate.isInitialized( store.getInventories() ) );
75+
76+
assertEquals( 2, getFactory().getStatistics().getSessionOpenCount() );
77+
assertEquals( 2, getFactory().getStatistics().getSessionCloseCount() );
78+
}
79+
80+
protected void configure(Configuration cfg) {
81+
}
82+
83+
public Class[] getAnnotatedClasses() {
84+
return new Class[] {
85+
Store.class,
86+
Inventory.class,
87+
Product.class
88+
};
89+
}
90+
91+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
8+
package org.hibernate.test.bytecode.enhancement.ondemandload;
9+
10+
import java.math.BigDecimal;
11+
12+
import org.hibernate.Hibernate;
13+
import org.hibernate.Session;
14+
import org.hibernate.cfg.Configuration;
15+
import org.hibernate.cfg.Environment;
16+
17+
import org.hibernate.test.bytecode.enhancement.AbstractEnhancerTestTask;
18+
19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertFalse;
21+
import static org.junit.Assert.assertNotNull;
22+
import static org.junit.Assert.assertTrue;
23+
24+
public class LazyEntityLoadingWithClosedSessionTestTask extends AbstractEnhancerTestTask {
25+
26+
public void prepare() {
27+
Configuration cfg = new Configuration();
28+
cfg.setProperty( Environment.ENABLE_LAZY_LOAD_NO_TRANS, "true" );
29+
cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
30+
cfg.setProperty( Environment.USE_SECOND_LEVEL_CACHE, "false" );
31+
super.prepare( cfg );
32+
33+
Session s = getFactory().openSession();
34+
s.beginTransaction();
35+
36+
Store store = new Store( 1 ).setName( "Acme Super Outlet" );
37+
s.persist( store );
38+
39+
Product product = new Product( "007" ).setName( "widget" ).setDescription( "FooBar" );
40+
s.persist( product );
41+
42+
store.addInventoryProduct( product ).setQuantity( 10L ).setStorePrice( new BigDecimal( 500 ) );
43+
44+
s.getTransaction().commit();
45+
s.close();
46+
}
47+
48+
public void cleanup() {
49+
}
50+
51+
public void execute() {
52+
getFactory().getStatistics().clear();
53+
54+
Session s = getFactory().openSession();
55+
s.beginTransaction();
56+
57+
// first load the store, making sure it is not initialized
58+
Store store = s.load( Store.class, 1 );
59+
assertNotNull( store );
60+
assertFalse( Hibernate.isInitialized( store ) );
61+
62+
assertEquals( 1, getFactory().getStatistics().getSessionOpenCount() );
63+
assertEquals( 0, getFactory().getStatistics().getSessionCloseCount() );
64+
65+
// close the session and try to initialize store
66+
s.getTransaction().commit();
67+
s.close();
68+
69+
assertEquals( 1, getFactory().getStatistics().getSessionOpenCount() );
70+
assertEquals( 1, getFactory().getStatistics().getSessionCloseCount() );
71+
72+
store.getName();
73+
assertTrue( Hibernate.isInitialized( store ) );
74+
75+
assertEquals( 2, getFactory().getStatistics().getSessionOpenCount() );
76+
assertEquals( 2, getFactory().getStatistics().getSessionCloseCount() );
77+
}
78+
79+
protected void configure(Configuration cfg) {
80+
}
81+
82+
public Class[] getAnnotatedClasses() {
83+
return new Class[] {
84+
Store.class,
85+
Inventory.class,
86+
Product.class
87+
};
88+
}
89+
90+
}

0 commit comments

Comments
 (0)