@Before public void setUp() { ctx = getApplicationContext(); service = ctx.getBean(JCacheableService.class); cacheManager = ctx.getBean("cacheManager", CacheManager.class); }
@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 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 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 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 earlyRemoveAllWithExceptionVetoRemove() { Cache cache = getCache(DEFAULT_CACHE); Object key = createKey(name.getMethodName()); cache.put(key, new Object()); try { service.earlyRemoveAllWithException(false); fail("Should have thrown an exception"); } catch (NullPointerException e) { // This is what we expect } // This will be remove anyway as the earlyRemove has removed the cache before assertTrue(isEmpty(cache)); }
@Test public void removeWithException() { String keyItem = name.getMethodName(); Cache cache = getCache(DEFAULT_CACHE); Object key = createKey(keyItem); Object value = new Object(); cache.put(key, value); try { service.removeWithException(keyItem, true); fail("Should have thrown an exception"); } catch (UnsupportedOperationException e) { // This is what we expect } assertNull(cache.get(key)); }
@Test public void earlyRemoveWithException() { String keyItem = name.getMethodName(); Cache cache = getCache(DEFAULT_CACHE); Object key = createKey(keyItem); Object value = new Object(); cache.put(key, value); try { service.earlyRemoveWithException(keyItem, true); fail("Should have thrown an exception"); } catch (UnsupportedOperationException e) { // This is what we expect } assertNull(cache.get(key)); }
@Test public void earlyRemoveWithExceptionVetoRemove() { String keyItem = name.getMethodName(); Cache cache = getCache(DEFAULT_CACHE); Object key = createKey(keyItem); Object value = new Object(); cache.put(key, value); try { service.earlyRemoveWithException(keyItem, false); fail("Should have thrown an exception"); } catch (NullPointerException e) { // This is what we expect } // This will be remove anyway as the earlyRemove has removed the cache before assertNull(cache.get(key)); }
@Test public void put() { String keyItem = name.getMethodName(); Cache cache = getCache(DEFAULT_CACHE); Object key = createKey(keyItem); Object value = new Object(); assertNull(cache.get(key)); service.put(keyItem, value); Cache.ValueWrapper result = cache.get(key); assertNotNull(result); assertEquals(value, result.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 putWithExceptionVetoPut() { String keyItem = name.getMethodName(); Cache cache = getCache(DEFAULT_CACHE); Object key = createKey(keyItem); Object value = new Object(); assertNull(cache.get(key)); try { service.putWithException(keyItem, value, false); fail("Should have thrown an exception"); } catch (NullPointerException e) { // This is what we expect } assertNull(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)); }