HHH-5942 - Migrate to JUnit 4

This commit is contained in:
Steve Ebersole 2011-03-11 00:12:58 -06:00
parent fd3fb8b316
commit a6f8767a3c
28 changed files with 1413 additions and 1661 deletions

View File

@ -43,7 +43,15 @@
import org.hibernate.jdbc.Work; import org.hibernate.jdbc.Work;
import org.hibernate.mapping.SimpleAuxiliaryDatabaseObject; import org.hibernate.mapping.SimpleAuxiliaryDatabaseObject;
import org.hibernate.service.spi.ServiceRegistry; import org.hibernate.service.spi.ServiceRegistry;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.hibernate.testing.ServiceRegistryBuilder; import org.hibernate.testing.ServiceRegistryBuilder;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import static org.junit.Assert.assertEquals;
/** /**
* I went back to 3.3 source and grabbed the code/logic as it existed back then and crafted this * I went back to 3.3 source and grabbed the code/logic as it existed back then and crafted this
@ -62,8 +70,6 @@ public class SequenceHiLoGeneratorNoIncrementTest extends BaseUnitTestCase {
@Before @Before
protected void setUp() throws Exception { protected void setUp() throws Exception {
super.setUp();
Properties properties = new Properties(); Properties properties = new Properties();
properties.setProperty( SequenceGenerator.SEQUENCE, TEST_SEQUENCE ); properties.setProperty( SequenceGenerator.SEQUENCE, TEST_SEQUENCE );
properties.setProperty( SequenceHiLoGenerator.MAX_LO, "0" ); // JPA allocationSize of 1 properties.setProperty( SequenceHiLoGenerator.MAX_LO, "0" ); // JPA allocationSize of 1
@ -108,7 +114,6 @@ protected void tearDown() throws Exception {
if ( serviceRegistry != null ) { if ( serviceRegistry != null ) {
ServiceRegistryBuilder.destroy( serviceRegistry ); ServiceRegistryBuilder.destroy( serviceRegistry );
} }
super.tearDown();
} }
@Test @Test

View File

@ -27,6 +27,7 @@
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Properties;
import org.hibernate.Hibernate; import org.hibernate.Hibernate;
import org.hibernate.Session; import org.hibernate.Session;

View File

@ -69,7 +69,7 @@ public void testSimpleColumnAddition() {
v1cfg.addResource( resource1 ); v1cfg.addResource( resource1 );
new SchemaExport( v1cfg ).execute( false, true, true, false ); new SchemaExport( v1cfg ).execute( false, true, true, false );
SchemaUpdate v1schemaUpdate = new SchemaUpdate( getJdbcServices( v1cfg.getProperties() ), v1cfg ); SchemaUpdate v1schemaUpdate = new SchemaUpdate( getJdbcServices(), v1cfg );
v1schemaUpdate.execute( true, true ); v1schemaUpdate.execute( true, true );
assertEquals( 0, v1schemaUpdate.getExceptions().size() ); assertEquals( 0, v1schemaUpdate.getExceptions().size() );
@ -77,11 +77,11 @@ public void testSimpleColumnAddition() {
Configuration v2cfg = new Configuration(); Configuration v2cfg = new Configuration();
v2cfg.addResource( resource2 ); v2cfg.addResource( resource2 );
SchemaUpdate v2schemaUpdate = new SchemaUpdate( getJdbcServices( v2cfg.getProperties() ), v2cfg ); SchemaUpdate v2schemaUpdate = new SchemaUpdate( getJdbcServices(), v2cfg );
v2schemaUpdate.execute( true, true ); v2schemaUpdate.execute( true, true );
assertEquals( 0, v2schemaUpdate.getExceptions().size() ); assertEquals( 0, v2schemaUpdate.getExceptions().size() );
new SchemaExport( getJdbcServices( v2cfg.getProperties() ), v2cfg ).drop( false, true ); new SchemaExport( getJdbcServices(), v2cfg ).drop( false, true );
} }

View File

@ -1,191 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.testing.junit;
import static org.hibernate.TestLogger.LOG;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import junit.framework.AssertionFailedError;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.hibernate.AssertionFailure;
import org.hibernate.TestLogger;
import org.hibernate.cfg.Environment;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.service.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.service.spi.ServiceRegistry;
import org.hibernate.testing.ServiceRegistryBuilder;
import org.jboss.logging.Logger;
/**
* A basic JUnit {@link junit.framework.TestCase} subclass for
* adding some Hibernate specific behavior and functionality.
*
* @author Steve Ebersole
*/
public abstract class UnitTestCase extends junit.framework.TestCase {
private ServiceRegistry serviceRegistry;
private Properties serviceRegistryProperties;
public UnitTestCase(String string) {
super( string );
}
/**
* runBare overridden in order to apply FailureExpected validations
* as well as start/complete logging
*
* @throws Throwable
*/
@Override
public void runBare() throws Throwable {
final boolean doValidate = getName().endsWith( "FailureExpected" ) && Boolean.getBoolean( "hibernate.test.validatefailureexpected" );
try {
Logger.getMessageLogger(TestLogger.class, TestLogger.class.getName()).info("Starting test [" + fullTestName() + "]");
super.runBare();
if ( doValidate ) {
throw new FailureExpectedTestPassedException();
}
}
catch ( FailureExpectedTestPassedException t ) {
throw t;
}
catch( Throwable t ) {
if ( doValidate ) {
skipExpectedFailure( t );
}
else {
throw t;
}
}
finally {
LOG.info("Completed test [" + fullTestName() + "]");
}
}
@Override
protected void tearDown() throws Exception {
if ( serviceRegistry != null ) {
ServiceRegistryBuilder.destroy( serviceRegistry );
serviceRegistry = null;
}
}
protected ServiceRegistry getServiceRegistry(Properties properties) {
if ( serviceRegistry == null ) {
serviceRegistryProperties = properties;
serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry( properties );
}
else if ( ! properties.equals( serviceRegistryProperties ) ) {
throw new AssertionFailure( "ServiceRegistry was already build using different properties." );
}
return serviceRegistry;
}
protected JdbcServices getJdbcServices(Properties properties) {
return getServiceRegistry( properties ).getService( JdbcServices.class );
}
private static class FailureExpectedTestPassedException extends Exception {
public FailureExpectedTestPassedException() {
super( "Test marked as FailureExpected, but did not fail!" );
}
}
protected void skipExpectedFailure(Throwable error) {
reportSkip( "ignoring *FailuredExpected methods", "Failed with: " + error.toString() );
}
// additional assertions ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public static void assertElementTypeAssignability(java.util.Collection collection, Class clazz) throws AssertionFailedError {
for ( Object aCollection : collection ) {
assertClassAssignability( aCollection.getClass(), clazz );
}
}
public static void assertClassAssignability(Class source, Class target) throws AssertionFailedError {
if ( !target.isAssignableFrom( source ) ) {
throw new AssertionFailedError(
"Classes were not assignment-compatible : source<" + source.getName() +
"> target<" + target.getName() + ">"
);
}
}
// test skipping ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public String fullTestName() {
return this.getClass().getName() + "#" + this.getName();
}
protected void reportSkip(String reason, String testDescription) {
LOG.warn("*** skipping [" + fullTestName() + "] - " + testDescription + " : " + reason, new Exception());
}
// testsuite utitities ---------------------------------------------------
/**
* Supports easy creation of TestSuites where a subclass' "FailureExpected"
* version of a base test is included in the suite, while the base test
* is excluded. E.g. test class FooTestCase includes method testBar(), while test
* class SubFooTestCase extends FooTestCase includes method testBarFailureExcluded().
* Passing SubFooTestCase.class to this method will return a suite that
* does not include testBar().
*/
public static TestSuite createFailureExpectedSuite(Class testClass) {
TestSuite allTests = new TestSuite(testClass);
Set failureExpected = new HashSet();
Enumeration tests = allTests.tests();
while (tests.hasMoreElements()) {
Test t = (Test) tests.nextElement();
if (t instanceof TestCase) {
String name = ((TestCase) t).getName();
if (name.endsWith("FailureExpected"))
failureExpected.add(name);
}
}
TestSuite result = new TestSuite();
tests = allTests.tests();
while (tests.hasMoreElements()) {
Test t = (Test) tests.nextElement();
if (t instanceof TestCase) {
String name = ((TestCase) t).getName();
if (!failureExpected.contains(name + "FailureExpected")) {
result.addTest(t);
}
}
}
return result;
}
}

View File

