From bf1afdfdc9d4c65391a1dcd198d6b9700a6b0593 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 24 Nov 2015 10:15:09 +0100 Subject: [PATCH] Add cache tests for JCache --- .../cache/AbstractCacheTests.java | 15 ++-- .../cache/jcache/JCacheEhCacheTests.java | 68 +++++++++++++++++++ .../cache/jcache/JCacheEhcache3Tests.java | 35 ++++++++++ 3 files changed, 114 insertions(+), 4 deletions(-) create mode 100644 spring-context-support/src/test/java/org/springframework/cache/jcache/JCacheEhCacheTests.java create mode 100644 spring-test/src/test/java/org/springframework/cache/jcache/JCacheEhcache3Tests.java diff --git a/spring-context-support/src/test/java/org/springframework/cache/AbstractCacheTests.java b/spring-context-support/src/test/java/org/springframework/cache/AbstractCacheTests.java index d2430cd2a9..cf550a9bd8 100644 --- a/spring-context-support/src/test/java/org/springframework/cache/AbstractCacheTests.java +++ b/spring-context-support/src/test/java/org/springframework/cache/AbstractCacheTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 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. @@ -16,6 +16,8 @@ package org.springframework.cache; +import java.util.UUID; + import org.junit.Test; import static org.junit.Assert.*; @@ -45,7 +47,7 @@ public abstract class AbstractCacheTests { public void testCachePut() throws Exception { T cache = getCache(); - Object key = "enescu"; + String key = createRandomKey(); Object value = "george"; assertNull(cache.get(key)); @@ -69,7 +71,7 @@ public abstract class AbstractCacheTests { public void testCachePutIfAbsent() throws Exception { T cache = getCache(); - Object key = new Object(); + String key = createRandomKey(); Object value = "initialValue"; assertNull(cache.get(key)); @@ -83,7 +85,7 @@ public abstract class AbstractCacheTests { public void testCacheRemove() throws Exception { T cache = getCache(); - Object key = "enescu"; + String key = createRandomKey(); Object value = "george"; assertNull(cache.get(key)); @@ -102,4 +104,9 @@ public abstract class AbstractCacheTests { assertNull(cache.get("vlaicu")); assertNull(cache.get("enescu")); } + + private String createRandomKey() { + return UUID.randomUUID().toString(); + } + } diff --git a/spring-context-support/src/test/java/org/springframework/cache/jcache/JCacheEhCacheTests.java b/spring-context-support/src/test/java/org/springframework/cache/jcache/JCacheEhCacheTests.java new file mode 100644 index 0000000000..d5ee79c9cf --- /dev/null +++ b/spring-context-support/src/test/java/org/springframework/cache/jcache/JCacheEhCacheTests.java @@ -0,0 +1,68 @@ +/* + * Copyright 2002-2015 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; + +import javax.cache.Cache; +import javax.cache.CacheManager; +import javax.cache.Caching; +import javax.cache.configuration.MutableConfiguration; +import javax.cache.spi.CachingProvider; + +import org.junit.After; +import org.junit.Before; + +import org.springframework.cache.AbstractCacheTests; + +/** + * @author Stephane Nicoll + */ +public class JCacheEhCacheTests extends AbstractCacheTests { + + private CacheManager cacheManager; + + private Cache nativeCache; + + private JCacheCache cache; + + @Before + public void setUp() { + this.cacheManager = getCachingProvider().getCacheManager(); + this.cacheManager.createCache(CACHE_NAME, new MutableConfiguration<>()); + this.nativeCache = this.cacheManager.getCache(CACHE_NAME); + this.cache = new JCacheCache(this.nativeCache); + } + + @After + public void shutdownCacheManager() { + this.cacheManager.close(); + } + + @Override + protected JCacheCache getCache() { + return this.cache; + } + + @Override + protected Object getNativeCache() { + return this.nativeCache; + } + + protected CachingProvider getCachingProvider() { + return Caching.getCachingProvider(); + } + +} diff --git a/spring-test/src/test/java/org/springframework/cache/jcache/JCacheEhcache3Tests.java b/spring-test/src/test/java/org/springframework/cache/jcache/JCacheEhcache3Tests.java new file mode 100644 index 0000000000..df7d587e31 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/cache/jcache/JCacheEhcache3Tests.java @@ -0,0 +1,35 @@ +/* + * Copyright 2002-2015 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; + +import javax.cache.Caching; +import javax.cache.spi.CachingProvider; + +/** + * Just here to be run against EHCache 3, whereas the original JCacheEhCacheAnnotationTests + * runs against EhCache 2.x with the EhCache-JCache add-on. + * + * @author Stephane Nicoll + */ +public class JCacheEhcache3Tests extends JCacheEhCacheTests { + + @Override + protected CachingProvider getCachingProvider() { + return Caching.getCachingProvider("org.ehcache.jsr107.EhcacheCachingProvider"); + } + +}