/** * The only purpose of this method is to invoke a particular method on the * service so that the call stack is different. */ private UnsupportedOperationException methodInCallStack(String keyItem) { try { service.cacheWithException(keyItem, true); throw new IllegalStateException("Should have thrown an exception"); } catch (UnsupportedOperationException e) { return e; } }
@Override @Bean public CacheResolver exceptionCacheResolver() { return new SimpleCacheResolver(cacheManager()); } }
@Test public void earlyRemoveAll() { Cache cache = getCache(DEFAULT_CACHE); Object key = createKey(name.getMethodName()); cache.put(key, new Object()); service.earlyRemoveAll(); assertTrue(isEmpty(cache)); }
@Test public void removeAllWithException() { Cache cache = getCache(DEFAULT_CACHE); Object key = createKey(name.getMethodName()); cache.put(key, new Object()); try { service.removeAllWithException(true); fail("Should have thrown an exception"); } catch (UnsupportedOperationException e) { // This is what we expect } assertTrue(isEmpty(cache)); }
@Test public void earlyRemoveAllWithException() { Cache cache = getCache(DEFAULT_CACHE); Object key = createKey(name.getMethodName()); cache.put(key, new Object()); try { service.earlyRemoveAllWithException(true); fail("Should have thrown an exception"); } catch (UnsupportedOperationException e) { // This is what we expect } assertTrue(isEmpty(cache)); }
@Test public void cacheWithCustomCacheResolver() { String keyItem = name.getMethodName(); Cache cache = getCache(DEFAULT_CACHE); Object key = createKey(keyItem); service.cacheWithCustomCacheResolver(keyItem); assertNull(cache.get(key)); // Cache in mock cache }
@Test public void cacheWithCustomKeyGenerator() { String keyItem = name.getMethodName(); Cache cache = getCache(DEFAULT_CACHE); Object key = createKey(keyItem); service.cacheWithCustomKeyGenerator(keyItem, "ignored"); assertNull(cache.get(key)); }
@Test public void earlyRemove() { String keyItem = name.getMethodName(); Cache cache = getCache(DEFAULT_CACHE); Object key = createKey(keyItem); Object value = new Object(); cache.put(key, value); service.earlyRemove(keyItem); assertNull(cache.get(key)); }
@Test public void removeAllWithExceptionVetoRemove() { Cache cache = getCache(DEFAULT_CACHE); Object key = createKey(name.getMethodName()); cache.put(key, new Object()); try { service.removeAllWithException(false); fail("Should have thrown an exception"); } catch (NullPointerException e) { // This is what we expect } assertNotNull(cache.get(key)); }
@Test public void cacheExceptionVetoed() { String keyItem = name.getMethodName(); Cache cache = getCache(EXCEPTION_CACHE); Object key = createKey(keyItem); assertNull(cache.get(key)); try { service.cacheWithException(keyItem, false); fail("Should have thrown an exception"); } catch (NullPointerException e) { // This is what we expect } assertNull(cache.get(key)); }
@Override @Bean public CacheResolver exceptionCacheResolver() { return new NamedCacheResolver(cacheManager(), "exception"); } }
@Bean public JCacheInterceptor jCacheInterceptor(JCacheOperationSource cacheOperationSource) { JCacheInterceptor cacheInterceptor = new TestCacheInterceptor(); cacheInterceptor.setCacheOperationSource(cacheOperationSource); return cacheInterceptor; } }
@Before public void setUp() { ctx = getApplicationContext(); service = ctx.getBean(JCacheableService.class); cacheManager = ctx.getBean("cacheManager", CacheManager.class); }
@Bean public JCacheableService<?> service() { return new AnnotatedJCacheableService(defaultCache()); }
@Bean public JCacheableService<?> cacheableService() { return new AnnotatedJCacheableService(defaultCache()); }
@Test public void customInterceptorAppliesWithRuntimeException() { Object o = cs.cacheWithException("id", true); assertEquals(55L, o); // See TestCacheInterceptor }
@Override @Bean public CacheResolver cacheResolver() { return new SimpleCacheResolver(cacheManager()); }
@Override @Bean public CacheResolver cacheResolver() { return new NamedCacheResolver(cacheManager(), "foo"); }