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:
parent
87c7915a5c
commit
1a4577d1f0
|
@ -208,6 +208,10 @@ public class StandardServiceRegistryBuilder {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void clearSettings() {
|
||||||
|
settings.clear();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a service initiator.
|
* Adds a service initiator.
|
||||||
*
|
*
|
||||||
|
|
|
@ -11,11 +11,15 @@ import java.util.Map;
|
||||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||||
import org.hibernate.cache.internal.RegionFactoryInitiator;
|
import org.hibernate.cache.internal.RegionFactoryInitiator;
|
||||||
import org.hibernate.cache.spi.RegionFactory;
|
import org.hibernate.cache.spi.RegionFactory;
|
||||||
|
import org.hibernate.cfg.AvailableSettings;
|
||||||
import org.hibernate.service.spi.ServiceRegistryImplementor;
|
import org.hibernate.service.spi.ServiceRegistryImplementor;
|
||||||
|
|
||||||
|
import org.hibernate.testing.cache.CachingRegionFactory;
|
||||||
import org.hibernate.testing.junit4.BaseUnitTestCase;
|
import org.hibernate.testing.junit4.BaseUnitTestCase;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -23,32 +27,62 @@ import static org.junit.Assert.assertTrue;
|
||||||
*/
|
*/
|
||||||
public class ServiceContributorTest extends BaseUnitTestCase {
|
public class ServiceContributorTest extends BaseUnitTestCase {
|
||||||
@Test
|
@Test
|
||||||
public void overrideInitiator() {
|
public void overrideCachingInitiator() {
|
||||||
StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder();
|
StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder();
|
||||||
|
ssrb.clearSettings();
|
||||||
class MyRegionFactoryInitiator extends RegionFactoryInitiator {
|
|
||||||
private boolean called = false;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public RegionFactory initiateService(
|
|
||||||
Map configurationValues,
|
|
||||||
ServiceRegistryImplementor registry) {
|
|
||||||
called = true;
|
|
||||||
return super.initiateService( configurationValues, registry );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
final MyRegionFactoryInitiator initiator = new MyRegionFactoryInitiator();
|
final MyRegionFactoryInitiator initiator = new MyRegionFactoryInitiator();
|
||||||
|
|
||||||
ssrb.addInitiator( initiator );
|
ssrb.addInitiator( initiator );
|
||||||
|
|
||||||
final ServiceRegistryImplementor registry = (ServiceRegistryImplementor) ssrb.build();
|
final ServiceRegistryImplementor registry = (ServiceRegistryImplementor) ssrb.build();
|
||||||
try {
|
try {
|
||||||
registry.getService( RegionFactory.class );
|
final RegionFactory regionFactory = registry.getService( RegionFactory.class );
|
||||||
assertTrue( initiator.called );
|
assertTrue( initiator.called );
|
||||||
|
assertTyping( MyRegionFactory.class, regionFactory );
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
StandardServiceRegistryBuilder.destroy( registry );
|
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 {
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue