Skip to content

Commit 0d4fa7c

Browse files
TINOTINO
authored andcommitted
BAEL - 2251
Removed Spring boot dependency.
1 parent e2aad41 commit 0d4fa7c

4 files changed

Lines changed: 111 additions & 52 deletions

File tree

‎spring-all/src/main/java/org/baeldung/caching/eviction/CacheEvictionMain.java‎

Lines changed: 0 additions & 17 deletions
This file was deleted.

spring-all/src/main/java/org/baeldung/caching/eviction/cotrollers/CachingController.java renamed to spring-all/src/main/java/org/baeldung/caching/eviction/controllers/CachingController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.baeldung.caching.eviction.cotrollers;
1+
package org.baeldung.caching.eviction.controllers;
22

33
import org.baeldung.caching.eviction.service.CachingService;
44
import org.springframework.beans.factory.annotation.Autowired;

‎spring-all/src/test/java/org/baeldung/caching/test/CacheEvictAnnotationIntegrationTest.java‎

Lines changed: 64 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,76 @@
44
import static org.hamcrest.CoreMatchers.nullValue;
55
import static org.junit.Assert.assertThat;
66

7-
import org.baeldung.caching.eviction.CacheEvictionMain;
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
810
import org.baeldung.caching.eviction.service.CachingService;
911
import org.junit.Before;
1012
import org.junit.Test;
1113
import org.junit.runner.RunWith;
1214
import org.springframework.beans.factory.annotation.Autowired;
13-
import org.springframework.boot.test.context.SpringBootTest;
14-
import org.springframework.test.context.junit4.SpringRunner;
15+
import org.springframework.cache.Cache;
16+
import org.springframework.cache.CacheManager;
17+
import org.springframework.cache.annotation.EnableCaching;
18+
import org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean;
19+
import org.springframework.cache.support.SimpleCacheManager;
20+
import org.springframework.context.annotation.Bean;
21+
import org.springframework.context.annotation.Configuration;
22+
import org.springframework.test.context.ContextConfiguration;
23+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
1524

16-
@RunWith(SpringRunner.class)
17-
@SpringBootTest(classes = { CacheEvictionMain.class, CachingService.class })
25+
@RunWith(SpringJUnit4ClassRunner.class)
26+
@ContextConfiguration
1827
public class CacheEvictAnnotationIntegrationTest {
1928

20-
@Autowired
21-
CachingService cachingService;
22-
23-
@Before
24-
public void before() {
25-
cachingService.putToCache("first", "key1", "Baeldung");
26-
cachingService.putToCache("first", "key2", "Article");
27-
}
28-
29-
@Test
30-
public void givenFirstCache_whenSingleCacheValueEvictRequested_thenEmptyCacheValue() {
31-
cachingService.evictSingleCacheValue("key1");
32-
String key1 = cachingService.getFromCache("first", "key1");
33-
assertThat(key1, is(nullValue()));
34-
}
35-
36-
@Test
37-
public void givenFirstCache_whenAllCacheValueEvictRequested_thenEmptyCache() {
38-
cachingService.evictAllCacheValues();
39-
String key1 = cachingService.getFromCache("first", "key1");
40-
String key2 = cachingService.getFromCache("first", "key2");
41-
assertThat(key1, is(nullValue()));
42-
assertThat(key2, is(nullValue()));
43-
}
29+
@Configuration
30+
@EnableCaching
31+
static class ContextConfiguration {
32+
33+
@Bean
34+
public CachingService cachingService() {
35+
return new CachingService();
36+
}
37+
38+
@Bean
39+
public CacheManager cacheManager(){
40+
SimpleCacheManager cacheManager = new SimpleCacheManager();
41+
List<Cache> caches = new ArrayList<>();
42+
caches.add(cacheBean().getObject());
43+
cacheManager.setCaches(caches );
44+
return cacheManager;
45+
}
46+
47+
@Bean
48+
public ConcurrentMapCacheFactoryBean cacheBean(){
49+
ConcurrentMapCacheFactoryBean cacheFactoryBean = new ConcurrentMapCacheFactoryBean();
50+
cacheFactoryBean.setName("first");
51+
return cacheFactoryBean;
52+
}
53+
}
54+
55+
@Autowired
56+
CachingService cachingService;
57+
58+
@Before
59+
public void before() {
60+
cachingService.putToCache("first", "key1", "Baeldung");
61+
cachingService.putToCache("first", "key2", "Article");
62+
}
63+
64+
@Test
65+
public void givenFirstCache_whenSingleCacheValueEvictRequested_thenEmptyCacheValue() {
66+
cachingService.evictSingleCacheValue("key1");
67+
String key1 = cachingService.getFromCache("first", "key1");
68+
assertThat(key1, is(nullValue()));
69+
}
70+
71+
@Test
72+
public void givenFirstCache_whenAllCacheValueEvictRequested_thenEmptyCache() {
73+
cachingService.evictAllCacheValues();
74+
String key1 = cachingService.getFromCache("first", "key1");
75+
String key2 = cachingService.getFromCache("first", "key2");
76+
assertThat(key1, is(nullValue()));
77+
assertThat(key2, is(nullValue()));
78+
}
4479
}

