HHH-12531 Default to using legacy region names when default query caches do not exist in the EhCache and JCache integrations
This commit is contained in:
parent
63cc63b386
commit
21eac287e9
|
@ -6,6 +6,9 @@
|
|||
*/
|
||||
package org.hibernate.cache.spi;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
|
@ -23,6 +26,24 @@ public abstract class AbstractRegionFactory implements RegionFactory {
|
|||
|
||||
private final AtomicBoolean started = new AtomicBoolean( false );
|
||||
|
||||
/**
|
||||
* Legacy names that used to be the default for the query results region.
|
||||
*/
|
||||
protected static final List<String> LEGACY_QUERY_RESULTS_REGION_UNQUALIFIED_NAMES =
|
||||
Collections.unmodifiableList( Arrays.asList(
|
||||
"org.hibernate.cache.spi.QueryResultsRegion",
|
||||
"org.hibernate.cache.internal.StandardQueryCache"
|
||||
) );
|
||||
|
||||
/**
|
||||
* Legacy names that used to be the default for the update timestamps region.
|
||||
*/
|
||||
protected static final List<String> LEGACY_UPDATE_TIMESTAMPS_REGION_UNQUALIFIED_NAMES =
|
||||
Collections.unmodifiableList( Arrays.asList(
|
||||
"org.hibernate.cache.spi.TimestampsRegion",
|
||||
"org.hibernate.cache.spi.UpdateTimestampsCache"
|
||||
) );
|
||||
|
||||
private Exception startingException;
|
||||
|
||||
private SessionFactoryOptions options;
|
||||
|
|
|
@ -82,4 +82,12 @@ public interface SecondLevelCacheLogger extends BasicLogger {
|
|||
)
|
||||
void missingCacheCreated(String regionName, String configurationPropertyToDisableKey, String configurationPropertyToDisableValue);
|
||||
|
||||
@LogMessage(level = WARN)
|
||||
@Message(
|
||||
value = "Using legacy cache name [%2$s] because configuration could not be found for cache [%1$s]." +
|
||||
" Update your configuration to rename cache [%2$s] to [%1$s].",
|
||||
id = NAMESPACE + 7
|
||||
)
|
||||
void usingLegacyCacheName(String currentName, String legacyName);
|
||||
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ package org.hibernate.cache.ehcache.internal;
|
|||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import net.sf.ehcache.Cache;
|
||||
|
@ -77,14 +78,43 @@ public class EhcacheRegionFactory extends RegionFactoryTemplate {
|
|||
protected StorageAccess createQueryResultsRegionStorageAccess(
|
||||
String regionName,
|
||||
SessionFactoryImplementor sessionFactory) {
|
||||
return new StorageAccessImpl( getOrCreateCache( regionName, sessionFactory ) );
|
||||
String defaultedRegionName = defaultRegionName(
|
||||
regionName,
|
||||
sessionFactory,
|
||||
DEFAULT_QUERY_RESULTS_REGION_UNQUALIFIED_NAME,
|
||||
LEGACY_QUERY_RESULTS_REGION_UNQUALIFIED_NAMES
|
||||
);
|
||||
return new StorageAccessImpl( getOrCreateCache( defaultedRegionName, sessionFactory ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StorageAccess createTimestampsRegionStorageAccess(
|
||||
String regionName,
|
||||
SessionFactoryImplementor sessionFactory) {
|
||||
return new StorageAccessImpl( getOrCreateCache( regionName, sessionFactory ) );
|
||||
String defaultedRegionName = defaultRegionName(
|
||||
regionName,
|
||||
sessionFactory,
|
||||
DEFAULT_UPDATE_TIMESTAMPS_REGION_UNQUALIFIED_NAME,
|
||||
LEGACY_UPDATE_TIMESTAMPS_REGION_UNQUALIFIED_NAMES
|
||||
);
|
||||
return new StorageAccessImpl( getOrCreateCache( defaultedRegionName, sessionFactory ) );
|
||||
}
|
||||
|
||||
protected final String defaultRegionName(String regionName, SessionFactoryImplementor sessionFactory,
|
||||
String defaultRegionName, List<String> legacyDefaultRegionNames) {
|
||||
if ( defaultRegionName.equals( regionName )
|
||||
&& !cacheExists( regionName, sessionFactory ) ) {
|
||||
// Maybe the user configured caches explicitly with legacy names; try them and use the first that exists
|
||||
|
||||
for ( String legacyDefaultRegionName : legacyDefaultRegionNames ) {
|
||||
if ( cacheExists( legacyDefaultRegionName, sessionFactory ) ) {
|
||||
SecondLevelCacheLogger.INSTANCE.usingLegacyCacheName( defaultRegionName, legacyDefaultRegionName );
|
||||
return legacyDefaultRegionName;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return regionName;
|
||||
}
|
||||
|
||||
protected Cache getOrCreateCache(String unqualifiedRegionName, SessionFactoryImplementor sessionFactory) {
|
||||
|
@ -122,6 +152,14 @@ public class EhcacheRegionFactory extends RegionFactoryTemplate {
|
|||
}
|
||||
}
|
||||
|
||||
protected boolean cacheExists(String unqualifiedRegionName, SessionFactoryImplementor sessionFactory) {
|
||||
final String qualifiedRegionName = RegionNameQualifier.INSTANCE.qualify(
|
||||
unqualifiedRegionName,
|
||||
sessionFactory.getSessionFactoryOptions()
|
||||
);
|
||||
return cacheManager.getCache( qualifiedRegionName ) != null;
|
||||
}
|
||||
|
||||
|
||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
// Lifecycle
|
||||
|
|
|
@ -26,6 +26,7 @@ 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;
|
||||
|
@ -66,6 +67,29 @@ public class MissingCacheStrategyTest extends BaseUnitTestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMissingCacheStrategyFail_legacyNames() {
|
||||
/*
|
||||
* The cache manager is created per session factory, and we don't use any specific ehcache configuration,
|
||||
* so we know the caches don't exist before we start the session factory.
|
||||
*/
|
||||
|
||||
// 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, "fail" )
|
||||
) ) {
|
||||
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 Ehcache Cache objects is not supported" ) );
|
||||
}
|
||||
catch (CacheException expected) {
|
||||
assertThat( expected.getMessage(), CoreMatchers.equalTo( "On-the-fly creation of Ehcache Cache objects is not supported" ) );
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMissingCacheStrategyCreate() {
|
||||
/*
|
||||
|
@ -78,7 +102,7 @@ public class MissingCacheStrategyTest extends BaseUnitTestCase {
|
|||
builder -> builder.applySetting( ConfigSettings.MISSING_CACHE_STRATEGY, "create" )
|
||||
) ) {
|
||||
// The caches should have been created automatically
|
||||
for ( String regionName : TestHelper.allRegionNames ) {
|
||||
for ( String regionName : TestHelper.allDomainRegionNames ) {
|
||||
assertThat( "Cache '" + regionName + "' should have been created",
|
||||
TestHelper.getCache( sessionFactory, regionName ), notNullValue() );
|
||||
}
|
||||
|
@ -99,7 +123,7 @@ public class MissingCacheStrategyTest extends BaseUnitTestCase {
|
|||
*/
|
||||
|
||||
Map<String, Triggerable> triggerables = new HashMap<>();
|
||||
for ( String regionName : TestHelper.allRegionNames ) {
|
||||
for ( String regionName : TestHelper.allDomainRegionNames ) {
|
||||
triggerables.put(
|
||||
regionName,
|
||||
logInspection.watchForLogMessages(
|
||||
|
@ -109,7 +133,7 @@ public class MissingCacheStrategyTest extends BaseUnitTestCase {
|
|||
}
|
||||
|
||||
try ( SessionFactoryImplementor sessionFactory = TestHelper.buildStandardSessionFactory( additionalSettings ) ) {
|
||||
for ( String regionName : TestHelper.allRegionNames ) {
|
||||
for ( String regionName : TestHelper.allDomainRegionNames ) {
|
||||
// The caches should have been created automatically
|
||||
assertThat(
|
||||
"Cache '" + regionName + "' should have been created",
|
||||
|
@ -124,4 +148,63 @@ public class MissingCacheStrategyTest extends BaseUnitTestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMissingCacheStrategyFailLegacyNames1() {
|
||||
doTestMissingCacheStrategyFailLegacyNames(
|
||||
"/hibernate-config/ehcache-explicitlegacy1.xml",
|
||||
TestHelper.queryRegionLegacyNames1, TestHelper.queryRegionLegacyNames2
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMissingCacheStrategyFailLegacyNames2() {
|
||||
doTestMissingCacheStrategyFailLegacyNames(
|
||||
"/hibernate-config/ehcache-explicitlegacy2.xml",
|
||||
TestHelper.queryRegionLegacyNames2, TestHelper.queryRegionLegacyNames1
|
||||
);
|
||||
}
|
||||
|
||||
private void doTestMissingCacheStrategyFailLegacyNames(String configurationPath,
|
||||
String[] existingLegacyCaches, String[] nonExistingLegacyCaches) {
|
||||
Map<String, Triggerable> triggerables = new HashMap<>();
|
||||
|
||||
// This is used later for log-related assertions
|
||||
for ( int i = 0; i < TestHelper.queryRegionNames.length; ++i ) {
|
||||
String currentName = TestHelper.queryRegionNames[i];
|
||||
String legacyName = existingLegacyCaches[i];
|
||||
|
||||
triggerables.put(
|
||||
legacyName,
|
||||
logInspection.watchForLogMessages(
|
||||
"HHH90001007: Using legacy cache name [" + legacyName +
|
||||
"] because configuration could not be found for cache [" + currentName + "]."
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// and now let's try to build the standard testing SessionFactory
|
||||
try ( SessionFactoryImplementor sessionFactory = TestHelper.buildStandardSessionFactory(
|
||||
builder -> builder.applySetting( ConfigSettings.MISSING_CACHE_STRATEGY, "fail" )
|
||||
.applySetting( ConfigSettings.EHCACHE_CONFIGURATION_RESOURCE_NAME, configurationPath )
|
||||
) ) {
|
||||
// The session should start successfully (if we reach this line, we're good)
|
||||
|
||||
// Logs should have been to notify that legacy cache names are being used
|
||||
for ( String regionName : existingLegacyCaches ) {
|
||||
assertTrue(
|
||||
"Use of cache '" + regionName + "' should have triggered a warning",
|
||||
triggerables.get( regionName ).wasTriggered()
|
||||
);
|
||||
}
|
||||
|
||||
// and these caches shouldn't exist
|
||||
for ( String regionName : nonExistingLegacyCaches ) {
|
||||
assertThat( TestHelper.getCache( sessionFactory, regionName ), nullValue() );
|
||||
}
|
||||
for ( String regionName : TestHelper.queryRegionNames ) {
|
||||
assertThat( TestHelper.getCache( sessionFactory, regionName ), nullValue() );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -41,13 +41,25 @@ public class TestHelper {
|
|||
Event.class.getName() + ".participants"
|
||||
};
|
||||
|
||||
public static String[] allRegionNames =
|
||||
public static String[] allDomainRegionNames =
|
||||
Stream.concat( Arrays.stream( entityRegionNames ), Arrays.stream( collectionRegionNames ) )
|
||||
.toArray( String[]::new );
|
||||
|
||||
public static String[] queryRegionNames = new String[] {
|
||||
RegionFactory.DEFAULT_QUERY_RESULTS_REGION_UNQUALIFIED_NAME,
|
||||
RegionFactory.DEFAULT_UPDATE_TIMESTAMPS_REGION_UNQUALIFIED_NAME
|
||||
};
|
||||
public static String[] queryRegionLegacyNames1 = new String[] {
|
||||
"org.hibernate.cache.spi.QueryResultsRegion",
|
||||
"org.hibernate.cache.spi.TimestampsRegion"
|
||||
};
|
||||
public static String[] queryRegionLegacyNames2 = new String[] {
|
||||
"org.hibernate.cache.internal.StandardQueryCache",
|
||||
"org.hibernate.cache.spi.UpdateTimestampsCache"
|
||||
};
|
||||
|
||||
public static SessionFactoryImplementor buildStandardSessionFactory() {
|
||||
return buildStandardSessionFactory( ignored -> { } );
|
||||
|
||||
}
|
||||
|
||||
public static SessionFactoryImplementor buildStandardSessionFactory(Consumer<StandardServiceRegistryBuilder> additionalSettings) {
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
<!--
|
||||
~ 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>.
|
||||
-->
|
||||
<ehcache>
|
||||
<diskStore path="java.io.tmpdir"/>
|
||||
|
||||
<!-- Domain caches -->
|
||||
<cache name="hibernate.test.org.hibernate.cache.ehcache.test.domain.Item" maxElementsInMemory="10000" />
|
||||
<cache name="hibernate.test.org.hibernate.cache.ehcache.test.domain.VersionedItem" maxElementsInMemory="10000" />
|
||||
<cache name="hibernate.test.org.hibernate.cache.ehcache.test.domain.Event" maxElementsInMemory="10000" />
|
||||
<cache name="hibernate.test.org.hibernate.cache.ehcache.test.domain.Event.participants" maxElementsInMemory="10000" />
|
||||
|
||||
<!-- Query caches with legacy names -->
|
||||
<cache name="hibernate.test.org.hibernate.cache.spi.QueryResultsRegion" maxElementsInMemory="10000" />
|
||||
<cache name="hibernate.test.org.hibernate.cache.spi.TimestampsRegion" maxElementsInMemory="10000" />
|
||||
|
||||
</ehcache>
|
|
@ -0,0 +1,20 @@
|
|||
<!--
|
||||
~ 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>.
|
||||
-->
|
||||
<ehcache>
|
||||
<diskStore path="java.io.tmpdir"/>
|
||||
|
||||
<!-- Domain caches -->
|
||||
<cache name="hibernate.test.org.hibernate.cache.ehcache.test.domain.Item" maxElementsInMemory="10000" />
|
||||
<cache name="hibernate.test.org.hibernate.cache.ehcache.test.domain.VersionedItem" maxElementsInMemory="10000" />
|
||||
<cache name="hibernate.test.org.hibernate.cache.ehcache.test.domain.Event" maxElementsInMemory="10000" />
|
||||
<cache name="hibernate.test.org.hibernate.cache.ehcache.test.domain.Event.participants" maxElementsInMemory="10000" />
|
||||
|
||||
<!-- Query caches with legacy names -->
|
||||
<cache name="hibernate.test.org.hibernate.cache.internal.StandardQueryCache" maxElementsInMemory="10000" />
|
||||
<cache name="hibernate.test.org.hibernate.cache.spi.UpdateTimestampsCache" maxElementsInMemory="10000" />
|
||||
|
||||
</ehcache>
|
|
@ -8,6 +8,7 @@ package org.hibernate.cache.jcache.internal;
|
|||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import javax.cache.Cache;
|
||||
import javax.cache.CacheManager;
|
||||
|
@ -103,12 +104,26 @@ public class JCacheRegionFactory extends RegionFactoryTemplate {
|
|||
}
|
||||
}
|
||||
|
||||
protected boolean cacheExists(String unqualifiedRegionName, SessionFactoryImplementor sessionFactory) {
|
||||
final String qualifiedRegionName = RegionNameQualifier.INSTANCE.qualify(
|
||||
unqualifiedRegionName,
|
||||
sessionFactory.getSessionFactoryOptions()
|
||||
);
|
||||
return cacheManager.getCache( qualifiedRegionName ) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StorageAccess createQueryResultsRegionStorageAccess(
|
||||
String regionName,
|
||||
SessionFactoryImplementor sessionFactory) {
|
||||
String defaultedRegionName = defaultRegionName(
|
||||
regionName,
|
||||
sessionFactory,
|
||||
DEFAULT_QUERY_RESULTS_REGION_UNQUALIFIED_NAME,
|
||||
LEGACY_QUERY_RESULTS_REGION_UNQUALIFIED_NAMES
|
||||
);
|
||||
return new JCacheAccessImpl(
|
||||
getOrCreateCache( regionName, sessionFactory )
|
||||
getOrCreateCache( defaultedRegionName, sessionFactory )
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -116,9 +131,32 @@ public class JCacheRegionFactory extends RegionFactoryTemplate {
|
|||
protected StorageAccess createTimestampsRegionStorageAccess(
|
||||
String regionName,
|
||||
SessionFactoryImplementor sessionFactory) {
|
||||
return new JCacheAccessImpl(
|
||||
getOrCreateCache( regionName, sessionFactory )
|
||||
String defaultedRegionName = defaultRegionName(
|
||||
regionName,
|
||||
sessionFactory,
|
||||
DEFAULT_UPDATE_TIMESTAMPS_REGION_UNQUALIFIED_NAME,
|
||||
LEGACY_UPDATE_TIMESTAMPS_REGION_UNQUALIFIED_NAMES
|
||||
);
|
||||
return new JCacheAccessImpl(
|
||||
getOrCreateCache( defaultedRegionName, sessionFactory )
|
||||
);
|
||||
}
|
||||
|
||||
protected final String defaultRegionName(String regionName, SessionFactoryImplementor sessionFactory,
|
||||
String defaultRegionName, List<String> legacyDefaultRegionNames) {
|
||||
if ( defaultRegionName.equals( regionName )
|
||||
&& !cacheExists( regionName, sessionFactory ) ) {
|
||||
// Maybe the user configured caches explicitly with legacy names; try them and use the first that exists
|
||||
|
||||
for ( String legacyDefaultRegionName : legacyDefaultRegionNames ) {
|
||||
if ( cacheExists( legacyDefaultRegionName, sessionFactory ) ) {
|
||||
SecondLevelCacheLogger.INSTANCE.usingLegacyCacheName( defaultRegionName, legacyDefaultRegionName );
|
||||
return legacyDefaultRegionName;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return regionName;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -21,7 +21,7 @@ public abstract class BaseFunctionalTest extends BaseUnitTestCase {
|
|||
@Before
|
||||
public void createSessionFactory() {
|
||||
assert sessionFactory == null || sessionFactory.isClosed();
|
||||
TestHelper.preBuildCaches();
|
||||
TestHelper.preBuildAllCaches();
|
||||
sessionFactory = TestHelper.buildStandardSessionFactory();
|
||||
}
|
||||
|
||||
|
|
|
@ -59,7 +59,7 @@ public class MissingCacheStrategyTest extends BaseUnitTestCase {
|
|||
|
||||
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 ) {
|
||||
for ( String regionName : TestHelper.allDomainRegionNames ) {
|
||||
assertThat( TestHelper.getCache( regionName ), nullValue() );
|
||||
}
|
||||
|
||||
|
@ -80,7 +80,7 @@ public class MissingCacheStrategyTest extends BaseUnitTestCase {
|
|||
@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 ) {
|
||||
for ( String regionName : TestHelper.allDomainRegionNames ) {
|
||||
assertThat( TestHelper.getCache( regionName ), nullValue() );
|
||||
}
|
||||
|
||||
|
@ -89,7 +89,7 @@ public class MissingCacheStrategyTest extends BaseUnitTestCase {
|
|||
builder -> builder.applySetting( ConfigSettings.MISSING_CACHE_STRATEGY, "create" )
|
||||
) ) {
|
||||
// The caches should have been created automatically
|
||||
for ( String regionName : TestHelper.allRegionNames ) {
|
||||
for ( String regionName : TestHelper.allDomainRegionNames ) {
|
||||
assertThat( "Cache '" + regionName + "' should have been created",
|
||||
TestHelper.getCache( regionName ), notNullValue() );
|
||||
}
|
||||
|
@ -101,7 +101,7 @@ public class MissingCacheStrategyTest extends BaseUnitTestCase {
|
|||
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 ) {
|
||||
for ( String regionName : TestHelper.allDomainRegionNames ) {
|
||||
assertThat( TestHelper.getCache( regionName ), nullValue() );
|
||||
triggerables.put(
|
||||
regionName,
|
||||
|
@ -114,7 +114,7 @@ public class MissingCacheStrategyTest extends BaseUnitTestCase {
|
|||
try ( SessionFactoryImplementor ignored = TestHelper.buildStandardSessionFactory(
|
||||
builder -> builder.applySetting( ConfigSettings.MISSING_CACHE_STRATEGY, "create-warn" )
|
||||
) ) {
|
||||
for ( String regionName : TestHelper.allRegionNames ) {
|
||||
for ( String regionName : TestHelper.allDomainRegionNames ) {
|
||||
// The caches should have been created automatically
|
||||
assertThat(
|
||||
"Cache '" + regionName + "' should have been created",
|
||||
|
@ -129,4 +129,69 @@ public class MissingCacheStrategyTest extends BaseUnitTestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMissingCacheStrategyFailLegacyNames1() {
|
||||
doTestMissingCacheStrategyFailLegacyNames( TestHelper.queryRegionLegacyNames1, TestHelper.queryRegionLegacyNames2 );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMissingCacheStrategyFailLegacyNames2() {
|
||||
doTestMissingCacheStrategyFailLegacyNames( TestHelper.queryRegionLegacyNames2, TestHelper.queryRegionLegacyNames1 );
|
||||
}
|
||||
|
||||
private void doTestMissingCacheStrategyFailLegacyNames(String[] existingLegacyCaches, String[] nonExistingLegacyCaches) {
|
||||
Map<String, Triggerable> triggerables = new HashMap<>();
|
||||
|
||||
// first, lets make sure that the regions used for model caches exist
|
||||
TestHelper.preBuildDomainCaches();
|
||||
|
||||
// and that caches exist with legacy configurations
|
||||
for ( int i = 0; i < TestHelper.queryRegionNames.length; ++i ) {
|
||||
String currentName = TestHelper.queryRegionNames[i];
|
||||
String legacyName = existingLegacyCaches[i];
|
||||
|
||||
TestHelper.createCache( legacyName );
|
||||
|
||||
// This is used later for log-related assertions
|
||||
triggerables.put(
|
||||
legacyName,
|
||||
logInspection.watchForLogMessages(
|
||||
"HHH90001007: Using legacy cache name [" + legacyName +
|
||||
"] because configuration could not be found for cache [" + currentName + "]."
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// and then lets make sure that the region names we think are non-existent really do not exist
|
||||
for ( String regionName : nonExistingLegacyCaches ) {
|
||||
assertThat( TestHelper.getCache( regionName ), nullValue() );
|
||||
}
|
||||
for ( String regionName : TestHelper.queryRegionNames ) {
|
||||
assertThat( TestHelper.getCache( regionName ), nullValue() );
|
||||
}
|
||||
|
||||
// and now let's try to build the standard testing SessionFactory
|
||||
try ( SessionFactoryImplementor ignored = TestHelper.buildStandardSessionFactory(
|
||||
builder -> builder.applySetting( ConfigSettings.MISSING_CACHE_STRATEGY, "fail" )
|
||||
) ) {
|
||||
// The session should start successfully (if we reach this line, we're good)
|
||||
|
||||
// Logs should have been to notify that legacy cache names are being used
|
||||
for ( String regionName : existingLegacyCaches ) {
|
||||
assertTrue(
|
||||
"Use of cache '" + regionName + "' should have triggered a warning",
|
||||
triggerables.get( regionName ).wasTriggered()
|
||||
);
|
||||
}
|
||||
|
||||
// and these caches still shouldn't exist
|
||||
for ( String regionName : nonExistingLegacyCaches ) {
|
||||
assertThat( TestHelper.getCache( regionName ), nullValue() );
|
||||
}
|
||||
for ( String regionName : TestHelper.queryRegionNames ) {
|
||||
assertThat( TestHelper.getCache( regionName ), nullValue() );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -35,14 +35,14 @@ public class StorageAccessTest extends BaseUnitTestCase {
|
|||
*/
|
||||
@Test
|
||||
public void testPreDefinedCachesAllowed() {
|
||||
TestHelper.preBuildCaches();
|
||||
TestHelper.preBuildAllCaches();
|
||||
SessionFactoryImplementor sessionFactory = TestHelper.buildStandardSessionFactory();
|
||||
sessionFactory.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBasicStorageAccessUse() {
|
||||
TestHelper.preBuildCaches();
|
||||
TestHelper.preBuildAllCaches();
|
||||
try (final SessionFactoryImplementor sessionFactory = TestHelper.buildStandardSessionFactory() ) {
|
||||
final Region region = sessionFactory.getCache().getRegion( TestHelper.entityRegionNames[0] );
|
||||
|
||||
|
@ -67,11 +67,11 @@ public class StorageAccessTest extends BaseUnitTestCase {
|
|||
@Test
|
||||
@SuppressWarnings({"EmptyTryBlock", "unused"})
|
||||
public void testCachesReleasedOnSessionFactoryClose() {
|
||||
TestHelper.preBuildCaches();
|
||||
TestHelper.preBuildAllCaches();
|
||||
try (SessionFactoryImplementor sessionFactory = TestHelper.buildStandardSessionFactory() ) {
|
||||
}
|
||||
|
||||
TestHelper.visitAllRegions(
|
||||
TestHelper.visitDomainRegions(
|
||||
cache -> {
|
||||
if ( cache == null ) {
|
||||
return;
|
||||
|
|
|
@ -48,15 +48,42 @@ public class TestHelper {
|
|||
org.hibernate.jcache.test.domain.Event.class.getName() + ".participants"
|
||||
};
|
||||
|
||||
public static String[] allRegionNames =
|
||||
public static String[] allDomainRegionNames =
|
||||
Stream.concat( Arrays.stream( entityRegionNames ), Arrays.stream( collectionRegionNames ) )
|
||||
.toArray( String[]::new );
|
||||
|
||||
public static void preBuildCaches() {
|
||||
preBuildCaches( true );
|
||||
public static String[] queryRegionNames = new String[] {
|
||||
RegionFactory.DEFAULT_QUERY_RESULTS_REGION_UNQUALIFIED_NAME,
|
||||
RegionFactory.DEFAULT_UPDATE_TIMESTAMPS_REGION_UNQUALIFIED_NAME
|
||||
};
|
||||
public static String[] queryRegionLegacyNames1 = new String[] {
|
||||
"org.hibernate.cache.spi.QueryResultsRegion",
|
||||
"org.hibernate.cache.spi.TimestampsRegion"
|
||||
};
|
||||
public static String[] queryRegionLegacyNames2 = new String[] {
|
||||
"org.hibernate.cache.internal.StandardQueryCache",
|
||||
"org.hibernate.cache.spi.UpdateTimestampsCache"
|
||||
};
|
||||
|
||||
public static void preBuildAllCaches() {
|
||||
preBuildAllCaches( true );
|
||||
}
|
||||
|
||||
public static void preBuildCaches(boolean prefixCaches) {
|
||||
public static void preBuildAllCaches(boolean prefixCaches) {
|
||||
preBuildDomainCaches( prefixCaches );
|
||||
|
||||
final CacheManager cacheManager = locateStandardCacheManager();
|
||||
|
||||
for ( String regionName : queryRegionNames ) {
|
||||
createCache( cacheManager, regionName, prefixCaches );
|
||||
}
|
||||
}
|
||||
|
||||
public static void preBuildDomainCaches() {
|
||||
preBuildDomainCaches( true );
|
||||
}
|
||||
|
||||
public static void preBuildDomainCaches(boolean prefixCaches) {
|
||||
final CacheManager cacheManager = locateStandardCacheManager();
|
||||
|
||||
for ( String regionName : entityRegionNames ) {
|
||||
|
@ -66,14 +93,10 @@ public class TestHelper {
|
|||
for ( String regionName : collectionRegionNames ) {
|
||||
createCache( cacheManager, regionName, prefixCaches );
|
||||
}
|
||||
|
||||
createCache( cacheManager, RegionFactory.DEFAULT_UPDATE_TIMESTAMPS_REGION_UNQUALIFIED_NAME, prefixCaches );
|
||||
createCache( cacheManager, RegionFactory.DEFAULT_QUERY_RESULTS_REGION_UNQUALIFIED_NAME, prefixCaches );
|
||||
}
|
||||
|
||||
public static SessionFactoryImplementor buildStandardSessionFactory() {
|
||||
return buildStandardSessionFactory( ignored -> { } );
|
||||
|
||||
}
|
||||
|
||||
public static SessionFactoryImplementor buildStandardSessionFactory(Consumer<StandardServiceRegistryBuilder> additionalSettings) {
|
||||
|
@ -129,7 +152,7 @@ public class TestHelper {
|
|||
return cacheManager.getCache( regionName );
|
||||
}
|
||||
|
||||
public static void visitAllRegions(Consumer<Cache> action) {
|
||||
public static void visitDomainRegions(Consumer<Cache> action) {
|
||||
final CacheManager cacheManager = JCacheHelper.locateStandardCacheManager();
|
||||
|
||||
for ( String regionName : entityRegionNames ) {
|
||||
|
@ -184,8 +207,9 @@ public class TestHelper {
|
|||
}
|
||||
|
||||
if ( queryRegions ) {
|
||||
createCache( cacheManager, RegionFactory.DEFAULT_UPDATE_TIMESTAMPS_REGION_UNQUALIFIED_NAME, prefixRegions );
|
||||
createCache( cacheManager, RegionFactory.DEFAULT_QUERY_RESULTS_REGION_UNQUALIFIED_NAME, prefixRegions );
|
||||
for ( String regionName : queryRegionNames ) {
|
||||
createCache( cacheManager, regionName, prefixRegions );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue