1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
| @ActiveProfiles("test") @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {TestConfig.class}) @WebAppConfiguration public class CompositeCacheManagerTest {
final Logger logger = LoggerFactory.getLogger(getClass()); public static final FastDateFormat DATETIME_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss:SSS"); public static final String REDIS_KEY = "redis-key"; public static final String EHCACHE_KEY = "ehcache-key"; public static final String REDIS_IDENTIFY_KEY = "redis-identify-key"; public static final String EHCACHE_IDENTIFY_KEY = "ehcache-identify-key"; public static final int SLEEP_MILLIS = 300; @Autowired private WebApplicationContext webApplicationContext;
protected MockMvc mockMvc;
@Before public void createMockMVC() { mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).addFilter(new GlobalFilter()) .build(); }
@Test public void applicationContextShouldBeInitialized() { Assert.assertNotNull(webApplicationContext); for (String beanDefinitionNames : webApplicationContext.getBeanDefinitionNames()) { Assert.assertNotNull(webApplicationContext.getBean(beanDefinitionNames)); } }
@Autowired StringRedisTemplate stringRedisTemplate; @Autowired RedisCacheService redisCacheService; @Autowired EhCacheService ehCacheService; @Autowired CacheManager ehCacheManager; @Autowired CacheManager redisCacheManager;
@Test public void testEhCacheLoad() throws Exception { Collection<String> cacheNames = ehCacheManager.getCacheNames(); logger.debug("cacheNames={}", cacheNames); Assert.assertThat(cacheNames, Matchers.contains(EHCACHE_KEY, EHCACHE_IDENTIFY_KEY)); }
@Test public void testRedisCacheLoad() throws Exception { Collection<String> cacheNames = redisCacheManager.getCacheNames(); logger.debug("cacheNames={}", cacheNames); Assert.assertThat(cacheNames, Matchers.contains(REDIS_KEY, REDIS_IDENTIFY_KEY)); }
@Before public void setUp() throws Exception { redisCacheService.evictAll(); ehCacheService.evictAll(); }
@After public void tearDown() throws Exception { redisCacheService.evictAll(); ehCacheService.evictAll(); }
@Test public void testRedis() throws Exception { logger.debug("start test redis"); redisCacheService.clearExecuteTime(); String currentDateStringBefore = redisCacheService.get(); logger.debug("currentDateStringBefore={}", currentDateStringBefore); Thread.currentThread().join(SLEEP_MILLIS); String currentDateStringAfter = redisCacheService.get(); logger.debug("currentDateStringAfter={}", currentDateStringAfter); Assert.assertEquals(currentDateStringAfter, currentDateStringBefore); int executeTime = redisCacheService.getExecuteTime(); logger.debug("executeTime={}", executeTime); Assert.assertEquals(executeTime, 1); logger.debug("finish test redis"); }
@Test public void testEhcache() throws Exception { logger.debug("start test ehcache"); ehCacheService.clearExecuteTime(); String currentDateStringBefore = ehCacheService.get(); logger.debug("currentDateStringBefore={}", currentDateStringBefore); Thread.currentThread().join(SLEEP_MILLIS); String currentDateStringAfter = ehCacheService.get(); logger.debug("currentDateStringAfter={}", currentDateStringAfter); Assert.assertEquals(currentDateStringAfter, currentDateStringBefore); int executeTime = ehCacheService.getExecuteTime(); logger.debug("executeTime={}", executeTime); Assert.assertEquals(executeTime, 1); logger.debug("finish test ehcache"); }
@Test public void testRedisIdentify() throws Exception { logger.debug("start test redis identify"); redisCacheService.clearExecuteTime(); Long identify1 = 1L; Long identify2 = 2L;
String value1Before = redisCacheService.get(identify1); logger.debug("value1Before={}", value1Before);
String value2Before = redisCacheService.get(identify2); logger.debug("value2Before={}", value2Before);
Thread.currentThread().join(SLEEP_MILLIS);
String value1After = redisCacheService.get(identify1); logger.debug("value1After={}", value1After); Assert.assertEquals(value1Before, value1After);
String value2After = redisCacheService.get(identify2); logger.debug("value2After={}", value2After); Assert.assertEquals(value2Before, value2After);
int executeTime = redisCacheService.getExecuteTime(); logger.debug("executeTime={}", executeTime); Assert.assertEquals(executeTime, 2);
logger.debug("finish test redis identify"); }
@Test public void testEhcacheIdentify() throws Exception { logger.debug("start test ehcache identify"); ehCacheService.clearExecuteTime(); Long identify1 = 1L; Long identify2 = 2L;
String value1Before = ehCacheService.get(identify1); logger.debug("value1Before={}", value1Before);
String value2Before = ehCacheService.get(identify2); logger.debug("value2Before={}", value2Before);
Thread.currentThread().join(SLEEP_MILLIS);
String value1After = ehCacheService.get(identify1); logger.debug("value1After={}", value1After); Assert.assertEquals(value1Before, value1After);
String value2After = ehCacheService.get(identify2); logger.debug("value2After={}", value2After); Assert.assertEquals(value2Before, value2After);
int executeTime = ehCacheService.getExecuteTime(); logger.debug("executeTime={}", executeTime); Assert.assertEquals(executeTime, 2);
logger.debug("finish test ehcache identify"); } }
|