‎spring-all/src/test/java/org/baeldung/caching/test/CacheManagerEvictIntegrationTest.java‎

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,59 @@
44
import static org.hamcrest.CoreMatchers.nullValue;
55
import static org.junit.Assert.assertThat;
66

7-
import org.baeldung.caching.eviction.CacheEvictionMain;
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
810
import org.baeldung.caching.eviction.service.CachingService;
911
import org.junit.Before;
1012
import org.junit.Test;
1113
import org.junit.runner.RunWith;
1214
import org.springframework.beans.factory.annotation.Autowired;
13-
import org.springframework.boot.test.context.SpringBootTest;
14-
import org.springframework.test.context.junit4.SpringRunner;
15+
import org.springframework.cache.Cache;
16+
import org.springframework.cache.CacheManager;
17+
import org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean;
18+
import org.springframework.cache.support.SimpleCacheManager;
19+
import org.springframework.context.annotation.Bean;
20+
import org.springframework.context.annotation.Configuration;
21+
import org.springframework.test.context.ContextConfiguration;
22+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
1523

16-
@RunWith(SpringRunner.class)
17-
@SpringBootTest(classes = { CacheEvictionMain.class, CachingService.class })
24+
@RunWith(SpringJUnit4ClassRunner.class)
25+
@ContextConfiguration
1826
public class CacheManagerEvictIntegrationTest {
27+
28+
@Configuration
29+
static class ContextConfiguration {
30+
31+
@Bean
32+
public CachingService cachingService() {
33+
return new CachingService();
34+
}
35+
36+
@Bean
37+
public CacheManager cacheManager(){
38+
SimpleCacheManager cacheManager = new SimpleCacheManager();
39+
List<Cache> caches = new ArrayList<>();
40+
caches.add(cacheBeanFirst().getObject());
41+
caches.add(cacheBeanSecond().getObject());
42+
cacheManager.setCaches(caches );
43+
return cacheManager;
44+
}
45+
46+
@Bean
47+
public ConcurrentMapCacheFactoryBean cacheBeanFirst(){
48+
ConcurrentMapCacheFactoryBean cacheFactoryBean = new ConcurrentMapCacheFactoryBean();
49+
cacheFactoryBean.setName("first");
50+
return cacheFactoryBean;
51+
}
52+
53+
@Bean
54+
public ConcurrentMapCacheFactoryBean cacheBeanSecond(){
55+
ConcurrentMapCacheFactoryBean cacheFactoryBean = new ConcurrentMapCacheFactoryBean();
56+
cacheFactoryBean.setName("second");
57+
return cacheFactoryBean;
58+
}
59+
}
1960

2061
@Autowired
2162
CachingService cachingService;

0 commit comments

Comments
 (0)