UpdateCounterInCacheAdvice extends CounterInCacheBase { @AfterReturning(pointcut = "updateCounter()", returning = "retVal") public void cacheCounterInCache(final JoinPoint jp, final Object retVal) throws Throwable { if (isDisabled()) { getLogger().info("Cache disabled"); return; } String cacheKey = null; UpdateCounterInCache annotation; try { Method methodToCache = getCacheBase().getMethodToCache(jp, UpdateCounterInCache.class); annotation = methodToCache.getAnnotation(UpdateCounterInCache.class); AnnotationData data = AnnotationDataBuilder.buildAnnotationData(annotation, UpdateCounterInCache.class, methodToCache); cacheKey = getCacheBase().getCacheKeyBuilder().getCacheKey(data, jp.getArgs(), methodToCache.toString()); Object dataObject = getCacheBase().getUpdateData(data, methodToCache, jp.getArgs(), retVal); if (checkData(dataObject, jp)) { long value = ((Number) dataObject).longValue(); getCacheBase().getCache(data).setCounter(cacheKey, annotation.expiration(), value); } } catch (Exception ex) { warn(ex, "Updating counter [%s] in cache via %s aborted due to an error.", cacheKey, jp.toShortString()); } }  @Pointcut("@annotation(com.google.code.ssm.api.counter.UpdateCounterInCache)") void updateCounter(); @AfterReturning(pointcut = "updateCounter()", returning = "retVal") void cacheCounterInCache(final JoinPoint jp, final Object retVal);  }
@Test public void validUpdateCounterInCache() throws Throwable { Assume.assumeTrue(isValid); advice.cacheCounterInCache(pjp, reternValue); verify(cache).setCounter(eq(cacheKey), eq(EXPIRATION), eq(((Number) expectedValue).longValue())); verify(pjp, never()).proceed(); }
@Test public void invalidUpdateCounterInCache() throws Throwable { Assume.assumeThat(isValid, CoreMatchers.is(false)); advice.cacheCounterInCache(pjp, reternValue); verify(cache, never()).setCounter(anyString(), anyInt(), anyLong()); verify(cache, never()).set(anyString(), anyInt(), anyLong(), any(SerializationType.class)); verify(pjp, never()).proceed(); }