From d47f010dc7bc9e5583f28a5571a57788cca88310 Mon Sep 17 00:00:00 2001 From: mrniko <1104661+mrniko@users.noreply.github.com> Date: Tue, 11 Feb 2025 14:18:10 +0300 Subject: [PATCH] tests added --- .../java/org/redisson/jcache/JCacheTest.java | 118 ++++++++++++------ 1 file changed, 80 insertions(+), 38 deletions(-) diff --git a/redisson/src/test/java/org/redisson/jcache/JCacheTest.java b/redisson/src/test/java/org/redisson/jcache/JCacheTest.java index a92f162b4..69f09f665 100644 --- a/redisson/src/test/java/org/redisson/jcache/JCacheTest.java +++ b/redisson/src/test/java/org/redisson/jcache/JCacheTest.java @@ -143,10 +143,7 @@ public class JCacheTest extends RedisDockerTest { @Test public void testAsync() throws Exception { - Configuration c = createJCacheConfig(); - Configuration config = RedissonConfiguration.fromInstance(redisson, c); - Cache cache = Caching.getCachingProvider().getCacheManager() - .createCache("test", config); + Cache cache = createCache(); CacheAsync async = cache.unwrap(CacheAsync.class); async.putAsync("1", "2").get(); @@ -157,10 +154,7 @@ public class JCacheTest extends RedisDockerTest { @Test public void testReactive() { - Configuration c = createJCacheConfig(); - Configuration config = RedissonConfiguration.fromInstance(redisson, c); - Cache cache = Caching.getCachingProvider().getCacheManager() - .createCache("test", config); + Cache cache = createCache(); CacheReactive reactive = cache.unwrap(CacheReactive.class); reactive.put("1", "2").block(); @@ -171,10 +165,7 @@ public class JCacheTest extends RedisDockerTest { @Test public void testRx() { - Configuration c = createJCacheConfig(); - Configuration config = RedissonConfiguration.fromInstance(redisson, c); - Cache cache = Caching.getCachingProvider().getCacheManager() - .createCache("test", config); + Cache cache = createCache(); CacheRx rx = cache.unwrap(CacheRx.class); rx.put("1", "2").blockingAwait(); @@ -185,10 +176,7 @@ public class JCacheTest extends RedisDockerTest { @Test public void testPutAll() throws Exception { - Configuration c = createJCacheConfig(); - Configuration config = RedissonConfiguration.fromInstance(redisson, c); - Cache cache = Caching.getCachingProvider().getCacheManager() - .createCache("test", config); + Cache cache = createCache(); Map map = new HashMap<>(); for (int i = 0; i < 10000; i++) { @@ -208,10 +196,7 @@ public class JCacheTest extends RedisDockerTest { @Test public void testRemoveAll() throws Exception { - Configuration c = createJCacheConfig(); - Configuration config = RedissonConfiguration.fromInstance(redisson, c); - Cache cache = Caching.getCachingProvider().getCacheManager() - .createCache("test", config); + Cache cache = createCache(); cache.put("1", "2"); cache.put("3", "4"); @@ -230,10 +215,7 @@ public class JCacheTest extends RedisDockerTest { @Test public void testGetAllHighVolume() { - Configuration c = createJCacheConfig(); - Configuration config = RedissonConfiguration.fromInstance(redisson, c); - Cache cache = Caching.getCachingProvider().getCacheManager() - .createCache("test", config); + Cache cache = createCache(); Map m = new HashMap<>(); for (int i = 0; i < 10000; i++) { @@ -249,10 +231,7 @@ public class JCacheTest extends RedisDockerTest { @Test public void testGetAll() { - Configuration c = createJCacheConfig(); - Configuration config = RedissonConfiguration.fromInstance(redisson, c); - Cache cache = Caching.getCachingProvider().getCacheManager() - .createCache("test", config); + Cache cache = createCache(); cache.put("1", "2"); cache.put("3", "4"); @@ -330,10 +309,7 @@ public class JCacheTest extends RedisDockerTest { @Test public void testGetAndPut() { - Configuration c = createJCacheConfig(); - Configuration config = RedissonConfiguration.fromInstance(redisson, c); - Cache cache = Caching.getCachingProvider().getCacheManager() - .createCache("test", config); + Cache cache = createCache(); cache.put("key", "value"); assertThat(cache.getAndPut("key", "value1")).isEqualTo("value"); @@ -342,12 +318,81 @@ public class JCacheTest extends RedisDockerTest { cache.close(); } - @Test - public void testGetAndReplace() { + private Cache createCache() { Configuration c = createJCacheConfig(); Configuration config = RedissonConfiguration.fromInstance(redisson, c); Cache cache = Caching.getCachingProvider().getCacheManager() .createCache("test", config); + return cache; + } + + @Test + void testReplaceKeyOnly() { + Cache cache = createCache(); + cache.put("key1", "value1"); + + boolean result = cache.replace("key1", "newValue"); + assertThat(result).isTrue(); + assertThat(cache.get("key1")).isEqualTo("newValue"); + + result = cache.replace("key2", "newValue"); + assertThat(result).isFalse(); + assertThat(cache.get("key2")).isNull(); + + cache.close(); + } + + @Test + void testReplaceKeyValue() { + Cache cache = createCache(); + cache.put("key1", "value1"); + + boolean result = cache.replace("key1", "value1", "newValue"); + assertThat(result).isTrue(); + assertThat(cache.get("key1")).isEqualTo("newValue"); + + result = cache.replace("key2", "value1", "newValue"); + assertThat(result).isFalse(); + assertThat(cache.get("key2")).isNull(); + + cache.close(); + } + + @Test + void testRemoveKeyValue() { + Cache cache = createCache(); + cache.put("key1", "value1"); + + boolean result = cache.remove("key1", "value1"); + assertThat(result).isTrue(); + assertThat(cache.get("key1")).isNull(); + + result = cache.remove("key2", "value1"); + assertThat(result).isFalse(); + + cache.close(); + } + + @Test + public void testPutIfAbsent() { + Cache cache = createCache(); + + String key = "key1"; + String value1 = "value1"; + String value2 = "value2"; + + assertThat(cache.putIfAbsent(key, value1)).isTrue(); + assertThat(cache.get(key)).isEqualTo(value1); + + assertThat(cache.putIfAbsent(key, value2)).isFalse(); + assertThat(cache.get(key)).isEqualTo(value1); + + cache.close(); + } + + @Test + public void testGetAndReplace() { + Cache cache = createCache(); assertThat(cache.getAndReplace("key", "value1")).isNull(); assertThat(cache.get("key")).isNull(); @@ -361,10 +406,7 @@ public class JCacheTest extends RedisDockerTest { @Test public void testRedissonConfig() throws IllegalArgumentException { - Configuration c = createJCacheConfig(); - Configuration config = RedissonConfiguration.fromInstance(redisson, c); - Cache cache = Caching.getCachingProvider().getCacheManager() - .createCache("test", config); + Cache cache = createCache(); cache.put("1", "2"); Assertions.assertEquals("2", cache.get("1"));