/** * 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 customInterceptorAppliesWithRuntimeException() { Object o = cs.cacheWithException("id", true); assertEquals(55L, o); // See TestCacheInterceptor }
@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(); } }
@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")); }
@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 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)); }