Skip to content

Commit 3cfa7c7

Browse files
committed
HHH-18872 Rename and cleanup test
1 parent 107e426 commit 3cfa7c7

File tree

2 files changed

+220
-128
lines changed

2 files changed

+220
-128
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/proxy/concrete/ConcreteProxyCacheTest.java

Lines changed: 0 additions & 128 deletions
This file was deleted.
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.proxy.concrete;
6+
7+
import jakarta.persistence.Cacheable;
8+
import jakarta.persistence.DiscriminatorColumn;
9+
import jakarta.persistence.DiscriminatorType;
10+
import jakarta.persistence.DiscriminatorValue;
11+
import jakarta.persistence.Entity;
12+
import jakarta.persistence.FetchType;
13+
import jakarta.persistence.Id;
14+
import jakarta.persistence.Inheritance;
15+
import jakarta.persistence.InheritanceType;
16+
import jakarta.persistence.JoinColumn;
17+
import jakarta.persistence.ManyToOne;
18+
import org.hibernate.Hibernate;
19+
import org.hibernate.annotations.Cache;
20+
import org.hibernate.annotations.CacheConcurrencyStrategy;
21+
import org.hibernate.annotations.ConcreteProxy;
22+
import org.hibernate.cfg.AvailableSettings;
23+
import org.hibernate.stat.Statistics;
24+
import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced;
25+
import org.hibernate.testing.orm.junit.DomainModel;
26+
import org.hibernate.testing.orm.junit.Jira;
27+
import org.hibernate.testing.orm.junit.ServiceRegistry;
28+
import org.hibernate.testing.orm.junit.SessionFactory;
29+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
30+
import org.hibernate.testing.orm.junit.Setting;
31+
import org.junit.jupiter.api.AfterAll;
32+
import org.junit.jupiter.api.BeforeAll;
33+
import org.junit.jupiter.api.BeforeEach;
34+
import org.junit.jupiter.api.Test;
35+
36+
import static org.assertj.core.api.Assertions.assertThat;
37+
38+
@DomainModel(annotatedClasses = {
39+
ConcreteProxyToOneSecondLevelCacheTest.TestNode.class,
40+
ConcreteProxyToOneSecondLevelCacheTest.TestCompositeNode.class
41+
})
42+
@ServiceRegistry(settings = {
43+
@Setting(name = AvailableSettings.GENERATE_STATISTICS, value = "true"),
44+
@Setting(name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "true")
45+
})
46+
@SessionFactory
47+
@BytecodeEnhanced(runNotEnhancedAsWell = true)
48+
@Jira("https://hibernate.atlassian.net/browse/HHH-18872")
49+
public class ConcreteProxyToOneSecondLevelCacheTest {
50+
@Test
51+
public void testToOneInCacheGetReference(SessionFactoryScope scope) {
52+
final Statistics stats = scope.getSessionFactory().getStatistics();
53+
stats.clear();
54+
// load only the node with ID 1 into the 2LC
55+
scope.inTransaction( session -> {
56+
assertThat( session.find( TestCompositeNode.class, 1 ) ).isNotNull();
57+
assertThat( session.find( TestCompositeNode.class, 2 ) ).isNotNull();
58+
} );
59+
assertCacheStats( stats, 0, 2, 2 );
60+
scope.inSession( session -> {
61+
final TestCompositeNode node1 = session.getReference( TestCompositeNode.class, 1 );
62+
assertThat( Hibernate.isInitialized( node1 ) ).isFalse();
63+
// this triggers node1 initialization, but should maintain laziness for parent
64+
final TestNode parent = node1.getParent();
65+
assertThat( Hibernate.isInitialized( node1 ) ).isTrue();
66+
// node 1 will be loaded from cache
67+
assertCacheStats( stats, 1, 2, 2 );
68+
assertParent( parent, stats, 2 );
69+
} );
70+
}
71+
72+
@Test
73+
public void testToOneNotInCacheGetReference(SessionFactoryScope scope) {
74+
final Statistics stats = scope.getSessionFactory().getStatistics();
75+
stats.clear();
76+
// load only the node with ID 1 into the 2LC
77+
scope.inTransaction( session -> assertThat( session.find( TestCompositeNode.class, 1 ) ).isNotNull() );
78+
assertCacheStats( stats, 0, 1, 1 );
79+
scope.inSession( session -> {
80+
final TestCompositeNode node1 = session.getReference( TestCompositeNode.class, 1 );
81+
assertThat( Hibernate.isInitialized( node1 ) ).isFalse();
82+
// this triggers node1 initialization, but should maintain laziness for parent
83+
final TestNode parent = node1.getParent();
84+
assertThat( Hibernate.isInitialized( node1 ) ).isTrue();
85+
// node 1 will be loaded from cache
86+
assertCacheStats( stats, 1, 1, 1 );
87+
assertParent( parent, stats, 1 );
88+
} );
89+
}
90+
91+
@Test
92+
public void testToOneInCacheFind(SessionFactoryScope scope) {
93+
final Statistics stats = scope.getSessionFactory().getStatistics();
94+
stats.clear();
95+
// load only the node with ID 1 into the 2LC
96+
scope.inTransaction( session -> {
97+
assertThat( session.find( TestCompositeNode.class, 1 ) ).isNotNull();
98+
assertThat( session.find( TestCompositeNode.class, 2 ) ).isNotNull();
99+
} );
100+
assertCacheStats( stats, 0, 2, 2 );
101+
scope.inSession( session -> {
102+
final TestCompositeNode node1 = session.find( TestCompositeNode.class, 1 );
103+
assertThat( Hibernate.isInitialized( node1 ) ).isTrue();
104+
// node 1 will be loaded from cache
105+
assertCacheStats( stats, 1, 2, 2 );
106+
assertParent( node1.getParent(), stats, 2 );
107+
} );
108+
}
109+
110+
@Test
111+
public void testToOneNotInCacheFind(SessionFactoryScope scope) {
112+
final Statistics stats = scope.getSessionFactory().getStatistics();
113+
stats.clear();
114+
// load only the node with ID 1 into the 2LC
115+
scope.inTransaction( session -> assertThat( session.find( TestCompositeNode.class, 1 ) ).isNotNull() );
116+
assertCacheStats( stats, 0, 1, 1 );
117+
scope.inSession( session -> {
118+
final TestCompositeNode node1 = session.find( TestCompositeNode.class, 1 );
119+
assertThat( Hibernate.isInitialized( node1 ) ).isTrue();
120+
// node 1 will be loaded from cache
121+
assertCacheStats( stats, 1, 1, 1 );
122+
assertParent( node1.getParent(), stats, 1 );
123+
} );
124+
}
125+
126+
private static void assertParent(final TestNode parent, final Statistics stats, final long hits) {
127+
assertThat( TestCompositeNode.class ).as( "Expecting parent proxy to be narrowed to concrete type" )
128+
.isAssignableFrom( parent.getClass() );
129+
final TestCompositeNode parentComposite = (TestCompositeNode) parent;
130+
assertThat( Hibernate.isInitialized( parentComposite ) ).isFalse();
131+
assertThat( parentComposite.getName() ).isEqualTo( "parent_node" );
132+
assertThat( Hibernate.isInitialized( parentComposite ) ).isTrue();
133+
// node 2 will not be found in cache
134+
assertCacheStats( stats, hits, 2, 2 );
135+
assertThat( parentComposite ).as( String.format(
136+
"Expecting parent to be an instance of TestCompositeNode but was: [%s]",
137+
parent.getClass()
138+
) ).isInstanceOf( TestCompositeNode.class );
139+
}
140+
141+
private static void assertCacheStats(final Statistics stats, final long hits, final long misses, final long puts) {
142+
assertThat( stats.getSecondLevelCacheHitCount() ).isEqualTo( hits );
143+
assertThat( stats.getSecondLevelCacheMissCount() ).isEqualTo( misses );
144+
assertThat( stats.getSecondLevelCachePutCount() ).isEqualTo( puts );
145+
}
146+
147+
@BeforeEach
148+
public void clearCache(SessionFactoryScope scope) {
149+
scope.getSessionFactory().getCache().evictAllRegions();
150+
}
151+
152+
@BeforeAll
153+
public void setUp(SessionFactoryScope scope) {
154+
scope.inTransaction( session -> {
155+
final TestCompositeNode node1 = new TestCompositeNode( 1, "child_node" );
156+
final TestCompositeNode node2 = new TestCompositeNode( 2, "parent_node" );
157+
node1.setParent( node2 );
158+
session.persist( node1 );
159+
session.persist( node2 );
160+
} );
161+
}
162+
163+
@AfterAll
164+
public void tearDown(SessionFactoryScope scope) {
165+
scope.getSessionFactory().getSchemaManager().truncateMappedObjects();
166+
}
167+
168+
@Entity(name = "TestNode")
169+
@Cacheable
170+
@ConcreteProxy
171+
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
172+
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
173+
@DiscriminatorColumn(discriminatorType = DiscriminatorType.STRING, name = "disc_col")
174+
@DiscriminatorValue(value = "simple")
175+
public static class TestNode {
176+
@Id
177+
private Integer id;
178+
179+
private String name;
180+
181+
public TestNode() {
182+
}
183+
184+
public TestNode(Integer id, String name) {
185+
this.id = id;
186+
this.name = name;
187+
}
188+
189+
public Integer getId() {
190+
return id;
191+
}
192+
193+
public String getName() {
194+
return name;
195+
}
196+
}
197+
198+
@Entity
199+
@DiscriminatorValue("composite")
200+
public static class TestCompositeNode extends TestNode {
201+
@ManyToOne(fetch = FetchType.LAZY)
202+
@JoinColumn(name = "parent_id")
203+
private TestNode parent;
204+
205+
public TestCompositeNode() {
206+
}
207+
208+
public TestCompositeNode(Integer id, String name) {
209+
super( id, name );
210+
}
211+
212+
public TestNode getParent() {
213+
return parent;
214+
}
215+
216+
public void setParent(TestNode parent) {
217+
this.parent = parent;
218+
}
219+
}
220+
}

0 commit comments

Comments
 (0)