Skip to content

Commit 556e8fa

Browse files
authored
Refactor jcache examples (eugenp#2620)
1 parent 8e7fc43 commit 556e8fa

4 files changed

Lines changed: 40 additions & 12 deletions

File tree

‎libraries/src/test/java/com/baeldung/jcache/CacheLoaderTest.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99
import javax.cache.configuration.MutableConfiguration;
1010
import javax.cache.spi.CachingProvider;
1111

12+
import org.junit.After;
1213
import org.junit.Before;
1314
import org.junit.Test;
1415

1516
public class CacheLoaderTest {
1617

18+
private static final String CACHE_NAME = "SimpleCache";
19+
1720
private Cache<Integer, String> cache;
1821

1922
@Before
@@ -25,6 +28,12 @@ public void setup() {
2528
this.cache = cacheManager.createCache("SimpleCache", config);
2629
}
2730

31+
@After
32+
public void tearDown() {
33+
Caching.getCachingProvider()
34+
.getCacheManager().destroyCache(CACHE_NAME);
35+
}
36+
2837
@Test
2938
public void whenReadingFromStorage_thenCorrect() {
3039
for (int i = 1; i < 4; i++) {

‎libraries/src/test/java/com/baeldung/jcache/EntryProcessorTest.java‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,38 @@
11
package com.baeldung.jcache;
22

3-
import static org.junit.Assert.assertEquals;
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
46

57
import javax.cache.Cache;
68
import javax.cache.CacheManager;
79
import javax.cache.Caching;
810
import javax.cache.configuration.MutableConfiguration;
911
import javax.cache.spi.CachingProvider;
1012

11-
import org.junit.Before;
12-
import org.junit.Test;
13+
import static org.junit.Assert.assertEquals;
1314

1415
public class EntryProcessorTest {
1516

17+
private static final String CACHE_NAME = "MyCache";
18+
1619
private Cache<String, String> cache;
1720

1821
@Before
1922
public void instantiateCache() {
2023
CachingProvider cachingProvider = Caching.getCachingProvider();
2124
CacheManager cacheManager = cachingProvider.getCacheManager();
22-
MutableConfiguration<String, String> config = new MutableConfiguration<String, String>();
23-
this.cache = cacheManager.createCache("MyCache", config);
25+
MutableConfiguration<String, String> config = new MutableConfiguration<>();
26+
this.cache = cacheManager.createCache(CACHE_NAME, config);
2427
this.cache.put("key", "value");
2528
}
2629

30+
@After
31+
public void tearDown() {
32+
Caching.getCachingProvider()
33+
.getCacheManager().destroyCache(CACHE_NAME);
34+
}
35+
2736
@Test
2837
public void whenModifyValue_thenCorrect() {
2938
this.cache.invoke("key", new SimpleEntryProcessor());

‎libraries/src/test/java/com/baeldung/jcache/EventListenerTest.java‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.baeldung.jcache;
22

3-
import static org.junit.Assert.assertEquals;
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
46

57
import javax.cache.Cache;
68
import javax.cache.CacheManager;
@@ -10,14 +12,15 @@
1012
import javax.cache.configuration.MutableConfiguration;
1113
import javax.cache.spi.CachingProvider;
1214

13-
import org.junit.Before;
14-
import org.junit.Test;
15+
import static org.junit.Assert.assertEquals;
1516

1617
public class EventListenerTest {
1718

19+
private static final String CACHE_NAME = "MyCache";
20+
1821
private Cache<String, String> cache;
1922
private SimpleCacheEntryListener listener;
20-
MutableCacheEntryListenerConfiguration<String, String> listenerConfiguration;
23+
private MutableCacheEntryListenerConfiguration<String, String> listenerConfiguration;
2124

2225
@Before
2326
public void setup() {
@@ -28,9 +31,16 @@ public void setup() {
2831
this.listener = new SimpleCacheEntryListener();
2932
}
3033

34+
@After
35+
public void tearDown() {
36+
Caching.getCachingProvider()
37+
.getCacheManager().destroyCache(CACHE_NAME);
38+
}
39+
3140
@Test
3241
public void whenRunEvent_thenCorrect() throws InterruptedException {
33-
this.listenerConfiguration = new MutableCacheEntryListenerConfiguration<String, String>(FactoryBuilder.factoryOf(this.listener), null, false, true);
42+
this.listenerConfiguration = new MutableCacheEntryListenerConfiguration<>(FactoryBuilder
43+
.factoryOf(this.listener), null, false, true);
3444
this.cache.registerCacheEntryListener(this.listenerConfiguration);
3545

3646
assertEquals(false, this.listener.getCreated());

‎libraries/src/test/java/com/baeldung/jcache/JCacheTest.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package com.baeldung.jcache;
22

3-
import static org.junit.Assert.assertEquals;
3+
import org.junit.Test;
44

55
import javax.cache.Cache;
66
import javax.cache.CacheManager;
77
import javax.cache.Caching;
88
import javax.cache.configuration.MutableConfiguration;
99
import javax.cache.spi.CachingProvider;
1010

11-
import org.junit.Test;
11+
import static org.junit.Assert.assertEquals;
1212

1313
public class JCacheTest {
1414

0 commit comments

Comments
 (0)