+ refactor a bit the internals of CacheAspect to allow invocations that do not throw any exceptions (AspectJ)
This commit is contained in:
Costin Leau
2011-09-02 15:37:42 +00:00
parent 91251812b1
commit d9de19d7b3
13 changed files with 180 additions and 58 deletions

View File

@@ -24,6 +24,7 @@ import org.aspectj.lang.annotation.SuppressAjWarnings;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.cache.interceptor.CacheAspectSupport;
import org.springframework.cache.interceptor.CacheOperationSource;
import org.springframework.cache.interceptor.CacheAspectSupport.Invoker;
/**
* Abstract superaspect for AspectJ cache aspects. Concrete
@@ -60,17 +61,13 @@ public abstract aspect AbstractCacheAspect extends CacheAspectSupport {
MethodSignature methodSignature = (MethodSignature) thisJoinPoint.getSignature();
Method method = methodSignature.getMethod();
Callable<Object> ajInvocation = new Callable<Object>() {
public Object call() {
Invoker aspectJInvoker = new Invoker() {
public Object invoke() {
return proceed(cachedObject);
}
};
try{
return execute(ajInvocation, thisJoinPoint.getTarget(), method, thisJoinPoint.getArgs());
} catch (Exception ex){
throw new RuntimeException("Cannot cache target ", ex);
}
return execute(aspectJInvoker, thisJoinPoint.getTarget(), method, thisJoinPoint.getArgs());
}
/**

View File

@@ -16,8 +16,9 @@
package org.springframework.cache.aspectj;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.*;
import java.util.UUID;
import org.junit.Before;
import org.junit.Test;
@@ -97,6 +98,26 @@ public abstract class AbstractAnnotationTest {
assertNotSame(r3, r4);
}
public void testCheckedThrowable(CacheableService service) throws Exception {
String arg = UUID.randomUUID().toString();
try {
service.throwChecked(arg);
fail("Excepted exception");
} catch (Exception ex) {
assertEquals(arg, ex.getMessage());
}
}
public void testUncheckedThrowable(CacheableService service) throws Exception {
try {
service.throwUnchecked(Long.valueOf(1));
fail("Excepted exception");
} catch (RuntimeException ex) {
assertTrue(ex instanceof UnsupportedOperationException);
// expected
}
}
@Test
public void testCacheable() throws Exception {
testCacheable(cs);
@@ -127,4 +148,24 @@ public abstract class AbstractAnnotationTest {
testInvalidate(ccs);
}
@Test
public void testCheckedException() throws Exception {
testCheckedThrowable(cs);
}
@Test
public void testClassCheckedException() throws Exception {
testCheckedThrowable(ccs);
}
@Test
public void testUncheckedException() throws Exception {
testUncheckedThrowable(cs);
}
@Test
public void testClassUncheckedException() throws Exception {
testUncheckedThrowable(ccs);
}
}

View File

@@ -1,29 +0,0 @@
/*
* Copyright 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cache.aspectj;
/**
* @author Costin Leau
*/
public class AspectJAnnotationTest extends AbstractAnnotationTest {
@Override
protected String getConfig() {
return "/org/springframework/cache/config/annotation-cache-aspectj.xml";
}
}

View File

@@ -45,4 +45,12 @@ public class AnnotatedClassCacheableService implements CacheableService {
public Object key(Object arg1, Object arg2) {
return counter.getAndIncrement();
}
public Long throwChecked(Object arg1) throws Exception {
throw new UnsupportedOperationException(arg1.toString());
}
public Long throwUnchecked(Object arg1) {
throw new UnsupportedOperationException();
}
}

View File

@@ -16,6 +16,7 @@
package org.springframework.cache.config;
/**
* Basic service interface.
*
@@ -31,4 +32,7 @@ public interface CacheableService<T> {
T key(Object arg1, Object arg2);
T throwChecked(Object arg1) throws Exception;
T throwUnchecked(Object arg1);
}

View File

@@ -49,4 +49,14 @@ public class DefaultCacheableService implements CacheableService<Long> {
public Long key(Object arg1, Object arg2) {
return counter.getAndIncrement();
}
@Cacheable("default")
public Long throwChecked(Object arg1) throws Exception {
throw new Exception(arg1.toString());
}
@Cacheable("default")
public Long throwUnchecked(Object arg1) {
throw new UnsupportedOperationException(arg1.toString());
}
}