forked from hibernate/hibernate-orm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HHH-15105 Test and fix for NPE when access default query cache region…
… statistics
- Loading branch information
Showing
2 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
hibernate-core/src/test/java/org/hibernate/orm/test/cache/CacheRegionStatisticsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. | ||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
*/ | ||
package org.hibernate.orm.test.cache; | ||
|
||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import jakarta.persistence.ElementCollection; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Id; | ||
|
||
import org.hibernate.boot.MetadataSources; | ||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; | ||
import org.hibernate.cfg.AvailableSettings; | ||
import org.hibernate.stat.CacheRegionStatistics; | ||
import org.hibernate.stat.Statistics; | ||
|
||
import org.hibernate.testing.TestForIssue; | ||
import org.hibernate.testing.cache.CachingRegionFactory; | ||
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* @author Christian Beikov | ||
*/ | ||
public class CacheRegionStatisticsTest extends BaseNonConfigCoreFunctionalTestCase { | ||
|
||
@Test | ||
@TestForIssue( jiraKey = "HHH-15105") | ||
public void testAccessDefaultQueryRegionStatistics() { | ||
final Statistics statistics = sessionFactory().getStatistics(); | ||
final CacheRegionStatistics queryRegionStatistics = statistics.getQueryRegionStatistics( | ||
"default-query-results-region" | ||
); | ||
doInHibernate( | ||
this::sessionFactory, session -> { | ||
List<Dog> resultList = session.createQuery( "from Dog", Dog.class ) | ||
.setCacheable( true ) | ||
.getResultList(); | ||
|
||
assertEquals( 1, queryRegionStatistics.getMissCount() ); | ||
} | ||
); | ||
} | ||
|
||
@Override | ||
protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { | ||
super.configureStandardServiceRegistryBuilder( ssrb ); | ||
ssrb.applySetting( AvailableSettings.USE_SECOND_LEVEL_CACHE, true ); | ||
ssrb.applySetting( AvailableSettings.USE_QUERY_CACHE, true ); | ||
ssrb.applySetting( AvailableSettings.CACHE_REGION_FACTORY, new CachingRegionFactory() ); | ||
ssrb.applySetting( AvailableSettings.GENERATE_STATISTICS, "true" ); | ||
} | ||
|
||
@Override | ||
protected void applyMetadataSources(MetadataSources metadataSources) { | ||
super.applyMetadataSources( metadataSources ); | ||
metadataSources.addAnnotatedClass( Dog.class ); | ||
} | ||
|
||
@Before | ||
public void setupData() { | ||
doInHibernate( | ||
this::sessionFactory, session -> { | ||
Dog yogi = new Dog( "Yogi" ); | ||
yogi.nickNames.add( "The Yog" ); | ||
yogi.nickNames.add( "Little Boy" ); | ||
yogi.nickNames.add( "Yogaroni Macaroni" ); | ||
Dog irma = new Dog( "Irma" ); | ||
irma.nickNames.add( "Squirmy" ); | ||
irma.nickNames.add( "Bird" ); | ||
session.persist( yogi ); | ||
session.persist( irma ); | ||
} | ||
); | ||
} | ||
|
||
@After | ||
public void cleanupData() { | ||
doInHibernate( | ||
this::sessionFactory, session -> { | ||
List<Dog> dogs = session.createQuery( "from Dog", Dog.class ).getResultList(); | ||
for ( Dog dog : dogs ) { | ||
session.delete( dog ); | ||
} | ||
} | ||
); | ||
} | ||
|
||
@Entity(name = "Dog") | ||
public static class Dog { | ||
@Id | ||
private String name; | ||
|
||
@ElementCollection | ||
private Set<String> nickNames = new HashSet<>(); | ||
|
||
public Dog(String name) { | ||
this.name = name; | ||
} | ||
|
||
public Dog() { | ||
} | ||
} | ||
} |