spring CacheResolverAdapterTests 源码
spring CacheResolverAdapterTests 代码
文件路径:/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/CacheResolverAdapterTests.java
/*
* Copyright 2002-2019 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
*
* https://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 java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Collection;
import javax.cache.annotation.CacheInvocationContext;
import javax.cache.annotation.CacheMethodDetails;
import javax.cache.annotation.CacheResolver;
import javax.cache.annotation.CacheResult;
import org.junit.jupiter.api.Test;
import org.springframework.cache.Cache;
import org.springframework.cache.jcache.AbstractJCacheTests;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
* @author Stephane Nicoll
*/
public class CacheResolverAdapterTests extends AbstractJCacheTests {
@Test
public void resolveSimpleCache() throws Exception {
DefaultCacheInvocationContext<?> dummyContext = createDummyContext();
CacheResolverAdapter adapter = new CacheResolverAdapter(getCacheResolver(dummyContext, "testCache"));
Collection<? extends Cache> caches = adapter.resolveCaches(dummyContext);
assertThat(caches).isNotNull();
assertThat(caches.size()).isEqualTo(1);
assertThat(caches.iterator().next().getName()).isEqualTo("testCache");
}
@Test
public void resolveUnknownCache() throws Exception {
DefaultCacheInvocationContext<?> dummyContext = createDummyContext();
CacheResolverAdapter adapter = new CacheResolverAdapter(getCacheResolver(dummyContext, null));
assertThatIllegalStateException().isThrownBy(() ->
adapter.resolveCaches(dummyContext));
}
@SuppressWarnings({ "rawtypes", "unchecked" })
protected CacheResolver getCacheResolver(CacheInvocationContext<? extends Annotation> context, String cacheName) {
CacheResolver cacheResolver = mock(CacheResolver.class);
javax.cache.Cache cache;
if (cacheName == null) {
cache = null;
}
else {
cache = mock(javax.cache.Cache.class);
given(cache.getName()).willReturn(cacheName);
}
given(cacheResolver.resolveCache(context)).willReturn(cache);
return cacheResolver;
}
protected DefaultCacheInvocationContext<?> createDummyContext() throws Exception {
Method method = Sample.class.getMethod("get", String.class);
CacheResult cacheAnnotation = method.getAnnotation(CacheResult.class);
CacheMethodDetails<CacheResult> methodDetails =
new DefaultCacheMethodDetails<>(method, cacheAnnotation, "test");
CacheResultOperation operation = new CacheResultOperation(methodDetails,
defaultCacheResolver, defaultKeyGenerator, defaultExceptionCacheResolver);
return new DefaultCacheInvocationContext<>(operation, new Sample(), new Object[] {"id"});
}
static class Sample {
@CacheResult
public Object get(String id) {
return null;
}
}
}
相关信息
相关文章
spring AbstractCacheOperationTests 源码
spring AnnotatedJCacheableService 源码
spring AnnotationCacheOperationSourceTests 源码
spring CachePutOperationTests 源码
spring CacheRemoveAllOperationTests 源码
spring CacheRemoveOperationTests 源码
spring CacheResultOperationTests 源码
spring JCacheErrorHandlerTests 源码
0
赞
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