@ -29,10 +29,15 @@
import org.hibernate.cache.access.AccessType; import org.hibernate.cache.access.AccessType;
import org.hibernate.cache.infinispan.InfinispanRegionFactory; import org.hibernate.cache.infinispan.InfinispanRegionFactory;
import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.service.jta.platform.internal.JtaPlatformInitiator; import org.hibernate.service.jta.platform.internal.JtaPlatformInitiator;
import org.hibernate.test.cache.infinispan.util.CacheTestUtil;
import org.hibernate.testing.ServiceRegistryBuilder; import org.hibernate.testing.ServiceRegistryBuilder;
import org.hibernate.test.cache.infinispan.util.CacheTestUtil;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
/** /**
* Base class for tests of EntityRegion and CollectionRegion implementations. * Base class for tests of EntityRegion and CollectionRegion implementations.
@ -41,16 +46,6 @@
* @since 3.5 * @since 3.5
*/ */
public abstract class AbstractEntityCollectionRegionTestCase extends AbstractRegionImplTestCase { public abstract class AbstractEntityCollectionRegionTestCase extends AbstractRegionImplTestCase {
/**
* Create a new EntityCollectionRegionTestCaseBase.
*
* @param name
*/
public AbstractEntityCollectionRegionTestCase(String name) {
super(name);
}
/** /**
* Creates a Region backed by an PESSIMISTIC locking JBoss Cache, and then ensures that it * Creates a Region backed by an PESSIMISTIC locking JBoss Cache, and then ensures that it
* handles calls to buildAccessStrategy as expected when all the various {@link AccessType}s are * handles calls to buildAccessStrategy as expected when all the various {@link AccessType}s are

View File

@ -23,16 +23,25 @@
*/ */
package org.hibernate.test.cache.infinispan; package org.hibernate.test.cache.infinispan;
import static org.hibernate.TestLogger.LOG;
import java.util.Set; import java.util.Set;
import org.infinispan.transaction.tm.BatchModeTransactionManager;
import org.hibernate.cache.GeneralDataRegion; import org.hibernate.cache.GeneralDataRegion;
import org.hibernate.cache.QueryResultsRegion; import org.hibernate.cache.QueryResultsRegion;
import org.hibernate.cache.Region; import org.hibernate.cache.Region;
import org.hibernate.cache.infinispan.InfinispanRegionFactory; import org.hibernate.cache.infinispan.InfinispanRegionFactory;
import org.hibernate.cache.infinispan.util.CacheAdapter; import org.hibernate.cache.infinispan.util.CacheAdapter;
import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Configuration;
import org.hibernate.service.internal.ServiceRegistryImpl;
import org.junit.Test;
import org.hibernate.test.cache.infinispan.util.CacheTestUtil; import org.hibernate.test.cache.infinispan.util.CacheTestUtil;
import org.infinispan.transaction.tm.BatchModeTransactionManager;
import static org.hibernate.TestLogger.LOG;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
/** /**
* Base class for tests of QueryResultsRegion and TimestampsRegion. * Base class for tests of QueryResultsRegion and TimestampsRegion.
@ -46,26 +55,21 @@ public abstract class AbstractGeneralDataRegionTestCase extends AbstractRegionIm
protected static final String VALUE1 = "value1"; protected static final String VALUE1 = "value1";
protected static final String VALUE2 = "value2"; protected static final String VALUE2 = "value2";
public AbstractGeneralDataRegionTestCase(String name) { protected Configuration createConfiguration() {
super(name); return CacheTestUtil.buildConfiguration( "test", InfinispanRegionFactory.class, false, true );
} }
@Override @Override
protected void putInRegion(Region region, Object key, Object value) { protected void putInRegion(Region region, Object key, Object value) {
((GeneralDataRegion) region).put(key, value); ((GeneralDataRegion) region).put( key, value );
} }
@Override @Override
protected void removeFromRegion(Region region, Object key) { protected void removeFromRegion(Region region, Object key) {
((GeneralDataRegion) region).evict(key); ((GeneralDataRegion) region).evict( key );
} }
/** @Test
* Test method for {@link QueryResultsRegion#evict(java.lang.Object)}.
*
* FIXME add testing of the "immediately without regard for transaction isolation" bit in the
* CollectionRegionAccessStrategy API.
*/
public void testEvict() throws Exception { public void testEvict() throws Exception {
evictOrRemoveTest(); evictOrRemoveTest();
} }
@ -73,130 +77,148 @@ public void testEvict() throws Exception {
private void evictOrRemoveTest() throws Exception { private void evictOrRemoveTest() throws Exception {
Configuration cfg = createConfiguration(); Configuration cfg = createConfiguration();
InfinispanRegionFactory regionFactory = CacheTestUtil.startRegionFactory( InfinispanRegionFactory regionFactory = CacheTestUtil.startRegionFactory(
getServiceRegistry( cfg.getProperties() ), cfg, getCacheTestSupport() new ServiceRegistryImpl( cfg.getProperties() ),
cfg,
getCacheTestSupport()
); );
CacheAdapter localCache = getInfinispanCache(regionFactory); CacheAdapter localCache = getInfinispanCache( regionFactory );
boolean invalidation = localCache.isClusteredInvalidation(); boolean invalidation = localCache.isClusteredInvalidation();
// Sleep a bit to avoid concurrent FLUSH problem // Sleep a bit to avoid concurrent FLUSH problem
avoidConcurrentFlush(); avoidConcurrentFlush();
GeneralDataRegion localRegion = (GeneralDataRegion) createRegion(regionFactory, GeneralDataRegion localRegion = (GeneralDataRegion) createRegion(
getStandardRegionName(REGION_PREFIX), cfg.getProperties(), null); regionFactory,
getStandardRegionName( REGION_PREFIX ), cfg.getProperties(), null
);
cfg = createConfiguration(); cfg = createConfiguration();
regionFactory = CacheTestUtil.startRegionFactory( regionFactory = CacheTestUtil.startRegionFactory(
getServiceRegistry( cfg.getProperties() ), cfg, getCacheTestSupport() new ServiceRegistryImpl( cfg.getProperties() ),
cfg,
getCacheTestSupport()
); );
GeneralDataRegion remoteRegion = (GeneralDataRegion) createRegion(regionFactory, GeneralDataRegion remoteRegion = (GeneralDataRegion) createRegion(
getStandardRegionName(REGION_PREFIX), cfg.getProperties(), null); regionFactory,
getStandardRegionName( REGION_PREFIX ),
cfg.getProperties(),
null
);
assertNull("local is clean", localRegion.get(KEY)); assertNull( "local is clean", localRegion.get( KEY ) );
assertNull("remote is clean", remoteRegion.get(KEY)); assertNull( "remote is clean", remoteRegion.get( KEY ) );
localRegion.put(KEY, VALUE1); localRegion.put( KEY, VALUE1 );
assertEquals(VALUE1, localRegion.get(KEY)); assertEquals( VALUE1, localRegion.get( KEY ) );
// allow async propagation // allow async propagation
sleep(250); sleep( 250 );
Object expected = invalidation ? null : VALUE1; Object expected = invalidation ? null : VALUE1;
assertEquals(expected, remoteRegion.get(KEY)); assertEquals( expected, remoteRegion.get( KEY ) );
localRegion.evict(KEY); localRegion.evict( KEY );
// allow async propagation // allow async propagation
sleep(250); sleep( 250 );
assertEquals(null, localRegion.get(KEY)); assertEquals( null, localRegion.get( KEY ) );
assertEquals(null, remoteRegion.get(KEY)); assertEquals( null, remoteRegion.get( KEY ) );
} }
protected abstract String getStandardRegionName(String regionPrefix); protected abstract String getStandardRegionName(String regionPrefix);
/** /**
* Test method for {@link QueryResultsRegion#evictAll()}. * Test method for {@link QueryResultsRegion#evictAll()}.
* * <p/>
* FIXME add testing of the "immediately without regard for transaction isolation" bit in the * FIXME add testing of the "immediately without regard for transaction isolation" bit in the
* CollectionRegionAccessStrategy API. * CollectionRegionAccessStrategy API.
*/ */
public void testEvictAll() throws Exception { public void testEvictAll() throws Exception {
evictOrRemoveAllTest("entity"); evictOrRemoveAllTest( "entity" );
} }
private void evictOrRemoveAllTest(String configName) throws Exception { private void evictOrRemoveAllTest(String configName) throws Exception {
Configuration cfg = createConfiguration(); Configuration cfg = createConfiguration();
InfinispanRegionFactory regionFactory = CacheTestUtil.startRegionFactory( InfinispanRegionFactory regionFactory = CacheTestUtil.startRegionFactory(
getServiceRegistry( cfg.getProperties() ), cfg, getCacheTestSupport() new ServiceRegistryImpl( cfg.getProperties() ),
cfg,
getCacheTestSupport()
); );
CacheAdapter localCache = getInfinispanCache(regionFactory); CacheAdapter localCache = getInfinispanCache( regionFactory );
// Sleep a bit to avoid concurrent FLUSH problem // Sleep a bit to avoid concurrent FLUSH problem
avoidConcurrentFlush(); avoidConcurrentFlush();
GeneralDataRegion localRegion = (GeneralDataRegion) createRegion(regionFactory, GeneralDataRegion localRegion = (GeneralDataRegion) createRegion(
getStandardRegionName(REGION_PREFIX), cfg.getProperties(), null); regionFactory,
getStandardRegionName( REGION_PREFIX ),
cfg.getProperties(),
null
);
cfg = createConfiguration(); cfg = createConfiguration();
regionFactory = CacheTestUtil.startRegionFactory( regionFactory = CacheTestUtil.startRegionFactory(
getServiceRegistry( cfg.getProperties() ), cfg, getCacheTestSupport() new ServiceRegistryImpl( cfg.getProperties() ),
cfg,
getCacheTestSupport()
); );
CacheAdapter remoteCache = getInfinispanCache(regionFactory); CacheAdapter remoteCache = getInfinispanCache( regionFactory );
// Sleep a bit to avoid concurrent FLUSH problem // Sleep a bit to avoid concurrent FLUSH problem
avoidConcurrentFlush(); avoidConcurrentFlush();
GeneralDataRegion remoteRegion = (GeneralDataRegion) createRegion(regionFactory, GeneralDataRegion remoteRegion = (GeneralDataRegion) createRegion(
getStandardRegionName(REGION_PREFIX), cfg.getProperties(), null); regionFactory,
getStandardRegionName( REGION_PREFIX ),
cfg.getProperties(),
null
);
Set keys = localCache.keySet(); Set keys = localCache.keySet();
assertEquals("No valid children in " + keys, 0, getValidKeyCount(keys)); assertEquals( "No valid children in " + keys, 0, getValidKeyCount( keys ) );
keys = remoteCache.keySet(); keys = remoteCache.keySet();
assertEquals("No valid children in " + keys, 0, getValidKeyCount(keys)); assertEquals( "No valid children in " + keys, 0, getValidKeyCount( keys ) );
assertNull("local is clean", localRegion.get(KEY)); assertNull( "local is clean", localRegion.get( KEY ) );
assertNull("remote is clean", remoteRegion.get(KEY)); assertNull( "remote is clean", remoteRegion.get( KEY ) );
localRegion.put(KEY, VALUE1); localRegion.put( KEY, VALUE1 );
assertEquals(VALUE1, localRegion.get(KEY)); assertEquals( VALUE1, localRegion.get( KEY ) );
// Allow async propagation // Allow async propagation
sleep(250); sleep( 250 );
remoteRegion.put(KEY, VALUE1); remoteRegion.put( KEY, VALUE1 );
assertEquals(VALUE1, remoteRegion.get(KEY)); assertEquals( VALUE1, remoteRegion.get( KEY ) );
// Allow async propagation // Allow async propagation
sleep(250); sleep( 250 );
localRegion.evictAll(); localRegion.evictAll();
// allow async propagation // allow async propagation
sleep(250); sleep( 250 );
// This should re-establish the region root node in the optimistic case // This should re-establish the region root node in the optimistic case
assertNull(localRegion.get(KEY)); assertNull( localRegion.get( KEY ) );
assertEquals("No valid children in " + keys, 0, getValidKeyCount(localCache.keySet())); assertEquals( "No valid children in " + keys, 0, getValidKeyCount( localCache.keySet() ) );
// Re-establishing the region root on the local node doesn't // Re-establishing the region root on the local node doesn't
// propagate it to other nodes. Do a get on the remote node to re-establish // propagate it to other nodes. Do a get on the remote node to re-establish
// This only adds a node in the case of optimistic locking // This only adds a node in the case of optimistic locking
assertEquals(null, remoteRegion.get(KEY)); assertEquals( null, remoteRegion.get( KEY ) );
assertEquals("No valid children in " + keys, 0, getValidKeyCount(remoteCache.keySet())); assertEquals( "No valid children in " + keys, 0, getValidKeyCount( remoteCache.keySet() ) );
assertEquals("local is clean", null, localRegion.get(KEY)); assertEquals( "local is clean", null, localRegion.get( KEY ) );
assertEquals("remote is clean", null, remoteRegion.get(KEY)); assertEquals( "remote is clean", null, remoteRegion.get( KEY ) );
}
protected Configuration createConfiguration() {
Configuration cfg = CacheTestUtil.buildConfiguration("test", InfinispanRegionFactory.class, false, true);
return cfg;
} }
protected void rollback() { protected void rollback() {
try { try {
BatchModeTransactionManager.getInstance().rollback(); BatchModeTransactionManager.getInstance().rollback();
} catch (Exception e) { }
LOG.error(e.getMessage(), e); catch (Exception e) {
LOG.error( e.getMessage(), e );
} }
} }
} }

View File

@ -23,13 +23,19 @@
*/ */
package org.hibernate.test.cache.infinispan; package org.hibernate.test.cache.infinispan;
import static org.hibernate.TestLogger.LOG;
import java.util.Set; import java.util.Set;
import org.infinispan.Cache;
import org.hibernate.cache.RegionFactory; import org.hibernate.cache.RegionFactory;
import org.hibernate.cache.infinispan.util.CacheHelper; import org.hibernate.cache.infinispan.util.CacheHelper;
import org.junit.After;
import org.junit.Before;
import org.hibernate.test.cache.infinispan.util.CacheTestSupport; import org.hibernate.test.cache.infinispan.util.CacheTestSupport;
import org.hibernate.testing.junit.UnitTestCase;
import org.infinispan.Cache; import static org.hibernate.TestLogger.LOG;
/** /**
* Base class for all non-functional tests of Infinispan integration. * Base class for all non-functional tests of Infinispan integration.
@ -37,29 +43,32 @@
* @author Galder Zamarreño * @author Galder Zamarreño
* @since 3.5 * @since 3.5
*/ */
public abstract class AbstractNonFunctionalTestCase extends UnitTestCase { public abstract class AbstractNonFunctionalTestCase extends org.hibernate.testing.junit4.BaseUnitTestCase {
public static final String REGION_PREFIX = "test"; public static final String REGION_PREFIX = "test";
private CacheTestSupport testSupport; private static final String PREFER_IPV4STACK = "java.net.preferIPv4Stack";
private String preferIPv4Stack;
public AbstractNonFunctionalTestCase(String name) { private CacheTestSupport testSupport = new CacheTestSupport();
super(name);
testSupport = new CacheTestSupport();
}
@Override @Before
protected void setUp() throws Exception { public void prepareCacheSupport() throws Exception {
super.setUp(); preferIPv4Stack = System.getProperty( PREFER_IPV4STACK );
System.setProperty( PREFER_IPV4STACK, "true" );
testSupport.setUp(); testSupport.setUp();
} }
@Override @After
protected void tearDown() throws Exception { public void releaseCachSupport() throws Exception {
super.tearDown();
testSupport.tearDown(); testSupport.tearDown();
if ( preferIPv4Stack == null ) {
System.clearProperty( PREFER_IPV4STACK );
}
else {
System.setProperty( PREFER_IPV4STACK, preferIPv4Stack );
}
} }
protected void registerCache(Cache cache) { protected void registerCache(Cache cache) {

View File

@ -38,10 +38,6 @@
*/ */
public abstract class AbstractRegionImplTestCase extends AbstractNonFunctionalTestCase { public abstract class AbstractRegionImplTestCase extends AbstractNonFunctionalTestCase {
public AbstractRegionImplTestCase(String name) {
super(name);
}
protected abstract CacheAdapter getInfinispanCache(InfinispanRegionFactory regionFactory); protected abstract CacheAdapter getInfinispanCache(InfinispanRegionFactory regionFactory);
protected abstract Region createRegion(InfinispanRegionFactory regionFactory, String regionName, Properties properties, CacheDataDescription cdd); protected abstract Region createRegion(InfinispanRegionFactory regionFactory, String regionName, Properties properties, CacheDataDescription cdd);

View File

@ -0,0 +1,138 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.cache.infinispan;
import java.util.HashMap;
import java.util.Map;
import org.hibernate.cache.CacheDataDescription;
import org.hibernate.cache.infinispan.InfinispanRegionFactory;
import org.hibernate.cache.infinispan.collection.CollectionRegionImpl;
import org.hibernate.cache.infinispan.entity.EntityRegionImpl;
import org.hibernate.cache.infinispan.util.FlagAdapter;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.internal.ServiceRegistryImpl;
import org.hibernate.test.cache.infinispan.util.CacheTestUtil;
/**
* Defines the environment for a node.
*
* @author Steve Ebersole
*/
public class NodeEnvironment {
private final Configuration configuration;
private ServiceRegistryImpl serviceRegistry;
private InfinispanRegionFactory regionFactory;
private Map<String,EntityRegionImpl> entityRegionMap;
private Map<String,CollectionRegionImpl> collectionRegionMap;
public NodeEnvironment(Configuration configuration) {
this.configuration = configuration;
}
public Configuration getConfiguration() {
return configuration;
}
public ServiceRegistryImpl getServiceRegistry() {
return serviceRegistry;
}
public EntityRegionImpl getEntityRegion(String name, CacheDataDescription cacheDataDescription) {
if ( entityRegionMap == null ) {
entityRegionMap = new HashMap<String, EntityRegionImpl>();
return buildAndStoreEntityRegion( name, cacheDataDescription );
}
EntityRegionImpl region = entityRegionMap.get( name );
if ( region == null ) {
region = buildAndStoreEntityRegion( name, cacheDataDescription );
}
return region;
}
private EntityRegionImpl buildAndStoreEntityRegion(String name, CacheDataDescription cacheDataDescription) {
EntityRegionImpl region = (EntityRegionImpl) regionFactory.buildEntityRegion(
name,
configuration.getProperties(),
cacheDataDescription
);
entityRegionMap.put( name, region );
return region;
}
public CollectionRegionImpl getCollectionRegion(String name, CacheDataDescription cacheDataDescription) {
if ( collectionRegionMap == null ) {
collectionRegionMap = new HashMap<String, CollectionRegionImpl>();
return buildAndStoreCollectionRegion( name, cacheDataDescription );
}
CollectionRegionImpl region = collectionRegionMap.get( name );
if ( region == null ) {
region = buildAndStoreCollectionRegion( name, cacheDataDescription );
collectionRegionMap.put( name, region );
}
return region;
}
private CollectionRegionImpl buildAndStoreCollectionRegion(String name, CacheDataDescription cacheDataDescription) {
CollectionRegionImpl region;
region = (CollectionRegionImpl) regionFactory.buildCollectionRegion(
name,
configuration.getProperties(),
cacheDataDescription
);
return region;
}
public void prepare() throws Exception {
serviceRegistry = new ServiceRegistryImpl( configuration.getProperties() );
regionFactory = CacheTestUtil.startRegionFactory( serviceRegistry, configuration );
}
public void release() throws Exception {
if ( entityRegionMap != null ) {
for ( EntityRegionImpl region : entityRegionMap.values() ) {
region.getCacheAdapter().withFlags( FlagAdapter.CACHE_MODE_LOCAL ).clear();
region.getCacheAdapter().stop();
}
entityRegionMap.clear();
}
if ( collectionRegionMap != null ) {
for ( CollectionRegionImpl collectionRegion : collectionRegionMap.values() ) {
collectionRegion.getCacheAdapter().withFlags( FlagAdapter.CACHE_MODE_LOCAL ).clear();
collectionRegion.getCacheAdapter().stop();
}
collectionRegionMap.clear();
}
if ( regionFactory != null ) {
// Currently the RegionFactory is shutdown by its registration with the CacheTestSetup from CacheTestUtil when built
regionFactory.stop();
}
if ( serviceRegistry != null ) {
serviceRegistry.destroy();
}
}
}

View File

@ -23,20 +23,17 @@
*/ */
package org.hibernate.test.cache.infinispan.collection; package org.hibernate.test.cache.infinispan.collection;
import static org.hibernate.TestLogger.LOG; import javax.transaction.TransactionManager;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import javax.transaction.TransactionManager;
import junit.extensions.TestSetup; import org.infinispan.transaction.tm.BatchModeTransactionManager;
import junit.framework.AssertionFailedError;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.hibernate.cache.CacheDataDescription; import org.hibernate.cache.CacheDataDescription;
import org.hibernate.cache.CollectionRegion;
import org.hibernate.cache.access.AccessType; import org.hibernate.cache.access.AccessType;
import org.hibernate.cache.access.CollectionRegionAccessStrategy; import org.hibernate.cache.access.CollectionRegionAccessStrategy;
import org.hibernate.cache.impl.CacheDataDescriptionImpl; import org.hibernate.cache.impl.CacheDataDescriptionImpl;
@ -44,17 +41,24 @@
import org.hibernate.cache.infinispan.access.PutFromLoadValidator; import org.hibernate.cache.infinispan.access.PutFromLoadValidator;
import org.hibernate.cache.infinispan.access.TransactionalAccessDelegate; import org.hibernate.cache.infinispan.access.TransactionalAccessDelegate;
import org.hibernate.cache.infinispan.collection.CollectionRegionImpl; import org.hibernate.cache.infinispan.collection.CollectionRegionImpl;
import org.hibernate.cache.infinispan.impl.BaseRegion;
import org.hibernate.cache.infinispan.util.CacheAdapter;
import org.hibernate.cache.infinispan.util.FlagAdapter;
import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Configuration;
import org.hibernate.internal.util.compare.ComparableComparator; import org.hibernate.internal.util.compare.ComparableComparator;
import org.hibernate.service.spi.ServiceRegistry;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import junit.framework.AssertionFailedError;
import org.hibernate.test.cache.infinispan.AbstractNonFunctionalTestCase; import org.hibernate.test.cache.infinispan.AbstractNonFunctionalTestCase;
import org.hibernate.test.cache.infinispan.NodeEnvironment;
import org.hibernate.test.cache.infinispan.functional.cluster.DualNodeJtaTransactionManagerImpl; import org.hibernate.test.cache.infinispan.functional.cluster.DualNodeJtaTransactionManagerImpl;
import org.hibernate.test.cache.infinispan.util.CacheTestUtil; import org.hibernate.test.cache.infinispan.util.CacheTestUtil;
import org.hibernate.testing.ServiceRegistryBuilder;
import org.infinispan.transaction.tm.BatchModeTransactionManager; import static org.hibernate.TestLogger.LOG;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/** /**
* Base class for tests of CollectionRegionAccessStrategy impls. * Base class for tests of CollectionRegionAccessStrategy impls.
@ -71,17 +75,12 @@ public abstract class AbstractCollectionRegionAccessStrategyTestCase extends Abs
protected static int testCount; protected static int testCount;
protected static Configuration localCfg; protected NodeEnvironment localEnvironment;
protected static InfinispanRegionFactory localRegionFactory; protected CollectionRegionImpl localCollectionRegion;
protected CacheAdapter localCache;
protected static Configuration remoteCfg;
protected static InfinispanRegionFactory remoteRegionFactory;
protected CacheAdapter remoteCache;
protected CollectionRegion localCollectionRegion;
protected CollectionRegionAccessStrategy localAccessStrategy; protected CollectionRegionAccessStrategy localAccessStrategy;
protected CollectionRegion remoteCollectionRegion; protected NodeEnvironment remoteEnvironment;
protected CollectionRegionImpl remoteCollectionRegion;
protected CollectionRegionAccessStrategy remoteAccessStrategy; protected CollectionRegionAccessStrategy remoteAccessStrategy;
protected boolean invalidation; protected boolean invalidation;
@ -93,92 +92,53 @@ public abstract class AbstractCollectionRegionAccessStrategyTestCase extends Abs
protected AssertionFailedError node1Failure; protected AssertionFailedError node1Failure;
protected AssertionFailedError node2Failure; protected AssertionFailedError node2Failure;
public static Test getTestSetup( Class testClass,
String configName ) {
TestSuite suite = new TestSuite(testClass);
return new AccessStrategyTestSetup(suite, configName);
}
public static Test getTestSetup( Test test,
String configName ) {
return new AccessStrategyTestSetup(test, configName);
}
/**
* Create a new TransactionalAccessTestCase.
*
* @param name
*/
public AbstractCollectionRegionAccessStrategyTestCase( String name ) {
super(name);
}
protected abstract AccessType getAccessType(); protected abstract AccessType getAccessType();
@Override @Before
protected void setUp() throws Exception { public void prepareResources() throws Exception {
super.setUp(); // to mimic exactly the old code results, both environments here are exactly the same...
Configuration cfg = createConfiguration( getConfigurationName() );
localEnvironment = new NodeEnvironment( cfg );
localEnvironment.prepare();
localCollectionRegion = localEnvironment.getCollectionRegion( REGION_NAME, getCacheDataDescription() );
localAccessStrategy = localCollectionRegion.buildAccessStrategy( getAccessType() );
invalidation = localCollectionRegion.getCacheAdapter().isClusteredInvalidation();
synchronous = localCollectionRegion.getCacheAdapter().isSynchronous();
// Sleep a bit to avoid concurrent FLUSH problem // Sleep a bit to avoid concurrent FLUSH problem
avoidConcurrentFlush(); avoidConcurrentFlush();
localCollectionRegion = localRegionFactory.buildCollectionRegion(REGION_NAME, remoteEnvironment = new NodeEnvironment( cfg );
localCfg.getProperties(), remoteEnvironment.prepare();
getCacheDataDescription());
localCache = ((BaseRegion)localCollectionRegion).getCacheAdapter();
localAccessStrategy = localCollectionRegion.buildAccessStrategy(getAccessType());
invalidation = localCache.isClusteredInvalidation();
synchronous = localCache.isSynchronous();
// Sleep a bit to avoid concurrent FLUSH problem remoteCollectionRegion = remoteEnvironment.getCollectionRegion( REGION_NAME, getCacheDataDescription() );
avoidConcurrentFlush(); remoteAccessStrategy = remoteCollectionRegion.buildAccessStrategy( getAccessType() );
remoteCollectionRegion = remoteRegionFactory.buildCollectionRegion(REGION_NAME,
remoteCfg.getProperties(),
getCacheDataDescription());
remoteCache = ((BaseRegion)remoteCollectionRegion).getCacheAdapter();
remoteAccessStrategy = remoteCollectionRegion.buildAccessStrategy(getAccessType());
node1Exception = null;
node2Exception = null;
node1Failure = null;
node2Failure = null;
} }
@Override protected abstract String getConfigurationName();
protected void tearDown() throws Exception {
super.tearDown(); protected static Configuration createConfiguration(String configName) {
Configuration cfg = CacheTestUtil.buildConfiguration(
try { REGION_PREFIX, InfinispanRegionFactory.class, true, false
localCache.withFlags(FlagAdapter.CACHE_MODE_LOCAL).clear(); );
} catch (Exception e) { cfg.setProperty( InfinispanRegionFactory.ENTITY_CACHE_RESOURCE_PROP, configName );
LOG.error("Problem purging local cache", e);
}
try {
remoteCache.withFlags(FlagAdapter.CACHE_MODE_LOCAL).clear();
} catch (Exception e) {
LOG.error("Problem purging remote cache", e);
}
node1Exception = null;
node2Exception = null;
node1Failure = null;
node2Failure = null;
}
protected static Configuration createConfiguration( String configName,
String configResource ) {
Configuration cfg = CacheTestUtil.buildConfiguration(REGION_PREFIX, InfinispanRegionFactory.class, true, false);
cfg.setProperty(InfinispanRegionFactory.ENTITY_CACHE_RESOURCE_PROP, configName);
return cfg; return cfg;
} }
protected CacheDataDescription getCacheDataDescription() { protected CacheDataDescription getCacheDataDescription() {
return new CacheDataDescriptionImpl(true, true, ComparableComparator.INSTANCE); return new CacheDataDescriptionImpl( true, true, ComparableComparator.INSTANCE );
}
@After
public void releaseResources() throws Exception {
if ( localEnvironment != null ) {
localEnvironment.release();
}
if ( remoteEnvironment != null ) {
remoteEnvironment.release();
}
} }
protected boolean isUsingInvalidation() { protected boolean isUsingInvalidation() {
@ -189,45 +149,45 @@ protected boolean isSynchronous() {
return synchronous; return synchronous;
} }
/** @Test
* This is just a setup test where we assert that the cache config is as we expected.
*/
public abstract void testCacheConfiguration(); public abstract void testCacheConfiguration();
/** @Test
* Test method for {@link CollectionRegionAccessStrategy#getRegion()}.
*/
public void testGetRegion() { public void testGetRegion() {
assertEquals("Correct region", localCollectionRegion, localAccessStrategy.getRegion()); assertEquals( "Correct region", localCollectionRegion, localAccessStrategy.getRegion() );
} }
@Test
public void testPutFromLoadRemoveDoesNotProduceStaleData() throws Exception { public void testPutFromLoadRemoveDoesNotProduceStaleData() throws Exception {
final CountDownLatch pferLatch = new CountDownLatch(1); final CountDownLatch pferLatch = new CountDownLatch( 1 );
final CountDownLatch removeLatch = new CountDownLatch(1); final CountDownLatch removeLatch = new CountDownLatch( 1 );
TransactionManager tm = DualNodeJtaTransactionManagerImpl.getInstance("test1234"); TransactionManager tm = DualNodeJtaTransactionManagerImpl.getInstance( "test1234" );
PutFromLoadValidator validator = new PutFromLoadValidator(tm) { PutFromLoadValidator validator = new PutFromLoadValidator( tm ) {
@Override @Override
public boolean acquirePutFromLoadLock( Object key ) { public boolean acquirePutFromLoadLock(Object key) {
boolean acquired = super.acquirePutFromLoadLock(key); boolean acquired = super.acquirePutFromLoadLock( key );
try { try {
removeLatch.countDown(); removeLatch.countDown();
pferLatch.await(2, TimeUnit.SECONDS); pferLatch.await( 2, TimeUnit.SECONDS );
} catch (InterruptedException e) { }
LOG.debug("Interrupted"); catch (InterruptedException e) {
LOG.debug( "Interrupted" );
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
} catch (Exception e) { }
LOG.error("Error", e); catch (Exception e) {
throw new RuntimeException("Error", e); LOG.error( "Error", e );
throw new RuntimeException( "Error", e );
} }
return acquired; return acquired;
} }
}; };
final TransactionalAccessDelegate delegate = new TransactionalAccessDelegate((CollectionRegionImpl)localCollectionRegion, final TransactionalAccessDelegate delegate = new TransactionalAccessDelegate(
validator); (CollectionRegionImpl) localCollectionRegion, validator
);
Callable<Void> pferCallable = new Callable<Void>() { Callable<Void> pferCallable = new Callable<Void>() {
public Void call() throws Exception { public Void call() throws Exception {
delegate.putFromLoad("k1", "v1", 0, null); delegate.putFromLoad( "k1", "v1", 0, null );
return null; return null;
} }
}; };
@ -235,83 +195,71 @@ public Void call() throws Exception {
Callable<Void> removeCallable = new Callable<Void>() { Callable<Void> removeCallable = new Callable<Void>() {
public Void call() throws Exception { public Void call() throws Exception {
removeLatch.await(); removeLatch.await();
delegate.remove("k1"); delegate.remove( "k1" );
pferLatch.countDown(); pferLatch.countDown();
return null; return null;
} }
}; };
ExecutorService executorService = Executors.newCachedThreadPool(); ExecutorService executorService = Executors.newCachedThreadPool();
Future<Void> pferFuture = executorService.submit(pferCallable); Future<Void> pferFuture = executorService.submit( pferCallable );
Future<Void> removeFuture = executorService.submit(removeCallable); Future<Void> removeFuture = executorService.submit( removeCallable );
pferFuture.get(); pferFuture.get();
removeFuture.get(); removeFuture.get();
assertFalse(localCache.containsKey("k1")); assertFalse( localCollectionRegion.getCacheAdapter().containsKey( "k1" ) );
} }
/** @Test
* Test method for
* {@link CollectionRegionAccessStrategy#putFromLoad(java.lang.Object, java.lang.Object, long, java.lang.Object)} .
*/
public void testPutFromLoad() throws Exception { public void testPutFromLoad() throws Exception {
putFromLoadTest(false); putFromLoadTest( false );
} }
/** @Test
* Test method for
* {@link CollectionRegionAccessStrategy#putFromLoad(java.lang.Object, java.lang.Object, long, java.lang.Object, boolean)} .
*/
public void testPutFromLoadMinimal() throws Exception { public void testPutFromLoadMinimal() throws Exception {
putFromLoadTest(true); putFromLoadTest( true );
} }
/** private void putFromLoadTest(final boolean useMinimalAPI) throws Exception {
* Simulate 2 nodes, both start, tx do a get, experience a cache miss, then 'read from db.' First does a putFromLoad, then an
* evict (to represent a change). Second tries to do a putFromLoad with stale data (i.e. it took longer to read from the db).
* Both commit their tx. Then both start a new tx and get. First should see the updated data; second should either see the
* updated data (isInvalidation()( == false) or null (isInvalidation() == true).
*
* @param useMinimalAPI
* @throws Exception
*/
private void putFromLoadTest( final boolean useMinimalAPI ) throws Exception {
final String KEY = KEY_BASE + testCount++; final String KEY = KEY_BASE + testCount++;
final CountDownLatch writeLatch1 = new CountDownLatch(1); final CountDownLatch writeLatch1 = new CountDownLatch( 1 );
final CountDownLatch writeLatch2 = new CountDownLatch(1); final CountDownLatch writeLatch2 = new CountDownLatch( 1 );
final CountDownLatch completionLatch = new CountDownLatch(2); final CountDownLatch completionLatch = new CountDownLatch( 2 );
Thread node1 = new Thread() { Thread node1 = new Thread() {
@Override
public void run() { public void run() {
try { try {
long txTimestamp = System.currentTimeMillis(); long txTimestamp = System.currentTimeMillis();
BatchModeTransactionManager.getInstance().begin(); BatchModeTransactionManager.getInstance().begin();
assertEquals("node1 starts clean", null, localAccessStrategy.get(KEY, txTimestamp)); assertEquals( "node1 starts clean", null, localAccessStrategy.get( KEY, txTimestamp ) );
writeLatch1.await(); writeLatch1.await();
if (useMinimalAPI) { if ( useMinimalAPI ) {
localAccessStrategy.putFromLoad(KEY, VALUE2, txTimestamp, new Integer(2), true); localAccessStrategy.putFromLoad( KEY, VALUE2, txTimestamp, new Integer( 2 ), true );
} else { }
localAccessStrategy.putFromLoad(KEY, VALUE2, txTimestamp, new Integer(2)); else {
localAccessStrategy.putFromLoad( KEY, VALUE2, txTimestamp, new Integer( 2 ) );
} }
BatchModeTransactionManager.getInstance().commit(); BatchModeTransactionManager.getInstance().commit();
} catch (Exception e) { }
LOG.error("node1 caught exception", e); catch (Exception e) {
LOG.error( "node1 caught exception", e );
node1Exception = e; node1Exception = e;
rollback(); rollback();
} catch (AssertionFailedError e) { }
catch (AssertionFailedError e) {
node1Failure = e; node1Failure = e;
rollback(); rollback();
} finally { }
finally {
// Let node2 write // Let node2 write
writeLatch2.countDown(); writeLatch2.countDown();
completionLatch.countDown(); completionLatch.countDown();
@ -321,14 +269,13 @@ public void run() {
Thread node2 = new Thread() { Thread node2 = new Thread() {
@Override
public void run() { public void run() {
try { try {
long txTimestamp = System.currentTimeMillis(); long txTimestamp = System.currentTimeMillis();
BatchModeTransactionManager.getInstance().begin(); BatchModeTransactionManager.getInstance().begin();
assertNull("node2 starts clean", remoteAccessStrategy.get(KEY, txTimestamp)); assertNull( "node2 starts clean", remoteAccessStrategy.get( KEY, txTimestamp ) );
// Let node1 write // Let node1 write
writeLatch1.countDown(); writeLatch1.countDown();
@ -336,239 +283,184 @@ public void run() {
writeLatch2.await(); writeLatch2.await();
// Let the first PFER propagate // Let the first PFER propagate
sleep(200); sleep( 200 );
if (useMinimalAPI) { if ( useMinimalAPI ) {
remoteAccessStrategy.putFromLoad(KEY, VALUE1, txTimestamp, new Integer(1), true); remoteAccessStrategy.putFromLoad( KEY, VALUE1, txTimestamp, new Integer( 1 ), true );
} else { }
remoteAccessStrategy.putFromLoad(KEY, VALUE1, txTimestamp, new Integer(1)); else {
remoteAccessStrategy.putFromLoad( KEY, VALUE1, txTimestamp, new Integer( 1 ) );
} }
BatchModeTransactionManager.getInstance().commit(); BatchModeTransactionManager.getInstance().commit();
} catch (Exception e) { }
LOG.error("node2 caught exception", e); catch (Exception e) {
LOG.error( "node2 caught exception", e );
node2Exception = e; node2Exception = e;
rollback(); rollback();
} catch (AssertionFailedError e) { }
catch (AssertionFailedError e) {
node2Failure = e; node2Failure = e;
rollback(); rollback();
} finally { }
finally {
completionLatch.countDown(); completionLatch.countDown();
} }
} }
}; };
node1.setDaemon(true); node1.setDaemon( true );
node2.setDaemon(true); node2.setDaemon( true );
node1.start(); node1.start();
node2.start(); node2.start();
assertTrue("Threads completed", completionLatch.await(2, TimeUnit.SECONDS)); assertTrue( "Threads completed", completionLatch.await( 2, TimeUnit.SECONDS ) );
if (node1Failure != null) throw node1Failure; if ( node1Failure != null ) {
if (node2Failure != null) throw node2Failure; throw node1Failure;
}
if ( node2Failure != null ) {
throw node2Failure;
}
assertEquals("node1 saw no exceptions", null, node1Exception); assertEquals( "node1 saw no exceptions", null, node1Exception );
assertEquals("node2 saw no exceptions", null, node2Exception); assertEquals( "node2 saw no exceptions", null, node2Exception );
// let the final PFER propagate // let the final PFER propagate
sleep(100); sleep( 100 );
long txTimestamp = System.currentTimeMillis(); long txTimestamp = System.currentTimeMillis();
String msg1 = "Correct node1 value"; String msg1 = "Correct node1 value";
String msg2 = "Correct node2 value"; String msg2 = "Correct node2 value";
Object expected1 = null; Object expected1 = null;
Object expected2 = null; Object expected2 = null;
if (isUsingInvalidation()) { if ( isUsingInvalidation() ) {
// PFER does not generate any invalidation, so each node should // PFER does not generate any invalidation, so each node should
// succeed. We count on database locking and Hibernate removing // succeed. We count on database locking and Hibernate removing
// the collection on any update to prevent the situation we have // the collection on any update to prevent the situation we have
// here where the caches have inconsistent data // here where the caches have inconsistent data
expected1 = VALUE2; expected1 = VALUE2;
expected2 = VALUE1; expected2 = VALUE1;
} else { }
else {
// the initial VALUE2 should prevent the node2 put // the initial VALUE2 should prevent the node2 put
expected1 = VALUE2; expected1 = VALUE2;
expected2 = VALUE2; expected2 = VALUE2;
} }
assertEquals(msg1, expected1, localAccessStrategy.get(KEY, txTimestamp)); assertEquals( msg1, expected1, localAccessStrategy.get( KEY, txTimestamp ) );
assertEquals(msg2, expected2, remoteAccessStrategy.get(KEY, txTimestamp)); assertEquals( msg2, expected2, remoteAccessStrategy.get( KEY, txTimestamp ) );
} }
/** @Test
* Test method for {@link CollectionRegionAccessStrategy#remove(java.lang.Object)}.
*/
public void testRemove() { public void testRemove() {
evictOrRemoveTest(false); evictOrRemoveTest( false );
} }
/** @Test
* Test method for {@link CollectionRegionAccessStrategy#removeAll()}.
*/
public void testRemoveAll() { public void testRemoveAll() {
evictOrRemoveAllTest(false); evictOrRemoveAllTest( false );
} }
/** @Test
* Test method for {@link CollectionRegionAccessStrategy#evict(java.lang.Object)}. FIXME add testing of the
* "immediately without regard for transaction isolation" bit in the CollectionRegionAccessStrategy API.
*/
public void testEvict() { public void testEvict() {
evictOrRemoveTest(true); evictOrRemoveTest( true );
} }
/** @Test
* Test method for {@link CollectionRegionAccessStrategy#evictAll()}. FIXME add testing of the
* "immediately without regard for transaction isolation" bit in the CollectionRegionAccessStrategy API.
*/
public void testEvictAll() { public void testEvictAll() {
evictOrRemoveAllTest(true); evictOrRemoveAllTest( true );
} }
private void evictOrRemoveTest( boolean evict ) { private void evictOrRemoveTest(boolean evict) {
final String KEY = KEY_BASE + testCount++; final String KEY = KEY_BASE + testCount++;
assertNull("local is clean", localAccessStrategy.get(KEY, System.currentTimeMillis())); assertNull( "local is clean", localAccessStrategy.get( KEY, System.currentTimeMillis() ) );
assertNull("remote is clean", remoteAccessStrategy.get(KEY, System.currentTimeMillis())); assertNull( "remote is clean", remoteAccessStrategy.get( KEY, System.currentTimeMillis() ) );
localAccessStrategy.putFromLoad(KEY, VALUE1, System.currentTimeMillis(), new Integer(1)); localAccessStrategy.putFromLoad( KEY, VALUE1, System.currentTimeMillis(), new Integer( 1 ) );
assertEquals(VALUE1, localAccessStrategy.get(KEY, System.currentTimeMillis())); assertEquals( VALUE1, localAccessStrategy.get( KEY, System.currentTimeMillis() ) );
remoteAccessStrategy.putFromLoad(KEY, VALUE1, System.currentTimeMillis(), new Integer(1)); remoteAccessStrategy.putFromLoad( KEY, VALUE1, System.currentTimeMillis(), new Integer( 1 ) );
assertEquals(VALUE1, remoteAccessStrategy.get(KEY, System.currentTimeMillis())); assertEquals( VALUE1, remoteAccessStrategy.get( KEY, System.currentTimeMillis() ) );
// Wait for async propagation // Wait for async propagation
sleep(250); sleep( 250 );
if (evict) localAccessStrategy.evict(KEY); if ( evict ) {
else localAccessStrategy.remove(KEY); localAccessStrategy.evict( KEY );
}
assertEquals(null, localAccessStrategy.get(KEY, System.currentTimeMillis())); else {
localAccessStrategy.remove( KEY );
assertEquals(null, remoteAccessStrategy.get(KEY, System.currentTimeMillis()));
} }
private void evictOrRemoveAllTest( boolean evict ) { assertEquals( null, localAccessStrategy.get( KEY, System.currentTimeMillis() ) );
assertEquals( null, remoteAccessStrategy.get( KEY, System.currentTimeMillis() ) );
}
private void evictOrRemoveAllTest(boolean evict) {
final String KEY = KEY_BASE + testCount++; final String KEY = KEY_BASE + testCount++;
assertEquals(0, getValidKeyCount(localCache.keySet())); assertEquals( 0, getValidKeyCount( localCollectionRegion.getCacheAdapter().keySet() ) );
assertEquals(0, getValidKeyCount(remoteCache.keySet())); assertEquals( 0, getValidKeyCount( remoteCollectionRegion.getCacheAdapter().keySet() ) );
assertNull("local is clean", localAccessStrategy.get(KEY, System.currentTimeMillis())); assertNull( "local is clean", localAccessStrategy.get( KEY, System.currentTimeMillis() ) );
assertNull("remote is clean", remoteAccessStrategy.get(KEY, System.currentTimeMillis())); assertNull( "remote is clean", remoteAccessStrategy.get( KEY, System.currentTimeMillis() ) );
localAccessStrategy.putFromLoad(KEY, VALUE1, System.currentTimeMillis(), new Integer(1)); localAccessStrategy.putFromLoad( KEY, VALUE1, System.currentTimeMillis(), new Integer( 1 ) );
assertEquals(VALUE1, localAccessStrategy.get(KEY, System.currentTimeMillis())); assertEquals( VALUE1, localAccessStrategy.get( KEY, System.currentTimeMillis() ) );
remoteAccessStrategy.putFromLoad(KEY, VALUE1, System.currentTimeMillis(), new Integer(1)); remoteAccessStrategy.putFromLoad( KEY, VALUE1, System.currentTimeMillis(), new Integer( 1 ) );
assertEquals(VALUE1, remoteAccessStrategy.get(KEY, System.currentTimeMillis())); assertEquals( VALUE1, remoteAccessStrategy.get( KEY, System.currentTimeMillis() ) );
// Wait for async propagation // Wait for async propagation
sleep(250); sleep( 250 );
if (evict) localAccessStrategy.evictAll(); if ( evict ) {
else localAccessStrategy.removeAll(); localAccessStrategy.evictAll();
}
else {
localAccessStrategy.removeAll();
}
// This should re-establish the region root node // This should re-establish the region root node
assertNull(localAccessStrategy.get(KEY, System.currentTimeMillis())); assertNull( localAccessStrategy.get( KEY, System.currentTimeMillis() ) );
assertEquals(0, getValidKeyCount(localCache.keySet())); assertEquals( 0, getValidKeyCount( localCollectionRegion.getCacheAdapter().keySet() ) );
// Re-establishing the region root on the local node doesn't // Re-establishing the region root on the local node doesn't
// propagate it to other nodes. Do a get on the remote node to re-establish // propagate it to other nodes. Do a get on the remote node to re-establish
assertEquals(null, remoteAccessStrategy.get(KEY, System.currentTimeMillis())); assertEquals( null, remoteAccessStrategy.get( KEY, System.currentTimeMillis() ) );
assertEquals(0, getValidKeyCount(remoteCache.keySet())); assertEquals( 0, getValidKeyCount( remoteCollectionRegion.getCacheAdapter().keySet() ) );
// Test whether the get above messes up the optimistic version // Test whether the get above messes up the optimistic version
remoteAccessStrategy.putFromLoad(KEY, VALUE1, System.currentTimeMillis(), new Integer(1)); remoteAccessStrategy.putFromLoad( KEY, VALUE1, System.currentTimeMillis(), new Integer( 1 ) );
assertEquals(VALUE1, remoteAccessStrategy.get(KEY, System.currentTimeMillis())); assertEquals( VALUE1, remoteAccessStrategy.get( KEY, System.currentTimeMillis() ) );
assertEquals(1, getValidKeyCount(remoteCache.keySet())); assertEquals( 1, getValidKeyCount( remoteCollectionRegion.getCacheAdapter().keySet() ) );
// Wait for async propagation of the putFromLoad // Wait for async propagation of the putFromLoad
sleep(250); sleep( 250 );
assertEquals("local is correct", assertEquals(
(isUsingInvalidation() ? null : VALUE1), "local is correct", (isUsingInvalidation() ? null : VALUE1), localAccessStrategy.get(
localAccessStrategy.get(KEY, System.currentTimeMillis())); KEY, System
assertEquals("remote is correct", VALUE1, remoteAccessStrategy.get(KEY, System.currentTimeMillis())); .currentTimeMillis()
)
);
assertEquals( "remote is correct", VALUE1, remoteAccessStrategy.get( KEY, System.currentTimeMillis() ) );
} }
private void rollback() { private void rollback() {
try { try {
BatchModeTransactionManager.getInstance().rollback(); BatchModeTransactionManager.getInstance().rollback();
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
}
private static class AccessStrategyTestSetup extends TestSetup {
private static final String PREFER_IPV4STACK = "java.net.preferIPv4Stack";
private final String configResource;
private final String configName;
private String preferIPv4Stack;
private ServiceRegistry localServiceRegistry;
private ServiceRegistry remoteServiceRegistry;
public AccessStrategyTestSetup( Test test,
String configName ) {
this(test, configName, null);
}
public AccessStrategyTestSetup( Test test,
String configName,
String configResource ) {
super(test);
this.configName = configName;
this.configResource = configResource;
}
@Override
protected void setUp() throws Exception {
super.setUp();
// Try to ensure we use IPv4; otherwise cluster formation is very slow
preferIPv4Stack = System.getProperty(PREFER_IPV4STACK);
System.setProperty(PREFER_IPV4STACK, "true");
localCfg = createConfiguration(configName, configResource);
localServiceRegistry = ServiceRegistryBuilder.buildServiceRegistry(localCfg.getProperties());
localRegionFactory = CacheTestUtil.startRegionFactory(localServiceRegistry, localCfg);
remoteCfg = createConfiguration(configName, configResource);
remoteServiceRegistry = ServiceRegistryBuilder.buildServiceRegistry(remoteCfg.getProperties());
remoteRegionFactory = CacheTestUtil.startRegionFactory(remoteServiceRegistry, remoteCfg);
}
@Override
protected void tearDown() throws Exception {
try {
super.tearDown();
} finally {
if (preferIPv4Stack == null) System.clearProperty(PREFER_IPV4STACK);
else System.setProperty(PREFER_IPV4STACK, preferIPv4Stack);
}
try {
if (localRegionFactory != null) localRegionFactory.stop();
if (remoteRegionFactory != null) remoteRegionFactory.stop();
} finally {
if (localServiceRegistry != null) {
ServiceRegistryBuilder.destroy(localServiceRegistry);
}
if (remoteServiceRegistry != null) {
ServiceRegistryBuilder.destroy(remoteServiceRegistry);
}
} }
catch (Exception e) {
LOG.error( e.getMessage(), e );
} }
} }

View File

@ -24,20 +24,17 @@
package org.hibernate.test.cache.infinispan.collection; package org.hibernate.test.cache.infinispan.collection;
import org.hibernate.cache.access.AccessType; import org.hibernate.cache.access.AccessType;
import org.junit.Test;
/** /**
* Base class for tests of TRANSACTIONAL access. * Base class for tests of TRANSACTIONAL access.
* *
* @author <a href="brian.stansberry@jboss.com">Brian Stansberry</a> * @author <a href="brian.stansberry@jboss.com">Brian Stansberry</a>
*/ */
public abstract class AbstractReadOnlyAccessTestCase extends AbstractCollectionRegionAccessStrategyTestCase { public abstract class AbstractReadOnlyAccessTestCase extends AbstractCollectionRegionAccessStrategyTestCase {
@Test
public AbstractReadOnlyAccessTestCase(String name) {
super(name);
}
@Override @Override
protected AccessType getAccessType() { protected AccessType getAccessType() {
return AccessType.READ_ONLY; return AccessType.READ_ONLY;
} }
} }

View File

@ -30,18 +30,8 @@
* @author <a href="brian.stansberry@jboss.com">Brian Stansberry</a> * @author <a href="brian.stansberry@jboss.com">Brian Stansberry</a>
*/ */
public abstract class AbstractTransactionalAccessTestCase extends AbstractCollectionRegionAccessStrategyTestCase { public abstract class AbstractTransactionalAccessTestCase extends AbstractCollectionRegionAccessStrategyTestCase {
/**
* Create a new AbstractTransactionalAccessTestCase.
*
*/
public AbstractTransactionalAccessTestCase(String name) {
super(name);
}
@Override @Override
protected AccessType getAccessType() { protected AccessType getAccessType() {
return AccessType.TRANSACTIONAL; return AccessType.TRANSACTIONAL;
} }
} }

View File

@ -33,19 +33,18 @@
import org.hibernate.cache.infinispan.InfinispanRegionFactory; import org.hibernate.cache.infinispan.InfinispanRegionFactory;
import org.hibernate.cache.infinispan.util.CacheAdapter; import org.hibernate.cache.infinispan.util.CacheAdapter;
import org.hibernate.cache.infinispan.util.CacheAdapterImpl; import org.hibernate.cache.infinispan.util.CacheAdapterImpl;
import org.hibernate.test.cache.infinispan.AbstractEntityCollectionRegionTestCase; import org.hibernate.test.cache.infinispan.AbstractEntityCollectionRegionTestCase;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
/** /**
* Tests of CollectionRegionImpl. * Tests of CollectionRegionImpl.
* *
* @author Galder Zamarreño * @author Galder Zamarreño
*/ */
public class CollectionRegionImplTestCase extends AbstractEntityCollectionRegionTestCase { public class CollectionRegionImplTestCase extends AbstractEntityCollectionRegionTestCase {
public CollectionRegionImplTestCase(String name) {
super(name);
}
@Override @Override
protected void supportedAccessTypeTest(RegionFactory regionFactory, Properties properties) { protected void supportedAccessTypeTest(RegionFactory regionFactory, Properties properties) {
CollectionRegion region = regionFactory.buildCollectionRegion("test", properties, null); CollectionRegion region = regionFactory.buildCollectionRegion("test", properties, null);

View File

@ -20,9 +20,10 @@
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/ */
package org.hibernate.test.cache.infinispan.collection; package org.hibernate.test.cache.infinispan.collection;
import junit.framework.Test;
import junit.framework.TestSuite; import org.junit.Test;
import org.hibernate.test.cache.infinispan.util.CacheTestUtil;
import static org.junit.Assert.assertTrue;
/** /**
* InvalidatedTransactionalTestCase. * InvalidatedTransactionalTestCase.
@ -31,19 +32,15 @@
* @since 3.5 * @since 3.5
*/ */
public class InvalidatedTransactionalTestCase extends AbstractTransactionalAccessTestCase { public class InvalidatedTransactionalTestCase extends AbstractTransactionalAccessTestCase {
@Test
public InvalidatedTransactionalTestCase(String name) {
super(name);
}
@Override @Override
public void testCacheConfiguration() { public void testCacheConfiguration() {
assertTrue("Using Invalidation", isUsingInvalidation()); assertTrue("Using Invalidation", isUsingInvalidation());
assertTrue("Synchronous mode", isSynchronous()); assertTrue("Synchronous mode", isSynchronous());
} }
public static Test suite() throws Exception { @Override
TestSuite suite = CacheTestUtil.createFailureExpectedSuite(InvalidatedTransactionalTestCase.class); protected String getConfigurationName() {
return getTestSetup(suite, "entity"); return "entity"; // todo : should this be "collection"? the original code used "entity"...
} }
} }

View File

@ -21,8 +21,10 @@
*/ */
package org.hibernate.test.cache.infinispan.collection; package org.hibernate.test.cache.infinispan.collection;
import org.hibernate.cache.access.AccessType; import org.hibernate.cache.access.AccessType;
import org.hibernate.cache.access.CollectionRegionAccessStrategy;
import org.junit.Test;
import static org.junit.Assert.fail;
/** /**
* ReadOnlyExtraAPITestCase. * ReadOnlyExtraAPITestCase.
* *
@ -30,50 +32,31 @@
* @since 3.5 * @since 3.5
*/ */
public class ReadOnlyExtraAPITestCase extends TransactionalExtraAPITestCase { public class ReadOnlyExtraAPITestCase extends TransactionalExtraAPITestCase {
public ReadOnlyExtraAPITestCase(String name) {
super(name);
}
private static CollectionRegionAccessStrategy localAccessStrategy;
@Override @Override
protected AccessType getAccessType() { protected AccessType getAccessType() {
return AccessType.READ_ONLY; return AccessType.READ_ONLY;
} }
@Override @Test
protected CollectionRegionAccessStrategy getCollectionAccessStrategy() {
return localAccessStrategy;
}
@Override
protected void setCollectionAccessStrategy(CollectionRegionAccessStrategy strategy) {
localAccessStrategy = strategy;
}
/**
* Test method for {@link TransactionalAccess#lockItem(java.lang.Object, java.lang.Object)}.
*/
@Override @Override
public void testLockItem() { public void testLockItem() {
try { try {
getCollectionAccessStrategy().lockItem(KEY, new Integer(1)); getCollectionAccessStrategy().lockItem( KEY, new Integer( 1 ) );
fail("Call to lockItem did not throw exception"); fail( "Call to lockItem did not throw exception" );
}
catch (UnsupportedOperationException expected) {
} }
catch (UnsupportedOperationException expected) {}
} }
/** @Test
* Test method for {@link TransactionalAccess#lockRegion()}.
*/
@Override @Override
public void testLockRegion() { public void testLockRegion() {
try { try {
getCollectionAccessStrategy().lockRegion(); getCollectionAccessStrategy().lockRegion();
fail("Call to lockRegion did not throw exception"); fail( "Call to lockRegion did not throw exception" );
}
catch (UnsupportedOperationException expected) {
} }
catch (UnsupportedOperationException expected) {}
} }
} }

View File

@ -20,9 +20,8 @@
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/ */
package org.hibernate.test.cache.infinispan.collection; package org.hibernate.test.cache.infinispan.collection;
import junit.framework.Test;
import junit.framework.TestSuite; import static org.junit.Assert.assertTrue;
import org.hibernate.test.cache.infinispan.util.CacheTestUtil;
/** /**
* Tests READ_ONLY access when invalidation is used. * Tests READ_ONLY access when invalidation is used.
@ -31,19 +30,13 @@
* @since 3.5 * @since 3.5
*/ */
public class ReadOnlyTestCase extends AbstractReadOnlyAccessTestCase { public class ReadOnlyTestCase extends AbstractReadOnlyAccessTestCase {
@Override
public ReadOnlyTestCase(String name) { public void testCacheConfiguration() {
super(name); assertTrue( "Using Invalidation", isUsingInvalidation() );
}
public static Test suite() throws Exception {
TestSuite suite = CacheTestUtil.createFailureExpectedSuite(ReadOnlyTestCase.class);
return getTestSetup(suite, "entity");
} }
@Override @Override
public void testCacheConfiguration() { protected String getConfigurationName() {
assertTrue("Using Invalidation", isUsingInvalidation()); return "entity"; // todo : should this be "collection"? the original code used "entity"...
} }
} }

View File

@ -20,15 +20,23 @@
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/ */
package org.hibernate.test.cache.infinispan.collection; package org.hibernate.test.cache.infinispan.collection;
import org.hibernate.cache.CollectionRegion;
import org.hibernate.cache.access.AccessType; import org.hibernate.cache.access.AccessType;
import org.hibernate.cache.access.CollectionRegionAccessStrategy; import org.hibernate.cache.access.CollectionRegionAccessStrategy;
import org.hibernate.cache.access.SoftLock; import org.hibernate.cache.access.SoftLock;
import org.hibernate.cache.infinispan.InfinispanRegionFactory; import org.hibernate.cache.infinispan.InfinispanRegionFactory;
import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.hibernate.test.cache.infinispan.AbstractNonFunctionalTestCase; import org.hibernate.test.cache.infinispan.AbstractNonFunctionalTestCase;
import org.hibernate.test.cache.infinispan.NodeEnvironment;
import org.hibernate.test.cache.infinispan.util.CacheTestUtil; import org.hibernate.test.cache.infinispan.util.CacheTestUtil;
import static org.junit.Assert.assertNull;
/** /**
* TransactionalExtraAPITestCase. * TransactionalExtraAPITestCase.
* *
@ -42,37 +50,24 @@ public class TransactionalExtraAPITestCase extends AbstractNonFunctionalTestCase
public static final String VALUE1 = "VALUE1"; public static final String VALUE1 = "VALUE1";
public static final String VALUE2 = "VALUE2"; public static final String VALUE2 = "VALUE2";
private static CollectionRegionAccessStrategy localAccessStrategy; private NodeEnvironment environment;
private static CollectionRegionAccessStrategy accessStrategy;
public TransactionalExtraAPITestCase(String name) { @Before
super(name); public final void prepareLocalAccessStrategy() throws Exception {
} environment = new NodeEnvironment( createConfiguration() );
protected void setUp() throws Exception {
super.setUp();
if (getCollectionAccessStrategy() == null) {
Configuration cfg = createConfiguration();
InfinispanRegionFactory rf = CacheTestUtil.startRegionFactory(
getServiceRegistry( cfg.getProperties() ), cfg, getCacheTestSupport()
);
// Sleep a bit to avoid concurrent FLUSH problem // Sleep a bit to avoid concurrent FLUSH problem
avoidConcurrentFlush(); avoidConcurrentFlush();
CollectionRegion localCollectionRegion = rf.buildCollectionRegion(REGION_NAME, cfg.getProperties(), null); accessStrategy = environment.getCollectionRegion( REGION_NAME, null ).buildAccessStrategy( getAccessType() );
setCollectionAccessStrategy(localCollectionRegion.buildAccessStrategy(getAccessType()));
}
}
protected void tearDown() throws Exception {
super.tearDown();
} }
protected Configuration createConfiguration() { protected Configuration createConfiguration() {
Configuration cfg = CacheTestUtil.buildConfiguration(REGION_PREFIX, InfinispanRegionFactory.class, true, false); Configuration cfg = CacheTestUtil.buildConfiguration(
cfg.setProperty(InfinispanRegionFactory.ENTITY_CACHE_RESOURCE_PROP, getCacheConfigName()); REGION_PREFIX, InfinispanRegionFactory.class, true, false
);
cfg.setProperty( InfinispanRegionFactory.ENTITY_CACHE_RESOURCE_PROP, getCacheConfigName() );
return cfg; return cfg;
} }
@ -84,31 +79,37 @@ protected AccessType getAccessType() {
return AccessType.TRANSACTIONAL; return AccessType.TRANSACTIONAL;
} }
@After
public final void releaseLocalAccessStrategy() throws Exception {
if ( environment != null ) {
environment.release();
}
}
protected CollectionRegionAccessStrategy getCollectionAccessStrategy() { protected CollectionRegionAccessStrategy getCollectionAccessStrategy() {
return localAccessStrategy; return accessStrategy;
}
protected void setCollectionAccessStrategy(CollectionRegionAccessStrategy strategy) {
localAccessStrategy = strategy;
} }
@Test
public void testLockItem() { public void testLockItem() {
assertNull(getCollectionAccessStrategy().lockItem(KEY, new Integer(1))); assertNull( getCollectionAccessStrategy().lockItem( KEY, new Integer( 1 ) ) );
} }
@Test
public void testLockRegion() { public void testLockRegion() {
assertNull(getCollectionAccessStrategy().lockRegion()); assertNull( getCollectionAccessStrategy().lockRegion() );
} }
@Test
public void testUnlockItem() { public void testUnlockItem() {
getCollectionAccessStrategy().unlockItem(KEY, new MockSoftLock()); getCollectionAccessStrategy().unlockItem( KEY, new MockSoftLock() );
} }
@Test
public void testUnlockRegion() { public void testUnlockRegion() {
getCollectionAccessStrategy().unlockItem(KEY, new MockSoftLock()); getCollectionAccessStrategy().unlockItem( KEY, new MockSoftLock() );
} }
public static class MockSoftLock implements SoftLock { public static class MockSoftLock implements SoftLock {
} }
} }

View File

@ -23,29 +23,33 @@
*/ */
package org.hibernate.test.cache.infinispan.entity; package org.hibernate.test.cache.infinispan.entity;
import static org.hibernate.TestLogger.LOG;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import junit.extensions.TestSetup;
import junit.framework.AssertionFailedError; import org.infinispan.transaction.tm.BatchModeTransactionManager;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.hibernate.cache.CacheDataDescription; import org.hibernate.cache.CacheDataDescription;
import org.hibernate.cache.EntityRegion;
import org.hibernate.cache.access.AccessType; import org.hibernate.cache.access.AccessType;
import org.hibernate.cache.access.EntityRegionAccessStrategy; import org.hibernate.cache.access.EntityRegionAccessStrategy;
import org.hibernate.cache.impl.CacheDataDescriptionImpl; import org.hibernate.cache.impl.CacheDataDescriptionImpl;
import org.hibernate.cache.infinispan.InfinispanRegionFactory; import org.hibernate.cache.infinispan.InfinispanRegionFactory;
import org.hibernate.cache.infinispan.impl.BaseRegion; import org.hibernate.cache.infinispan.entity.EntityRegionImpl;
import org.hibernate.cache.infinispan.util.CacheAdapter;
import org.hibernate.cache.infinispan.util.FlagAdapter;
import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Configuration;
import org.hibernate.internal.util.compare.ComparableComparator; import org.hibernate.internal.util.compare.ComparableComparator;
import org.hibernate.service.spi.ServiceRegistry;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import junit.framework.AssertionFailedError;
import org.hibernate.test.cache.infinispan.AbstractNonFunctionalTestCase; import org.hibernate.test.cache.infinispan.AbstractNonFunctionalTestCase;
import org.hibernate.test.cache.infinispan.NodeEnvironment;
import org.hibernate.test.cache.infinispan.util.CacheTestUtil; import org.hibernate.test.cache.infinispan.util.CacheTestUtil;
import org.hibernate.testing.ServiceRegistryBuilder;
import org.infinispan.transaction.tm.BatchModeTransactionManager; import static org.hibernate.testing.TestingLogger.LOG;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/** /**
* Base class for tests of EntityRegionAccessStrategy impls. * Base class for tests of EntityRegionAccessStrategy impls.
@ -62,113 +66,54 @@ public abstract class AbstractEntityRegionAccessStrategyTestCase extends Abstrac
protected static int testCount; protected static int testCount;
protected static Configuration localCfg; protected NodeEnvironment localEnvironment;
protected static InfinispanRegionFactory localRegionFactory; protected EntityRegionImpl localEntityRegion;
protected CacheAdapter localCache; protected EntityRegionAccessStrategy localAccessStrategy;
protected static Configuration remoteCfg;
protected static InfinispanRegionFactory remoteRegionFactory; protected NodeEnvironment remoteEnvironment;
protected CacheAdapter remoteCache; protected EntityRegionImpl remoteEntityRegion;
protected EntityRegionAccessStrategy remoteAccessStrategy;
protected boolean invalidation; protected boolean invalidation;
protected boolean synchronous; protected boolean synchronous;
protected EntityRegion localEntityRegion;
protected EntityRegionAccessStrategy localAccessStrategy;
protected EntityRegion remoteEntityRegion;
protected EntityRegionAccessStrategy remoteAccessStrategy;
protected Exception node1Exception; protected Exception node1Exception;
protected Exception node2Exception; protected Exception node2Exception;
protected AssertionFailedError node1Failure; protected AssertionFailedError node1Failure;
protected AssertionFailedError node2Failure; protected AssertionFailedError node2Failure;
public static Test getTestSetup(Class testClass, String configName) { @Before
TestSuite suite = new TestSuite( testClass ); public void prepareResources() throws Exception {
return new AccessStrategyTestSetup( suite, configName ); // to mimic exactly the old code results, both environments here are exactly the same...
} Configuration cfg = createConfiguration( getConfigurationName() );
localEnvironment = new NodeEnvironment( cfg );
localEnvironment.prepare();
public static Test getTestSetup(Test test, String configName) { localEntityRegion = localEnvironment.getEntityRegion( REGION_NAME, getCacheDataDescription() );
return new AccessStrategyTestSetup( test, configName );
}
/**
* Create a new TransactionalAccessTestCase.
*
* @param name
*/
public AbstractEntityRegionAccessStrategyTestCase(String name) {
super( name );
}
protected abstract AccessType getAccessType();
@Override
protected void setUp() throws Exception {
super.setUp();
// Sleep a bit to avoid concurrent FLUSH problem
avoidConcurrentFlush();
localEntityRegion = localRegionFactory.buildEntityRegion(
REGION_NAME, localCfg
.getProperties(), getCacheDataDescription()
);
localAccessStrategy = localEntityRegion.buildAccessStrategy( getAccessType() ); localAccessStrategy = localEntityRegion.buildAccessStrategy( getAccessType() );
localCache = ((BaseRegion) localEntityRegion).getCacheAdapter(); invalidation = localEntityRegion.getCacheAdapter().isClusteredInvalidation();
synchronous = localEntityRegion.getCacheAdapter().isSynchronous();
invalidation = localCache.isClusteredInvalidation();
synchronous = localCache.isSynchronous();
// Sleep a bit to avoid concurrent FLUSH problem // Sleep a bit to avoid concurrent FLUSH problem
avoidConcurrentFlush(); avoidConcurrentFlush();
remoteEntityRegion = remoteRegionFactory.buildEntityRegion( remoteEnvironment = new NodeEnvironment( cfg );
REGION_NAME, remoteCfg remoteEnvironment.prepare();
.getProperties(), getCacheDataDescription()
); remoteEntityRegion = remoteEnvironment.getEntityRegion( REGION_NAME, getCacheDataDescription() );
remoteAccessStrategy = remoteEntityRegion.buildAccessStrategy( getAccessType() ); remoteAccessStrategy = remoteEntityRegion.buildAccessStrategy( getAccessType() );
remoteCache = ((BaseRegion) remoteEntityRegion).getCacheAdapter();
node1Exception = null;
node2Exception = null;
node1Failure = null;
node2Failure = null;
} }
@Override protected abstract String getConfigurationName();
protected void tearDown() throws Exception {
super.tearDown();
try {
localCache.withFlags( FlagAdapter.CACHE_MODE_LOCAL ).clear();
}
catch (Exception e) {
LOG.error("Problem purging local cache", e);
}
try {
remoteCache.withFlags( FlagAdapter.CACHE_MODE_LOCAL ).clear();
}
catch (Exception e) {
LOG.error("Problem purging remote cache", e);
}
node1Exception = null;
node2Exception = null;
node1Failure = null;
node2Failure = null;
}
protected static Configuration createConfiguration(String configName) { protected static Configuration createConfiguration(String configName) {
Configuration cfg = CacheTestUtil.buildConfiguration( Configuration cfg = CacheTestUtil.buildConfiguration(
REGION_PREFIX, InfinispanRegionFactory.class, true, false REGION_PREFIX,
InfinispanRegionFactory.class,
true,
false
); );
cfg.setProperty( InfinispanRegionFactory.ENTITY_CACHE_RESOURCE_PROP, configName ); cfg.setProperty( InfinispanRegionFactory.ENTITY_CACHE_RESOURCE_PROP, configName );
return cfg; return cfg;
@ -178,6 +123,18 @@ protected CacheDataDescription getCacheDataDescription() {
return new CacheDataDescriptionImpl( true, true, ComparableComparator.INSTANCE ); return new CacheDataDescriptionImpl( true, true, ComparableComparator.INSTANCE );
} }
@After
public void releaseResources() throws Exception {
if ( localEnvironment != null ) {
localEnvironment.release();
}
if ( remoteEnvironment != null ) {
remoteEnvironment.release();
}
}
protected abstract AccessType getAccessType();
protected boolean isUsingInvalidation() { protected boolean isUsingInvalidation() {
return invalidation; return invalidation;
} }
@ -205,19 +162,20 @@ protected void assertThreadsRanCleanly() {
} }
} }
/** @Test
* This is just a setup test where we assert that the cache config is as we expected.
*/
public abstract void testCacheConfiguration(); public abstract void testCacheConfiguration();
@Test
public void testGetRegion() { public void testGetRegion() {
assertEquals( "Correct region", localEntityRegion, localAccessStrategy.getRegion() ); assertEquals( "Correct region", localEntityRegion, localAccessStrategy.getRegion() );
} }
@Test
public void testPutFromLoad() throws Exception { public void testPutFromLoad() throws Exception {
putFromLoadTest( false ); putFromLoadTest( false );
} }
@Test
public void testPutFromLoadMinimal() throws Exception { public void testPutFromLoadMinimal() throws Exception {
putFromLoadTest( true ); putFromLoadTest( true );
} }
@ -344,6 +302,7 @@ public void run() {
} }
} }
@Test
public void testInsert() throws Exception { public void testInsert() throws Exception {
final String KEY = KEY_BASE + testCount++; final String KEY = KEY_BASE + testCount++;
@ -438,6 +397,7 @@ public void run() {
assertEquals( "Correct node2 value", expected, remoteAccessStrategy.get( KEY, txTimestamp ) ); assertEquals( "Correct node2 value", expected, remoteAccessStrategy.get( KEY, txTimestamp ) );
} }
@Test
public void testUpdate() throws Exception { public void testUpdate() throws Exception {
final String KEY = KEY_BASE + testCount++; final String KEY = KEY_BASE + testCount++;
@ -541,26 +501,30 @@ public void run() {
assertEquals( "Correct node2 value", expected, remoteAccessStrategy.get( KEY, txTimestamp ) ); assertEquals( "Correct node2 value", expected, remoteAccessStrategy.get( KEY, txTimestamp ) );
} }
@Test
public void testRemove() { public void testRemove() {
evictOrRemoveTest( false ); evictOrRemoveTest( false );
} }
@Test
public void testRemoveAll() { public void testRemoveAll() {
evictOrRemoveAllTest( false ); evictOrRemoveAllTest( false );
} }
@Test
public void testEvict() { public void testEvict() {
evictOrRemoveTest( true ); evictOrRemoveTest( true );
} }
@Test
public void testEvictAll() { public void testEvictAll() {
evictOrRemoveAllTest( true ); evictOrRemoveAllTest( true );
} }
private void evictOrRemoveTest(boolean evict) { private void evictOrRemoveTest(boolean evict) {
final String KEY = KEY_BASE + testCount++; final String KEY = KEY_BASE + testCount++;
assertEquals( 0, getValidKeyCount( localCache.keySet() ) ); assertEquals( 0, getValidKeyCount( localEntityRegion.getCacheAdapter().keySet() ) );
assertEquals( 0, getValidKeyCount( remoteCache.keySet() ) ); assertEquals( 0, getValidKeyCount( remoteEntityRegion.getCacheAdapter().keySet() ) );
assertNull( "local is clean", localAccessStrategy.get( KEY, System.currentTimeMillis() ) ); assertNull( "local is clean", localAccessStrategy.get( KEY, System.currentTimeMillis() ) );
assertNull( "remote is clean", remoteAccessStrategy.get( KEY, System.currentTimeMillis() ) ); assertNull( "remote is clean", remoteAccessStrategy.get( KEY, System.currentTimeMillis() ) );
@ -578,15 +542,15 @@ private void evictOrRemoveTest(boolean evict) {
} }
assertEquals( null, localAccessStrategy.get( KEY, System.currentTimeMillis() ) ); assertEquals( null, localAccessStrategy.get( KEY, System.currentTimeMillis() ) );
assertEquals( 0, getValidKeyCount( localCache.keySet() ) ); assertEquals( 0, getValidKeyCount( localEntityRegion.getCacheAdapter().keySet() ) );
assertEquals( null, remoteAccessStrategy.get( KEY, System.currentTimeMillis() ) ); assertEquals( null, remoteAccessStrategy.get( KEY, System.currentTimeMillis() ) );
assertEquals( 0, getValidKeyCount( remoteCache.keySet() ) ); assertEquals( 0, getValidKeyCount( remoteEntityRegion.getCacheAdapter().keySet() ) );
} }
private void evictOrRemoveAllTest(boolean evict) { private void evictOrRemoveAllTest(boolean evict) {
final String KEY = KEY_BASE + testCount++; final String KEY = KEY_BASE + testCount++;
assertEquals( 0, getValidKeyCount( localCache.keySet() ) ); assertEquals( 0, getValidKeyCount( localEntityRegion.getCacheAdapter().keySet() ) );
assertEquals( 0, getValidKeyCount( remoteCache.keySet() ) ); assertEquals( 0, getValidKeyCount( remoteEntityRegion.getCacheAdapter().keySet() ) );
assertNull( "local is clean", localAccessStrategy.get( KEY, System.currentTimeMillis() ) ); assertNull( "local is clean", localAccessStrategy.get( KEY, System.currentTimeMillis() ) );
assertNull( "remote is clean", remoteAccessStrategy.get( KEY, System.currentTimeMillis() ) ); assertNull( "remote is clean", remoteAccessStrategy.get( KEY, System.currentTimeMillis() ) );
@ -612,17 +576,17 @@ private void evictOrRemoveAllTest(boolean evict) {
// This should re-establish the region root node in the optimistic case // This should re-establish the region root node in the optimistic case
assertNull( localAccessStrategy.get( KEY, System.currentTimeMillis() ) ); assertNull( localAccessStrategy.get( KEY, System.currentTimeMillis() ) );
assertEquals( 0, getValidKeyCount( localCache.keySet() ) ); assertEquals( 0, getValidKeyCount( localEntityRegion.getCacheAdapter().keySet() ) );
// Re-establishing the region root on the local node doesn't // Re-establishing the region root on the local node doesn't
// propagate it to other nodes. Do a get on the remote node to re-establish // propagate it to other nodes. Do a get on the remote node to re-establish
assertEquals( null, remoteAccessStrategy.get( KEY, System.currentTimeMillis() ) ); assertEquals( null, remoteAccessStrategy.get( KEY, System.currentTimeMillis() ) );
assertEquals( 0, getValidKeyCount( remoteCache.keySet() ) ); assertEquals( 0, getValidKeyCount( remoteEntityRegion.getCacheAdapter().keySet() ) );
// Test whether the get above messes up the optimistic version // Test whether the get above messes up the optimistic version
remoteAccessStrategy.putFromLoad( KEY, VALUE1, System.currentTimeMillis(), new Integer( 1 ) ); remoteAccessStrategy.putFromLoad( KEY, VALUE1, System.currentTimeMillis(), new Integer( 1 ) );
assertEquals( VALUE1, remoteAccessStrategy.get( KEY, System.currentTimeMillis() ) ); assertEquals( VALUE1, remoteAccessStrategy.get( KEY, System.currentTimeMillis() ) );
assertEquals( 1, getValidKeyCount( remoteCache.keySet() ) ); assertEquals( 1, getValidKeyCount( remoteEntityRegion.getCacheAdapter().keySet() ) );
// Wait for async propagation // Wait for async propagation
sleep( 250 ); sleep( 250 );
@ -647,71 +611,4 @@ protected void rollback() {
LOG.error(e.getMessage(), e); LOG.error(e.getMessage(), e);
} }
} }
private static class AccessStrategyTestSetup extends TestSetup {
private static final String PREFER_IPV4STACK = "java.net.preferIPv4Stack";
private final String configName;
private String preferIPv4Stack;
private ServiceRegistry localServiceRegistry;
private ServiceRegistry remoteServiceRegistry;
public AccessStrategyTestSetup(Test test, String configName) {
super( test );
this.configName = configName;
}
@Override
protected void setUp() throws Exception {
try {
super.tearDown();
}
finally {
if ( preferIPv4Stack == null ) {
System.clearProperty( PREFER_IPV4STACK );
}
else {
System.setProperty( PREFER_IPV4STACK, preferIPv4Stack );
}
}
// Try to ensure we use IPv4; otherwise cluster formation is very slow
preferIPv4Stack = System.getProperty( PREFER_IPV4STACK );
System.setProperty( PREFER_IPV4STACK, "true" );
localCfg = createConfiguration( configName );
localServiceRegistry = ServiceRegistryBuilder.buildServiceRegistry( localCfg.getProperties() );
localRegionFactory = CacheTestUtil.startRegionFactory( localServiceRegistry, localCfg );
remoteCfg = createConfiguration( configName );
remoteServiceRegistry = ServiceRegistryBuilder.buildServiceRegistry( remoteCfg.getProperties() );
remoteRegionFactory = CacheTestUtil.startRegionFactory( remoteServiceRegistry, remoteCfg );
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
try {
if ( localRegionFactory != null ) {
localRegionFactory.stop();
}
if ( remoteRegionFactory != null ) {
remoteRegionFactory.stop();
}
}
finally {
if ( localServiceRegistry != null ) {
ServiceRegistryBuilder.destroy( localServiceRegistry );
}
if ( remoteServiceRegistry != null ) {
ServiceRegistryBuilder.destroy( remoteServiceRegistry );
}
}
}
}
} }

View File

@ -22,9 +22,17 @@
* Boston, MA 02110-1301 USA * Boston, MA 02110-1301 USA
*/ */
package org.hibernate.test.cache.infinispan.entity; package org.hibernate.test.cache.infinispan.entity;
import org.hibernate.cache.access.AccessType;
import org.infinispan.transaction.tm.BatchModeTransactionManager; import org.infinispan.transaction.tm.BatchModeTransactionManager;
import org.hibernate.cache.access.AccessType;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
/** /**
* Base class for tests of TRANSACTIONAL access. * Base class for tests of TRANSACTIONAL access.
* *
@ -32,25 +40,18 @@
* @since 3.5 * @since 3.5
*/ */
public abstract class AbstractReadOnlyAccessTestCase extends AbstractEntityRegionAccessStrategyTestCase { public abstract class AbstractReadOnlyAccessTestCase extends AbstractEntityRegionAccessStrategyTestCase {
/**
* Create a new AbstractTransactionalAccessTestCase.
*
*/
public AbstractReadOnlyAccessTestCase(String name) {
super(name);
}
@Override @Override
protected AccessType getAccessType() { protected AccessType getAccessType() {
return AccessType.READ_ONLY; return AccessType.READ_ONLY;
} }
@Test
@Override @Override
public void testPutFromLoad() throws Exception { public void testPutFromLoad() throws Exception {
putFromLoadTest(false); putFromLoadTest(false);
} }
@Test
@Override @Override
public void testPutFromLoadMinimal() throws Exception { public void testPutFromLoadMinimal() throws Exception {
putFromLoadTest(true); putFromLoadTest(true);
@ -77,6 +78,7 @@ private void putFromLoadTest(boolean minimal) throws Exception {
assertEquals(expected, remoteAccessStrategy.get(KEY, System.currentTimeMillis())); assertEquals(expected, remoteAccessStrategy.get(KEY, System.currentTimeMillis()));
} }
@Test
@Override @Override
public void testUpdate() throws Exception { public void testUpdate() throws Exception {

View File

@ -22,13 +22,20 @@
* Boston, MA 02110-1301 USA * Boston, MA 02110-1301 USA
*/ */
package org.hibernate.test.cache.infinispan.entity; package org.hibernate.test.cache.infinispan.entity;
import static org.hibernate.TestLogger.LOG;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import junit.framework.AssertionFailedError;
import org.hibernate.cache.access.AccessType;
import org.infinispan.transaction.tm.BatchModeTransactionManager; import org.infinispan.transaction.tm.BatchModeTransactionManager;
import org.hibernate.cache.access.AccessType;
import junit.framework.AssertionFailedError;
import static org.hibernate.TestLogger.LOG;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/** /**
* Base class for tests of TRANSACTIONAL access. * Base class for tests of TRANSACTIONAL access.
* *
@ -36,11 +43,6 @@
* @since 3.5 * @since 3.5
*/ */
public abstract class AbstractTransactionalAccessTestCase extends AbstractEntityRegionAccessStrategyTestCase { public abstract class AbstractTransactionalAccessTestCase extends AbstractEntityRegionAccessStrategyTestCase {
public AbstractTransactionalAccessTestCase( String name ) {
super(name);
}
@Override @Override
protected AccessType getAccessType() { protected AccessType getAccessType() {
return AccessType.TRANSACTIONAL; return AccessType.TRANSACTIONAL;

View File

@ -32,8 +32,12 @@
import org.hibernate.cache.infinispan.InfinispanRegionFactory; import org.hibernate.cache.infinispan.InfinispanRegionFactory;
import org.hibernate.cache.infinispan.util.CacheAdapter; import org.hibernate.cache.infinispan.util.CacheAdapter;
import org.hibernate.cache.infinispan.util.CacheAdapterImpl; import org.hibernate.cache.infinispan.util.CacheAdapterImpl;
import org.hibernate.test.cache.infinispan.AbstractEntityCollectionRegionTestCase; import org.hibernate.test.cache.infinispan.AbstractEntityCollectionRegionTestCase;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
/** /**
* Tests of EntityRegionImpl. * Tests of EntityRegionImpl.
* *
@ -42,10 +46,6 @@
*/ */
public class EntityRegionImplTestCase extends AbstractEntityCollectionRegionTestCase { public class EntityRegionImplTestCase extends AbstractEntityCollectionRegionTestCase {
public EntityRegionImplTestCase(String name) {
super(name);
}
@Override @Override
protected void supportedAccessTypeTest(RegionFactory regionFactory, Properties properties) { protected void supportedAccessTypeTest(RegionFactory regionFactory, Properties properties) {
EntityRegion region = regionFactory.buildEntityRegion("test", properties, null); EntityRegion region = regionFactory.buildEntityRegion("test", properties, null);

View File

@ -20,9 +20,10 @@
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/ */
package org.hibernate.test.cache.infinispan.entity; package org.hibernate.test.cache.infinispan.entity;
import junit.framework.Test;
import junit.framework.TestSuite; import org.junit.Test;
import org.hibernate.test.cache.infinispan.util.CacheTestUtil;
import static org.junit.Assert.assertTrue;
/** /**
* InvalidatedTransactionalTestCase. * InvalidatedTransactionalTestCase.
@ -31,20 +32,15 @@
* @since 3.5 * @since 3.5
*/ */
public class InvalidatedTransactionalTestCase extends AbstractTransactionalAccessTestCase { public class InvalidatedTransactionalTestCase extends AbstractTransactionalAccessTestCase {
@Test
public InvalidatedTransactionalTestCase(String name) {
super(name);
}
@Override @Override
public void testCacheConfiguration() { public void testCacheConfiguration() {
assertTrue("Using Invalidation", isUsingInvalidation()); assertTrue("Using Invalidation", isUsingInvalidation());
assertTrue("Synchronous mode", isSynchronous()); assertTrue("Synchronous mode", isSynchronous());
} }
public static Test suite() throws Exception { @Override
TestSuite suite = CacheTestUtil.createFailureExpectedSuite(InvalidatedTransactionalTestCase.class); protected String getConfigurationName() {
return getTestSetup(suite, "entity"); return "entity";
} }
} }

View File

@ -21,11 +21,12 @@
*/ */
package org.hibernate.test.cache.infinispan.entity; package org.hibernate.test.cache.infinispan.entity;
import org.hibernate.cache.access.AccessType; import org.hibernate.cache.access.AccessType;
import org.hibernate.cache.access.EntityRegionAccessStrategy;
import static org.junit.Assert.fail;
/** /**
* Tests for the "extra API" in EntityRegionAccessStrategy; * Tests for the "extra API" in EntityRegionAccessStrategy;
* <p> * <p/>
* By "extra API" we mean those methods that are superfluous to the * By "extra API" we mean those methods that are superfluous to the
* function of the Infinispan integration, where the impl is a no-op or a static * function of the Infinispan integration, where the impl is a no-op or a static
* false return value, UnsupportedOperationException, etc. * false return value, UnsupportedOperationException, etc.
@ -34,61 +35,45 @@
* @since 3.5 * @since 3.5
*/ */
public class ReadOnlyExtraAPITestCase extends TransactionalExtraAPITestCase { public class ReadOnlyExtraAPITestCase extends TransactionalExtraAPITestCase {
private static EntityRegionAccessStrategy localAccessStrategy;
public ReadOnlyExtraAPITestCase(String name) {
super(name);
}
@Override @Override
protected AccessType getAccessType() { protected AccessType getAccessType() {
return AccessType.READ_ONLY; return AccessType.READ_ONLY;
} }
@Override
protected EntityRegionAccessStrategy getEntityAccessStrategy() {
return localAccessStrategy;
}
@Override
protected void setEntityRegionAccessStrategy(EntityRegionAccessStrategy strategy) {
localAccessStrategy = strategy;
}
/**
* Test method for {@link TransactionalAccess#lockItem(java.lang.Object, java.lang.Object)}.
*/
@Override @Override
public void testLockItem() { public void testLockItem() {
try { try {
getEntityAccessStrategy().lockItem(KEY, new Integer(1)); getEntityAccessStrategy().lockItem( KEY, Integer.valueOf( 1 ) );
fail("Call to lockItem did not throw exception"); fail( "Call to lockItem did not throw exception" );
}
catch (UnsupportedOperationException expected) {
} }
catch (UnsupportedOperationException expected) {}
} }
/**
* Test method for {@link TransactionalAccess#lockRegion()}.
*/
@Override @Override
public void testLockRegion() { public void testLockRegion() {
try { try {
getEntityAccessStrategy().lockRegion(); getEntityAccessStrategy().lockRegion();
fail("Call to lockRegion did not throw exception"); fail( "Call to lockRegion did not throw exception" );
}
catch (UnsupportedOperationException expected) {
} }
catch (UnsupportedOperationException expected) {}
} }
/**
* Test method for {@link TransactionalAccess#afterUpdate(java.lang.Object, java.lang.Object, java.lang.Object, java.lang.Object, org.hibernate.cache.access.SoftLock)}.
*/
@Override @Override
public void testAfterUpdate() { public void testAfterUpdate() {
try { try {
getEntityAccessStrategy().afterUpdate(KEY, VALUE2, new Integer(1), new Integer(2), new MockSoftLock()); getEntityAccessStrategy().afterUpdate(
fail("Call to afterUpdate did not throw exception"); KEY,
VALUE2,
Integer.valueOf( 1 ),
Integer.valueOf( 2 ),
new MockSoftLock()
);
fail( "Call to afterUpdate did not throw exception" );
}
catch (UnsupportedOperationException expected) {
} }
catch (UnsupportedOperationException expected) {}
} }
} }

View File

@ -22,9 +22,10 @@
* Boston, MA 02110-1301 USA * Boston, MA 02110-1301 USA
*/ */
package org.hibernate.test.cache.infinispan.entity; package org.hibernate.test.cache.infinispan.entity;
import junit.framework.Test;
import junit.framework.TestSuite; import org.junit.Test;
import org.hibernate.test.cache.infinispan.util.CacheTestUtil;
import static org.junit.Assert.assertTrue;
/** /**
* Tests READ_ONLY access when pessimistic locking and invalidation are used. * Tests READ_ONLY access when pessimistic locking and invalidation are used.
@ -33,28 +34,14 @@
* @since 3.5 * @since 3.5
*/ */
public class ReadOnlyTestCase extends AbstractReadOnlyAccessTestCase { public class ReadOnlyTestCase extends AbstractReadOnlyAccessTestCase {
@Override
/** protected String getConfigurationName() {
* Create a new PessimisticTransactionalAccessTestCase. return "entity";
*
* @param name
*/
public ReadOnlyTestCase(String name) {
super(name);
} }
public static Test suite() throws Exception { @Test
TestSuite suite = CacheTestUtil.createFailureExpectedSuite(ReadOnlyTestCase.class);
return getTestSetup(suite, "entity");
}
// Known failures
// Overrides
@Override @Override
public void testCacheConfiguration() { public void testCacheConfiguration() {
assertTrue("Using Invalidation", isUsingInvalidation()); assertTrue("Using Invalidation", isUsingInvalidation());
} }
} }

View File

@ -26,9 +26,19 @@
import org.hibernate.cache.access.SoftLock; import org.hibernate.cache.access.SoftLock;
import org.hibernate.cache.infinispan.InfinispanRegionFactory; import org.hibernate.cache.infinispan.InfinispanRegionFactory;
import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Configuration;
import org.hibernate.service.internal.ServiceRegistryImpl;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.hibernate.test.cache.infinispan.AbstractNonFunctionalTestCase; import org.hibernate.test.cache.infinispan.AbstractNonFunctionalTestCase;
import org.hibernate.test.cache.infinispan.NodeEnvironment;
import org.hibernate.test.cache.infinispan.util.CacheTestUtil; import org.hibernate.test.cache.infinispan.util.CacheTestUtil;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
/** /**
* Tests for the "extra API" in EntityRegionAccessStrategy;. * Tests for the "extra API" in EntityRegionAccessStrategy;.
* <p> * <p>
@ -40,40 +50,22 @@
* @since 3.5 * @since 3.5
*/ */
public class TransactionalExtraAPITestCase extends AbstractNonFunctionalTestCase { public class TransactionalExtraAPITestCase extends AbstractNonFunctionalTestCase {
public TransactionalExtraAPITestCase(String name) {
super(name);
}
public static final String REGION_NAME = "test/com.foo.test"; public static final String REGION_NAME = "test/com.foo.test";
public static final String KEY = "KEY"; public static final String KEY = "KEY";
public static final String VALUE1 = "VALUE1"; public static final String VALUE1 = "VALUE1";
public static final String VALUE2 = "VALUE2"; public static final String VALUE2 = "VALUE2";
private static EntityRegionAccessStrategy localAccessStrategy; private NodeEnvironment environment;
private EntityRegionAccessStrategy accessStrategy;
private static boolean optimistic; @Before
public final void prepareLocalAccessStrategy() throws Exception {
protected void setUp() throws Exception { environment = new NodeEnvironment( createConfiguration() );
super.setUp();
if (getEntityAccessStrategy() == null) {
Configuration cfg = createConfiguration();
InfinispanRegionFactory rf = CacheTestUtil.startRegionFactory(
getServiceRegistry( cfg.getProperties() ), cfg, getCacheTestSupport()
);
// Sleep a bit to avoid concurrent FLUSH problem // Sleep a bit to avoid concurrent FLUSH problem
avoidConcurrentFlush(); avoidConcurrentFlush();
EntityRegion localEntityRegion = rf.buildEntityRegion(REGION_NAME, cfg.getProperties(), null); accessStrategy = environment.getEntityRegion( REGION_NAME, null ).buildAccessStrategy( getAccessType() );
setEntityRegionAccessStrategy(localEntityRegion.buildAccessStrategy(getAccessType()));
}
}
protected void tearDown() throws Exception {
super.tearDown();
} }
protected Configuration createConfiguration() { protected Configuration createConfiguration() {
@ -82,6 +74,17 @@ protected Configuration createConfiguration() {
return cfg; return cfg;
} }
@After
public final void releaseLocalAccessStrategy() throws Exception {
if ( environment != null ) {
environment.release();
}
}
protected final EntityRegionAccessStrategy getEntityAccessStrategy() {
return accessStrategy;
}
protected String getCacheConfigName() { protected String getCacheConfigName() {
return "entity"; return "entity";
} }
@ -90,41 +93,55 @@ protected AccessType getAccessType() {
return AccessType.TRANSACTIONAL; return AccessType.TRANSACTIONAL;
} }
protected EntityRegionAccessStrategy getEntityAccessStrategy() { @Test
return localAccessStrategy; @SuppressWarnings( {"UnnecessaryBoxing"})
}
protected void setEntityRegionAccessStrategy(EntityRegionAccessStrategy strategy) {
localAccessStrategy = strategy;
}
public void testLockItem() { public void testLockItem() {
assertNull(getEntityAccessStrategy().lockItem(KEY, new Integer(1))); assertNull( getEntityAccessStrategy().lockItem( KEY, Integer.valueOf( 1 ) ) );
} }
@Test
public void testLockRegion() { public void testLockRegion() {
assertNull(getEntityAccessStrategy().lockRegion()); assertNull( getEntityAccessStrategy().lockRegion() );
} }
@Test
public void testUnlockItem() { public void testUnlockItem() {
getEntityAccessStrategy().unlockItem(KEY, new MockSoftLock()); getEntityAccessStrategy().unlockItem( KEY, new MockSoftLock() );
} }
@Test
public void testUnlockRegion() { public void testUnlockRegion() {
getEntityAccessStrategy().unlockItem(KEY, new MockSoftLock()); getEntityAccessStrategy().unlockItem( KEY, new MockSoftLock() );
} }
@Test
@SuppressWarnings( {"UnnecessaryBoxing"})
public void testAfterInsert() { public void testAfterInsert() {
assertFalse("afterInsert always returns false", getEntityAccessStrategy().afterInsert(KEY, VALUE1, new Integer(1))); assertFalse(
"afterInsert always returns false",
getEntityAccessStrategy().afterInsert(
KEY,
VALUE1,
Integer.valueOf( 1 )
)
);
} }
@Test
@SuppressWarnings( {"UnnecessaryBoxing"})
public void testAfterUpdate() { public void testAfterUpdate() {
assertFalse("afterInsert always returns false", getEntityAccessStrategy().afterUpdate(KEY, VALUE2, new Integer(1), new Integer(2), new MockSoftLock())); assertFalse(
"afterInsert always returns false",
getEntityAccessStrategy().afterUpdate(
KEY,
VALUE2,
Integer.valueOf( 1 ),
Integer.valueOf( 2 ),
new MockSoftLock()
)
);
} }
public static class MockSoftLock implements SoftLock { public static class MockSoftLock implements SoftLock {
} }
} }

View File

@ -23,11 +23,16 @@
*/ */
package org.hibernate.test.cache.infinispan.query; package org.hibernate.test.cache.infinispan.query;
import static org.hibernate.TestLogger.LOG;
import java.util.Properties; import java.util.Properties;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import junit.framework.AssertionFailedError;
import org.infinispan.notifications.Listener;
import org.infinispan.notifications.cachelistener.annotation.CacheEntryVisited;
import org.infinispan.notifications.cachelistener.event.CacheEntryVisitedEvent;
import org.infinispan.transaction.tm.BatchModeTransactionManager;
import org.infinispan.util.concurrent.IsolationLevel;
import org.hibernate.cache.CacheDataDescription; import org.hibernate.cache.CacheDataDescription;
import org.hibernate.cache.QueryResultsRegion; import org.hibernate.cache.QueryResultsRegion;
import org.hibernate.cache.Region; import org.hibernate.cache.Region;
@ -36,13 +41,17 @@
import org.hibernate.cache.infinispan.util.CacheAdapter; import org.hibernate.cache.infinispan.util.CacheAdapter;
import org.hibernate.cache.infinispan.util.CacheAdapterImpl; import org.hibernate.cache.infinispan.util.CacheAdapterImpl;
import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Configuration;
import org.hibernate.service.internal.ServiceRegistryImpl;
import junit.framework.AssertionFailedError;
import org.hibernate.test.cache.infinispan.AbstractGeneralDataRegionTestCase; import org.hibernate.test.cache.infinispan.AbstractGeneralDataRegionTestCase;
import org.hibernate.test.cache.infinispan.util.CacheTestUtil; import org.hibernate.test.cache.infinispan.util.CacheTestUtil;
import org.infinispan.notifications.Listener;
import org.infinispan.notifications.cachelistener.annotation.CacheEntryVisited; import static org.hibernate.TestLogger.LOG;
import org.infinispan.notifications.cachelistener.event.CacheEntryVisitedEvent; import static org.junit.Assert.assertEquals;
import org.infinispan.transaction.tm.BatchModeTransactionManager; import static org.junit.Assert.assertFalse;
import org.infinispan.util.concurrent.IsolationLevel; import static org.junit.Assert.assertTrue;
/** /**
* Tests of QueryResultRegionImpl. * Tests of QueryResultRegionImpl.
@ -52,60 +61,52 @@
*/ */
public class QueryRegionImplTestCase extends AbstractGeneralDataRegionTestCase { public class QueryRegionImplTestCase extends AbstractGeneralDataRegionTestCase {
// protected static final String REGION_NAME = "test/" + StandardQueryCache.class.getName();
/**
* Create a new EntityRegionImplTestCase.
*
* @param name
*/
public QueryRegionImplTestCase( String name ) {
super(name);
}
@Override @Override
protected Region createRegion( InfinispanRegionFactory regionFactory, protected Region createRegion(
InfinispanRegionFactory regionFactory,
String regionName, String regionName,
Properties properties, Properties properties,
CacheDataDescription cdd ) { CacheDataDescription cdd) {
return regionFactory.buildQueryResultsRegion(regionName, properties); return regionFactory.buildQueryResultsRegion( regionName, properties );
} }
@Override @Override
protected String getStandardRegionName( String regionPrefix ) { protected String getStandardRegionName(String regionPrefix) {
return regionPrefix + "/" + StandardQueryCache.class.getName(); return regionPrefix + "/" + StandardQueryCache.class.getName();
} }
@Override @Override
protected CacheAdapter getInfinispanCache( InfinispanRegionFactory regionFactory ) { protected CacheAdapter getInfinispanCache(InfinispanRegionFactory regionFactory) {
return CacheAdapterImpl.newInstance(regionFactory.getCacheManager().getCache("local-query")); return CacheAdapterImpl.newInstance( regionFactory.getCacheManager().getCache( "local-query" ) );
} }
@Override @Override
protected Configuration createConfiguration() { protected Configuration createConfiguration() {
return CacheTestUtil.buildCustomQueryCacheConfiguration("test", "replicated-query"); return CacheTestUtil.buildCustomQueryCacheConfiguration( "test", "replicated-query" );
}
public void testPutDoesNotBlockGet() throws Exception {
putDoesNotBlockGetTest();
} }
private void putDoesNotBlockGetTest() throws Exception { private void putDoesNotBlockGetTest() throws Exception {
Configuration cfg = createConfiguration(); Configuration cfg = createConfiguration();
InfinispanRegionFactory regionFactory = CacheTestUtil.startRegionFactory(getServiceRegistry(cfg.getProperties()), cfg, getCacheTestSupport()); InfinispanRegionFactory regionFactory = CacheTestUtil.startRegionFactory(
new ServiceRegistryImpl( cfg.getProperties() ),
cfg,
getCacheTestSupport()
);
// Sleep a bit to avoid concurrent FLUSH problem // Sleep a bit to avoid concurrent FLUSH problem
avoidConcurrentFlush(); avoidConcurrentFlush();
final QueryResultsRegion region = regionFactory.buildQueryResultsRegion(getStandardRegionName(REGION_PREFIX), final QueryResultsRegion region = regionFactory.buildQueryResultsRegion(
cfg.getProperties()); getStandardRegionName( REGION_PREFIX ),
cfg.getProperties()
);
region.put(KEY, VALUE1); region.put( KEY, VALUE1 );
assertEquals(VALUE1, region.get(KEY)); assertEquals( VALUE1, region.get( KEY ) );
final CountDownLatch readerLatch = new CountDownLatch(1); final CountDownLatch readerLatch = new CountDownLatch( 1 );
final CountDownLatch writerLatch = new CountDownLatch(1); final CountDownLatch writerLatch = new CountDownLatch( 1 );
final CountDownLatch completionLatch = new CountDownLatch(1); final CountDownLatch completionLatch = new CountDownLatch( 1 );
final ExceptionHolder holder = new ExceptionHolder(); final ExceptionHolder holder = new ExceptionHolder();
Thread reader = new Thread() { Thread reader = new Thread() {
@ -113,16 +114,19 @@ private void putDoesNotBlockGetTest() throws Exception {
public void run() { public void run() {
try { try {
BatchModeTransactionManager.getInstance().begin(); BatchModeTransactionManager.getInstance().begin();
LOG.debug("Transaction began, get value for key"); LOG.debug( "Transaction began, get value for key" );
assertTrue(VALUE2.equals(region.get(KEY)) == false); assertTrue( VALUE2.equals( region.get( KEY ) ) == false );
BatchModeTransactionManager.getInstance().commit(); BatchModeTransactionManager.getInstance().commit();
} catch (AssertionFailedError e) { }
catch (AssertionFailedError e) {
holder.a1 = e; holder.a1 = e;
rollback(); rollback();
} catch (Exception e) { }
catch (Exception e) {
holder.e1 = e; holder.e1 = e;
rollback(); rollback();
} finally { }
finally {
readerLatch.countDown(); readerLatch.countDown();
} }
} }
@ -133,42 +137,48 @@ public void run() {
public void run() { public void run() {
try { try {
BatchModeTransactionManager.getInstance().begin(); BatchModeTransactionManager.getInstance().begin();
LOG.debug("Put value2"); LOG.debug( "Put value2" );
region.put(KEY, VALUE2); region.put( KEY, VALUE2 );
LOG.debug("Put finished for value2, await writer latch"); LOG.debug( "Put finished for value2, await writer latch" );
writerLatch.await(); writerLatch.await();
LOG.debug("Writer latch finished"); LOG.debug( "Writer latch finished" );
BatchModeTransactionManager.getInstance().commit(); BatchModeTransactionManager.getInstance().commit();
LOG.debug("Transaction committed"); LOG.debug( "Transaction committed" );
} catch (Exception e) { }
catch (Exception e) {
holder.e2 = e; holder.e2 = e;
rollback(); rollback();
} finally { }
finally {
completionLatch.countDown(); completionLatch.countDown();
} }
} }
}; };
reader.setDaemon(true); reader.setDaemon( true );
writer.setDaemon(true); writer.setDaemon( true );
writer.start(); writer.start();
assertFalse("Writer is blocking", completionLatch.await(100, TimeUnit.MILLISECONDS)); assertFalse( "Writer is blocking", completionLatch.await( 100, TimeUnit.MILLISECONDS ) );
// Start the reader // Start the reader
reader.start(); reader.start();
assertTrue("Reader finished promptly", readerLatch.await(1000000000, TimeUnit.MILLISECONDS)); assertTrue( "Reader finished promptly", readerLatch.await( 1000000000, TimeUnit.MILLISECONDS ) );
writerLatch.countDown(); writerLatch.countDown();
assertTrue("Reader finished promptly", completionLatch.await(100, TimeUnit.MILLISECONDS)); assertTrue( "Reader finished promptly", completionLatch.await( 100, TimeUnit.MILLISECONDS ) );
assertEquals(VALUE2, region.get(KEY)); assertEquals( VALUE2, region.get( KEY ) );
if (holder.a1 != null) throw holder.a1; if ( holder.a1 != null ) {
else if (holder.a2 != null) throw holder.a2; throw holder.a1;
}
else if ( holder.a2 != null ) {
throw holder.a2;
}
assertEquals("writer saw no exceptions", null, holder.e1); assertEquals( "writer saw no exceptions", null, holder.e1 );
assertEquals("reader saw no exceptions", null, holder.e2); assertEquals( "reader saw no exceptions", null, holder.e2 );
} }
public void testGetDoesNotBlockPut() throws Exception { public void testGetDoesNotBlockPut() throws Exception {
@ -177,23 +187,29 @@ public void testGetDoesNotBlockPut() throws Exception {
private void getDoesNotBlockPutTest() throws Exception { private void getDoesNotBlockPutTest() throws Exception {
Configuration cfg = createConfiguration(); Configuration cfg = createConfiguration();
InfinispanRegionFactory regionFactory = CacheTestUtil.startRegionFactory(getServiceRegistry(cfg.getProperties()), cfg, getCacheTestSupport()); InfinispanRegionFactory regionFactory = CacheTestUtil.startRegionFactory(
new ServiceRegistryImpl( cfg.getProperties() ),
cfg,
getCacheTestSupport()
);
// Sleep a bit to avoid concurrent FLUSH problem // Sleep a bit to avoid concurrent FLUSH problem
avoidConcurrentFlush(); avoidConcurrentFlush();
final QueryResultsRegion region = regionFactory.buildQueryResultsRegion(getStandardRegionName(REGION_PREFIX), final QueryResultsRegion region = regionFactory.buildQueryResultsRegion(
cfg.getProperties()); getStandardRegionName( REGION_PREFIX ),
cfg.getProperties()
);
region.put(KEY, VALUE1); region.put( KEY, VALUE1 );
assertEquals(VALUE1, region.get(KEY)); assertEquals( VALUE1, region.get( KEY ) );
// final Fqn rootFqn = getRegionFqn(getStandardRegionName(REGION_PREFIX), REGION_PREFIX); // final Fqn rootFqn = getRegionFqn(getStandardRegionName(REGION_PREFIX), REGION_PREFIX);
final CacheAdapter jbc = getInfinispanCache(regionFactory); final CacheAdapter jbc = getInfinispanCache( regionFactory );
final CountDownLatch blockerLatch = new CountDownLatch(1); final CountDownLatch blockerLatch = new CountDownLatch( 1 );
final CountDownLatch writerLatch = new CountDownLatch(1); final CountDownLatch writerLatch = new CountDownLatch( 1 );
final CountDownLatch completionLatch = new CountDownLatch(1); final CountDownLatch completionLatch = new CountDownLatch( 1 );
final ExceptionHolder holder = new ExceptionHolder(); final ExceptionHolder holder = new ExceptionHolder();
Thread blocker = new Thread() { Thread blocker = new Thread() {
@ -201,18 +217,20 @@ private void getDoesNotBlockPutTest() throws Exception {
@Override @Override
public void run() { public void run() {
// Fqn toBlock = new Fqn(rootFqn, KEY); // Fqn toBlock = new Fqn(rootFqn, KEY);
GetBlocker blocker = new GetBlocker(blockerLatch, KEY); GetBlocker blocker = new GetBlocker( blockerLatch, KEY );
try { try {
jbc.addListener(blocker); jbc.addListener( blocker );
BatchModeTransactionManager.getInstance().begin(); BatchModeTransactionManager.getInstance().begin();
region.get(KEY); region.get( KEY );
BatchModeTransactionManager.getInstance().commit(); BatchModeTransactionManager.getInstance().commit();
} catch (Exception e) { }
catch (Exception e) {
holder.e1 = e; holder.e1 = e;
rollback(); rollback();
} finally { }
jbc.removeListener(blocker); finally {
jbc.removeListener( blocker );
} }
} }
}; };
@ -225,46 +243,56 @@ public void run() {
writerLatch.await(); writerLatch.await();
BatchModeTransactionManager.getInstance().begin(); BatchModeTransactionManager.getInstance().begin();
region.put(KEY, VALUE2); region.put( KEY, VALUE2 );
BatchModeTransactionManager.getInstance().commit(); BatchModeTransactionManager.getInstance().commit();
} catch (Exception e) { }
catch (Exception e) {
holder.e2 = e; holder.e2 = e;
rollback(); rollback();
} finally { }
finally {
completionLatch.countDown(); completionLatch.countDown();
} }
} }
}; };
blocker.setDaemon(true); blocker.setDaemon( true );
writer.setDaemon(true); writer.setDaemon( true );
boolean unblocked = false; boolean unblocked = false;
try { try {
blocker.start(); blocker.start();
writer.start(); writer.start();
assertFalse("Blocker is blocking", completionLatch.await(100, TimeUnit.MILLISECONDS)); assertFalse( "Blocker is blocking", completionLatch.await( 100, TimeUnit.MILLISECONDS ) );
// Start the writer // Start the writer
writerLatch.countDown(); writerLatch.countDown();
assertTrue("Writer finished promptly", completionLatch.await(100, TimeUnit.MILLISECONDS)); assertTrue( "Writer finished promptly", completionLatch.await( 100, TimeUnit.MILLISECONDS ) );
blockerLatch.countDown(); blockerLatch.countDown();
unblocked = true; unblocked = true;
if (IsolationLevel.REPEATABLE_READ.equals(jbc.getConfiguration().getIsolationLevel())) { if ( IsolationLevel.REPEATABLE_READ.equals( jbc.getConfiguration().getIsolationLevel() ) ) {
assertEquals(VALUE1, region.get(KEY)); assertEquals( VALUE1, region.get( KEY ) );
} else { }
assertEquals(VALUE2, region.get(KEY)); else {
assertEquals( VALUE2, region.get( KEY ) );
} }
if (holder.a1 != null) throw holder.a1; if ( holder.a1 != null ) {
else if (holder.a2 != null) throw holder.a2; throw holder.a1;
}
else if ( holder.a2 != null ) {
throw holder.a2;
}
assertEquals("blocker saw no exceptions", null, holder.e1); assertEquals( "blocker saw no exceptions", null, holder.e1 );
assertEquals("writer saw no exceptions", null, holder.e2); assertEquals( "writer saw no exceptions", null, holder.e2 );
} finally { }
if (!unblocked) blockerLatch.countDown(); finally {
if ( !unblocked ) {
blockerLatch.countDown();
}
} }
} }
@ -275,19 +303,22 @@ public class GetBlocker {
// private Fqn fqn; // private Fqn fqn;
private Object key; private Object key;
GetBlocker( CountDownLatch latch, GetBlocker(
Object key ) { CountDownLatch latch,
Object key
) {
this.latch = latch; this.latch = latch;
this.key = key; this.key = key;
} }
@CacheEntryVisited @CacheEntryVisited
public void nodeVisisted( CacheEntryVisitedEvent event ) { public void nodeVisisted(CacheEntryVisitedEvent event) {
if (event.isPre() && event.getKey().equals(key)) { if ( event.isPre() && event.getKey().equals( key ) ) {
try { try {
latch.await(); latch.await();
} catch (InterruptedException e) { }
LOG.error("Interrupted waiting for latch", e); catch (InterruptedException e) {
LOG.error( "Interrupted waiting for latch", e );
} }
} }
} }

View File

@ -22,22 +22,9 @@
* Boston, MA 02110-1301 USA * Boston, MA 02110-1301 USA
*/ */
package org.hibernate.test.cache.infinispan.timestamp; package org.hibernate.test.cache.infinispan.timestamp;
import java.util.Properties; import java.util.Properties;
import org.hibernate.cache.CacheDataDescription;
import org.hibernate.cache.Region;
import org.hibernate.cache.UpdateTimestampsCache;
import org.hibernate.cache.infinispan.InfinispanRegionFactory;
import org.hibernate.cache.infinispan.impl.ClassLoaderAwareCache;
import org.hibernate.cache.infinispan.timestamp.TimestampsRegionImpl;
import org.hibernate.cache.infinispan.util.CacheAdapter;
import org.hibernate.cache.infinispan.util.CacheAdapterImpl;
import org.hibernate.cache.infinispan.util.FlagAdapter;
import org.hibernate.cfg.Configuration;
import org.hibernate.test.cache.infinispan.AbstractGeneralDataRegionTestCase;
import org.hibernate.test.cache.infinispan.functional.classloader.Account;
import org.hibernate.test.cache.infinispan.functional.classloader.AccountHolder;
import org.hibernate.test.cache.infinispan.functional.classloader.SelectedClassnameClassLoader;
import org.hibernate.test.cache.infinispan.util.CacheTestUtil;
import org.infinispan.AdvancedCache; import org.infinispan.AdvancedCache;
import org.infinispan.notifications.Listener; import org.infinispan.notifications.Listener;
import org.infinispan.notifications.cachelistener.annotation.CacheEntryActivated; import org.infinispan.notifications.cachelistener.annotation.CacheEntryActivated;
@ -51,6 +38,24 @@
import org.infinispan.notifications.cachelistener.annotation.CacheEntryVisited; import org.infinispan.notifications.cachelistener.annotation.CacheEntryVisited;
import org.infinispan.notifications.cachelistener.event.Event; import org.infinispan.notifications.cachelistener.event.Event;
import org.hibernate.cache.CacheDataDescription;
import org.hibernate.cache.Region;
import org.hibernate.cache.UpdateTimestampsCache;
import org.hibernate.cache.infinispan.InfinispanRegionFactory;
import org.hibernate.cache.infinispan.impl.ClassLoaderAwareCache;
import org.hibernate.cache.infinispan.timestamp.TimestampsRegionImpl;
import org.hibernate.cache.infinispan.util.CacheAdapter;
import org.hibernate.cache.infinispan.util.CacheAdapterImpl;
import org.hibernate.cache.infinispan.util.FlagAdapter;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.internal.ServiceRegistryImpl;
import org.hibernate.test.cache.infinispan.AbstractGeneralDataRegionTestCase;
import org.hibernate.test.cache.infinispan.functional.classloader.Account;
import org.hibernate.test.cache.infinispan.functional.classloader.AccountHolder;
import org.hibernate.test.cache.infinispan.functional.classloader.SelectedClassnameClassLoader;
import org.hibernate.test.cache.infinispan.util.CacheTestUtil;
/** /**
* Tests of TimestampsRegionImpl. * Tests of TimestampsRegionImpl.
* *
@ -59,10 +64,6 @@
*/ */
public class TimestampsRegionImplTestCase extends AbstractGeneralDataRegionTestCase { public class TimestampsRegionImplTestCase extends AbstractGeneralDataRegionTestCase {
public TimestampsRegionImplTestCase(String name) {
super(name);
}
@Override @Override
protected String getStandardRegionName(String regionPrefix) { protected String getStandardRegionName(String regionPrefix) {
return regionPrefix + "/" + UpdateTimestampsCache.class.getName(); return regionPrefix + "/" + UpdateTimestampsCache.class.getName();
@ -81,14 +82,18 @@ protected CacheAdapter getInfinispanCache(InfinispanRegionFactory regionFactory)
public void testClearTimestampsRegionInIsolated() throws Exception { public void testClearTimestampsRegionInIsolated() throws Exception {
Configuration cfg = createConfiguration(); Configuration cfg = createConfiguration();
InfinispanRegionFactory regionFactory = CacheTestUtil.startRegionFactory( InfinispanRegionFactory regionFactory = CacheTestUtil.startRegionFactory(
getServiceRegistry(cfg.getProperties()), cfg, getCacheTestSupport() new ServiceRegistryImpl( cfg.getProperties() ),
cfg,
getCacheTestSupport()
); );
// Sleep a bit to avoid concurrent FLUSH problem // Sleep a bit to avoid concurrent FLUSH problem
avoidConcurrentFlush(); avoidConcurrentFlush();
Configuration cfg2 = createConfiguration(); Configuration cfg2 = createConfiguration();
InfinispanRegionFactory regionFactory2 = CacheTestUtil.startRegionFactory( InfinispanRegionFactory regionFactory2 = CacheTestUtil.startRegionFactory(
getServiceRegistry(cfg2.getProperties()), cfg2, getCacheTestSupport() new ServiceRegistryImpl( cfg.getProperties() ),
cfg2,
getCacheTestSupport()
); );
// Sleep a bit to avoid concurrent FLUSH problem // Sleep a bit to avoid concurrent FLUSH problem
avoidConcurrentFlush(); avoidConcurrentFlush();
@ -118,8 +123,7 @@ public void testClearTimestampsRegionInIsolated() throws Exception {
@Override @Override
protected Configuration createConfiguration() { protected Configuration createConfiguration() {
Configuration cfg = CacheTestUtil.buildConfiguration("test", MockInfinispanRegionFactory.class, false, true); return CacheTestUtil.buildConfiguration("test", MockInfinispanRegionFactory.class, false, true);
return cfg;
} }
public static class MockInfinispanRegionFactory extends InfinispanRegionFactory { public static class MockInfinispanRegionFactory extends InfinispanRegionFactory {

View File

@ -22,13 +22,17 @@
* Boston, MA 02110-1301 USA * Boston, MA 02110-1301 USA
*/ */
package org.hibernate.test.cache.infinispan.util; package org.hibernate.test.cache.infinispan.util;
import static org.hibernate.TestLogger.LOG;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.Set; import java.util.Set;
import org.hibernate.cache.RegionFactory;
import org.infinispan.Cache; import org.infinispan.Cache;
import org.hibernate.cache.RegionFactory;
import static org.hibernate.TestLogger.LOG;
/** /**
* Support class for tracking and cleaning up objects used in tests. * Support class for tracking and cleaning up objects used in tests.
* *
@ -52,11 +56,11 @@ public void registerFactory(RegionFactory factory) {
} }
public void unregisterCache(Cache cache) { public void unregisterCache(Cache cache) {
caches.remove(cache); caches.remove( cache );
} }
public void unregisterFactory(RegionFactory factory) { public void unregisterFactory(RegionFactory factory) {
factories.remove(factory); factories.remove( factory );
} }
public void setUp() throws Exception { public void setUp() throws Exception {
@ -83,7 +87,7 @@ public void tearDown() throws Exception {
public void avoidConcurrentFlush() { public void avoidConcurrentFlush() {
// JG 2.6.1 has a problem where calling flush more than once too quickly // JG 2.6.1 has a problem where calling flush more than once too quickly
// can result in several second delays // can result in several second delays
sleep(100); sleep( 100 );
} }
private void sleep(long ms) { private void sleep(long ms) {