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 javax.cache.CacheManager;
20  import javax.cache.Caching;
21  import javax.cache.configuration.MutableConfiguration;
22  
23  import org.ehcache.jcache.JCacheConfiguration;
24  import org.junit.After;
25  import org.junit.Ignore;
26  import org.junit.Test;
27  
28  import org.springframework.cache.annotation.CachingConfigurerSupport;
29  import org.springframework.cache.annotation.EnableCaching;
30  import org.springframework.cache.config.AbstractAnnotationTests;
31  import org.springframework.cache.config.AnnotatedClassCacheableService;
32  import org.springframework.cache.config.CacheableService;
33  import org.springframework.cache.config.DefaultCacheableService;
34  import org.springframework.cache.config.SomeCustomKeyGenerator;
35  import org.springframework.cache.interceptor.KeyGenerator;
36  import org.springframework.cache.interceptor.SimpleKeyGenerator;
37  import org.springframework.context.ConfigurableApplicationContext;
38  import org.springframework.context.annotation.AnnotationConfigApplicationContext;
39  import org.springframework.context.annotation.Bean;
40  import org.springframework.context.annotation.Configuration;
41  
42  /**
43   * @author Stephane Nicoll
44   */
45  public class JCacheEhCacheTests extends AbstractAnnotationTests {
46  
47  	private CacheManager jCacheManager;
48  
49  
50  	@Override
51  	protected ConfigurableApplicationContext getApplicationContext() {
52  		ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(EnableCachingConfig.class);
53  		jCacheManager = context.getBean("jCacheManager", CacheManager.class);
54  		return context;
55  	}
56  
57  	@After
58  	public void shutdown() {
59  		jCacheManager.close();
60  	}
61  
62  
63  	@Override
64  	@Test
65  	@Ignore("Multi cache manager support to be added")
66  	public void testCustomCacheManager() {
67  	}
68  
69  
70  	@Configuration
71  	@EnableCaching
72  	static class EnableCachingConfig extends CachingConfigurerSupport {
73  
74  		@Override
75  		@Bean
76  		public org.springframework.cache.CacheManager cacheManager() {
77  			return new JCacheCacheManager(jCacheManager());
78  		}
79  
80  		@Bean
81  		public CacheManager jCacheManager() {
82  			CacheManager cacheManager = Caching.getCachingProvider().getCacheManager();
83  			MutableConfiguration<Object, Object> mutableConfiguration = new MutableConfiguration<Object, Object>();
84  			mutableConfiguration.setStoreByValue(false);  // otherwise value has to be Serializable
85  			cacheManager.createCache("testCache", new JCacheConfiguration<Object, Object>(mutableConfiguration));
86  			cacheManager.createCache("primary", new JCacheConfiguration<Object, Object>(mutableConfiguration));
87  			cacheManager.createCache("secondary", new JCacheConfiguration<Object, Object>(mutableConfiguration));
88  			return cacheManager;
89  		}
90  
91  		@Bean
92  		public CacheableService<?> service() {
93  			return new DefaultCacheableService();
94  		}
95  
96  		@Bean
97  		public CacheableService<?> classService() {
98  			return new AnnotatedClassCacheableService();
99  		}
100 
101 		@Override
102 		@Bean
103 		public KeyGenerator keyGenerator() {
104 			return new SimpleKeyGenerator();
105 		}
106 
107 		@Bean
108 		public KeyGenerator customKeyGenerator() {
109 			return new SomeCustomKeyGenerator();
110 		}
111 	}
112 
113 }