Skip to content

Commit c84a30c

Browse files
committed
HHH-17188 Add test for issue
1 parent 520538b commit c84a30c

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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.orm.test.querycache;
8+
9+
import java.util.function.Consumer;
10+
11+
import org.hibernate.cfg.AvailableSettings;
12+
import org.hibernate.engine.spi.SessionImplementor;
13+
import org.hibernate.stat.spi.StatisticsImplementor;
14+
15+
import org.hibernate.testing.orm.junit.DomainModel;
16+
import org.hibernate.testing.orm.junit.Jira;
17+
import org.hibernate.testing.orm.junit.ServiceRegistry;
18+
import org.hibernate.testing.orm.junit.SessionFactory;
19+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
20+
import org.hibernate.testing.orm.junit.Setting;
21+
import org.junit.jupiter.api.BeforeAll;
22+
import org.junit.jupiter.api.Test;
23+
24+
import jakarta.persistence.Cacheable;
25+
import jakarta.persistence.Entity;
26+
import jakarta.persistence.Id;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
import static org.junit.jupiter.api.Assertions.assertEquals;
30+
31+
/**
32+
* @author Marco Belladelli
33+
*/
34+
@DomainModel( annotatedClasses = QueryCacheExistingEntityInstanceTest.TestEntity.class )
35+
@SessionFactory( generateStatistics = true )
36+
@ServiceRegistry( settings = {
37+
@Setting( name = AvailableSettings.USE_QUERY_CACHE, value = "true" ),
38+
@Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "true" )
39+
} )
40+
@Jira( "https://hibernate.atlassian.net/browse/HHH-17188" )
41+
public class QueryCacheExistingEntityInstanceTest {
42+
private static final String TEXT = "text";
43+
private static final String QUERY = "select e from TestEntity e where text = :text";
44+
45+
@BeforeAll
46+
public void setUp(SessionFactoryScope scope) {
47+
scope.inTransaction( session -> session.persist( new TestEntity( 1L, TEXT ) ) );
48+
}
49+
50+
@Test
51+
public void testNormalBehavior(SessionFactoryScope scope) {
52+
testQueryCache( scope, session -> {
53+
} );
54+
}
55+
56+
@Test
57+
public void testAfterFind(SessionFactoryScope scope) {
58+
testQueryCache( scope, session -> session.find( TestEntity.class, 1L ) );
59+
}
60+
61+
@Test
62+
public void testAfterGetReference(SessionFactoryScope scope) {
63+
testQueryCache( scope, session -> session.getReference( TestEntity.class, 1L ) );
64+
}
65+
66+
private void testQueryCache(SessionFactoryScope scope, Consumer<SessionImplementor> beforeQuery) {
67+
scope.getSessionFactory().getCache().evictQueryRegions();
68+
final StatisticsImplementor statistics = scope.getSessionFactory().getStatistics();
69+
statistics.clear();
70+
71+
scope.inTransaction( session -> {
72+
beforeQuery.accept( session );
73+
executeQuery( session );
74+
} );
75+
76+
assertQueryCacheStatistics( statistics, 0, 1, 1 );
77+
78+
scope.inTransaction( QueryCacheExistingEntityInstanceTest::executeQuery );
79+
80+
assertQueryCacheStatistics( statistics, 1, 1, 1 );
81+
}
82+
83+
private static void executeQuery(SessionImplementor session) {
84+
final TestEntity entity = session.createQuery( QUERY, TestEntity.class )
85+
.setParameter( "text", TEXT )
86+
.setCacheable( true )
87+
.getSingleResult();
88+
assertEquals( TEXT, entity.getText() );
89+
}
90+
91+
@SuppressWarnings( "SameParameterValue" )
92+
private static void assertQueryCacheStatistics(
93+
StatisticsImplementor statistics,
94+
int hitCount,
95+
int missCount,
96+
int putCount) {
97+
assertThat( statistics.getQueryCacheHitCount() ).isEqualTo( hitCount );
98+
assertThat( statistics.getQueryCacheMissCount() ).isEqualTo( missCount );
99+
assertThat( statistics.getQueryCachePutCount() ).isEqualTo( putCount );
100+
}
101+
102+
@Entity( name = "TestEntity" )
103+
@Cacheable
104+
public static class TestEntity {
105+
@Id
106+
private Long id;
107+
108+
private String text;
109+
110+
public TestEntity() {
111+
}
112+
113+
public TestEntity(Long id, String text) {
114+
this.id = id;
115+
this.text = text;
116+
}
117+
118+
public Long getId() {
119+
return id;
120+
}
121+
122+
public String getText() {
123+
return text;
124+
}
125+
}
126+
}

0 commit comments

Comments
 (0)