update infinispan test due to the change of cache service

This commit is contained in:
Strong Liu 2013-05-08 15:37:37 +08:00
parent b9249e548a
commit a069d18a4f
3 changed files with 123 additions and 119 deletions

View File

@ -50,6 +50,7 @@ import org.hibernate.engine.transaction.jta.platform.internal.AbstractJtaPlatfor
import org.hibernate.engine.transaction.jta.platform.internal.JBossStandAloneJtaPlatform; import org.hibernate.engine.transaction.jta.platform.internal.JBossStandAloneJtaPlatform;
import org.hibernate.metamodel.MetadataSources; import org.hibernate.metamodel.MetadataSources;
import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.spi.ServiceException;
import org.hibernate.test.cache.infinispan.functional.SingleNodeTestCase; import org.hibernate.test.cache.infinispan.functional.SingleNodeTestCase;
import org.hibernate.testing.ServiceRegistryBuilder; import org.hibernate.testing.ServiceRegistryBuilder;
@ -437,6 +438,9 @@ public class InfinispanRegionFactoryTestCase {
fail( "Should fail cos no eviction configurations are allowed for timestamp caches" ); fail( "Should fail cos no eviction configurations are allowed for timestamp caches" );
} }
catch ( CacheException ce ) { catch ( CacheException ce ) {
}
catch ( ServiceException e) {
} }
finally { finally {
if ( sf != null ) { if ( sf != null ) {

View File

@ -26,9 +26,7 @@ package org.hibernate.test.cache.infinispan.tm;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map;
import java.util.Properties; import java.util.Properties;
import javax.naming.Context; import javax.naming.Context;
@ -82,6 +80,7 @@ public class JBossStandaloneJtaExampleTest {
Context ctx; Context ctx;
Main jndiServer; Main jndiServer;
private ServiceRegistry serviceRegistry; private ServiceRegistry serviceRegistry;
private SessionFactory sessionFactory;
@Before @Before
@ -92,6 +91,45 @@ public class JBossStandaloneJtaExampleTest {
lookup.init( new ConfigurationBuilder().build() ); lookup.init( new ConfigurationBuilder().build() );
bindTransactionManager(); bindTransactionManager();
bindUserTransaction(); bindUserTransaction();
// Extra options located in src/test/resources/hibernate.properties
Configuration cfg = new Configuration();
cfg.setProperty( Environment.DIALECT, "HSQL" );
cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
cfg.setProperty( Environment.CONNECTION_PROVIDER, JtaAwareConnectionProviderImpl.class.getName() );
cfg.setProperty( Environment.JNDI_CLASS, "org.jnp.interfaces.NamingContextFactory" );
cfg.setProperty( Environment.TRANSACTION_STRATEGY, "jta" );
cfg.setProperty( Environment.CURRENT_SESSION_CONTEXT_CLASS, "jta" );
cfg.setProperty( Environment.RELEASE_CONNECTIONS, "auto" );
cfg.setProperty( Environment.USE_SECOND_LEVEL_CACHE, "true" );
cfg.setProperty( Environment.USE_QUERY_CACHE, "true" );
Properties envProps = Environment.getProperties();
envProps.putAll( cfg.getProperties() );
envProps.put( AvailableSettings.JTA_PLATFORM, new JBossStandAloneJtaPlatform() );
envProps.setProperty(
Environment.CACHE_REGION_FACTORY,
"org.hibernate.test.cache.infinispan.functional.SingleNodeTestCase$TestInfinispanRegionFactory"
);
serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry( envProps );
String[] mappings = new String[] { "org/hibernate/test/cache/infinispan/functional/Item.hbm.xml" };
for ( String mapping : mappings ) {
cfg.addResource( mapping, Thread.currentThread().getContextClassLoader() );
}
cfg.buildMappings();
Iterator iter = cfg.getClassMappings();
while ( iter.hasNext() ) {
PersistentClass clazz = (PersistentClass) iter.next();
cfg.setCacheConcurrencyStrategy( clazz.getEntityName(), "transactional" );
}
iter = cfg.getCollectionMappings();
while ( iter.hasNext() ) {
Collection coll = (Collection) iter.next();
cfg.setCollectionCacheConcurrencyStrategy( coll.getRole(), "transactional" );
}
sessionFactory = cfg.buildSessionFactory( serviceRegistry );
} }
@After @After
@ -103,6 +141,9 @@ public class JBossStandaloneJtaExampleTest {
jndiServer.stop(); jndiServer.stop();
} }
finally { finally {
if ( sessionFactory != null ) {
sessionFactory.close();
}
if ( serviceRegistry != null ) { if ( serviceRegistry != null ) {
ServiceRegistryBuilder.destroy( serviceRegistry ); ServiceRegistryBuilder.destroy( serviceRegistry );
} }
@ -112,84 +153,76 @@ public class JBossStandaloneJtaExampleTest {
@Test @Test
public void testPersistAndLoadUnderJta() throws Exception { public void testPersistAndLoadUnderJta() throws Exception {
Item item; Item item;
SessionFactory sessionFactory = buildSessionFactory(); UserTransaction ut = (UserTransaction) ctx.lookup( "UserTransaction" );
ut.begin();
try { try {
UserTransaction ut = (UserTransaction) ctx.lookup( "UserTransaction" ); Session session = sessionFactory.openSession();
ut.begin(); session.getTransaction().begin();
try { item = new Item( "anItem", "An item owned by someone" );
Session session = sessionFactory.openSession(); session.persist( item );
session.getTransaction().begin(); session.getTransaction().commit();
item = new Item( "anItem", "An item owned by someone" ); session.close();
session.persist( item ); }
session.getTransaction().commit(); catch ( Exception e ) {
session.close(); ut.setRollbackOnly();
} throw e;
catch ( Exception e ) {
ut.setRollbackOnly();
throw e;
}
finally {
if ( ut.getStatus() == Status.STATUS_ACTIVE ) {
ut.commit();
}
else {
ut.rollback();
}
}
ut = (UserTransaction) ctx.lookup( "UserTransaction" );
ut.begin();
try {
Session session = sessionFactory.openSession();
session.getTransaction().begin();
Item found = (Item) session.load( Item.class, item.getId() );
Statistics stats = session.getSessionFactory().getStatistics();
log.info( stats.toString() );
assertEquals( item.getDescription(), found.getDescription() );
assertEquals( 0, stats.getSecondLevelCacheMissCount() );
assertEquals( 1, stats.getSecondLevelCacheHitCount() );
session.delete( found );
session.getTransaction().commit();
session.close();
}
catch ( Exception e ) {
ut.setRollbackOnly();
throw e;
}
finally {
if ( ut.getStatus() == Status.STATUS_ACTIVE ) {
ut.commit();
}
else {
ut.rollback();
}
}
ut = (UserTransaction) ctx.lookup( "UserTransaction" );
ut.begin();
try {
Session session = sessionFactory.openSession();
session.getTransaction().begin();
assertNull( session.get( Item.class, item.getId() ) );
session.getTransaction().commit();
session.close();
}
catch ( Exception e ) {
ut.setRollbackOnly();
throw e;
}
finally {
if ( ut.getStatus() == Status.STATUS_ACTIVE ) {
ut.commit();
}
else {
ut.rollback();
}
}
} }
finally { finally {
if ( sessionFactory != null ) { if ( ut.getStatus() == Status.STATUS_ACTIVE ) {
sessionFactory.close(); ut.commit();
}
else {
ut.rollback();
}
}
ut = (UserTransaction) ctx.lookup( "UserTransaction" );
ut.begin();
try {
Session session = sessionFactory.openSession();
session.getTransaction().begin();
Item found = (Item) session.load( Item.class, item.getId() );
Statistics stats = session.getSessionFactory().getStatistics();
log.info( stats.toString() );
assertEquals( item.getDescription(), found.getDescription() );
assertEquals( 0, stats.getSecondLevelCacheMissCount() );
assertEquals( 1, stats.getSecondLevelCacheHitCount() );
session.delete( found );
session.getTransaction().commit();
session.close();
}
catch ( Exception e ) {
ut.setRollbackOnly();
throw e;
}
finally {
if ( ut.getStatus() == Status.STATUS_ACTIVE ) {
ut.commit();
}
else {
ut.rollback();
}
}
ut = (UserTransaction) ctx.lookup( "UserTransaction" );
ut.begin();
try {
Session session = sessionFactory.openSession();
session.getTransaction().begin();
assertNull( session.get( Item.class, item.getId() ) );
session.getTransaction().commit();
session.close();
}
catch ( Exception e ) {
ut.setRollbackOnly();
throw e;
}
finally {
if ( ut.getStatus() == Status.STATUS_ACTIVE ) {
ut.commit();
}
else {
ut.rollback();
} }
} }
@ -266,42 +299,5 @@ public class JBossStandaloneJtaExampleTest {
ctx.unbind( jndiName ); ctx.unbind( jndiName );
} }
private SessionFactory buildSessionFactory() {
// Extra options located in src/test/resources/hibernate.properties
Configuration cfg = new Configuration();
cfg.setProperty( Environment.DIALECT, "HSQL" );
cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
cfg.setProperty( Environment.CONNECTION_PROVIDER, JtaAwareConnectionProviderImpl.class.getName() );
cfg.setProperty(Environment.JNDI_CLASS, "org.jnp.interfaces.NamingContextFactory");
cfg.setProperty(Environment.TRANSACTION_STRATEGY, "jta");
cfg.setProperty(Environment.CURRENT_SESSION_CONTEXT_CLASS, "jta");
cfg.setProperty(Environment.RELEASE_CONNECTIONS, "auto");
cfg.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "true");
cfg.setProperty(Environment.USE_QUERY_CACHE, "true");
Properties envProps = Environment.getProperties();
envProps.putAll( cfg.getProperties() );
envProps.put(AvailableSettings.JTA_PLATFORM, new JBossStandAloneJtaPlatform());
envProps.setProperty(Environment.CACHE_REGION_FACTORY,
"org.hibernate.test.cache.infinispan.functional.SingleNodeTestCase$TestInfinispanRegionFactory");
serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry(envProps);
String[] mappings = new String[] { "org/hibernate/test/cache/infinispan/functional/Item.hbm.xml" };
for ( String mapping : mappings ) {
cfg.addResource( mapping, Thread.currentThread().getContextClassLoader() );
}
cfg.buildMappings();
Iterator iter = cfg.getClassMappings();
while ( iter.hasNext() ) {
PersistentClass clazz = (PersistentClass) iter.next();
cfg.setCacheConcurrencyStrategy( clazz.getEntityName(), "transactional" );
}
iter = cfg.getCollectionMappings();
while ( iter.hasNext() ) {
Collection coll = (Collection) iter.next();
cfg.setCollectionCacheConcurrencyStrategy( coll.getRole(), "transactional" );
}
return cfg.buildSessionFactory( serviceRegistry );
}
} }

View File

@ -206,8 +206,8 @@ public abstract class BaseCoreFunctionalTestCase extends BaseFunctionalTestCase
protected void configure(Configuration configuration) { protected void configure(Configuration configuration) {
} }
protected void applyCacheSettings(MetadataImplementor metadataImplementor){ public static void applyCacheSettings(MetadataImplementor metadataImplementor, String strategy, boolean overrideCacheStrategy){
if( StringHelper.isEmpty(getCacheConcurrencyStrategy())){ if( StringHelper.isEmpty(strategy)){
return; return;
} }
for( EntityBinding entityBinding : metadataImplementor.getEntityBindings()){ for( EntityBinding entityBinding : metadataImplementor.getEntityBindings()){
@ -224,14 +224,14 @@ public abstract class BaseCoreFunctionalTestCase extends BaseFunctionalTestCase
} }
} }
} }
if ( !hasLob && entityBinding.getSuperEntityBinding() == null && overrideCacheStrategy() ) { if ( !hasLob && entityBinding.getSuperEntityBinding() == null && overrideCacheStrategy ) {
Caching caching = entityBinding.getHierarchyDetails().getCaching(); Caching caching = entityBinding.getHierarchyDetails().getCaching();
if ( caching == null ) { if ( caching == null ) {
caching = new Caching(); caching = new Caching();
} }
caching.setRegion( entityBinding.getEntity().getName() ); caching.setRegion( entityBinding.getEntity().getName() );
caching.setCacheLazyProperties( true ); caching.setCacheLazyProperties( true );
caching.setAccessType( AccessType.fromExternalName( getCacheConcurrencyStrategy() ) ); caching.setAccessType( AccessType.fromExternalName( strategy ) );
entityBinding.getHierarchyDetails().setCaching( caching ); entityBinding.getHierarchyDetails().setCaching( caching );
} }
for( AttributeBinding attributeBinding : entityBinding.getAttributeBindingClosure()){ for( AttributeBinding attributeBinding : entityBinding.getAttributeBindingClosure()){
@ -243,13 +243,17 @@ public abstract class BaseCoreFunctionalTestCase extends BaseFunctionalTestCase
} }
caching.setRegion( StringHelper.qualify( entityBinding.getEntity().getName() , attributeBinding.getAttribute().getName() ) ); caching.setRegion( StringHelper.qualify( entityBinding.getEntity().getName() , attributeBinding.getAttribute().getName() ) );
caching.setCacheLazyProperties( true ); caching.setCacheLazyProperties( true );
caching.setAccessType( AccessType.fromExternalName( getCacheConcurrencyStrategy() ) ); caching.setAccessType( AccessType.fromExternalName( strategy ) );
binding.setCaching( caching ); binding.setCaching( caching );
} }
} }
} }
} }
protected void applyCacheSettings(MetadataImplementor metadataImplementor){
applyCacheSettings( metadataImplementor, getCacheConcurrencyStrategy(), overrideCacheStrategy() );
}
protected void applyCacheSettings(Configuration configuration) { protected void applyCacheSettings(Configuration configuration) {
if ( getCacheConcurrencyStrategy() != null ) { if ( getCacheConcurrencyStrategy() != null ) {
Iterator itr = configuration.getClassMappings(); Iterator itr = configuration.getClassMappings();