View Javadoc
1   /*
2    * Copyright 2002-2014 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.cache.jcache;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import org.junit.Rule;
23  import org.junit.rules.ExpectedException;
24  import org.junit.rules.TestName;
25  
26  import org.springframework.cache.Cache;
27  import org.springframework.cache.CacheManager;
28  import org.springframework.cache.concurrent.ConcurrentMapCache;
29  import org.springframework.cache.interceptor.CacheResolver;
30  import org.springframework.cache.interceptor.KeyGenerator;
31  import org.springframework.cache.interceptor.SimpleCacheResolver;
32  import org.springframework.cache.interceptor.SimpleKeyGenerator;
33  import org.springframework.cache.jcache.interceptor.SimpleExceptionCacheResolver;
34  import org.springframework.cache.support.SimpleCacheManager;
35  
36  /**
37   * @author Stephane Nicoll
38   */
39  public abstract class AbstractJCacheTests {
40  
41  	@Rule
42  	public final ExpectedException thrown = ExpectedException.none();
43  
44  	@Rule
45  	public final TestName name = new TestName();
46  
47  	protected final CacheManager cacheManager = createSimpleCacheManager("default", "simpleCache");
48  
49  	protected final CacheResolver defaultCacheResolver = new SimpleCacheResolver(cacheManager);
50  
51  	protected final CacheResolver defaultExceptionCacheResolver = new SimpleExceptionCacheResolver(cacheManager);
52  
53  	protected final KeyGenerator defaultKeyGenerator = new SimpleKeyGenerator();
54  
55  	protected static CacheManager createSimpleCacheManager(String... cacheNames) {
56  		SimpleCacheManager result = new SimpleCacheManager();
57  		List<Cache> caches = new ArrayList<Cache>();
58  		for (String cacheName : cacheNames) {
59  			caches.add(new ConcurrentMapCache(cacheName));
60  		}
61  		result.setCaches(caches);
62  		result.afterPropertiesSet();
63  		return result;
64  	}
65  
66  }