polishing
This commit is contained in:
@@ -28,7 +28,7 @@ import org.junit.rules.TestName;
|
||||
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cache.jcache.interceptor.SimpleGeneratedCacheKey;
|
||||
import org.springframework.cache.interceptor.SimpleKeyGenerator;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
/**
|
||||
@@ -523,7 +523,7 @@ public abstract class AbstractJCacheAnnotationTests {
|
||||
|
||||
|
||||
private Object createKey(Object... params) {
|
||||
return new SimpleGeneratedCacheKey(params);
|
||||
return SimpleKeyGenerator.generateKey(params);
|
||||
}
|
||||
|
||||
private Cache getCache(String name) {
|
||||
|
||||
@@ -61,11 +61,11 @@ public class JCacheJavaConfigTests extends AbstractJCacheAnnotationTests {
|
||||
AnnotationConfigApplicationContext context =
|
||||
new AnnotationConfigApplicationContext(FullCachingConfig.class);
|
||||
DefaultJCacheOperationSource cos = context.getBean(DefaultJCacheOperationSource.class);
|
||||
assertSame(context.getBean(KeyGenerator.class), cos.getDefaultKeyGenerator());
|
||||
assertSame(context.getBean(KeyGenerator.class), cos.getKeyGenerator());
|
||||
assertSame(context.getBean("cacheResolver", CacheResolver.class),
|
||||
cos.getDefaultCacheResolver());
|
||||
cos.getCacheResolver());
|
||||
assertSame(context.getBean("exceptionCacheResolver", CacheResolver.class),
|
||||
cos.getDefaultExceptionCacheResolver());
|
||||
cos.getExceptionCacheResolver());
|
||||
JCacheInterceptor interceptor = context.getBean(JCacheInterceptor.class);
|
||||
assertSame(context.getBean("errorHandler", CacheErrorHandler.class), interceptor.getErrorHandler());
|
||||
}
|
||||
@@ -76,14 +76,14 @@ public class JCacheJavaConfigTests extends AbstractJCacheAnnotationTests {
|
||||
new AnnotationConfigApplicationContext(EmptyConfigSupportConfig.class);
|
||||
|
||||
DefaultJCacheOperationSource cos = context.getBean(DefaultJCacheOperationSource.class);
|
||||
assertNotNull(cos.getDefaultCacheResolver());
|
||||
assertEquals(SimpleCacheResolver.class, cos.getDefaultCacheResolver().getClass());
|
||||
assertNotNull(cos.getCacheResolver());
|
||||
assertEquals(SimpleCacheResolver.class, cos.getCacheResolver().getClass());
|
||||
assertSame(context.getBean(CacheManager.class),
|
||||
((SimpleCacheResolver) cos.getDefaultCacheResolver()).getCacheManager());
|
||||
assertNotNull(cos.getDefaultExceptionCacheResolver());
|
||||
assertEquals(SimpleExceptionCacheResolver.class, cos.getDefaultExceptionCacheResolver().getClass());
|
||||
((SimpleCacheResolver) cos.getCacheResolver()).getCacheManager());
|
||||
assertNotNull(cos.getExceptionCacheResolver());
|
||||
assertEquals(SimpleExceptionCacheResolver.class, cos.getExceptionCacheResolver().getClass());
|
||||
assertSame(context.getBean(CacheManager.class),
|
||||
((SimpleExceptionCacheResolver) cos.getDefaultExceptionCacheResolver()).getCacheManager());
|
||||
((SimpleExceptionCacheResolver) cos.getExceptionCacheResolver()).getCacheManager());
|
||||
context.close();
|
||||
}
|
||||
|
||||
@@ -93,9 +93,9 @@ public class JCacheJavaConfigTests extends AbstractJCacheAnnotationTests {
|
||||
new AnnotationConfigApplicationContext(FullCachingConfigSupport.class);
|
||||
|
||||
DefaultJCacheOperationSource cos = context.getBean(DefaultJCacheOperationSource.class);
|
||||
assertSame(context.getBean("cacheResolver"), cos.getDefaultCacheResolver());
|
||||
assertSame(context.getBean("keyGenerator"), cos.getDefaultKeyGenerator());
|
||||
assertSame(context.getBean("exceptionCacheResolver"), cos.getDefaultExceptionCacheResolver());
|
||||
assertSame(context.getBean("cacheResolver"), cos.getCacheResolver());
|
||||
assertSame(context.getBean("keyGenerator"), cos.getKeyGenerator());
|
||||
assertSame(context.getBean("exceptionCacheResolver"), cos.getExceptionCacheResolver());
|
||||
context.close();
|
||||
}
|
||||
|
||||
|
||||
@@ -44,7 +44,7 @@ public class JCacheNamespaceDrivenTests extends AbstractJCacheAnnotationTests {
|
||||
"/org/springframework/cache/jcache/config/jCacheNamespaceDriven-resolver.xml");
|
||||
|
||||
DefaultJCacheOperationSource ci = context.getBean(DefaultJCacheOperationSource.class);
|
||||
assertSame(context.getBean("cacheResolver"), ci.getDefaultCacheResolver());
|
||||
assertSame(context.getBean("cacheResolver"), ci.getCacheResolver());
|
||||
context.close();
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cache.jcache.model;
|
||||
package org.springframework.cache.jcache.interceptor;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@@ -29,6 +29,7 @@ import javax.cache.annotation.CacheResult;
|
||||
import javax.cache.annotation.CacheValue;
|
||||
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.interceptor.SimpleKeyGenerator;
|
||||
import org.springframework.cache.jcache.config.JCacheableService;
|
||||
import org.springframework.cache.jcache.support.TestableCacheKeyGenerator;
|
||||
import org.springframework.cache.jcache.support.TestableCacheResolverFactory;
|
||||
@@ -109,7 +110,7 @@ public class AnnotatedJCacheableService implements JCacheableService<Long> {
|
||||
@Override
|
||||
@CachePut(afterInvocation = false)
|
||||
public void earlyPut(String id, @CacheValue Object value) {
|
||||
SimpleGeneratedCacheKey key = new SimpleGeneratedCacheKey(id);
|
||||
Object key = SimpleKeyGenerator.generateKey(id);
|
||||
Cache.ValueWrapper valueWrapper = defaultCache.get(key);
|
||||
if (valueWrapper == null) {
|
||||
throw new AssertionError("Excepted value to be put in cache with key " + key);
|
||||
@@ -141,7 +142,7 @@ public class AnnotatedJCacheableService implements JCacheableService<Long> {
|
||||
@Override
|
||||
@CacheRemove(afterInvocation = false)
|
||||
public void earlyRemove(String id) {
|
||||
SimpleGeneratedCacheKey key = new SimpleGeneratedCacheKey(id);
|
||||
Object key = SimpleKeyGenerator.generateKey(id);
|
||||
Cache.ValueWrapper valueWrapper = defaultCache.get(key);
|
||||
if (valueWrapper != null) {
|
||||
throw new AssertionError("Value with key " + key + " expected to be already remove from cache");
|
||||
|
||||
@@ -34,12 +34,6 @@ import org.junit.Test;
|
||||
import org.springframework.cache.interceptor.CacheResolver;
|
||||
import org.springframework.cache.interceptor.KeyGenerator;
|
||||
import org.springframework.cache.jcache.AbstractJCacheTests;
|
||||
import org.springframework.cache.jcache.model.BaseKeyCacheOperation;
|
||||
import org.springframework.cache.jcache.model.CachePutOperation;
|
||||
import org.springframework.cache.jcache.model.CacheRemoveAllOperation;
|
||||
import org.springframework.cache.jcache.model.CacheRemoveOperation;
|
||||
import org.springframework.cache.jcache.model.CacheResultOperation;
|
||||
import org.springframework.cache.jcache.model.JCacheOperation;
|
||||
import org.springframework.cache.jcache.support.TestableCacheKeyGenerator;
|
||||
import org.springframework.cache.jcache.support.TestableCacheResolver;
|
||||
import org.springframework.cache.jcache.support.TestableCacheResolverFactory;
|
||||
@@ -142,7 +136,8 @@ public class AnnotationCacheOperationSourceTests extends AbstractJCacheTests {
|
||||
getCacheOperation(CacheResultOperation.class, CustomService.class, name.getMethodName(), Long.class);
|
||||
assertJCacheResolver(operation.getCacheResolver(), TestableCacheResolver.class);
|
||||
assertJCacheResolver(operation.getExceptionCacheResolver(), null);
|
||||
assertEquals(defaultKeyGenerator, operation.getKeyGenerator());
|
||||
assertEquals(KeyGeneratorAdapter.class, operation.getKeyGenerator().getClass());
|
||||
assertEquals(defaultKeyGenerator, ((KeyGeneratorAdapter) operation.getKeyGenerator()).getTarget());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -184,9 +179,10 @@ public class AnnotationCacheOperationSourceTests extends AbstractJCacheTests {
|
||||
assertCacheKeyGenerator(operation.getKeyGenerator(), TestableCacheKeyGenerator.class);
|
||||
}
|
||||
|
||||
private void assertDefaults(BaseKeyCacheOperation<?> operation) {
|
||||
private void assertDefaults(AbstractJCacheKeyOperation<?> operation) {
|
||||
assertEquals(defaultCacheResolver, operation.getCacheResolver());
|
||||
assertEquals(defaultKeyGenerator, operation.getKeyGenerator());
|
||||
assertEquals(KeyGeneratorAdapter.class, operation.getKeyGenerator().getClass());
|
||||
assertEquals(defaultKeyGenerator, ((KeyGeneratorAdapter) operation.getKeyGenerator()).getTarget());
|
||||
}
|
||||
|
||||
protected <T extends JCacheOperation<?>> T getDefaultCacheOperation(Class<T> operationType, Class<?>... parameterTypes) {
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cache.jcache.model;
|
||||
package org.springframework.cache.jcache.interceptor;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cache.jcache.model;
|
||||
package org.springframework.cache.jcache.interceptor;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cache.jcache.model;
|
||||
package org.springframework.cache.jcache.interceptor;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@@ -34,8 +34,6 @@ import org.junit.rules.ExpectedException;
|
||||
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.jcache.AbstractJCacheTests;
|
||||
import org.springframework.cache.jcache.model.CacheResultOperation;
|
||||
import org.springframework.cache.jcache.model.DefaultCacheMethodDetails;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cache.jcache.model;
|
||||
package org.springframework.cache.jcache.interceptor;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@@ -40,6 +40,7 @@ import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.cache.interceptor.CacheErrorHandler;
|
||||
import org.springframework.cache.interceptor.SimpleKeyGenerator;
|
||||
import org.springframework.cache.jcache.config.JCacheConfigurerSupport;
|
||||
import org.springframework.cache.support.SimpleCacheManager;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
@@ -72,7 +73,7 @@ public class JCacheErrorHandlerTests {
|
||||
@Test
|
||||
public void getFail() {
|
||||
UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on get");
|
||||
SimpleGeneratedCacheKey key = new SimpleGeneratedCacheKey(0L);
|
||||
Object key = SimpleKeyGenerator.generateKey(0L);
|
||||
doThrow(exception).when(cache).get(key);
|
||||
|
||||
this.simpleService.get(0L);
|
||||
@@ -82,7 +83,7 @@ public class JCacheErrorHandlerTests {
|
||||
@Test
|
||||
public void putFail() {
|
||||
UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on put");
|
||||
SimpleGeneratedCacheKey key = new SimpleGeneratedCacheKey(0L);
|
||||
Object key = SimpleKeyGenerator.generateKey(0L);
|
||||
doThrow(exception).when(cache).put(key, 234L);
|
||||
|
||||
this.simpleService.put(0L, 234L);
|
||||
@@ -92,7 +93,7 @@ public class JCacheErrorHandlerTests {
|
||||
@Test
|
||||
public void evictFail() {
|
||||
UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on evict");
|
||||
SimpleGeneratedCacheKey key = new SimpleGeneratedCacheKey(0L);
|
||||
Object key = SimpleKeyGenerator.generateKey(0L);
|
||||
doThrow(exception).when(cache).evict(key);
|
||||
|
||||
this.simpleService.evict(0L);
|
||||
|
||||
@@ -0,0 +1,159 @@
|
||||
/*
|
||||
* Copyright 2002-2014 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.jcache.interceptor;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import javax.cache.annotation.CacheDefaults;
|
||||
import javax.cache.annotation.CacheKey;
|
||||
import javax.cache.annotation.CacheResult;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
|
||||
import org.springframework.cache.interceptor.KeyGenerator;
|
||||
import org.springframework.cache.interceptor.SimpleKey;
|
||||
import org.springframework.cache.interceptor.SimpleKeyGenerator;
|
||||
import org.springframework.cache.jcache.config.JCacheConfigurerSupport;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class JCacheKeyGeneratorTests {
|
||||
|
||||
private TestKeyGenerator keyGenerator;
|
||||
|
||||
private SimpleService simpleService;
|
||||
|
||||
private Cache cache;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
|
||||
this.keyGenerator = context.getBean(TestKeyGenerator.class);
|
||||
this.simpleService = context.getBean(SimpleService.class);
|
||||
this.cache = context.getBean(CacheManager.class).getCache("test");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getSimple() {
|
||||
this.keyGenerator.expect(1L);
|
||||
Object first = this.simpleService.get(1L);
|
||||
Object second = this.simpleService.get(1L);
|
||||
assertSame(first, second);
|
||||
|
||||
Object key = new SimpleKey(1L);
|
||||
assertEquals(first, cache.get(key).get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getFlattenVararg() {
|
||||
this.keyGenerator.expect(1L, "foo", "bar");
|
||||
Object first = this.simpleService.get(1L, "foo", "bar");
|
||||
Object second = this.simpleService.get(1L, "foo", "bar");
|
||||
assertSame(first, second);
|
||||
|
||||
Object key = new SimpleKey(1L, "foo", "bar");
|
||||
assertEquals(first, cache.get(key).get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getFiltered() {
|
||||
this.keyGenerator.expect(1L);
|
||||
Object first = this.simpleService.getFiltered(1L, "foo", "bar");
|
||||
Object second = this.simpleService.getFiltered(1L, "foo", "bar");
|
||||
assertSame(first, second);
|
||||
|
||||
Object key = new SimpleKey(1L);
|
||||
assertEquals(first, cache.get(key).get());
|
||||
}
|
||||
|
||||
|
||||
@Configuration
|
||||
@EnableCaching
|
||||
static class Config extends JCacheConfigurerSupport {
|
||||
|
||||
@Bean
|
||||
@Override
|
||||
public CacheManager cacheManager() {
|
||||
return new ConcurrentMapCacheManager();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Override
|
||||
public KeyGenerator keyGenerator() {
|
||||
return new TestKeyGenerator();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SimpleService simpleService() {
|
||||
return new SimpleService();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@CacheDefaults(cacheName = "test")
|
||||
public static class SimpleService {
|
||||
private AtomicLong counter = new AtomicLong();
|
||||
|
||||
@CacheResult
|
||||
public Object get(long id) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@CacheResult
|
||||
public Object get(long id, String... items) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@CacheResult
|
||||
public Object getFiltered(@CacheKey long id, String... items) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private static class TestKeyGenerator extends SimpleKeyGenerator {
|
||||
|
||||
private Object[] expectedParams;
|
||||
|
||||
private void expect(Object... params) {
|
||||
this.expectedParams = params;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object generate(Object target, Method method, Object... params) {
|
||||
assertTrue("Unexpected parameters: expected: "
|
||||
+ Arrays.toString(this.expectedParams) + " but got: " + Arrays.toString(params),
|
||||
Arrays.equals(expectedParams, params));
|
||||
return new SimpleKey(params);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.springframework.cache.jcache.model;
|
||||
package org.springframework.cache.jcache.interceptor;
|
||||
|
||||
import javax.cache.annotation.CacheKey;
|
||||
import javax.cache.annotation.CachePut;
|
||||
@@ -6,7 +6,7 @@ import javax.cache.annotation.CacheKeyGenerator;
|
||||
import javax.cache.annotation.CacheKeyInvocationContext;
|
||||
import javax.cache.annotation.GeneratedCacheKey;
|
||||
|
||||
import org.springframework.cache.jcache.interceptor.SimpleGeneratedCacheKey;
|
||||
import org.springframework.cache.interceptor.SimpleKey;
|
||||
|
||||
/**
|
||||
* A simple test key generator that only takes the first key arguments into
|
||||
@@ -22,4 +22,13 @@ public class TestableCacheKeyGenerator implements CacheKeyGenerator {
|
||||
return new SimpleGeneratedCacheKey(context.getKeyParameters()[0]);
|
||||
}
|
||||
|
||||
|
||||
private static class SimpleGeneratedCacheKey extends SimpleKey implements GeneratedCacheKey {
|
||||
|
||||
public SimpleGeneratedCacheKey(Object... elements) {
|
||||
super(elements);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user