HHH-12549 Test the various missing cache strategies for hibernate-jcache
This commit is contained in:
parent
c52ff4d06c
commit
f70bb50412
|
@ -21,7 +21,8 @@ public abstract class BaseFunctionalTest extends BaseUnitTestCase {
|
|||
@Before
|
||||
public void createSessionFactory() {
|
||||
assert sessionFactory == null || sessionFactory.isClosed();
|
||||
sessionFactory = TestHelper.buildStandardSessionFactory( true );
|
||||
TestHelper.preBuildCaches();
|
||||
sessionFactory = TestHelper.buildStandardSessionFactory();
|
||||
}
|
||||
|
||||
@After
|
||||
|
|
|
@ -0,0 +1,132 @@
|
|||
/*
|
||||
* 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.jcache.test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
import org.hibernate.cache.CacheException;
|
||||
import org.hibernate.cache.jcache.ConfigSettings;
|
||||
import org.hibernate.cache.jcache.internal.JCacheMessageLogger;
|
||||
import org.hibernate.engine.spi.SessionFactoryImplementor;
|
||||
import org.hibernate.service.spi.ServiceException;
|
||||
|
||||
import org.hibernate.testing.junit4.BaseUnitTestCase;
|
||||
import org.hibernate.testing.logger.LoggerInspectionRule;
|
||||
import org.hibernate.testing.logger.Triggerable;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.hamcrest.CoreMatchers;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.notNullValue;
|
||||
import static org.hamcrest.CoreMatchers.nullValue;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* Tests around {@link org.hibernate.cache.jcache.MissingCacheStrategy}
|
||||
*
|
||||
* @author Steve Ebersole
|
||||
* @author Yoann Rodiere
|
||||
*/
|
||||
public class MissingCacheStrategyTest extends BaseUnitTestCase {
|
||||
|
||||
@Rule
|
||||
public LoggerInspectionRule logInspection = new LoggerInspectionRule( JCacheMessageLogger.INSTANCE );
|
||||
|
||||
@Test
|
||||
public void testMissingCacheStrategyDefault() {
|
||||
doTestMissingCacheStrategyFail(
|
||||
ignored -> { } // default settings
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMissingCacheStrategyFail() {
|
||||
doTestMissingCacheStrategyFail(
|
||||
builder -> builder.applySetting( ConfigSettings.MISSING_CACHE_STRATEGY, "fail" )
|
||||
);
|
||||
}
|
||||
|
||||
private void doTestMissingCacheStrategyFail(Consumer<StandardServiceRegistryBuilder> additionalSettings) {
|
||||
// first, lets make sure that the region names we think are non-existent really do not exist
|
||||
for ( String regionName : TestHelper.allRegionNames ) {
|
||||
assertThat( TestHelper.getCache( regionName ), nullValue() );
|
||||
}
|
||||
|
||||
// and now let's try to build the standard testing SessionFactory, without pre-defining caches
|
||||
try ( SessionFactoryImplementor ignored = TestHelper.buildStandardSessionFactory( additionalSettings ) ) {
|
||||
fail();
|
||||
}
|
||||
catch (ServiceException expected) {
|
||||
assertTyping( CacheException.class, expected.getCause() );
|
||||
assertThat( expected.getMessage(), CoreMatchers.equalTo( "Unable to create requested service [" + org.hibernate.cache.spi.CacheImplementor.class.getName() + "]" ) );
|
||||
assertThat( expected.getCause().getMessage(), CoreMatchers.startsWith( "On-the-fly creation of JCache Cache objects is not supported" ) );
|
||||
}
|
||||
catch (CacheException expected) {
|
||||
assertThat( expected.getMessage(), CoreMatchers.equalTo( "On-the-fly creation of JCache Cache objects is not supported" ) );
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMissingCacheStrategyCreate() {
|
||||
// first, lets make sure that the region names we think are non-existent really do not exist
|
||||
for ( String regionName : TestHelper.allRegionNames ) {
|
||||
assertThat( TestHelper.getCache( regionName ), nullValue() );
|
||||
}
|
||||
|
||||
// and now let's try to build the standard testing SessionFactory, without pre-defining caches
|
||||
try ( SessionFactoryImplementor ignored = TestHelper.buildStandardSessionFactory(
|
||||
builder -> builder.applySetting( ConfigSettings.MISSING_CACHE_STRATEGY, "create" )
|
||||
) ) {
|
||||
// The caches should have been created automatically
|
||||
for ( String regionName : TestHelper.allRegionNames ) {
|
||||
assertThat( "Cache '" + regionName + "' should have been created",
|
||||
TestHelper.getCache( regionName ), notNullValue() );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMissingCacheStrategyCreateWarn() {
|
||||
Map<String, Triggerable> triggerables = new HashMap<>();
|
||||
|
||||
// first, lets make sure that the region names we think are non-existent really do not exist
|
||||
for ( String regionName : TestHelper.allRegionNames ) {
|
||||
assertThat( TestHelper.getCache( regionName ), nullValue() );
|
||||
triggerables.put(
|
||||
regionName,
|
||||
logInspection.watchForLogMessages(
|
||||
"HHH025001: Missing cache[" + TestHelper.prefix( regionName ) + "] was created on-the-fly"
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
try ( SessionFactoryImplementor ignored = TestHelper.buildStandardSessionFactory(
|
||||
builder -> builder.applySetting( ConfigSettings.MISSING_CACHE_STRATEGY, "create-warn" )
|
||||
) ) {
|
||||
for ( String regionName : TestHelper.allRegionNames ) {
|
||||
// The caches should have been created automatically
|
||||
assertThat(
|
||||
"Cache '" + regionName + "' should have been created",
|
||||
TestHelper.getCache( regionName ), notNullValue()
|
||||
);
|
||||
// Logs should have been triggered
|
||||
assertTrue(
|
||||
"Cache '" + regionName + "' should have triggered a warning",
|
||||
triggerables.get( regionName ).wasTriggered()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -7,25 +7,18 @@
|
|||
package org.hibernate.jcache.test;
|
||||
|
||||
import javax.cache.Cache;
|
||||
import javax.cache.CacheManager;
|
||||
|
||||
import org.hibernate.cache.CacheException;
|
||||
import org.hibernate.cache.jcache.JCacheHelper;
|
||||
import org.hibernate.cache.jcache.internal.JCacheAccessImpl;
|
||||
import org.hibernate.cache.spi.Region;
|
||||
import org.hibernate.cache.spi.support.DomainDataRegionTemplate;
|
||||
import org.hibernate.engine.spi.SessionFactoryImplementor;
|
||||
import org.hibernate.service.spi.ServiceException;
|
||||
|
||||
import org.hibernate.testing.junit4.BaseUnitTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.hamcrest.CoreMatchers;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.nullValue;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
|
||||
import static org.hibernate.testing.transaction.TransactionUtil2.inSession;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
|
@ -34,42 +27,23 @@ import static org.junit.Assert.fail;
|
|||
*
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class StorageAccessTests extends BaseUnitTestCase {
|
||||
|
||||
public static final String NON_CACHE_NAME = "not-a-cache";
|
||||
|
||||
@Test
|
||||
public void testOnTheFlyCreationDisallowed() {
|
||||
// first, lets make sure that the region name we think is non-existent really does not exist
|
||||
final CacheManager cacheManager = JCacheHelper.locateStandardCacheManager();
|
||||
assertThat( cacheManager.getCache( NON_CACHE_NAME ), nullValue() );
|
||||
|
||||
// and now let's try to build the standard testing SessionFactory, without pre-defining caches
|
||||
try (SessionFactoryImplementor sessionFactory = TestHelper.buildStandardSessionFactory( false ) ) {
|
||||
fail();
|
||||
}
|
||||
catch (ServiceException expected) {
|
||||
assertTyping( CacheException.class, expected.getCause() );
|
||||
assertThat( expected.getMessage(), CoreMatchers.equalTo( "Unable to create requested service [" + org.hibernate.cache.spi.CacheImplementor.class.getName() + "]" ) );
|
||||
assertThat( expected.getCause().getMessage(), CoreMatchers.startsWith( "On-the-fly creation of JCache Cache objects is not supported" ) );
|
||||
}
|
||||
catch (CacheException expected) {
|
||||
assertThat( expected.getMessage(), CoreMatchers.equalTo( "On-the-fly creation of JCache Cache objects is not supported" ) );
|
||||
}
|
||||
}
|
||||
public class StorageAccessTest extends BaseUnitTestCase {
|
||||
|
||||
/**
|
||||
* Sort of the inverse test of {@link MissingCacheStrategyTest#testMissingCacheStrategyFail()}.
|
||||
* Here building the SF should succeed.
|
||||
*/
|
||||
@Test
|
||||
public void testPreDefinedCachesAllowed() {
|
||||
// sort of the inverse test of #testOnTheFlyCreationDisallowed. Here building the SF
|
||||
// should succeed
|
||||
|
||||
SessionFactoryImplementor sessionFactory = TestHelper.buildStandardSessionFactory( true );
|
||||
TestHelper.preBuildCaches();
|
||||
SessionFactoryImplementor sessionFactory = TestHelper.buildStandardSessionFactory();
|
||||
sessionFactory.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBasicStorageAccessUse() {
|
||||
try (final SessionFactoryImplementor sessionFactory = TestHelper.buildStandardSessionFactory( true ) ) {
|
||||
TestHelper.preBuildCaches();
|
||||
try (final SessionFactoryImplementor sessionFactory = TestHelper.buildStandardSessionFactory() ) {
|
||||
final Region region = sessionFactory.getCache().getRegion( TestHelper.entityRegionNames[0] );
|
||||
|
||||
final JCacheAccessImpl access = (JCacheAccessImpl) ( (DomainDataRegionTemplate) region ).getCacheStorageAccess();
|
||||
|
@ -93,7 +67,8 @@ public class StorageAccessTests extends BaseUnitTestCase {
|
|||
@Test
|
||||
@SuppressWarnings({"EmptyTryBlock", "unused"})
|
||||
public void testCachesReleasedOnSessionFactoryClose() {
|
||||
try (SessionFactoryImplementor sessionFactory = TestHelper.buildStandardSessionFactory( true ) ) {
|
||||
TestHelper.preBuildCaches();
|
||||
try (SessionFactoryImplementor sessionFactory = TestHelper.buildStandardSessionFactory() ) {
|
||||
}
|
||||
|
||||
TestHelper.visitAllRegions(
|
|
@ -6,10 +6,12 @@
|
|||
*/
|
||||
package org.hibernate.jcache.test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Stream;
|
||||
import javax.cache.Cache;
|
||||
import javax.cache.CacheManager;
|
||||
import javax.cache.configuration.MutableConfiguration;
|
||||
|
@ -49,28 +51,39 @@ public class TestHelper {
|
|||
org.hibernate.jcache.test.domain.Event.class.getName() + ".participants"
|
||||
};
|
||||
|
||||
public static SessionFactoryImplementor buildStandardSessionFactory(boolean preBuildCaches) {
|
||||
return buildStandardSessionFactory( preBuildCaches, true );
|
||||
public static String[] allRegionNames =
|
||||
Stream.concat( Arrays.stream( entityRegionNames ), Arrays.stream( collectionRegionNames ) )
|
||||
.toArray( String[]::new );
|
||||
|
||||
public static void preBuildCaches() {
|
||||
preBuildCaches( true );
|
||||
}
|
||||
|
||||
public static SessionFactoryImplementor buildStandardSessionFactory(boolean preBuildCaches, boolean prefixCaches) {
|
||||
if ( preBuildCaches ) {
|
||||
final CacheManager cacheManager = locateStandardCacheManager();
|
||||
public static void preBuildCaches(boolean prefixCaches) {
|
||||
final CacheManager cacheManager = locateStandardCacheManager();
|
||||
|
||||
for ( String regionName : entityRegionNames ) {
|
||||
createCache( cacheManager, regionName, prefixCaches );
|
||||
}
|
||||
|
||||
for ( String regionName : collectionRegionNames ) {
|
||||
createCache( cacheManager, regionName, prefixCaches );
|
||||
}
|
||||
|
||||
createCache( cacheManager, TimestampsRegion.class.getName(), prefixCaches );
|
||||
createCache( cacheManager, QueryResultsRegion.class.getName(), prefixCaches );
|
||||
for ( String regionName : entityRegionNames ) {
|
||||
createCache( cacheManager, regionName, prefixCaches );
|
||||
}
|
||||
|
||||
for ( String regionName : collectionRegionNames ) {
|
||||
createCache( cacheManager, regionName, prefixCaches );
|
||||
}
|
||||
|
||||
createCache( cacheManager, TimestampsRegion.class.getName(), prefixCaches );
|
||||
createCache( cacheManager, QueryResultsRegion.class.getName(), prefixCaches );
|
||||
}
|
||||
|
||||
public static SessionFactoryImplementor buildStandardSessionFactory() {
|
||||
return buildStandardSessionFactory( ignored -> { } );
|
||||
|
||||
}
|
||||
|
||||
public static SessionFactoryImplementor buildStandardSessionFactory(Consumer<StandardServiceRegistryBuilder> additionalSettings) {
|
||||
final StandardServiceRegistryBuilder ssrb = getStandardServiceRegistryBuilder();
|
||||
|
||||
additionalSettings.accept( ssrb );
|
||||
|
||||
final StandardServiceRegistry ssr = ssrb.build();
|
||||
|
||||
return (SessionFactoryImplementor) new MetadataSources( ssr ).buildMetadata().buildSessionFactory();
|
||||
|
@ -95,7 +108,7 @@ public class TestHelper {
|
|||
|
||||
public static void createCache(CacheManager cacheManager, String name, boolean usePrefix) {
|
||||
if ( usePrefix ) {
|
||||
name = RegionNameQualifier.INSTANCE.qualify( "hibernate.test", name );
|
||||
name = prefix( name );
|
||||
}
|
||||
|
||||
if ( cacheManager.getCache( name ) != null ) {
|
||||
|
@ -109,6 +122,16 @@ public class TestHelper {
|
|||
createCache( locateStandardCacheManager(), name );
|
||||
}
|
||||
|
||||
public static String prefix(String regionName) {
|
||||
return RegionNameQualifier.INSTANCE.qualify( "hibernate.test", regionName );
|
||||
}
|
||||
|
||||
public static Cache<?, ?> getCache(String regionName) {
|
||||
final CacheManager cacheManager = JCacheHelper.locateStandardCacheManager();
|
||||
regionName = prefix( regionName );
|
||||
return cacheManager.getCache( regionName );
|
||||
}
|
||||
|
||||
public static void visitAllRegions(Consumer<Cache> action) {
|
||||
final CacheManager cacheManager = JCacheHelper.locateStandardCacheManager();
|
||||
|
||||
|
|
Loading…
Reference in New Issue