Commit 93fd874cf949df242bb09a001803506232742b82
Exists in
master
Merge branch '2.4.0' of git@github.com:demoiselle/framework.git into 2.4.0
Showing
3 changed files
with
29 additions
and
8 deletions
Show diff stats
impl/core/src/main/java/br/gov/frameworkdemoiselle/exception/ExceptionHandlerInterceptor.java
| ... | ... | @@ -79,7 +79,7 @@ public class ExceptionHandlerInterceptor implements Serializable { |
| 79 | 79 | loadHandlers(type); |
| 80 | 80 | } |
| 81 | 81 | |
| 82 | - Method handler = getMethod(type, cause); | |
| 82 | + Method handler = getMethod(type, cause.getClass()); | |
| 83 | 83 | if (handler != null) { |
| 84 | 84 | invoke(handler, target, cause); |
| 85 | 85 | handled = true; |
| ... | ... | @@ -103,19 +103,26 @@ public class ExceptionHandlerInterceptor implements Serializable { |
| 103 | 103 | } |
| 104 | 104 | |
| 105 | 105 | /** |
| 106 | - * If there is an handler in the current class for the expected exception, then this method will be returned; Else | |
| 106 | + * If there is an handler in the current class or superClass for the expected exception, then this method will be returned; Else | |
| 107 | 107 | * returns null; |
| 108 | 108 | * |
| 109 | 109 | * @param type |
| 110 | - * @param cause | |
| 110 | + * @param causeClass | |
| 111 | 111 | * @return |
| 112 | 112 | */ |
| 113 | - private final Method getMethod(final Class<?> type, final Exception cause) { | |
| 113 | + private final Method getMethod(final Class<?> type, final Class<?> causeClass) { | |
| 114 | 114 | Method handler = null; |
| 115 | 115 | |
| 116 | - if (cache.containsKey(type) && cache.get(type).containsKey(cause.getClass())) { | |
| 117 | - handler = cache.get(type).get(cause.getClass()); | |
| 118 | - } | |
| 116 | + if (cache.containsKey(type) ){ | |
| 117 | + Map<Class<?>, Method> map = cache.get(type); | |
| 118 | + if(Throwable.class.isAssignableFrom(causeClass)){ | |
| 119 | + if(map.containsKey(causeClass)){ | |
| 120 | + handler = map.get(causeClass); | |
| 121 | + }else{ | |
| 122 | + handler = getMethod(type, causeClass.getSuperclass()); | |
| 123 | + } | |
| 124 | + } | |
| 125 | + } | |
| 119 | 126 | |
| 120 | 127 | return handler; |
| 121 | 128 | } | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/interceptor/ExceptionHandlerInterceptor.java
impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/interceptor/ExceptionHandlerInterceptorTest.java
| ... | ... | @@ -210,6 +210,19 @@ public class ExceptionHandlerInterceptorTest { |
| 210 | 210 | } |
| 211 | 211 | |
| 212 | 212 | @Test |
| 213 | + public void manageWithClassThatContainsParentExceptionHandleMethod() throws Exception { | |
| 214 | + ClassWithMethodsAnnotatedWithExceptionHandler classWithException = new ClassWithMethodsAnnotatedWithExceptionHandler(); | |
| 215 | + expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); | |
| 216 | + expect(this.context.proceed()).andThrow(new DemoiselleException("")); | |
| 217 | + expect(this.coreBootstrap.isAnnotatedType(ClassWithMethodsAnnotatedWithExceptionHandler.class)).andReturn(true); | |
| 218 | + replayAll(this.context, this.coreBootstrap, Beans.class); | |
| 219 | + | |
| 220 | + assertNull(this.interceptor.manage(this.context)); | |
| 221 | + assertEquals(1, classWithException.times); | |
| 222 | + verifyAll(); | |
| 223 | + } | |
| 224 | + | |
| 225 | + @Test | |
| 213 | 226 | public void manageWithClassThatContainsHandleMethodWithDiferentException() throws Exception { |
| 214 | 227 | ClassWithMethodsAnnotatedWithExceptionHandler classWithException = new ClassWithMethodsAnnotatedWithExceptionHandler(); |
| 215 | 228 | expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); |
| ... | ... | @@ -338,5 +351,5 @@ public class ExceptionHandlerInterceptorTest { |
| 338 | 351 | assertEquals(1, testClass.times); |
| 339 | 352 | } |
| 340 | 353 | } |
| 341 | - | |
| 342 | 354 | } |
| 355 | + | ... | ... |