Skip to content

Instantly share code, notes, and snippets.

@sebersole
Forked from scottmarlow/MultipleInitiatorTest.java
Last active September 21, 2018 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebersole/d93a28b1dcf16f6f7eb823b7341a9097 to your computer and use it in GitHub Desktop.
Save sebersole/d93a28b1dcf16f6f7eb823b7341a9097 to your computer and use it in GitHub Desktop.
MultipleInitiatorTest
package org.hibernate.service;
import java.util.Map;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cache.internal.RegionFactoryInitiator;
import org.hibernate.cache.spi.RegionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.spi.ServiceContributor;
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.junit.Assert.assertEquals;
public class MultipleInitiatorTest extends BaseUnitTestCase {
@Test
public void testTwoSessionFactories() {
SessionFactoryImplementor sf1 = null, sf2 = null;
try {
// NOTE : Assumes that the initiator is discovered through a ServiceContributor...
// NOTE : Also assumes that the fallback RegionFactory is triggered
assertEquals( 0, initiator.contributeCalledCount );
assertEquals( 0, initiator.getFallbackCalledCount );
sf1 = new Configuration().buildSessionFactory();
assertEquals( 1, initiator.contributeCalledCount );
assertEquals( 1, initiator.getFallbackCalledCount );
sf2 = new Configuration().buildSessionFactory();
assertEquals( 2, initiator.contributeCalledCount );
assertEquals( 2, initiator.getFallbackCalledCount );
// addionally:
final RegionFactory rf1 = ( (SessionFactoryImplementor) sf1 ).getServiceRegistry();
final RegionFactory rf2 = ( (SessionFactoryImplementor) sf2 ).getServiceRegistry();
// we should have hit the fallback (apparently)
assert rf1 instanceof MyRegionFactory;
assert rf2 instanceof MyRegionFactory;
// we should have 2 distinct RegionFactory instances
assert rf1 != rf2;
}
finally {
if ( sf1 != null ) {
sf1.close();
}
if ( sf2 != null ) {
sf2.close();
}
}
}
class MyRegionFactoryInitiator extends RegionFactoryInitiator implements ServiceContributor, Service {
private static int getFallbackCalledCount = 0;
private static int contributeCalledCount = 0;
@Override
protected RegionFactory getFallback(
Map configurationValues,
ServiceRegistryImplementor registry) {
getFallbackCalledCount++;
return new MyRegionFactory();
}
@Override
public void contribute(StandardServiceRegistryBuilder serviceRegistryBuilder) {
contributeCalledCount++;
}
}
class MyRegionFactory extends CachingRegionFactory {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment