moved cache abstraction from context.support to context

This commit is contained in:
Costin Leau
2011-02-07 17:41:25 +00:00
parent fbb1fa33a1
commit 4da39b48f7
79 changed files with 195 additions and 56 deletions

View File

@@ -0,0 +1,39 @@
/*
* Copyright 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cache.concurrent;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.springframework.cache.Cache;
import org.springframework.cache.vendor.AbstractNativeCacheTest;
/**
* @author Costin Leau
*/
public class ConcurrentCacheTest extends AbstractNativeCacheTest<ConcurrentMap<Object, Object>> {
@Override
protected Cache createCache(ConcurrentMap<Object, Object> nativeCache) {
return new ConcurrentCache(nativeCache, CACHE_NAME);
}
@Override
protected ConcurrentMap<Object, Object> createNativeCache() throws Exception {
return new ConcurrentHashMap<Object, Object>();
}
}

View File

@@ -0,0 +1,183 @@
/*
* Copyright 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cache.config;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Abstract annotation test (containing several reusable methods).
*
* @author Costin Leau
*/
public abstract class AbstractAnnotationTest {
protected ApplicationContext ctx;
protected CacheableService cs;
protected CacheableService ccs;
protected CacheManager cm;
protected abstract String getConfig();
@Before
public void setup() {
ctx = new ClassPathXmlApplicationContext(getConfig());
cs = ctx.getBean("service", CacheableService.class);
ccs = ctx.getBean("classService", CacheableService.class);
cm = ctx.getBean(CacheManager.class);
}
public void testCacheable(CacheableService service) throws Exception {
Object o1 = new Object();
Object o2 = new Object();
Object r1 = service.cache(o1);
Object r2 = service.cache(o1);
Object r3 = service.cache(o1);
assertSame(r1, r2);
assertSame(r1, r3);
}
public void testInvalidate(CacheableService service) throws Exception {
Object o1 = new Object();
Object o2 = new Object();
Object r1 = service.cache(o1);
Object r2 = service.cache(o1);
assertSame(r1, r2);
service.invalidate(o1);
Object r3 = service.cache(o1);
Object r4 = service.cache(o1);
assertNotSame(r1, r3);
assertSame(r3, r4);
}
public void testConditionalExpression(CacheableService service)
throws Exception {
Object r1 = service.conditional(4);
Object r2 = service.conditional(4);
assertNotSame(r1, r2);
Object r3 = service.conditional(3);
Object r4 = service.conditional(3);
assertSame(r3, r4);
}
public void testKeyExpression(CacheableService service) throws Exception {
Object r1 = service.key(5, 1);
Object r2 = service.key(5, 2);
assertSame(r1, r2);
Object r3 = service.key(1, 5);
Object r4 = service.key(2, 5);
assertNotSame(r3, r4);
}
public void testNullValue(CacheableService service) throws Exception {
Object key = new Object();
assertNull(service.nullValue(key));
int nr = service.nullInvocations().intValue();
assertNull(service.nullValue(key));
assertEquals(nr, service.nullInvocations().intValue());
assertNull(service.nullValue(new Object()));
assertEquals(nr + 1, service.nullInvocations().intValue());
}
public void testMethodName(CacheableService service, String keyName)
throws Exception {
Object key = new Object();
Object r1 = service.name(key);
assertSame(r1, service.name(key));
Cache<Object, Object> cache = cm.getCache("default");
// assert the method name is used
assertTrue(cache.containsKey(keyName));
}
@Test
public void testCacheable() throws Exception {
testCacheable(cs);
}
@Test
public void testInvalidate() throws Exception {
testInvalidate(cs);
}
@Test
public void testConditionalExpression() throws Exception {
testConditionalExpression(cs);
}
@Test
public void testKeyExpression() throws Exception {
testKeyExpression(cs);
}
@Test
public void testClassCacheCacheable() throws Exception {
testCacheable(ccs);
}
@Test
public void testClassCacheInvalidate() throws Exception {
testInvalidate(ccs);
}
@Test
public void testNullValue() throws Exception {
testNullValue(cs);
}
@Test
public void testClassNullValue() throws Exception {
Object key = new Object();
assertNull(ccs.nullValue(key));
int nr = ccs.nullInvocations().intValue();
assertNull(ccs.nullValue(key));
assertEquals(nr, ccs.nullInvocations().intValue());
assertNull(ccs.nullValue(new Object()));
// the check method is also cached
assertEquals(nr, ccs.nullInvocations().intValue());
assertEquals(nr + 1, AnnotatedClassCacheableService.nullInvocations
.intValue());
}
@Test
public void testMethodName() throws Exception {
testMethodName(cs, "name");
}
@Test
public void testClassMethodName() throws Exception {
testMethodName(ccs, "namedefault");
}
}

View File

@@ -0,0 +1,63 @@
/*
* Copyright 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cache.config;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
/**
* @author Costin Leau
*/
@Cacheable("default")
public class AnnotatedClassCacheableService implements CacheableService {
private final AtomicLong counter = new AtomicLong();
public static final AtomicLong nullInvocations = new AtomicLong();
public Object cache(Object arg1) {
return counter.getAndIncrement();
}
public Object conditional(int field) {
return null;
}
@CacheEvict("default")
public void invalidate(Object arg1) {
}
@Cacheable(value = "default", key = "#p0")
public Object key(Object arg1, Object arg2) {
return counter.getAndIncrement();
}
@Cacheable(value = "default", key = "#root.methodName + #root.caches[0].name")
public Object name(Object arg1) {
return counter.getAndIncrement();
}
public Object nullValue(Object arg1) {
nullInvocations.incrementAndGet();
return null;
}
public Number nullInvocations() {
return nullInvocations.get();
}
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cache.config;
/**
* @author Costin Leau
*/
public class AnnotationNamespaceDrivenTest extends AbstractAnnotationTest {
@Override
protected String getConfig() {
return "/org/springframework/cache/config/annotationDrivenCacheNamespace.xml";
}
}

View File

@@ -0,0 +1,29 @@
/*
* Copyright 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cache.config;
/**
* @author Costin Leau
*/
public class AnnotationTest extends AbstractAnnotationTest {
@Override
protected String getConfig() {
return "/org/springframework/cache/config/annotationDrivenCacheConfig.xml";
}
}

View File

@@ -0,0 +1,41 @@
/*
* Copyright 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cache.config;
/**
* Basic service interface.
*
* @author Costin Leau
*/
public interface CacheableService<T> {
T cache(Object arg1);
void invalidate(Object arg1);
T conditional(int field);
T key(Object arg1, Object arg2);
T name(Object arg1);
T nullValue(Object arg1);
Number nullInvocations();
}

View File

@@ -0,0 +1,67 @@
/*
* Copyright 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cache.config;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
/**
* Simple cacheable service
*
* @author Costin Leau
*/
public class DefaultCacheableService implements CacheableService<Long> {
private final AtomicLong counter = new AtomicLong();
private final AtomicLong nullInvocations = new AtomicLong();
@Cacheable("default")
public Long cache(Object arg1) {
return counter.getAndIncrement();
}
@CacheEvict("default")
public void invalidate(Object arg1) {
}
@Cacheable(value = "default", condition = "#classField == 3")
public Long conditional(int classField) {
return counter.getAndIncrement();
}
@Cacheable(value = "default", key = "#p0")
public Long key(Object arg1, Object arg2) {
return counter.getAndIncrement();
}
@Cacheable(value = "default", key = "#root.methodName")
public Long name(Object arg1) {
return counter.getAndIncrement();
}
@Cacheable("default")
public Long nullValue(Object arg1) {
nullInvocations.incrementAndGet();
return null;
}
public Number nullInvocations() {
return nullInvocations.get();
}
}

View File

@@ -0,0 +1,44 @@
/*
* Copyright 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cache.ehcache;
import net.sf.ehcache.Ehcache;
import org.springframework.cache.Cache;
import org.springframework.cache.vendor.AbstractNativeCacheTest;
/**
* Integration test for EhCache cache.
*
* @author Costin Leau
*/
public class EhCacheCacheTest extends AbstractNativeCacheTest<Ehcache> {
@Override
protected Ehcache createNativeCache() throws Exception {
EhCacheFactoryBean fb = new EhCacheFactoryBean();
fb.setBeanName(CACHE_NAME);
fb.setCacheName(CACHE_NAME);
fb.afterPropertiesSet();
return fb.getObject();
}
@Override
protected Cache createCache(Ehcache nativeCache) {
return new EhCacheCache(nativeCache);
}
}

View File

@@ -0,0 +1,226 @@
/*
* Copyright 2002-2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cache.ehcache;
import junit.framework.TestCase;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.config.CacheConfiguration;
import net.sf.ehcache.constructs.blocking.BlockingCache;
import net.sf.ehcache.constructs.blocking.CacheEntryFactory;
import net.sf.ehcache.constructs.blocking.SelfPopulatingCache;
import net.sf.ehcache.constructs.blocking.UpdatingCacheEntryFactory;
import net.sf.ehcache.constructs.blocking.UpdatingSelfPopulatingCache;
import org.springframework.core.io.ClassPathResource;
/**
* @author Dmitriy Kopylenko
* @author Juergen Hoeller
* @since 27.09.2004
*/
public class EhCacheSupportTests extends TestCase {
public void testLoadingBlankCacheManager() throws Exception {
EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean();
assertEquals(CacheManager.class, cacheManagerFb.getObjectType());
assertTrue("Singleton property", cacheManagerFb.isSingleton());
cacheManagerFb.afterPropertiesSet();
try {
CacheManager cm = cacheManagerFb.getObject();
assertTrue("Loaded CacheManager with no caches", cm.getCacheNames().length == 0);
Cache myCache1 = cm.getCache("myCache1");
assertTrue("No myCache1 defined", myCache1 == null);
}
finally {
cacheManagerFb.destroy();
}
}
public void testLoadingCacheManagerFromConfigFile() throws Exception {
EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean();
cacheManagerFb.setConfigLocation(new ClassPathResource("testEhcache.xml", getClass()));
cacheManagerFb.setCacheManagerName("myCacheManager");
cacheManagerFb.afterPropertiesSet();
try {
CacheManager cm = cacheManagerFb.getObject();
assertTrue("Correct number of caches loaded", cm.getCacheNames().length == 1);
Cache myCache1 = cm.getCache("myCache1");
assertFalse("myCache1 is not eternal", myCache1.getCacheConfiguration().isEternal());
assertTrue("myCache1.maxElements == 300", myCache1.getCacheConfiguration().getMaxElementsInMemory() == 300);
}
finally {
cacheManagerFb.destroy();
}
}
public void testEhCacheFactoryBeanWithDefaultCacheManager() throws Exception {
doTestEhCacheFactoryBean(false);
}
public void testEhCacheFactoryBeanWithExplicitCacheManager() throws Exception {
doTestEhCacheFactoryBean(true);
}
private void doTestEhCacheFactoryBean(boolean useCacheManagerFb) throws Exception {
Cache cache = null;
EhCacheManagerFactoryBean cacheManagerFb = null;
try {
EhCacheFactoryBean cacheFb = new EhCacheFactoryBean();
assertEquals(Ehcache.class, cacheFb.getObjectType());
assertTrue("Singleton property", cacheFb.isSingleton());
if (useCacheManagerFb) {
cacheManagerFb = new EhCacheManagerFactoryBean();
cacheManagerFb.setConfigLocation(new ClassPathResource("testEhcache.xml", getClass()));
cacheManagerFb.afterPropertiesSet();
cacheFb.setCacheManager(cacheManagerFb.getObject());
}
cacheFb.setCacheName("myCache1");
cacheFb.afterPropertiesSet();
cache = (Cache) cacheFb.getObject();
CacheConfiguration config = cache.getCacheConfiguration();
assertEquals("myCache1", cache.getName());
if (useCacheManagerFb){
assertEquals("myCache1.maxElements", 300, config.getMaxElementsInMemory());
}
else {
assertEquals("myCache1.maxElements", 10000, config.getMaxElementsInMemory());
}
// Cache region is not defined. Should create one with default properties.
cacheFb = new EhCacheFactoryBean();
if (useCacheManagerFb) {
cacheFb.setCacheManager(cacheManagerFb.getObject());
}
cacheFb.setCacheName("undefinedCache");
cacheFb.afterPropertiesSet();
cache = (Cache) cacheFb.getObject();
config = cache.getCacheConfiguration();
assertEquals("undefinedCache", cache.getName());
assertTrue("default maxElements is correct", config.getMaxElementsInMemory() == 10000);
assertTrue("default overflowToDisk is correct", config.isOverflowToDisk());
assertFalse("default eternal is correct", config.isEternal());
assertTrue("default timeToLive is correct", config.getTimeToLiveSeconds() == 120);
assertTrue("default timeToIdle is correct", config.getTimeToIdleSeconds() == 120);
assertTrue("default diskPersistent is correct", !config.isDiskPersistent());
assertTrue("default diskExpiryThreadIntervalSeconds is correct", config.getDiskExpiryThreadIntervalSeconds() == 120);
// overriding the default properties
cacheFb = new EhCacheFactoryBean();
if (useCacheManagerFb) {
cacheFb.setCacheManager(cacheManagerFb.getObject());
}
cacheFb.setBeanName("undefinedCache2");
cacheFb.setMaxElementsInMemory(5);
cacheFb.setOverflowToDisk(false);
cacheFb.setEternal(true);
cacheFb.setTimeToLive(8);
cacheFb.setTimeToIdle(7);
cacheFb.setDiskPersistent(true);
cacheFb.setDiskExpiryThreadIntervalSeconds(10);
cacheFb.afterPropertiesSet();
cache = (Cache) cacheFb.getObject();
config = cache.getCacheConfiguration();
assertEquals("undefinedCache2", cache.getName());
assertTrue("overridden maxElements is correct", config.getMaxElementsInMemory() == 5);
assertFalse("overridden overflowToDisk is correct", config.isOverflowToDisk());
assertTrue("overridden eternal is correct", config.isEternal());
assertTrue("default timeToLive is correct", config.getTimeToLiveSeconds() == 8);
assertTrue("default timeToIdle is correct", config.getTimeToIdleSeconds() == 7);
assertTrue("overridden diskPersistent is correct", config.isDiskPersistent());
assertTrue("overridden diskExpiryThreadIntervalSeconds is correct", config.getDiskExpiryThreadIntervalSeconds() == 10);
}
finally {
if (useCacheManagerFb) {
cacheManagerFb.destroy();
}
else {
CacheManager.getInstance().shutdown();
}
}
}
public void testEhCacheFactoryBeanWithBlockingCache() throws Exception {
EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean();
cacheManagerFb.afterPropertiesSet();
try {
CacheManager cm = cacheManagerFb.getObject();
EhCacheFactoryBean cacheFb = new EhCacheFactoryBean();
cacheFb.setCacheManager(cm);
cacheFb.setCacheName("myCache1");
cacheFb.setBlocking(true);
cacheFb.afterPropertiesSet();
Ehcache myCache1 = cm.getEhcache("myCache1");
assertTrue(myCache1 instanceof BlockingCache);
}
finally {
cacheManagerFb.destroy();
}
}
public void testEhCacheFactoryBeanWithSelfPopulatingCache() throws Exception {
EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean();
cacheManagerFb.afterPropertiesSet();
try {
CacheManager cm = cacheManagerFb.getObject();
EhCacheFactoryBean cacheFb = new EhCacheFactoryBean();
cacheFb.setCacheManager(cm);
cacheFb.setCacheName("myCache1");
cacheFb.setCacheEntryFactory(new CacheEntryFactory() {
public Object createEntry(Object key) throws Exception {
return key;
}
});
cacheFb.afterPropertiesSet();
Ehcache myCache1 = cm.getEhcache("myCache1");
assertTrue(myCache1 instanceof SelfPopulatingCache);
assertEquals("myKey1", myCache1.get("myKey1").getValue());
}
finally {
cacheManagerFb.destroy();
}
}
public void testEhCacheFactoryBeanWithUpdatingSelfPopulatingCache() throws Exception {
EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean();
cacheManagerFb.afterPropertiesSet();
try {
CacheManager cm = cacheManagerFb.getObject();
EhCacheFactoryBean cacheFb = new EhCacheFactoryBean();
cacheFb.setCacheManager(cm);
cacheFb.setCacheName("myCache1");
cacheFb.setCacheEntryFactory(new UpdatingCacheEntryFactory() {
public Object createEntry(Object key) throws Exception {
return key;
}
public void updateEntryValue(Object key, Object value) throws Exception {
}
});
cacheFb.afterPropertiesSet();
Ehcache myCache1 = cm.getEhcache("myCache1");
assertTrue(myCache1 instanceof UpdatingSelfPopulatingCache);
assertEquals("myKey1", myCache1.get("myKey1").getValue());
}
finally {
cacheManagerFb.destroy();
}
}
}

View File

@@ -0,0 +1,155 @@
/*
* Copyright 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cache.vendor;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import org.junit.Before;
import org.junit.Test;
import org.springframework.cache.Cache;
/**
* Test for native cache implementations.
*
* @author Costin Leau
*/
public abstract class AbstractNativeCacheTest<T> {
private T nativeCache;
private Cache cache;
protected final static String CACHE_NAME = "testCache";
@Before
public void setUp() throws Exception {
nativeCache = createNativeCache();
cache = createCache(nativeCache);
cache.clear();
}
protected abstract T createNativeCache() throws Exception;
protected abstract Cache createCache(T nativeCache);
@Test
public void testCacheName() throws Exception {
assertEquals(CACHE_NAME, cache.getName());
}
@Test
public void testNativeCache() throws Exception {
assertSame(nativeCache, cache.getNativeCache());
}
@Test
public void testCachePut() throws Exception {
Object key = "enescu";
Object value = "george";
assertNull(cache.get(key));
cache.put(key, value);
assertEquals(value, cache.get(key));
}
@Test
public void testCacheRemove() throws Exception {
Object key = "enescu";
Object value = "george";
assertNull(cache.get(key));
cache.put(key, value);
assertEquals(value, cache.remove(key));
assertNull(cache.get(key));
}
@Test
public void testCacheClear() throws Exception {
assertNull(cache.get("enescu"));
cache.put("enescu", "george");
assertNull(cache.get("vlaicu"));
cache.put("vlaicu", "aurel");
cache.clear();
assertNull(cache.get("vlaicu"));
assertNull(cache.get("enescu"));
}
// concurrent map tests
@Test
public void testPutIfAbsent() throws Exception {
Object key = "enescu";
Object value1 = "george";
Object value2 = "geo";
assertNull(cache.get("enescu"));
cache.put(key, value1);
cache.putIfAbsent(key, value2);
assertEquals(value1, cache.get(key));
}
@Test
public void testConcurrentRemove() throws Exception {
Object key = "enescu";
Object value1 = "george";
Object value2 = "geo";
assertNull(cache.get("enescu"));
cache.put(key, value1);
// no remove
cache.remove(key, value2);
assertEquals(value1, cache.get(key));
// one remove
cache.remove(key, value1);
assertNull(cache.get("enescu"));
}
@Test
public void testConcurrentReplace() throws Exception {
Object key = "enescu";
Object value1 = "george";
Object value2 = "geo";
assertNull(cache.get("enescu"));
cache.put(key, value1);
cache.replace(key, value2);
assertEquals(value2, cache.get(key));
cache.remove(key);
cache.replace(key, value1);
assertNull(cache.get("enescu"));
}
@Test
public void testConcurrentReplaceIfEqual() throws Exception {
Object key = "enescu";
Object value1 = "george";
Object value2 = "geo";
assertNull(cache.get("enescu"));
cache.put(key, value1);
assertEquals(value1, cache.get(key));
// no replace
cache.replace(key, value2, value1);
assertEquals(value1, cache.get(key));
cache.replace(key, value1, value2);
assertEquals(value2, cache.get(key));
cache.replace(key, value2, value1);
assertEquals(value1, cache.get(key));
}
}