diff --git a/src/main/java/com/googlecode/hibernate/memcached/AbstractKeyStrategy.java b/src/main/java/com/googlecode/hibernate/memcached/AbstractKeyStrategy.java index ad68693..1d5c2f5 100644 --- a/src/main/java/com/googlecode/hibernate/memcached/AbstractKeyStrategy.java +++ b/src/main/java/com/googlecode/hibernate/memcached/AbstractKeyStrategy.java @@ -1,11 +1,10 @@ package com.googlecode.hibernate.memcached; -import com.googlecode.hibernate.memcached.utils.StringUtils; +import java.util.regex.Pattern; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.regex.Pattern; - /** * KeyStrategy base class that handles concatenation, cleaning, and truncating the final cache key. *

@@ -30,7 +29,8 @@ public String toKey(String regionName, long clearIndex, Object key) { String keyString = concatenateKey(regionName, clearIndex, transformKeyObject(key)); if (keyString.length() > MAX_KEY_LENGTH) { - throw new IllegalArgumentException("Key is longer than " + MAX_KEY_LENGTH + " characters, try using the Sha1KeyStrategy: " + keyString); + throw new IllegalArgumentException("Key is longer than " + MAX_KEY_LENGTH + + " characters, try using the Sha1KeyStrategy: " + keyString); } String finalKey = CLEAN_PATTERN.matcher(keyString).replaceAll(""); @@ -41,11 +41,6 @@ public String toKey(String regionName, long clearIndex, Object key) { protected abstract String transformKeyObject(Object key); protected String concatenateKey(String regionName, long clearIndex, Object key) { - return new StringBuilder() - .append(regionName) - .append(":") - .append(clearIndex) - .append(":") - .append(String.valueOf(key)).toString(); + return regionName + ":" + clearIndex + ":" + key; } } diff --git a/src/main/java/com/googlecode/hibernate/memcached/DigestKeyStrategy.java b/src/main/java/com/googlecode/hibernate/memcached/DigestKeyStrategy.java index 5b0a199..bdd1ea1 100644 --- a/src/main/java/com/googlecode/hibernate/memcached/DigestKeyStrategy.java +++ b/src/main/java/com/googlecode/hibernate/memcached/DigestKeyStrategy.java @@ -1,16 +1,16 @@ package com.googlecode.hibernate.memcached; -import com.googlecode.hibernate.memcached.utils.StringUtils; - /** * @author Ray Krueger */ public abstract class DigestKeyStrategy extends AbstractKeyStrategy { + @Override protected String transformKeyObject(Object key) { return key.toString() + ":" + key.hashCode(); } + @Override protected String concatenateKey(String regionName, long clearIndex, Object key) { String longKey = super.concatenateKey(regionName, clearIndex, key); return digest(longKey); diff --git a/src/main/java/com/googlecode/hibernate/memcached/Md5KeyStrategy.java b/src/main/java/com/googlecode/hibernate/memcached/Md5KeyStrategy.java index 2f44138..721443c 100755 --- a/src/main/java/com/googlecode/hibernate/memcached/Md5KeyStrategy.java +++ b/src/main/java/com/googlecode/hibernate/memcached/Md5KeyStrategy.java @@ -6,6 +6,7 @@ * @author Ray Krueger */ public class Md5KeyStrategy extends DigestKeyStrategy { + @Override protected String digest(String key) { return StringUtils.md5Hex(key); } diff --git a/src/main/java/com/googlecode/hibernate/memcached/MemcachedCache.java b/src/main/java/com/googlecode/hibernate/memcached/MemcachedCache.java index 17cd429..2447862 100644 --- a/src/main/java/com/googlecode/hibernate/memcached/MemcachedCache.java +++ b/src/main/java/com/googlecode/hibernate/memcached/MemcachedCache.java @@ -14,13 +14,13 @@ */ package com.googlecode.hibernate.memcached; +import java.util.Map; + import org.hibernate.cache.Cache; import org.hibernate.cache.CacheException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Map; - /** * Wrapper around MemcachedClient instance to provide the bridge between Hiberante and Memcached. * Uses the regionName given by Hibernate via the {@link com.googlecode.hibernate.memcached.MemcachedCacheProvider} @@ -47,7 +47,7 @@ */ public class MemcachedCache implements Cache { - private final Logger log = LoggerFactory.getLogger(MemcachedCache.class); + private final Logger log = LoggerFactory.getLogger(getClass()); private final String regionName; private final Memcache memcache; @@ -110,22 +110,20 @@ private Object memcacheGet(Object key) { if (dogpilePreventionEnabled) { return getUsingDogpilePrevention(objectKey); - - } else { - log.debug("Memcache.get({})", objectKey); - return memcache.get(objectKey); } + + log.debug("Memcache.get({})", objectKey); + return memcache.get(objectKey); } private Object getUsingDogpilePrevention(String objectKey) { - Map multi; String dogpileKey = dogpileTokenKey(objectKey); log.debug("Checking dogpile key: [{}]", dogpileKey); log.debug("Memcache.getMulti({}, {})", objectKey, dogpileKey); - multi = memcache.getMulti(dogpileKey, objectKey); + Map multi = memcache.getMulti(dogpileKey, objectKey); if ((multi == null) || (multi.get(dogpileKey) == null)) { log.debug("Dogpile key ({}) not found updating token and returning null", dogpileKey); memcache.set(dogpileKey, cacheTimeSeconds, DOGPILE_TOKEN); @@ -227,6 +225,7 @@ public Map toMap() { throw new UnsupportedOperationException(); } + @Override public String toString() { return "Memcached (" + regionName + ")"; } diff --git a/src/main/java/com/googlecode/hibernate/memcached/MemcachedCacheProvider.java b/src/main/java/com/googlecode/hibernate/memcached/MemcachedCacheProvider.java index afe4dcf..478e38d 100644 --- a/src/main/java/com/googlecode/hibernate/memcached/MemcachedCacheProvider.java +++ b/src/main/java/com/googlecode/hibernate/memcached/MemcachedCacheProvider.java @@ -14,15 +14,15 @@ */ package com.googlecode.hibernate.memcached; +import java.lang.reflect.Constructor; +import java.util.Properties; + import org.hibernate.cache.Cache; import org.hibernate.cache.CacheException; import org.hibernate.cache.CacheProvider; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.lang.reflect.Constructor; -import java.util.Properties; - /** * Configures an instance of {@link MemcachedCache} for use as a second-level cache in Hibernate. * To use set the hibernate property hibernate.cache.provider_class to the name of this class. @@ -83,7 +83,7 @@ */ public class MemcachedCacheProvider implements CacheProvider { - private final Logger log = LoggerFactory.getLogger(MemcachedCacheProvider.class); + private final Logger log = LoggerFactory.getLogger(getClass()); private Memcache client; @@ -100,13 +100,9 @@ public Cache buildCache(String regionName, Properties properties) throws CacheEx setKeyStrategy(keyStrategy, cache); } - cache.setCacheTimeSeconds( - config.getCacheTimeSeconds(regionName) - ); + cache.setCacheTimeSeconds(config.getCacheTimeSeconds(regionName)); - cache.setClearSupported( - config.isClearSupported(regionName) - ); + cache.setClearSupported(config.isClearSupported(regionName)); boolean dogpilePrevention = config.isDogpilePreventionEnabled(regionName); cache.setDogpilePreventionEnabled(dogpilePrevention); diff --git a/src/main/java/com/googlecode/hibernate/memcached/Sha1KeyStrategy.java b/src/main/java/com/googlecode/hibernate/memcached/Sha1KeyStrategy.java index 38b8edd..27e50c7 100755 --- a/src/main/java/com/googlecode/hibernate/memcached/Sha1KeyStrategy.java +++ b/src/main/java/com/googlecode/hibernate/memcached/Sha1KeyStrategy.java @@ -6,6 +6,7 @@ * @author Ray Krueger */ public class Sha1KeyStrategy extends DigestKeyStrategy { + @Override protected String digest(String key) { return StringUtils.sha1Hex(key); } diff --git a/src/main/java/com/googlecode/hibernate/memcached/spymemcached/SpyMemcache.java b/src/main/java/com/googlecode/hibernate/memcached/spymemcached/SpyMemcache.java index a3bb844..015339e 100644 --- a/src/main/java/com/googlecode/hibernate/memcached/spymemcached/SpyMemcache.java +++ b/src/main/java/com/googlecode/hibernate/memcached/spymemcached/SpyMemcache.java @@ -1,14 +1,21 @@ package com.googlecode.hibernate.memcached.spymemcached; -import com.googlecode.hibernate.memcached.LoggingMemcacheExceptionHandler; -import com.googlecode.hibernate.memcached.Memcache; -import com.googlecode.hibernate.memcached.MemcacheExceptionHandler; -import com.googlecode.hibernate.memcached.utils.StringUtils; +import java.util.Map; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import net.spy.memcached.ConnectionFactory; import net.spy.memcached.MemcachedClient; +import net.spy.memcached.internal.OperationFuture; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Map; +import com.googlecode.hibernate.memcached.LoggingMemcacheExceptionHandler; +import com.googlecode.hibernate.memcached.Memcache; +import com.googlecode.hibernate.memcached.MemcacheExceptionHandler; +import com.googlecode.hibernate.memcached.utils.StringUtils; /** * DOCUMENT ME! @@ -21,9 +28,29 @@ public class SpyMemcache implements Memcache { private MemcacheExceptionHandler exceptionHandler = new LoggingMemcacheExceptionHandler(); private final MemcachedClient memcachedClient; + private final ConnectionFactory connectionFactory; + private final Long operationTimeout; + private final boolean asyncWrites; public SpyMemcache(MemcachedClient memcachedClient) { + this(memcachedClient, null, true); + } + + public SpyMemcache(MemcachedClient memcachedClient, ConnectionFactory connectionFactory, boolean asyncWrites) { this.memcachedClient = memcachedClient; + this.connectionFactory = connectionFactory; + this.asyncWrites = asyncWrites; + if (connectionFactory == null) { + operationTimeout = null; + } + else { + operationTimeout = connectionFactory.getOperationTimeout(); + log.debug("Using operationTimeout {}ms", operationTimeout); + } + } + + public ConnectionFactory getConnectionFactory() { + return connectionFactory; } public Object get(String key) { @@ -48,7 +75,8 @@ public Map getMulti(String... keys) { public void set(String key, int cacheTimeSeconds, Object o) { log.debug("MemcachedClient.set({})", key); try { - memcachedClient.set(key, cacheTimeSeconds, o); + OperationFuture future = memcachedClient.set(key, cacheTimeSeconds, o); + waitIfNecessary(future); } catch (Exception e) { exceptionHandler.handleErrorOnSet(key, cacheTimeSeconds, o, e); } @@ -56,7 +84,8 @@ public void set(String key, int cacheTimeSeconds, Object o) { public void delete(String key) { try { - memcachedClient.delete(key); + OperationFuture future = memcachedClient.delete(key); + waitIfNecessary(future); } catch (Exception e) { exceptionHandler.handleErrorOnDelete(key, e); } @@ -78,4 +107,10 @@ public void shutdown() { public void setExceptionHandler(MemcacheExceptionHandler exceptionHandler) { this.exceptionHandler = exceptionHandler; } + + protected void waitIfNecessary(OperationFuture future) throws InterruptedException, TimeoutException, ExecutionException { + if (!asyncWrites || operationTimeout == null) { + future.get(operationTimeout, TimeUnit.MILLISECONDS); + } + } } diff --git a/src/main/java/com/googlecode/hibernate/memcached/spymemcached/SpyMemcacheClientFactory.java b/src/main/java/com/googlecode/hibernate/memcached/spymemcached/SpyMemcacheClientFactory.java index edcc126..9fd83e0 100644 --- a/src/main/java/com/googlecode/hibernate/memcached/spymemcached/SpyMemcacheClientFactory.java +++ b/src/main/java/com/googlecode/hibernate/memcached/spymemcached/SpyMemcacheClientFactory.java @@ -10,6 +10,9 @@ import net.spy.memcached.auth.AuthDescriptor; import net.spy.memcached.auth.PlainCallbackHandler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import com.googlecode.hibernate.memcached.Config; import com.googlecode.hibernate.memcached.Memcache; import com.googlecode.hibernate.memcached.MemcacheClientFactory; @@ -23,6 +26,8 @@ */ public class SpyMemcacheClientFactory implements MemcacheClientFactory { + private final Logger log = LoggerFactory.getLogger(getClass()); + public static final String PROP_SERVERS = Config.PROP_PREFIX + "servers"; public static final String PROP_OPERATION_QUEUE_LENGTH = Config.PROP_PREFIX + "operationQueueLength"; public static final String PROP_READ_BUFFER_SIZE = Config.PROP_PREFIX + "readBufferSize"; @@ -32,6 +37,7 @@ public class SpyMemcacheClientFactory implements MemcacheClientFactory { public static final String PROP_DAEMON_MODE = Config.PROP_PREFIX + "daemonMode"; public static final String PROP_USERNAME = Config.PROP_PREFIX + "username"; public static final String PROP_PASSWORD = Config.PROP_PREFIX + "password"; + public static final String PROP_ASYNC_WRITES = Config.PROP_PREFIX + "asyncWrites"; private final PropertiesHelper properties; public SpyMemcacheClientFactory(PropertiesHelper properties) { @@ -43,7 +49,13 @@ public Memcache createMemcacheClient() throws Exception { ConnectionFactory connectionFactory = getConnectionFactory(); MemcachedClient client = new MemcachedClient(connectionFactory, AddrUtil.getAddresses(getServerList())); - return new SpyMemcache(client); + + boolean asyncWrites = "true".equals(properties.get(PROP_ASYNC_WRITES, "true")); + + log.debug("Creating new SpyMemcache with connectionFactory {} and asyncWrites: {}", + connectionFactory.getClass().getName(), asyncWrites); + + return new SpyMemcache(client, connectionFactory, asyncWrites); } protected ConnectionFactory getConnectionFactory() { diff --git a/src/test/groovy/com/googlecode/hibernate/memcached/AbstractKeyStrategyTestCase.groovy b/src/test/groovy/com/googlecode/hibernate/memcached/AbstractKeyStrategyTestCase.groovy index cb1b8a7..f25ba71 100755 --- a/src/test/groovy/com/googlecode/hibernate/memcached/AbstractKeyStrategyTestCase.groovy +++ b/src/test/groovy/com/googlecode/hibernate/memcached/AbstractKeyStrategyTestCase.groovy @@ -10,6 +10,7 @@ abstract class AbstractKeyStrategyTestCase extends BaseTestCase { protected KeyStrategy strategy protected void setUp() { + super.setUp() strategy = getKeyStrategy() } @@ -19,11 +20,10 @@ abstract class AbstractKeyStrategyTestCase extends BaseTestCase { } void assert_null_key_does_not_validate() { - shouldFailWithCause(IllegalArgumentException.class) { + shouldFail(IllegalArgumentException) { strategy.toKey(null, 0, null) } } - abstract KeyStrategy getKeyStrategy(); - + abstract KeyStrategy getKeyStrategy() } diff --git a/src/test/groovy/com/googlecode/hibernate/memcached/BaseTestCase.groovy b/src/test/groovy/com/googlecode/hibernate/memcached/BaseTestCase.groovy index 9b6b4a6..129eef5 100644 --- a/src/test/groovy/com/googlecode/hibernate/memcached/BaseTestCase.groovy +++ b/src/test/groovy/com/googlecode/hibernate/memcached/BaseTestCase.groovy @@ -1,9 +1,10 @@ package com.googlecode.hibernate.memcached + /** * DOCUMENT ME! * @author Ray Krueger */ -abstract class BaseTestCase extends groovy.util.GroovyTestCase { +abstract class BaseTestCase extends GroovyTestCase { static { LoggingConfig.initializeLogging() diff --git a/src/test/groovy/com/googlecode/hibernate/memcached/ConfigTest.groovy b/src/test/groovy/com/googlecode/hibernate/memcached/ConfigTest.groovy index 8bff345..1999952 100644 --- a/src/test/groovy/com/googlecode/hibernate/memcached/ConfigTest.groovy +++ b/src/test/groovy/com/googlecode/hibernate/memcached/ConfigTest.groovy @@ -1,4 +1,5 @@ package com.googlecode.hibernate.memcached + /** * DOCUMENT ME! * @author Ray Krueger @@ -72,7 +73,5 @@ class ConfigTest extends BaseTestCase { p["hibernate.memcached.memcacheClientFactory"] = "blah" assertEquals "blah", config.getMemcachedClientFactoryName() - - } } diff --git a/src/test/groovy/com/googlecode/hibernate/memcached/HashCodeKeyStrategyTest.groovy b/src/test/groovy/com/googlecode/hibernate/memcached/HashCodeKeyStrategyTest.groovy index 60a8f9a..18acc87 100644 --- a/src/test/groovy/com/googlecode/hibernate/memcached/HashCodeKeyStrategyTest.groovy +++ b/src/test/groovy/com/googlecode/hibernate/memcached/HashCodeKeyStrategyTest.groovy @@ -7,8 +7,8 @@ package com.googlecode.hibernate.memcached */ class HashCodeKeyStrategyTest extends AbstractKeyStrategyTestCase { - public KeyStrategy getKeyStrategy() { - return new HashCodeKeyStrategy() + KeyStrategy getKeyStrategy() { + new HashCodeKeyStrategy() } void test() { @@ -28,11 +28,10 @@ class HashCodeKeyStrategyTest extends AbstractKeyStrategyTestCase { } void test_really_long_key_throws_exception() { - String regionName = "" - 250.times {regionName += "x"} + StringBuilder regionName = new StringBuilder() + 250.times {regionName << "x"} shouldFail(IllegalArgumentException) { - getKeyStrategy().toKey(regionName, 0, "blah blah blah") + getKeyStrategy().toKey(regionName.toString(), 0, "blah blah blah") } } - } diff --git a/src/test/groovy/com/googlecode/hibernate/memcached/LoggingConfig.groovy b/src/test/groovy/com/googlecode/hibernate/memcached/LoggingConfig.groovy index ab98621..2b94077 100644 --- a/src/test/groovy/com/googlecode/hibernate/memcached/LoggingConfig.groovy +++ b/src/test/groovy/com/googlecode/hibernate/memcached/LoggingConfig.groovy @@ -12,9 +12,8 @@ class LoggingConfig { System.setProperty "net.spy.log.LoggerImpl", "net.spy.log.Log4JLogger" } - public static void initializeLogging() { - BasicConfigurator.resetConfiguration(); - BasicConfigurator.configure(); + static void initializeLogging() { + BasicConfigurator.resetConfiguration() + BasicConfigurator.configure() } - -} \ No newline at end of file +} diff --git a/src/test/groovy/com/googlecode/hibernate/memcached/LoggingMemcacheExceptionHandlerTest.groovy b/src/test/groovy/com/googlecode/hibernate/memcached/LoggingMemcacheExceptionHandlerTest.groovy index b836f86..96cbdac 100755 --- a/src/test/groovy/com/googlecode/hibernate/memcached/LoggingMemcacheExceptionHandlerTest.groovy +++ b/src/test/groovy/com/googlecode/hibernate/memcached/LoggingMemcacheExceptionHandlerTest.groovy @@ -1,12 +1,6 @@ package com.googlecode.hibernate.memcached -import org.apache.log4j.Appender -import org.apache.log4j.Layout import org.apache.log4j.Logger -import org.apache.log4j.spi.ErrorHandler -import org.apache.log4j.spi.Filter -import org.apache.log4j.spi.LoggingEvent -import org.junit.Assert /** * This test is lame, I have no idea what I should do to make it better. @@ -18,6 +12,7 @@ class LoggingMemcacheExceptionHandlerTest extends BaseTestCase { Logger logger = Logger.getLogger(LoggingMemcacheExceptionHandler) protected void setUp() { + super.setUp() logger.removeAllAppenders() } @@ -52,5 +47,4 @@ class LoggingMemcacheExceptionHandlerTest extends BaseTestCase { handler.handleErrorOnSet "blah", 300, new Object(), exception assert appender.appenderCalled } - } diff --git a/src/test/groovy/com/googlecode/hibernate/memcached/Md5KeyStrategyTest.groovy b/src/test/groovy/com/googlecode/hibernate/memcached/Md5KeyStrategyTest.groovy index 9c8bf62..681677a 100755 --- a/src/test/groovy/com/googlecode/hibernate/memcached/Md5KeyStrategyTest.groovy +++ b/src/test/groovy/com/googlecode/hibernate/memcached/Md5KeyStrategyTest.groovy @@ -7,8 +7,8 @@ package com.googlecode.hibernate.memcached */ class Md5KeyStrategyTest extends AbstractKeyStrategyTestCase { - public KeyStrategy getKeyStrategy() { - return new Md5KeyStrategy() + KeyStrategy getKeyStrategy() { + new Md5KeyStrategy() } void test() { @@ -28,9 +28,9 @@ class Md5KeyStrategyTest extends AbstractKeyStrategyTestCase { } void test_really_long_keys_get_truncated() { - String regionName = "" - 250.times {regionName += "x"} - assert_cache_key_equals "16df3d87c2f8bde43fcdbb545be10626", regionName, 0, "blah blah blah" + StringBuilder regionName = new StringBuilder() + 250.times {regionName << "x"} + assert_cache_key_equals "16df3d87c2f8bde43fcdbb545be10626", regionName.toString(), 0, "blah blah blah" } } diff --git a/src/test/groovy/com/googlecode/hibernate/memcached/MemcachedCacheTest.groovy b/src/test/groovy/com/googlecode/hibernate/memcached/MemcachedCacheTest.groovy index 78148ec..e273581 100644 --- a/src/test/groovy/com/googlecode/hibernate/memcached/MemcachedCacheTest.groovy +++ b/src/test/groovy/com/googlecode/hibernate/memcached/MemcachedCacheTest.groovy @@ -19,13 +19,12 @@ class MemcachedCacheTest extends BaseTestCase { cache.remove "test" assertNull cache.get("test") - } void test_dogpile_cache_miss() { MockMemcached mockCache = new MockMemcached() cache = new MemcachedCache("region", mockCache) - cache.setKeyStrategy(new HashCodeKeyStrategy()); + cache.setKeyStrategy(new HashCodeKeyStrategy()) cache.dogpilePreventionEnabled = true cache.cacheTimeSeconds = 1 cache.dogpilePreventionExpirationFactor = 2 @@ -40,7 +39,7 @@ class MemcachedCacheTest extends BaseTestCase { void test_dogpile_cache_hit() { MockMemcached mockCache = new MockMemcached() cache = new MemcachedCache("region", mockCache) - cache.setKeyStrategy(new HashCodeKeyStrategy()); + cache.setKeyStrategy(new HashCodeKeyStrategy()) cache.dogpilePreventionEnabled = true cache.cacheTimeSeconds = 1 cache.dogpilePreventionExpirationFactor = 2 @@ -49,5 +48,4 @@ class MemcachedCacheTest extends BaseTestCase { assertEquals "value", mockCache.cache["region:0:3556498"] assertEquals MemcachedCache.DOGPILE_TOKEN, mockCache.cache["region:0:3556498.dogpileTokenKey"] } - } diff --git a/src/test/groovy/com/googlecode/hibernate/memcached/MemcachedProviderTest.groovy b/src/test/groovy/com/googlecode/hibernate/memcached/MemcachedProviderTest.groovy index b9c3409..2064691 100644 --- a/src/test/groovy/com/googlecode/hibernate/memcached/MemcachedProviderTest.groovy +++ b/src/test/groovy/com/googlecode/hibernate/memcached/MemcachedProviderTest.groovy @@ -1,4 +1,4 @@ -package com.googlecode.hibernate.memcached; +package com.googlecode.hibernate.memcached /** * DOCUMENT ME! @@ -9,20 +9,21 @@ class MemcachedProviderTest extends BaseTestCase { MemcachedCacheProvider provider - void setUp() { + protected void setUp() { + super.setUp() provider = new MemcachedCacheProvider() } void test_defaults() { Properties properties = new Properties() provider.start(properties) - MemcachedCache cache = (MemcachedCache) provider.buildCache("test", properties) + MemcachedCache cache = provider.buildCache("test", properties) assertNotNull(cache) //assert Defaults assertFalse(cache.isClearSupported()) assertEquals(300, cache.getCacheTimeSeconds()) - assertEquals Sha1KeyStrategy.class, cache.getKeyStrategy().class + assertEquals Sha1KeyStrategy, cache.getKeyStrategy().getClass() } void test_region_properties() { @@ -31,29 +32,30 @@ class MemcachedProviderTest extends BaseTestCase { properties.setProperty "hibernate.memcached.serverList", "127.0.0.1:11211" properties.setProperty "hibernate.memcached.test.cacheTimeSeconds", "500" properties.setProperty "hibernate.memcached.test.clearSupported", "true" - properties.setProperty "hibernate.memcached.test.keyStrategy", StringKeyStrategy.class.getName() + properties.setProperty "hibernate.memcached.test.keyStrategy", StringKeyStrategy.name provider.start(properties) - MemcachedCache cache = (MemcachedCache) provider.buildCache("test", properties) + MemcachedCache cache = provider.buildCache("test", properties) assertNotNull(cache) //assert Defaults assertTrue(cache.isClearSupported()) assertEquals(500, cache.getCacheTimeSeconds()) - assertEquals(StringKeyStrategy.class, cache.getKeyStrategy().class) + assertEquals(StringKeyStrategy, cache.getKeyStrategy().getClass()) } void test_string_key_strategy() { Properties properties = new Properties() - properties.setProperty("hibernate.memcached.keyStrategy", StringKeyStrategy.class.getName()) + properties.setProperty("hibernate.memcached.keyStrategy", StringKeyStrategy.name) provider.start(properties) - MemcachedCache cache = (MemcachedCache) provider.buildCache("test", properties) + MemcachedCache cache = provider.buildCache("test", properties) assertNotNull(cache) } - void tearDown() { + protected void tearDown() { + super.tearDown() provider.stop() } } diff --git a/src/test/groovy/com/googlecode/hibernate/memcached/MockMemcached.groovy b/src/test/groovy/com/googlecode/hibernate/memcached/MockMemcached.groovy index 871a36a..b5d85e5 100644 --- a/src/test/groovy/com/googlecode/hibernate/memcached/MockMemcached.groovy +++ b/src/test/groovy/com/googlecode/hibernate/memcached/MockMemcached.groovy @@ -1,4 +1,5 @@ package com.googlecode.hibernate.memcached + /** * DOCUMENT ME! * @author Ray Krueger @@ -7,33 +8,32 @@ class MockMemcached implements Memcache { def cache = [:] - public Object get(String key) { + Object get(String key) { cache[key] } - public void set(String key, int cacheTimeSeconds, Object o) { + void set(String key, int cacheTimeSeconds, Object o) { cache[key] = o } - public void delete(String key) { + void delete(String key) { cache.remove key } - public void incr(String key, int factor, int startingValue) { - Integer counter = (Integer) cache[key] - if (counter != null) { - cache[key] = counter + 1 - } else { + void incr(String key, int factor, int startingValue) { + Integer counter = cache[key] + if (counter == null) { cache[key] = counter + } else { + cache[key] = counter + 1 } } - public void shutdown() { + void shutdown() { } - - public Map getMulti(String[] keys) { - return cache.findAll {key, value -> keys.toList().contains(key)} - } -} \ No newline at end of file + Map getMulti(String[] keys) { + cache.findAll {key, value -> keys.toList().contains(key)} + } +} diff --git a/src/test/groovy/com/googlecode/hibernate/memcached/PropertiesHelperTest.groovy b/src/test/groovy/com/googlecode/hibernate/memcached/PropertiesHelperTest.groovy index 97d9c0a..de2e84b 100644 --- a/src/test/groovy/com/googlecode/hibernate/memcached/PropertiesHelperTest.groovy +++ b/src/test/groovy/com/googlecode/hibernate/memcached/PropertiesHelperTest.groovy @@ -7,11 +7,10 @@ class PropertiesHelperTest extends BaseTestCase { PropertiesHelper helper protected void setUp() { + super.setUp() helper = newHelper() } - - void test_strings() { assertEquals "world", helper.get("hello") assertEquals "world", helper.get("hello", "blah") @@ -53,7 +52,6 @@ class PropertiesHelperTest extends BaseTestCase { shouldFail(IllegalArgumentException) { helper.getEnum("hello", TimeUnit, TimeUnit.NANOSECONDS) } - } void test_find_values() { @@ -62,14 +60,11 @@ class PropertiesHelperTest extends BaseTestCase { } PropertiesHelper newHelper() { - Properties props = new Properties() - props.hello = "world" - props.one = "1" - props.thisIsTrue = "true" - props.thisIsFalse = "false" - props.seconds = "SECONDS" - - new PropertiesHelper(props) + new PropertiesHelper( + [hello: "world", + one: "1", + thisIsTrue: "true", + thisIsFalse: "false", + seconds: "SECONDS"] as Properties) } - -} \ No newline at end of file +} diff --git a/src/test/groovy/com/googlecode/hibernate/memcached/Sha1KeyStrategyTest.groovy b/src/test/groovy/com/googlecode/hibernate/memcached/Sha1KeyStrategyTest.groovy index b155863..26cdb7b 100755 --- a/src/test/groovy/com/googlecode/hibernate/memcached/Sha1KeyStrategyTest.groovy +++ b/src/test/groovy/com/googlecode/hibernate/memcached/Sha1KeyStrategyTest.groovy @@ -7,8 +7,8 @@ package com.googlecode.hibernate.memcached */ class Sha1KeyStrategyTest extends AbstractKeyStrategyTestCase { - public KeyStrategy getKeyStrategy() { - return new Sha1KeyStrategy() + KeyStrategy getKeyStrategy() { + new Sha1KeyStrategy() } void test() { @@ -28,9 +28,8 @@ class Sha1KeyStrategyTest extends AbstractKeyStrategyTestCase { } void test_really_long_keys_get_truncated() { - String regionName = "" - 250.times {regionName += "x"} - assert_cache_key_equals "7f00c6faf1fefaf62cabb512285cc60ce641d5c8", regionName, 0, "blah blah blah" + StringBuilder regionName = new StringBuilder() + 250.times {regionName << "x"} + assert_cache_key_equals "7f00c6faf1fefaf62cabb512285cc60ce641d5c8", regionName.toString(), 0, "blah blah blah" } - } diff --git a/src/test/groovy/com/googlecode/hibernate/memcached/StringKeyStrategyTest.groovy b/src/test/groovy/com/googlecode/hibernate/memcached/StringKeyStrategyTest.groovy index 5011ccf..cf29f31 100644 --- a/src/test/groovy/com/googlecode/hibernate/memcached/StringKeyStrategyTest.groovy +++ b/src/test/groovy/com/googlecode/hibernate/memcached/StringKeyStrategyTest.groovy @@ -7,7 +7,7 @@ package com.googlecode.hibernate.memcached */ class StringKeyStrategyTest extends AbstractKeyStrategyTestCase { - public KeyStrategy getKeyStrategy() { + KeyStrategy getKeyStrategy() { new StringKeyStrategy() } @@ -28,11 +28,10 @@ class StringKeyStrategyTest extends AbstractKeyStrategyTestCase { } void test_really_long_key_throws_exception() { - String regionName = "" - 250.times {regionName += "x"} + StringBuilder regionName = new StringBuilder() + 250.times {regionName << "x"} shouldFail(IllegalArgumentException) { - getKeyStrategy().toKey(regionName, 0, "blah blah blah") + getKeyStrategy().toKey(regionName.toString(), 0, "blah blah blah") } } - } diff --git a/src/test/groovy/com/googlecode/hibernate/memcached/spymemcached/SpyMemcacheClientFactoryTest.groovy b/src/test/groovy/com/googlecode/hibernate/memcached/spymemcached/SpyMemcacheClientFactoryTest.groovy index ab9b825..6eff26d 100644 --- a/src/test/groovy/com/googlecode/hibernate/memcached/spymemcached/SpyMemcacheClientFactoryTest.groovy +++ b/src/test/groovy/com/googlecode/hibernate/memcached/spymemcached/SpyMemcacheClientFactoryTest.groovy @@ -61,6 +61,7 @@ class SpyMemcacheClientFactoryTest extends BaseTestCase { } protected void tearDown() { + super.tearDown() client?.shutdown() } } diff --git a/src/test/groovy/com/googlecode/hibernate/memcached/spymemcached/SpyMemcacheTest.groovy b/src/test/groovy/com/googlecode/hibernate/memcached/spymemcached/SpyMemcacheTest.groovy index 41a8b81..ae32911 100644 --- a/src/test/groovy/com/googlecode/hibernate/memcached/spymemcached/SpyMemcacheTest.groovy +++ b/src/test/groovy/com/googlecode/hibernate/memcached/spymemcached/SpyMemcacheTest.groovy @@ -16,11 +16,13 @@ class SpyMemcacheTest extends BaseTestCase { MemcachedClient client protected void setUp() { + super.setUp() client = new MemcachedClient(AddrUtil.getAddresses("localhost:11211")) cache = new MemcachedCache("MemcachedCacheTest", new SpyMemcache(client)) } protected void tearDown() { + super.tearDown() client.shutdown() } @@ -39,5 +41,4 @@ class SpyMemcacheTest extends BaseTestCase { Thread.sleep(100) assertNull(cache.get("test")) } - } diff --git a/src/test/groovy/com/googlecode/hibernate/memcached/utils/StringUtilsTest.groovy b/src/test/groovy/com/googlecode/hibernate/memcached/utils/StringUtilsTest.groovy index 025af9d..ffefec8 100644 --- a/src/test/groovy/com/googlecode/hibernate/memcached/utils/StringUtilsTest.groovy +++ b/src/test/groovy/com/googlecode/hibernate/memcached/utils/StringUtilsTest.groovy @@ -29,6 +29,4 @@ class StringUtilsTest extends BaseTestCase { StringUtils.md5Hex(null) } } - - } \ No newline at end of file diff --git a/src/test/java/com/googlecode/hibernate/memcached/integration/AbstractHibernateTestCase.java b/src/test/java/com/googlecode/hibernate/memcached/integration/AbstractHibernateTestCase.java index 9377c3c..8a4fd28 100644 --- a/src/test/java/com/googlecode/hibernate/memcached/integration/AbstractHibernateTestCase.java +++ b/src/test/java/com/googlecode/hibernate/memcached/integration/AbstractHibernateTestCase.java @@ -1,13 +1,14 @@ package com.googlecode.hibernate.memcached.integration; -import com.googlecode.hibernate.memcached.BaseTestCase; +import java.util.Properties; + import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.cfg.Configuration; -import java.util.Properties; +import com.googlecode.hibernate.memcached.BaseTestCase; /** * DOCUMENT ME! @@ -29,7 +30,6 @@ private Configuration getConfiguration() { config.setProperties(properties); config.addAnnotatedClass(Contact.class); - return config; } @@ -53,7 +53,8 @@ void setupBeforeTransaction() { } @Override - protected void setUp() { + protected void setUp() throws Exception { + super.setUp(); setupBeforeTransaction(); SessionFactory sessionFactory = getConfiguration().buildSessionFactory(); session = sessionFactory.openSession(); @@ -68,7 +69,8 @@ protected void tearDownInTransaction() { } @Override - protected void tearDown() { + protected void tearDown() throws Exception { + super.tearDown(); try { tearDownInTransaction(); } finally { @@ -76,5 +78,4 @@ protected void tearDown() { session.close(); } } - } diff --git a/src/test/java/com/googlecode/hibernate/memcached/integration/Contact.java b/src/test/java/com/googlecode/hibernate/memcached/integration/Contact.java index 036b677..ac76824 100644 --- a/src/test/java/com/googlecode/hibernate/memcached/integration/Contact.java +++ b/src/test/java/com/googlecode/hibernate/memcached/integration/Contact.java @@ -1,14 +1,14 @@ package com.googlecode.hibernate.memcached.integration; -import org.hibernate.annotations.Cache; -import org.hibernate.annotations.CacheConcurrencyStrategy; -import org.hibernate.annotations.Type; +import java.util.Date; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; -import java.util.Date; +import org.hibernate.annotations.Cache; +import org.hibernate.annotations.CacheConcurrencyStrategy; +import org.hibernate.annotations.Type; /** * @author Ray Krueger @@ -60,6 +60,7 @@ public void setBirthday(Date birthday) { this.birthday = birthday; } + @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; @@ -71,6 +72,7 @@ public boolean equals(Object o) { return true; } + @Override public int hashCode() { return (id != null ? id.hashCode() : 0); }