@Override @Nullable public Object invoke(final MethodInvocation invocation) throws Throwable { Method method = invocation.getMethod(); CacheOperationInvoker aopAllianceInvoker = () -> { try { return invocation.proceed(); } catch (Throwable ex) { throw new CacheOperationInvoker.ThrowableWrapper(ex); } }; try { return execute(aopAllianceInvoker, invocation.getThis(), method, invocation.getArguments()); } catch (CacheOperationInvoker.ThrowableWrapper th) { throw th.getOriginal(); } }
@Override @Nullable public Object invoke(final MethodInvocation invocation) throws Throwable { Method method = invocation.getMethod(); CacheOperationInvoker aopAllianceInvoker = () -> { try { return invocation.proceed(); } catch (Throwable ex) { throw new CacheOperationInvoker.ThrowableWrapper(ex); } }; try { return execute(aopAllianceInvoker, invocation.getThis(), method, invocation.getArguments()); } catch (CacheOperationInvoker.ThrowableWrapper th) { throw th.getOriginal(); } }
@Test public void severalCachesNotSupported() { JCacheInterceptor interceptor = createInterceptor(createOperationSource( cacheManager, new NamedCacheResolver(cacheManager, "default", "simpleCache"), defaultExceptionCacheResolver, defaultKeyGenerator)); AnnotatedJCacheableService service = new AnnotatedJCacheableService(cacheManager.getCache("default")); Method m = ReflectionUtils.findMethod(AnnotatedJCacheableService.class, "cache", String.class); try { interceptor.execute(dummyInvoker, service, m, new Object[] {"myId"}); } catch (IllegalStateException ex) { assertTrue(ex.getMessage().contains("JSR-107 only supports a single cache")); } catch (Throwable ex) { fail("Unexpected: " + ex); } }
@Test public void noCacheCouldBeResolved() { JCacheInterceptor interceptor = createInterceptor(createOperationSource( cacheManager, new NamedCacheResolver(cacheManager), // Returns empty list defaultExceptionCacheResolver, defaultKeyGenerator)); AnnotatedJCacheableService service = new AnnotatedJCacheableService(cacheManager.getCache("default")); Method m = ReflectionUtils.findMethod(AnnotatedJCacheableService.class, "cache", String.class); try { interceptor.execute(dummyInvoker, service, m, new Object[] {"myId"}); } catch (IllegalStateException ex) { assertTrue(ex.getMessage().contains("Cache could not have been resolved for")); } catch (Throwable ex) { fail("Unexpected: " + ex); } }
@Test public void cacheResultReturnsProperType() throws Throwable { JCacheInterceptor interceptor = createInterceptor(createOperationSource( cacheManager, defaultCacheResolver, defaultExceptionCacheResolver, defaultKeyGenerator)); AnnotatedJCacheableService service = new AnnotatedJCacheableService(cacheManager.getCache("default")); Method method = ReflectionUtils.findMethod(AnnotatedJCacheableService.class, "cache", String.class); CacheOperationInvoker invoker = new DummyInvoker(0L); Object execute = interceptor.execute(invoker, service, method, new Object[] {"myId"}); assertNotNull("result cannot be null.", execute); assertEquals("Wrong result type", Long.class, execute.getClass()); assertEquals("Wrong result", 0L, execute); }
@Override public Object invoke(final MethodInvocation invocation) throws Throwable { Method method = invocation.getMethod(); CacheOperationInvoker aopAllianceInvoker = new CacheOperationInvoker() { @Override public Object invoke() { try { return invocation.proceed(); } catch (Throwable ex) { throw new ThrowableWrapper(ex); } } }; try { return execute(aopAllianceInvoker, invocation.getThis(), method, invocation.getArguments()); } catch (CacheOperationInvoker.ThrowableWrapper th) { throw th.getOriginal(); } }