Commit 9c3b0fb835cec9f362d07a38002d3b6934356516
Exists in
master
Merge branch '2.4.0' of git@github.com:demoiselle/framework.git into 2.4.0
Showing
9 changed files
with
204 additions
and
10 deletions
Show diff stats
impl/core/src/main/java/br/gov/frameworkdemoiselle/exception/ExceptionHandlerInterceptor.java
... | ... | @@ -39,7 +39,10 @@ package br.gov.frameworkdemoiselle.exception; |
39 | 39 | import java.io.Serializable; |
40 | 40 | import java.lang.reflect.InvocationTargetException; |
41 | 41 | import java.lang.reflect.Method; |
42 | +import java.util.ArrayList; | |
43 | +import java.util.Arrays; | |
42 | 44 | import java.util.HashMap; |
45 | +import java.util.List; | |
43 | 46 | import java.util.Map; |
44 | 47 | |
45 | 48 | import javax.interceptor.AroundInvoke; |
... | ... | @@ -70,7 +73,7 @@ public class ExceptionHandlerInterceptor implements Serializable { |
70 | 73 | getLogger().info(getBundle().getString("handling-exception", cause.getClass().getCanonicalName())); |
71 | 74 | |
72 | 75 | boolean handled = false; |
73 | - Class<?> type = target.getClass().getSuperclass(); | |
76 | + Class<?> type = target.getClass(); | |
74 | 77 | |
75 | 78 | if (!isLoaded(type)) { |
76 | 79 | loadHandlers(type); |
... | ... | @@ -115,7 +118,14 @@ public class ExceptionHandlerInterceptor implements Serializable { |
115 | 118 | */ |
116 | 119 | private void loadHandlers(final Class<?> type) { |
117 | 120 | Map<Class<?>, Method> mapHandlers = new HashMap<Class<?>, Method>(); |
118 | - Method[] methods = type.getMethods(); | |
121 | + | |
122 | + List<Method> methods = new ArrayList<Method>(); | |
123 | + Class<?> tempType = type; | |
124 | + | |
125 | + while (tempType != null) { | |
126 | + methods.addAll(Arrays.asList(tempType.getMethods())); | |
127 | + tempType = tempType.getSuperclass(); | |
128 | + } | |
119 | 129 | |
120 | 130 | for (Method method : methods) { |
121 | 131 | if (method.isAnnotationPresent(ExceptionHandler.class)) { |
... | ... | @@ -123,6 +133,7 @@ public class ExceptionHandlerInterceptor implements Serializable { |
123 | 133 | mapHandlers.put(method.getParameterTypes()[0], method); |
124 | 134 | } |
125 | 135 | } |
136 | + | |
126 | 137 | cache.put(type, mapHandlers); |
127 | 138 | } |
128 | 139 | ... | ... |
... | ... | @@ -0,0 +1,11 @@ |
1 | +package exception; | |
2 | + | |
3 | +import br.gov.frameworkdemoiselle.exception.ApplicationException; | |
4 | + | |
5 | + | |
6 | +@ApplicationException | |
7 | +public class CustomException extends RuntimeException { | |
8 | + | |
9 | + private static final long serialVersionUID = 1L; | |
10 | + | |
11 | +} | |
0 | 12 | \ No newline at end of file | ... | ... |
impl/core/src/test/java/exception/CustomExceptionHandler.java
0 → 100644
... | ... | @@ -0,0 +1,27 @@ |
1 | +package exception; | |
2 | + | |
3 | +import br.gov.frameworkdemoiselle.exception.ExceptionHandler; | |
4 | +import br.gov.frameworkdemoiselle.stereotype.Controller; | |
5 | + | |
6 | +@Controller | |
7 | +public class CustomExceptionHandler { | |
8 | + | |
9 | + private boolean exceptionHandler = false; | |
10 | + | |
11 | + public boolean isExceptionHandler() { | |
12 | + return exceptionHandler; | |
13 | + } | |
14 | + | |
15 | + public void setExceptionHandler(boolean exceptionHandler) { | |
16 | + this.exceptionHandler = exceptionHandler; | |
17 | + } | |
18 | + | |
19 | + public void throwExceptionWithMessage() { | |
20 | + throw new CustomException(); | |
21 | + } | |
22 | + | |
23 | + @ExceptionHandler | |
24 | + public void handler(CustomException exception) { | |
25 | + setExceptionHandler(true); | |
26 | + } | |
27 | +} | ... | ... |
impl/core/src/test/java/exception/CustomExceptionTest.java
0 → 100644
... | ... | @@ -0,0 +1,32 @@ |
1 | +package exception; | |
2 | + | |
3 | +import static junit.framework.Assert.assertEquals; | |
4 | + | |
5 | +import javax.inject.Inject; | |
6 | + | |
7 | +import org.jboss.arquillian.container.test.api.Deployment; | |
8 | +import org.jboss.arquillian.junit.Arquillian; | |
9 | +import org.jboss.shrinkwrap.api.spec.JavaArchive; | |
10 | +import org.junit.Test; | |
11 | +import org.junit.runner.RunWith; | |
12 | + | |
13 | +import test.Tests; | |
14 | + | |
15 | +@RunWith(Arquillian.class) | |
16 | +public class CustomExceptionTest { | |
17 | + | |
18 | + @Inject | |
19 | + private CustomExceptionHandler exception; | |
20 | + | |
21 | + @Deployment | |
22 | + public static JavaArchive createDeployment() { | |
23 | + JavaArchive deployment = Tests.createDeployment(CustomExceptionTest.class); | |
24 | + return deployment; | |
25 | + } | |
26 | + | |
27 | + @Test | |
28 | + public void testCustomExceptionHandler() { | |
29 | + exception.throwExceptionWithMessage(); | |
30 | + assertEquals(true, exception.isExceptionHandler()); | |
31 | + } | |
32 | +} | ... | ... |
impl/core/src/test/java/exception/ExceptionInheritance.java
0 → 100644
... | ... | @@ -0,0 +1,39 @@ |
1 | +package exception; | |
2 | + | |
3 | +import br.gov.frameworkdemoiselle.exception.ExceptionHandler; | |
4 | +import br.gov.frameworkdemoiselle.stereotype.Controller; | |
5 | + | |
6 | +@Controller | |
7 | +public class ExceptionInheritance { | |
8 | + | |
9 | + private boolean handlerSuperClass = false; | |
10 | + | |
11 | + private boolean handlerClass = false; | |
12 | + | |
13 | + public boolean isHandlerSuperClass() { | |
14 | + return handlerSuperClass; | |
15 | + } | |
16 | + | |
17 | + public boolean isHandlerClass() { | |
18 | + return handlerClass; | |
19 | + } | |
20 | + | |
21 | + public void throwNullPointerException() { | |
22 | + throw new NullPointerException(); | |
23 | + } | |
24 | + | |
25 | + public void throwArithmeticException() { | |
26 | + throw new ArithmeticException(); | |
27 | + } | |
28 | + | |
29 | + @ExceptionHandler | |
30 | + public void handle(ArithmeticException e) { | |
31 | + handlerClass = true; | |
32 | + } | |
33 | + | |
34 | + @ExceptionHandler | |
35 | + public void handle(RuntimeException e) { | |
36 | + handlerSuperClass = true; | |
37 | + } | |
38 | + | |
39 | +} | ... | ... |
impl/core/src/test/java/exception/ExceptionInheritanceTest.java
0 → 100644
... | ... | @@ -0,0 +1,39 @@ |
1 | +package exception; | |
2 | + | |
3 | +import static junit.framework.Assert.assertEquals; | |
4 | + | |
5 | +import javax.inject.Inject; | |
6 | + | |
7 | +import org.jboss.arquillian.container.test.api.Deployment; | |
8 | +import org.jboss.arquillian.junit.Arquillian; | |
9 | +import org.jboss.shrinkwrap.api.spec.JavaArchive; | |
10 | +import org.junit.Test; | |
11 | +import org.junit.runner.RunWith; | |
12 | + | |
13 | +import test.Tests; | |
14 | + | |
15 | +@RunWith(Arquillian.class) | |
16 | +public class ExceptionInheritanceTest { | |
17 | + | |
18 | + @Inject | |
19 | + private ExceptionInheritance exceptionInheritance; | |
20 | + | |
21 | + @Deployment | |
22 | + public static JavaArchive createDeployment() { | |
23 | + JavaArchive deployment = Tests.createDeployment(ExceptionInheritanceTest.class); | |
24 | + return deployment; | |
25 | + } | |
26 | + | |
27 | + @Test | |
28 | + public void testExceptionInheritanceSuperClass() { | |
29 | + exceptionInheritance.throwNullPointerException(); | |
30 | + assertEquals(true, exceptionInheritance.isHandlerSuperClass()); | |
31 | + } | |
32 | + | |
33 | + @Test | |
34 | + public void testExceptionInheritanceClass() { | |
35 | + exceptionInheritance.throwArithmeticException(); | |
36 | + assertEquals(false, exceptionInheritance.isHandlerSuperClass()); | |
37 | + assertEquals(true, exceptionInheritance.isHandlerClass()); | |
38 | + } | |
39 | +} | ... | ... |
impl/core/src/test/java/exception/MultiException.java
1 | 1 | package exception; |
2 | 2 | |
3 | +import java.awt.geom.IllegalPathStateException; | |
4 | + | |
3 | 5 | import br.gov.frameworkdemoiselle.exception.ExceptionHandler; |
4 | 6 | import br.gov.frameworkdemoiselle.stereotype.Controller; |
5 | 7 | |
... | ... | @@ -9,7 +11,7 @@ public class MultiException { |
9 | 11 | private boolean nullPointerExceptionHandler = false; |
10 | 12 | |
11 | 13 | private boolean arithmeticExceptionHandler = false; |
12 | - | |
14 | + | |
13 | 15 | public boolean isNullPointerExceptionHandler() { |
14 | 16 | return nullPointerExceptionHandler; |
15 | 17 | } |
... | ... | @@ -17,15 +19,15 @@ public class MultiException { |
17 | 19 | public boolean isArithmeticExceptionHandler() { |
18 | 20 | return arithmeticExceptionHandler; |
19 | 21 | } |
20 | - | |
21 | - public void throwExceptionNullPointer() { | |
22 | + | |
23 | + public void throwNullPointerException() { | |
22 | 24 | throw new NullPointerException(); |
23 | 25 | } |
24 | 26 | |
25 | - public void throwExceptionArithmetic() { | |
27 | + public void throwArithmeticException() { | |
26 | 28 | throw new ArithmeticException(); |
27 | 29 | } |
28 | - | |
30 | + | |
29 | 31 | @SuppressWarnings({ "null", "unused" }) |
30 | 32 | public void throwTwoException() { |
31 | 33 | String txt = null; |
... | ... | @@ -41,5 +43,5 @@ public class MultiException { |
41 | 43 | @ExceptionHandler |
42 | 44 | public void handler(ArithmeticException cause) { |
43 | 45 | arithmeticExceptionHandler = true; |
44 | - } | |
46 | + } | |
45 | 47 | } | ... | ... |
impl/core/src/test/java/exception/MultiExceptionOneHandler.java
0 → 100644
... | ... | @@ -0,0 +1,18 @@ |
1 | +package exception; | |
2 | + | |
3 | +import java.awt.geom.IllegalPathStateException; | |
4 | + | |
5 | +import br.gov.frameworkdemoiselle.exception.ExceptionHandler; | |
6 | +import br.gov.frameworkdemoiselle.stereotype.Controller; | |
7 | + | |
8 | +@Controller | |
9 | +public class MultiExceptionOneHandler { | |
10 | + | |
11 | + public void throwIllegalPathException() { | |
12 | + throw new IllegalPathStateException(); | |
13 | + } | |
14 | + | |
15 | + @ExceptionHandler | |
16 | + public void handler(IllegalPathStateException cause, IllegalStateException cause2) { | |
17 | + } | |
18 | +} | ... | ... |
impl/core/src/test/java/exception/MultiExceptionTest.java
... | ... | @@ -11,6 +11,8 @@ import org.jboss.shrinkwrap.api.spec.JavaArchive; |
11 | 11 | import org.junit.Test; |
12 | 12 | import org.junit.runner.RunWith; |
13 | 13 | |
14 | +import br.gov.frameworkdemoiselle.DemoiselleException; | |
15 | + | |
14 | 16 | import test.Tests; |
15 | 17 | |
16 | 18 | @RunWith(Arquillian.class) |
... | ... | @@ -18,6 +20,9 @@ public class MultiExceptionTest { |
18 | 20 | |
19 | 21 | @Inject |
20 | 22 | private MultiException multiException; |
23 | + | |
24 | + @Inject | |
25 | + private MultiExceptionOneHandler multiExceptionOneHandler; | |
21 | 26 | |
22 | 27 | @Deployment |
23 | 28 | public static JavaArchive createDeployment() { |
... | ... | @@ -27,13 +32,13 @@ public class MultiExceptionTest { |
27 | 32 | |
28 | 33 | @Test |
29 | 34 | public void testNullPointerExceptionHandler() { |
30 | - multiException.throwExceptionNullPointer(); | |
35 | + multiException.throwNullPointerException(); | |
31 | 36 | assertEquals(true, multiException.isNullPointerExceptionHandler()); |
32 | 37 | } |
33 | 38 | |
34 | 39 | @Test |
35 | 40 | public void testArithmeticExceptionHandler() { |
36 | - multiException.throwExceptionArithmetic(); | |
41 | + multiException.throwArithmeticException(); | |
37 | 42 | assertEquals(true, multiException.isArithmeticExceptionHandler()); |
38 | 43 | } |
39 | 44 | |
... | ... | @@ -43,4 +48,14 @@ public class MultiExceptionTest { |
43 | 48 | assertEquals(true, multiException.isNullPointerExceptionHandler()); |
44 | 49 | assertEquals(true, multiException.isArithmeticExceptionHandler()); |
45 | 50 | } |
51 | + | |
52 | + @Test | |
53 | + public void testExceptionHandlerWithTwoException() { | |
54 | + try { | |
55 | + multiExceptionOneHandler.throwIllegalPathException(); | |
56 | + fail(); | |
57 | + } catch (Exception e) { | |
58 | + assertEquals(DemoiselleException.class, e.getClass()); | |
59 | + } | |
60 | + } | |
46 | 61 | } | ... | ... |