HHH-12560 - Make sure only one Service registration (initiator/provided) exists per role

- additional test - this one needed a minor change.. all tests implicitly use values defined in `hibernate.properties`, which hibernate-core has.  This lead to `#getFallback` being called, which was the intended solution.  The fix for that was simply the next point.
- added the ability to clear the settings contained on StandardServiceRegistryBuilder.  This is used in the test to make sure that no explicit RegionFactory is set.
This commit is contained in:
Steve Ebersole 2018-05-11 15:38:47 -05:00
parent 87c7915a5c
commit 1a4577d1f0
2 changed files with 53 additions and 15 deletions

View File

@ -208,6 +208,10 @@ public class StandardServiceRegistryBuilder {
return this;
}
public void clearSettings() {
settings.clear();
}
/**
* Adds a service initiator.
*

View File

@ -11,11 +11,15 @@ import java.util.Map;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cache.internal.RegionFactoryInitiator;
import org.hibernate.cache.spi.RegionFactory;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.service.spi.ServiceRegistryImplementor;
import org.hibernate.testing.cache.CachingRegionFactory;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.junit.Test;
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
@ -23,32 +27,62 @@ import static org.junit.Assert.assertTrue;
*/
public class ServiceContributorTest extends BaseUnitTestCase {
@Test
public void overrideInitiator() {
public void overrideCachingInitiator() {
StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder();
class MyRegionFactoryInitiator extends RegionFactoryInitiator {
private boolean called = false;
@Override
public RegionFactory initiateService(
Map configurationValues,
ServiceRegistryImplementor registry) {
called = true;
return super.initiateService( configurationValues, registry );
}
}
ssrb.clearSettings();
final MyRegionFactoryInitiator initiator = new MyRegionFactoryInitiator();
ssrb.addInitiator( initiator );
final ServiceRegistryImplementor registry = (ServiceRegistryImplementor) ssrb.build();
try {
registry.getService( RegionFactory.class );
final RegionFactory regionFactory = registry.getService( RegionFactory.class );
assertTrue( initiator.called );
assertTyping( MyRegionFactory.class, regionFactory );
}
finally {
StandardServiceRegistryBuilder.destroy( registry );
}
}
@Test
public void overrideCachingInitiatorExplicitSet() {
StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder();
final MyRegionFactoryInitiator initiator = new MyRegionFactoryInitiator();
ssrb.addInitiator( initiator );
ssrb.applySetting( AvailableSettings.CACHE_REGION_FACTORY, new MyRegionFactory() );
final ServiceRegistryImplementor registry = (ServiceRegistryImplementor) ssrb.build();
try {
registry.getService( RegionFactory.class );
assertFalse( initiator.called );
}
finally {
StandardServiceRegistryBuilder.destroy( registry );
}
}
class MyRegionFactoryInitiator extends RegionFactoryInitiator {
private boolean called = false;
@Override
protected RegionFactory getFallback(
Map configurationValues,
ServiceRegistryImplementor registry) {
called = true;
return new MyRegionFactory();
}
// @Override
// public RegionFactory initiateService(
// Map configurationValues,
// ServiceRegistryImplementor registry) {
// called = true;
// return super.initiateService( configurationValues, registry );
// }
}
class MyRegionFactory extends CachingRegionFactory {
}
}