@Test public void exceptionCacheResolverLazilyRequired() { ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(NoExceptionCacheResolverConfig.class); try { DefaultJCacheOperationSource cos = context.getBean(DefaultJCacheOperationSource.class); assertSame(context.getBean("cacheResolver"), cos.getCacheResolver()); JCacheableService<?> service = context.getBean(JCacheableService.class); service.cache("id"); // This call requires the cache manager to be set thrown.expect(IllegalStateException.class); service.cacheWithException("test", false); } finally { context.close(); } }
@Test public void cacheAlwaysInvoke() { String keyItem = name.getMethodName(); Object first = service.cacheAlwaysInvoke(keyItem); Object second = service.cacheAlwaysInvoke(keyItem); assertNotSame(first, second); }
@Test public void customInterceptorAppliesWithCheckedException() { try { cs.cacheWithCheckedException("id", true); fail("Should have failed"); } catch (RuntimeException e) { assertNotNull("missing original exception", e.getCause()); assertEquals(IOException.class, e.getCause().getClass()); } catch (Exception e) { fail("Wrong exception type " + e); } }
@SuppressWarnings("ThrowableResultOfMethodCallIgnored") @Test public void cacheExceptionRewriteCallStack() { final String keyItem = name.getMethodName(); UnsupportedOperationException first = null; long ref = service.exceptionInvocations(); try { service.cacheWithException(keyItem, true); fail("Should have thrown an exception"); } catch (UnsupportedOperationException e) { first = e; } // Sanity check, this particular call has called the service assertEquals("First call should not have been cached", ref + 1, service.exceptionInvocations()); UnsupportedOperationException second = methodInCallStack(keyItem); // Sanity check, this particular call has *not* called the service assertEquals("Second call should have been cached", ref + 1, service.exceptionInvocations()); assertEquals(first.getCause(), second.getCause()); assertEquals(first.getMessage(), second.getMessage()); assertFalse("Original stack must not contain any reference to methodInCallStack", contain(first, AbstractJCacheAnnotationTests.class.getName(), "methodInCallStack")); assertTrue("Cached stack should have been rewritten with a reference to methodInCallStack", contain(second, AbstractJCacheAnnotationTests.class.getName(), "methodInCallStack")); }
/** * 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; } }
@Test public void earlyPutWithException() { String keyItem = name.getMethodName(); Cache cache = getCache(DEFAULT_CACHE); Object key = createKey(keyItem); Object value = new Object(); assertNull(cache.get(key)); try { service.earlyPutWithException(keyItem, value, true); fail("Should have thrown an exception"); } catch (UnsupportedOperationException e) { // This is what we expect } Cache.ValueWrapper result = cache.get(key); assertNotNull(result); assertEquals(value, result.get()); }
@Test public void cacheWithPartialKey() { String keyItem = name.getMethodName(); Object first = service.cacheWithPartialKey(keyItem, true); Object second = service.cacheWithPartialKey(keyItem, false); assertSame(first, second); // second argument not used, see config }
@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 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 cacheNull() { Cache cache = getCache(DEFAULT_CACHE); String keyItem = name.getMethodName(); assertNull(cache.get(keyItem)); Object first = service.cacheNull(keyItem); Object second = service.cacheNull(keyItem); assertSame(first, second); Cache.ValueWrapper wrapper = cache.get(keyItem); assertNotNull(wrapper); assertSame(first, wrapper.get()); assertNull("Cached value should be null", wrapper.get()); }
@Test public void earlyPut() { String keyItem = name.getMethodName(); Cache cache = getCache(DEFAULT_CACHE); Object key = createKey(keyItem); Object value = new Object(); assertNull(cache.get(key)); service.earlyPut(keyItem, value); Cache.ValueWrapper result = cache.get(key); assertNotNull(result); assertEquals(value, result.get()); }
@Test public void customInterceptorAppliesWithRuntimeException() { Object o = cs.cacheWithException("id", true); assertEquals(55L, o); // See TestCacheInterceptor }
@Test public void earlyPutWithExceptionVetoPut() { String keyItem = name.getMethodName(); Cache cache = getCache(DEFAULT_CACHE); Object key = createKey(keyItem); Object value = new Object(); assertNull(cache.get(key)); try { service.earlyPutWithException(keyItem, value, false); fail("Should have thrown an exception"); } catch (NullPointerException e) { // This is what we expect } // This will be cached anyway as the earlyPut has updated the cache before Cache.ValueWrapper result = cache.get(key); assertNotNull(result); assertEquals(value, result.get()); }
@Test public void cacheException() { String keyItem = name.getMethodName(); Cache cache = getCache(EXCEPTION_CACHE); Object key = createKey(keyItem); assertNull(cache.get(key)); try { service.cacheWithException(keyItem, true); fail("Should have thrown an exception"); } catch (UnsupportedOperationException e) { // This is what we expect } Cache.ValueWrapper result = cache.get(key); assertNotNull(result); assertEquals(UnsupportedOperationException.class, result.get().getClass()); }
@Test public void cacheCheckedException() { String keyItem = name.getMethodName(); Cache cache = getCache(EXCEPTION_CACHE); Object key = createKey(keyItem); assertNull(cache.get(key)); try { service.cacheWithCheckedException(keyItem, true); fail("Should have thrown an exception"); } catch (IOException e) { // This is what we expect } Cache.ValueWrapper result = cache.get(key); assertNotNull(result); assertEquals(IOException.class, result.get().getClass()); }
@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)); }