JcsCacheService implements ICacheService { @Override public void initialise(final Properties properties) { String configFile = properties.getProperty(CacheProperties.CACHE_CONFIG_FILE); manager = CompositeCacheManager.getUnconfiguredInstance(); if (null != configFile) { try { Properties cacheProperties = readProperties(configFile); manager.configure(cacheProperties); return; } catch (final IOException e) { throw new IllegalArgumentException("Cannot create cache using config file " + configFile, e); } } LOGGER.debug("No config file configured. Using default."); manager.configure(); }  @Override void initialise(final Properties properties); @Override ICache<K, V> getCache(final String cacheName); @Override void shutdown();  }
@Test public void shouldThrowAnExceptionIfPathIsMisconfigured() { String badFileName = "/made/up/file/name"; String expected = String.format("Cannot create cache using config file %s", badFileName); serviceProps.setProperty(CacheProperties.CACHE_CONFIG_FILE, badFileName); IllegalArgumentException actual = assertThrows(IllegalArgumentException.class, () -> service.initialise(serviceProps)); assertEquals(expected, actual.getMessage()); }
@Test public void shouldAddEntriesToCache() throws CacheOperationException { service.initialise(serviceProps); service.putInCache(TEST_REGION, "test", 1); assertEquals((Integer) 1, service.getFromCache(TEST_REGION, "test")); }
@Test public void shouldOnlyUpdateIfInstructed() throws CacheOperationException { service.initialise(serviceProps); service.putInCache(TEST_REGION, "test", 1); try { service.putSafeInCache(TEST_REGION, "test", 2); fail("Expected an exception"); } catch (final OverwritingException e) { assertEquals((Integer) 1, service.getFromCache(TEST_REGION, "test")); } service.putInCache(TEST_REGION, "test", 2); assertEquals((Integer) 2, service.getFromCache(TEST_REGION, "test")); }
@Test public void shouldBeAbleToDeleteCacheEntries() throws CacheOperationException { service.initialise(serviceProps); service.putInCache(TEST_REGION, "test", 1); service.removeFromCache(TEST_REGION, "test"); assertEquals(0, service.sizeOfCache(TEST_REGION)); }
@Test public void shouldBeAbleToClearCache() throws CacheOperationException { service.initialise(serviceProps); service.putInCache(TEST_REGION, "test1", 1); service.putInCache(TEST_REGION, "test2", 2); service.putInCache(TEST_REGION, "test3", 3); service.clearCache(TEST_REGION); assertEquals(0, service.sizeOfCache(TEST_REGION)); }
@Test public void shouldGetAllKeysFromCache() throws CacheOperationException { service.initialise(serviceProps); service.putInCache(TEST_REGION, "test1", 1); service.putInCache(TEST_REGION, "test2", 2); service.putInCache(TEST_REGION, "test3", 3); assertEquals(3, service.sizeOfCache(TEST_REGION)); assertThat(service.getAllKeysFromCache(TEST_REGION), IsCollectionContaining.hasItems("test1", "test2", "test3")); }
@Test public void shouldGetAllValues() throws CacheOperationException { service.initialise(serviceProps); service.putInCache(TEST_REGION, "test1", 1); service.putInCache(TEST_REGION, "test2", 2); service.putInCache(TEST_REGION, "test3", 3); service.putInCache(TEST_REGION, "duplicate", 3); assertEquals(4, service.sizeOfCache(TEST_REGION)); assertEquals(4, service.getAllValuesFromCache(TEST_REGION).size()); assertThat(service.getAllValuesFromCache(TEST_REGION), IsCollectionContaining.hasItems(1, 2, 3)); }
@Test public void shouldAgeOffValues() throws CacheOperationException { String filePath = new File("src/test/resources/cache.ccf").getAbsolutePath(); serviceProps.setProperty(CacheProperties.CACHE_CONFIG_FILE, filePath); service.initialise(serviceProps); service.putInCache(AGE_OFF_REGION, "test", 1); Uninterruptibles.sleepUninterruptibly(3, TimeUnit.SECONDS); assertNull(service.getFromCache(AGE_OFF_REGION, "test")); }
@Test public void shouldAllowAgedOffValuesToBeReplaced() throws CacheOperationException { String filePath = new File("src/test/resources/cache.ccf").getAbsolutePath(); serviceProps.setProperty(CacheProperties.CACHE_CONFIG_FILE, filePath); service.initialise(serviceProps); service.putInCache(AGE_OFF_REGION, "test", 1); Uninterruptibles.sleepUninterruptibly(3, TimeUnit.SECONDS); service.putInCache(AGE_OFF_REGION, "test", 1); assertEquals((Integer) 1, service.getFromCache(AGE_OFF_REGION, "test")); }