Skip to content

Commit 340b901

Browse files
dreab8Sanne
authored andcommitted
HHH-14826 Extract issue specific tests from OneToOneCacheTest
1 parent a652822 commit 340b901

File tree

2 files changed

+158
-132
lines changed

2 files changed

+158
-132
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package org.hibernate.test.onetoone.cache;
2+
3+
import java.util.concurrent.atomic.AtomicLong;
4+
import javax.persistence.Cacheable;
5+
import javax.persistence.CascadeType;
6+
import javax.persistence.Entity;
7+
import javax.persistence.FetchType;
8+
import javax.persistence.GeneratedValue;
9+
import javax.persistence.Id;
10+
import javax.persistence.OneToOne;
11+
import javax.persistence.Version;
12+
13+
import org.hibernate.cfg.AvailableSettings;
14+
import org.hibernate.cfg.Configuration;
15+
16+
import org.hibernate.testing.TestForIssue;
17+
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
18+
import org.junit.Test;
19+
20+
import static org.junit.Assert.assertNotEquals;
21+
import static org.junit.Assert.assertNotNull;
22+
import static org.junit.Assert.assertTrue;
23+
24+
@TestForIssue( jiraKey = "HHH-14826")
25+
public class OneToOneCacheEnableSelectingTest extends BaseCoreFunctionalTestCase {
26+
@Override
27+
protected Class<?>[] getAnnotatedClasses() {
28+
return new Class[] {
29+
Product.class,
30+
ProductConfig.class
31+
};
32+
}
33+
34+
@Override
35+
protected void configure(Configuration configuration) {
36+
configuration.setProperty( AvailableSettings.USE_SECOND_LEVEL_CACHE, "true");
37+
configuration.setProperty(AvailableSettings.JPA_SHARED_CACHE_MODE, "ENABLE_SELECTIVE");
38+
configuration.setProperty(AvailableSettings.GENERATE_STATISTICS, "true");
39+
}
40+
41+
@Test
42+
public void testFieldShouldNotBeNull() {
43+
final AtomicLong pid = new AtomicLong();
44+
45+
// create Product
46+
inTransaction(s -> {
47+
Product product = new Product();
48+
s.persist(product);
49+
pid.set(product.getId());
50+
});
51+
52+
// create ProductConfig and associate with a Product
53+
inTransaction(s -> {
54+
Product product = s.find(Product.class, pid.get());
55+
ProductConfig config = new ProductConfig();
56+
config.setProduct(product);
57+
s.persist(config);
58+
});
59+
60+
assertTrue(sessionFactory().getCache().containsEntity(Product.class, pid.get()));
61+
62+
sessionFactory().getStatistics().clear();
63+
64+
// now fetch the Product again
65+
inTransaction(s -> {
66+
Product product = s.find(Product.class, pid.get());
67+
68+
// should have been from cache
69+
assertNotEquals (0, sessionFactory().getStatistics().getSecondLevelCacheHitCount());
70+
71+
// this should not fail
72+
assertNotNull("one-to-one field should not be null", product.getConfig());
73+
});
74+
}
75+
76+
@Entity(name = "Product")
77+
@Cacheable
78+
public static class Product {
79+
80+
@Id
81+
@GeneratedValue
82+
private Long id;
83+
84+
@Version
85+
private Integer version;
86+
87+
@OneToOne(mappedBy = "product", fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
88+
private ProductConfig config;
89+
90+
public Product() {}
91+
92+
public Long getId() {
93+
return id;
94+
}
95+
96+
public void setId(Long id) {
97+
this.id = id;
98+
}
99+
100+
public Integer getVersion() {
101+
return version;
102+
}
103+
104+
public void setVersion(Integer version) {
105+
this.version = version;
106+
}
107+
108+
public ProductConfig getConfig() {
109+
return config;
110+
}
111+
112+
public void setConfig(ProductConfig config) {
113+
this.config = config;
114+
}
115+
}
116+
117+
@Entity(name = "ProductConfig")
118+
@Cacheable
119+
public static class ProductConfig {
120+
121+
@Id
122+
@GeneratedValue
123+
private Long id;
124+
125+
@Version
126+
private Integer version;
127+
128+
@OneToOne(fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
129+
private Product product;
130+
131+
public ProductConfig() {}
132+
133+
public Long getId() {
134+
return id;
135+
}
136+
137+
public void setId(Long id) {
138+
this.id = id;
139+
}
140+
141+
public Integer getVersion() {
142+
return version;
143+
}
144+
145+
public void setVersion(Integer version) {
146+
this.version = version;
147+
}
148+
149+
public Product getProduct() {
150+
return product;
151+
}
152+
153+
public void setProduct(Product product) {
154+
this.product = product;
155+
}
156+
}
157+
158+
}
Lines changed: 0 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
package org.hibernate.test.onetoone.cache;
22

33
import static org.junit.Assert.assertEquals;
4-
import static org.junit.Assert.assertTrue;
5-
import static org.junit.Assert.assertNotEquals;
6-
import static org.junit.Assert.assertNotNull;
74

85
import java.io.Serializable;
96
import java.lang.reflect.Constructor;
107
import java.util.ArrayList;
118
import java.util.List;
12-
import java.util.concurrent.atomic.AtomicLong;
139

1410
import org.hibernate.Session;
1511
import org.hibernate.Transaction;
@@ -21,8 +17,6 @@
2117
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
2218
import org.junit.Test;
2319

24-
import javax.persistence.*;
25-
2620
public class OneToOneCacheTest extends BaseCoreFunctionalTestCase {
2721
@Override
2822
public String[] getMappings() {
@@ -32,18 +26,9 @@ public String[] getMappings() {
3226
};
3327
}
3428

35-
@Override
36-
protected Class<?>[] getAnnotatedClasses() {
37-
return new Class[] {
38-
Product.class,
39-
ProductConfig.class
40-
};
41-
}
42-
4329
@Override
4430
protected void configure(Configuration configuration) {
4531
configuration.setProperty(AvailableSettings.USE_SECOND_LEVEL_CACHE, "true");
46-
configuration.setProperty(AvailableSettings.JPA_SHARED_CACHE_MODE, "ENABLE_SELECTIVE");
4732
configuration.setProperty(AvailableSettings.GENERATE_STATISTICS, "true");
4833
}
4934

@@ -132,121 +117,4 @@ public void OneToOneCacheByForeignKey() throws Exception {
132117
public void OneToOneCacheByRef() throws Exception {
133118
OneToOneTest(PersonByRef.class, DetailsByRef.class);
134119
}
135-
136-
@Test
137-
public void testFieldShouldNotBeNull2() {
138-
final AtomicLong pid = new AtomicLong();
139-
140-
// create Product
141-
inTransaction(s -> {
142-
Product product = new Product();
143-
s.persist(product);
144-
pid.set(product.getId());
145-
});
146-
147-
// create ProductConfig and associate with a Product
148-
inTransaction(s -> {
149-
Product product = s.find(Product.class, pid.get());
150-
ProductConfig config = new ProductConfig();
151-
config.setProduct(product);
152-
s.persist(config);
153-
});
154-
155-
assertTrue(sessionFactory().getCache().containsEntity(Product.class, pid.get()));
156-
157-
sessionFactory().getStatistics().clear();
158-
159-
// now fetch the Product again
160-
inTransaction(s -> {
161-
Product product = s.find(Product.class, pid.get());
162-
163-
// should have been from cache
164-
assertNotEquals (0, sessionFactory().getStatistics().getSecondLevelCacheHitCount());
165-
166-
// this should not fail
167-
assertNotNull("one-to-one field should not be null", product.getConfig());
168-
});
169-
}
170-
171-
@Entity(name = "Product")
172-
@Cacheable
173-
public static class Product {
174-
175-
@Id
176-
@GeneratedValue
177-
private Long id;
178-
179-
@Version
180-
private Integer version;
181-
182-
@OneToOne(mappedBy = "product", fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
183-
private ProductConfig config;
184-
185-
public Product() {}
186-
187-
public Long getId() {
188-
return id;
189-
}
190-
191-
public void setId(Long id) {
192-
this.id = id;
193-
}
194-
195-
public Integer getVersion() {
196-
return version;
197-
}
198-
199-
public void setVersion(Integer version) {
200-
this.version = version;
201-
}
202-
203-
public ProductConfig getConfig() {
204-
return config;
205-
}
206-
207-
public void setConfig(ProductConfig config) {
208-
this.config = config;
209-
}
210-
}
211-
212-
@Entity(name = "ProductConfig")
213-
@Cacheable
214-
public static class ProductConfig {
215-
216-
@Id
217-
@GeneratedValue
218-
private Long id;
219-
220-
@Version
221-
private Integer version;
222-
223-
@OneToOne(fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
224-
private Product product;
225-
226-
public ProductConfig() {}
227-
228-
public Long getId() {
229-
return id;
230-
}
231-
232-
public void setId(Long id) {
233-
this.id = id;
234-
}
235-
236-
public Integer getVersion() {
237-
return version;
238-
}
239-
240-
public void setVersion(Integer version) {
241-
this.version = version;
242-
}
243-
244-
public Product getProduct() {
245-
return product;
246-
}
247-
248-
public void setProduct(Product product) {
249-
this.product = product;
250-
}
251-
}
252120
}

0 commit comments

Comments
 (0)