Added tests for newly introduced RegionFactories and upgraded to 2.4.1 (2.3 is busted with Hibernate 4.0)

This commit is contained in:
Alex Snaps 2011-04-05 17:46:57 +02:00
parent 1da2262a4a
commit 8bd56a097a
4 changed files with 84 additions and 13 deletions

View File

@ -2,6 +2,7 @@
dependencies {
compile( project( ':hibernate-core' ) )
compile( [group: 'net.sf.ehcache', name: 'ehcache-core', version: '2.3.1'] )
compile( [group: 'net.sf.ehcache', name: 'ehcache-core', version: '2.4.1'] )
testCompile( project(':hibernate-testing') )
testRuntime( [group: 'org.slf4j', name: 'slf4j-simple', version: '1.6.1'] )
}

View File

@ -0,0 +1,39 @@
package org.hibernate.test.cache.ehcache;
import org.hibernate.cache.EhCacheRegionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import java.lang.reflect.Field;
import java.util.Map;
/**
* @author Alex Snaps
*/
public class EhCacheRegionFactoryImpl extends EhCacheTest {
@Override
protected void configCache(final Configuration cfg) {
cfg.setProperty(Environment.CACHE_REGION_FACTORY, EhCacheRegionFactory.class.getName());
cfg.setProperty( Environment.CACHE_PROVIDER_CONFIG, "ehcache.xml" );
}
@Override
protected Map getMapFromCacheEntry(final Object entry) {
final Map map;
if ("net.sf.ehcache.hibernate.strategy.AbstractReadWriteEhcacheAccessStrategy$Item".equals(entry.getClass().getName())) {
try {
Field field = entry.getClass().getDeclaredField("value");
field.setAccessible(true);
map = (Map)field.get(entry);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
} else {
map = (Map)entry;
}
return map;
}
}

View File

@ -0,0 +1,31 @@
package org.hibernate.test.cache.ehcache;
import org.hibernate.cache.EhCacheProvider;
import org.hibernate.cache.ReadWriteCache;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import java.util.Map;
/**
* @author Alex Snaps
*/
public class EhCacheRegionTest extends EhCacheTest {
@Override
protected void configCache(final Configuration cfg) {
cfg.setProperty( Environment.CACHE_PROVIDER, EhCacheProvider.class.getName() );
cfg.setProperty( Environment.CACHE_PROVIDER_CONFIG, "ehcache.xml" );
}
@Override
protected Map getMapFromCacheEntry(final Object entry) {
final Map map;
if ( entry instanceof ReadWriteCache.Item ) {
map = (Map) ( (ReadWriteCache.Item) entry ).getValue();
}
else {
map = (Map) entry;
}
return map;
}
}

View File

@ -44,8 +44,9 @@ import static org.junit.Assert.fail;
/**
* @author Emmanuel Bernard
* @author Alex Snaps
*/
public class EhCacheTest extends BaseCoreFunctionalTestCase {
public abstract class EhCacheTest extends BaseCoreFunctionalTestCase {
@Override
public String getBaseForMappings() {
return "org/hibernate/test/cache/ehcache/";
@ -68,11 +69,12 @@ public class EhCacheTest extends BaseCoreFunctionalTestCase {
cfg.setProperty( Environment.USE_SECOND_LEVEL_CACHE, "true" );
cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
cfg.setProperty( Environment.USE_STRUCTURED_CACHE, "true" );
cfg.setProperty( Environment.CACHE_PROVIDER, EhCacheProvider.class.getName() );
cfg.setProperty( Environment.CACHE_PROVIDER_CONFIG, "ehcache.xml" );
configCache(cfg);
cfg.setProperty( Environment.TRANSACTION_STRATEGY, JdbcTransactionFactory.class.getName() );
}
protected abstract void configCache(final Configuration cfg);
@Test
public void testQueryCacheInvalidation() {
Session s = openSession();
@ -107,12 +109,7 @@ public class EhCacheTest extends BaseCoreFunctionalTestCase {
Object entry = slcs.getEntries().get( i.getId() );
Map map;
if ( entry instanceof ReadWriteCache.Item ) {
map = (Map) ( (ReadWriteCache.Item) entry ).getValue();
}
else {
map = (Map) entry;
}
map = getMapFromCacheEntry(entry);
assertTrue( map.get("description").equals("A bog standard item") );
assertTrue( map.get("name").equals("widget") );
@ -124,6 +121,8 @@ public class EhCacheTest extends BaseCoreFunctionalTestCase {
s.close();
}
protected abstract Map getMapFromCacheEntry(final Object entry);
@Test
public void testEmptySecondLevelCacheEntry() throws Exception {
sessionFactory().getCache().evictEntityRegion( Item.class.getName() );
@ -188,9 +187,10 @@ public class EhCacheTest extends BaseCoreFunctionalTestCase {
if ( entry instanceof ReadWriteCache.Lock ) {
//FIXME don't know what to test here
cachedVersionValue = Long.valueOf( ((ReadWriteCache.Lock) entry).getUnlockTimestamp() );
}
else {
cachedVersionValue = ( Long ) ( (Map) entry ).get( "_version" );
} else if(entry.getClass().getName().equals("net.sf.ehcache.hibernate.strategy.AbstractReadWriteEhcacheAccessStrategy$Lock")) {
//FIXME don't know what to test here
} else {
cachedVersionValue = ( Long ) getMapFromCacheEntry(entry).get( "_version" );
assertEquals( initialVersion.longValue(), cachedVersionValue.longValue() );
}