/** * Execute {@link Cache#clear()} on the specified {@link Cache} and * invoke the error handler if an exception occurs. */ protected void doClear(Cache cache) { try { cache.clear(); } catch (RuntimeException ex) { getErrorHandler().handleCacheClearError(ex, cache); } }
/** * Execute {@link Cache#evict(Object)} on the specified {@link Cache} and * invoke the error handler if an exception occurs. */ protected void doEvict(Cache cache, Object key) { try { cache.evict(key); } catch (RuntimeException ex) { getErrorHandler().handleCacheEvictError(ex, cache, key); } }
/** * Execute {@link Cache#get(Object)} on the specified {@link Cache} and * invoke the error handler if an exception occurs. Return {@code null} * if the handler does not throw any exception, which simulates a cache * miss in case of error. * @see Cache#get(Object) */ @Nullable protected Cache.ValueWrapper doGet(Cache cache, Object key) { try { return cache.get(key); } catch (RuntimeException ex) { getErrorHandler().handleCacheGetError(ex, cache, key); return null; // If the exception is handled, return a cache miss } }
/** * Execute {@link Cache#put(Object, Object)} on the specified {@link Cache} * and invoke the error handler if an exception occurs. */ protected void doPut(Cache cache, Object key, @Nullable Object result) { try { cache.put(key, result); } catch (RuntimeException ex) { getErrorHandler().handleCachePutError(ex, cache, key, result); } }
/** * Execute {@link Cache#put(Object, Object)} on the specified {@link Cache} * and invoke the error handler if an exception occurs. */ protected void doPut(Cache cache, Object key, @Nullable Object result) { try { cache.put(key, result); } catch (RuntimeException ex) { getErrorHandler().handleCachePutError(ex, cache, key, result); } }
@Test public void putFail() { UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on put"); willThrow(exception).given(this.cache).put(0L, 0L); this.simpleService.put(0L); verify(this.errorHandler).handleCachePutError(exception, cache, 0L, 0L); }
/** * Execute {@link Cache#evict(Object)} on the specified {@link Cache} and * invoke the error handler if an exception occurs. */ protected void doEvict(Cache cache, Object key) { try { cache.evict(key); } catch (RuntimeException ex) { getErrorHandler().handleCacheEvictError(ex, cache, key); } }
/** * Execute {@link Cache#clear()} on the specified {@link Cache} and * invoke the error handler if an exception occurs. */ protected void doClear(Cache cache) { try { cache.clear(); } catch (RuntimeException ex) { getErrorHandler().handleCacheClearError(ex, cache); } }
/** * Execute {@link Cache#get(Object)} on the specified {@link Cache} and * invoke the error handler if an exception occurs. Return {@code null} * if the handler does not throw any exception, which simulates a cache * miss in case of error. * @see Cache#get(Object) */ @Nullable protected Cache.ValueWrapper doGet(Cache cache, Object key) { try { return cache.get(key); } catch (RuntimeException ex) { getErrorHandler().handleCacheGetError(ex, cache, key); return null; // If the exception is handled, return a cache miss } }
@Test public void putFail() { UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on put"); Object key = SimpleKeyGenerator.generateKey(0L); willThrow(exception).given(this.cache).put(key, 234L); this.simpleService.put(0L, 234L); verify(this.errorHandler).handleCachePutError(exception, this.cache, key, 234L); }
@Test public void evictFail() { UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on evict"); willThrow(exception).given(this.cache).evict(0L); this.simpleService.evict(0L); verify(this.errorHandler).handleCacheEvictError(exception, cache, 0L); }
@Test public void clearFail() { UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on evict"); willThrow(exception).given(this.cache).clear(); this.simpleService.clear(); verify(this.errorHandler).handleCacheClearError(exception, cache); }
@Test public void getFail() { UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on get"); Object key = SimpleKeyGenerator.generateKey(0L); willThrow(exception).given(this.cache).get(key); this.simpleService.get(0L); verify(this.errorHandler).handleCacheGetError(exception, this.cache, key); }
@Test public void getFailPutExceptionFail() { UnsupportedOperationException exceptionOnPut = new UnsupportedOperationException("Test exception on put"); Object key = SimpleKeyGenerator.generateKey(0L); given(this.cache.get(key)).willReturn(null); willThrow(exceptionOnPut).given(this.errorCache).put(key, SimpleService.TEST_EXCEPTION); try { this.simpleService.getFail(0L); } catch (IllegalStateException ex) { assertEquals("Test exception", ex.getMessage()); } verify(this.errorHandler).handleCachePutError( exceptionOnPut, this.errorCache, key, SimpleService.TEST_EXCEPTION); }
@Test public void evictFail() { UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on evict"); Object key = SimpleKeyGenerator.generateKey(0L); willThrow(exception).given(this.cache).evict(key); this.simpleService.evict(0L); verify(this.errorHandler).handleCacheEvictError(exception, this.cache, key); }
@Test public void clearFail() { UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on evict"); willThrow(exception).given(this.cache).clear(); this.simpleService.clear(); verify(this.errorHandler).handleCacheClearError(exception, this.cache); }
@Test public void getFail() { UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on get"); willThrow(exception).given(this.cache).get(0L); Object result = this.simpleService.get(0L); verify(this.errorHandler).handleCacheGetError(exception, cache, 0L); verify(this.cache).get(0L); verify(this.cache).put(0L, result); // result of the invocation }
@Test public void getPutNewElementFail() { UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on put"); Object key = SimpleKeyGenerator.generateKey(0L); given(this.cache.get(key)).willReturn(null); willThrow(exception).given(this.cache).put(key, 0L); this.simpleService.get(0L); verify(this.errorHandler).handleCachePutError(exception, this.cache, key, 0L); }
/** * Execute {@link Cache#evict(Object)} on the specified {@link Cache} and * invoke the error handler if an exception occurs. */ protected void doEvict(Cache cache, Object key) { try { cache.evict(key); } catch (RuntimeException ex) { getErrorHandler().handleCacheEvictError(ex, cache, key); } }
/** * Execute {@link Cache#clear()} on the specified {@link Cache} and * invoke the error handler if an exception occurs. */ protected void doClear(Cache cache) { try { cache.clear(); } catch (RuntimeException ex) { getErrorHandler().handleCacheClearError(ex, cache); } }