Skip to content

Commit bf1afdf

Browse files
committed
Add cache tests for JCache
1 parent 29303ef commit bf1afdf

File tree

3 files changed

+114
-4
lines changed

3 files changed

+114
-4
lines changed

spring-context-support/src/test/java/org/springframework/cache/AbstractCacheTests.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.cache;
1818

19+
import java.util.UUID;
20+
1921
import org.junit.Test;
2022

2123
import static org.junit.Assert.*;
@@ -45,7 +47,7 @@ public void testNativeCache() throws Exception {
4547
public void testCachePut() throws Exception {
4648
T cache = getCache();
4749

48-
Object key = "enescu";
50+
String key = createRandomKey();
4951
Object value = "george";
5052

5153
assertNull(cache.get(key));
@@ -69,7 +71,7 @@ public void testCachePut() throws Exception {
6971
public void testCachePutIfAbsent() throws Exception {
7072
T cache = getCache();
7173

72-
Object key = new Object();
74+
String key = createRandomKey();
7375
Object value = "initialValue";
7476

7577
assertNull(cache.get(key));
@@ -83,7 +85,7 @@ public void testCachePutIfAbsent() throws Exception {
8385
public void testCacheRemove() throws Exception {
8486
T cache = getCache();
8587

86-
Object key = "enescu";
88+
String key = createRandomKey();
8789
Object value = "george";
8890

8991
assertNull(cache.get(key));
@@ -102,4 +104,9 @@ public void testCacheClear() throws Exception {
102104
assertNull(cache.get("vlaicu"));
103105
assertNull(cache.get("enescu"));
104106
}
107+
108+
private String createRandomKey() {
109+
return UUID.randomUUID().toString();
110+
}
111+
105112
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2002-2015 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.Cache;
20+
import javax.cache.CacheManager;
21+
import javax.cache.Caching;
22+
import javax.cache.configuration.MutableConfiguration;
23+
import javax.cache.spi.CachingProvider;
24+
25+
import org.junit.After;
26+
import org.junit.Before;
27+
28+
import org.springframework.cache.AbstractCacheTests;
29+
30+
/**
31+
* @author Stephane Nicoll
32+
*/
33+
public class JCacheEhCacheTests extends AbstractCacheTests<JCacheCache> {
34+
35+
private CacheManager cacheManager;
36+
37+
private Cache<Object, Object> nativeCache;
38+
39+
private JCacheCache cache;
40+
41+
@Before
42+
public void setUp() {
43+
this.cacheManager = getCachingProvider().getCacheManager();
44+
this.cacheManager.createCache(CACHE_NAME, new MutableConfiguration<>());
45+
this.nativeCache = this.cacheManager.getCache(CACHE_NAME);
46+
this.cache = new JCacheCache(this.nativeCache);
47+
}
48+
49+
@After
50+
public void shutdownCacheManager() {
51+
this.cacheManager.close();
52+
}
53+
54+
@Override
55+
protected JCacheCache getCache() {
56+
return this.cache;
57+
}
58+
59+
@Override
60+
protected Object getNativeCache() {
61+
return this.nativeCache;
62+
}
63+
64+
protected CachingProvider getCachingProvider() {
65+
return Caching.getCachingProvider();
66+
}
67+
68+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2002-2015 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.Caching;
20+
import javax.cache.spi.CachingProvider;
21+
22+
/**
23+
* Just here to be run against EHCache 3, whereas the original JCacheEhCacheAnnotationTests
24+
* runs against EhCache 2.x with the EhCache-JCache add-on.
25+
*
26+
* @author Stephane Nicoll
27+
*/
28+
public class JCacheEhcache3Tests extends JCacheEhCacheTests {
29+
30+
@Override
31+
protected CachingProvider getCachingProvider() {
32+
return Caching.getCachingProvider("org.ehcache.jsr107.EhcacheCachingProvider");
33+
}
34+
35+
}

0 commit comments

Comments
 (0)