Commit c20d0c4b06bc45ca0608873fbc4009aa5be1406e
1 parent
22ab66e0
Exists in
master
Testes de Exceptions
Showing
8 changed files
with
353 additions
and
1 deletions
Show diff stats
... | ... | @@ -0,0 +1,45 @@ |
1 | +package exception; | |
2 | + | |
3 | +import br.gov.frameworkdemoiselle.exception.ExceptionHandler; | |
4 | +import br.gov.frameworkdemoiselle.stereotype.Controller; | |
5 | + | |
6 | +@Controller | |
7 | +public class MultiException { | |
8 | + | |
9 | + private boolean nullPointerExceptionHandler = false; | |
10 | + | |
11 | + private boolean arithmeticExceptionHandler = false; | |
12 | + | |
13 | + public boolean isNullPointerExceptionHandler() { | |
14 | + return nullPointerExceptionHandler; | |
15 | + } | |
16 | + | |
17 | + public boolean isArithmeticExceptionHandler() { | |
18 | + return arithmeticExceptionHandler; | |
19 | + } | |
20 | + | |
21 | + public void throwExceptionNullPointer() { | |
22 | + throw new NullPointerException(); | |
23 | + } | |
24 | + | |
25 | + public void throwExceptionArithmetic() { | |
26 | + throw new ArithmeticException(); | |
27 | + } | |
28 | + | |
29 | + @SuppressWarnings({ "null", "unused" }) | |
30 | + public void throwTwoException() { | |
31 | + String txt = null; | |
32 | + txt.toString(); | |
33 | + int result = 4 / 0; | |
34 | + } | |
35 | + | |
36 | + @ExceptionHandler | |
37 | + public void handler(NullPointerException cause) { | |
38 | + nullPointerExceptionHandler = true; | |
39 | + } | |
40 | + | |
41 | + @ExceptionHandler | |
42 | + public void handler(ArithmeticException cause) { | |
43 | + arithmeticExceptionHandler = true; | |
44 | + } | |
45 | +} | ... | ... |
impl/core/src/test/java/exception/MultiExceptionTest.java
0 → 100644
... | ... | @@ -0,0 +1,46 @@ |
1 | +package exception; | |
2 | + | |
3 | +import javax.inject.Inject; | |
4 | + | |
5 | +import static junit.framework.Assert.fail; | |
6 | +import static junit.framework.Assert.assertEquals; | |
7 | + | |
8 | +import org.jboss.arquillian.container.test.api.Deployment; | |
9 | +import org.jboss.arquillian.junit.Arquillian; | |
10 | +import org.jboss.shrinkwrap.api.spec.JavaArchive; | |
11 | +import org.junit.Test; | |
12 | +import org.junit.runner.RunWith; | |
13 | + | |
14 | +import test.Tests; | |
15 | + | |
16 | +@RunWith(Arquillian.class) | |
17 | +public class MultiExceptionTest { | |
18 | + | |
19 | + @Inject | |
20 | + private MultiException multiException; | |
21 | + | |
22 | + @Deployment | |
23 | + public static JavaArchive createDeployment() { | |
24 | + JavaArchive deployment = Tests.createDeployment(MultiExceptionTest.class); | |
25 | + return deployment; | |
26 | + } | |
27 | + | |
28 | + @Test | |
29 | + public void testNullPointerExceptionHandler() { | |
30 | + multiException.throwExceptionNullPointer(); | |
31 | + assertEquals(true, multiException.isNullPointerExceptionHandler()); | |
32 | + } | |
33 | + | |
34 | + @Test | |
35 | + public void testArithmeticExceptionHandler() { | |
36 | + multiException.throwExceptionArithmetic(); | |
37 | + assertEquals(true, multiException.isArithmeticExceptionHandler()); | |
38 | + } | |
39 | + | |
40 | + @Test | |
41 | + public void testMultiExceptionHandler() { | |
42 | + multiException.throwTwoException(); | |
43 | + assertEquals(true, multiException.isNullPointerExceptionHandler()); | |
44 | + assertEquals(true, multiException.isArithmeticExceptionHandler()); | |
45 | + } | |
46 | +} | ... | ... |
impl/core/src/test/java/exception/MultiStrategyExceptionHandler.java
0 → 100644
... | ... | @@ -0,0 +1,70 @@ |
1 | +package exception; | |
2 | + | |
3 | +import br.gov.frameworkdemoiselle.exception.ExceptionHandler; | |
4 | +import br.gov.frameworkdemoiselle.stereotype.Controller; | |
5 | + | |
6 | +@Controller | |
7 | +public class MultiStrategyExceptionHandler { | |
8 | + | |
9 | + private boolean exceptionHandler = false; | |
10 | + | |
11 | + private boolean exceptionTryCacth = false; | |
12 | + | |
13 | + String txt = null; | |
14 | + | |
15 | + public boolean isExceptionHandler() { | |
16 | + return exceptionHandler; | |
17 | + } | |
18 | + | |
19 | + public boolean isExceptionTryCacth() { | |
20 | + return exceptionTryCacth; | |
21 | + } | |
22 | + | |
23 | + @SuppressWarnings("unused") | |
24 | + public void exceptionMultiStrategyTryAndHandler() { | |
25 | + try { | |
26 | + int result = 4 / 0; | |
27 | + } catch (ArithmeticException e) { | |
28 | + exceptionTryCacth = true; | |
29 | + } | |
30 | + txt.toString(); | |
31 | + } | |
32 | + | |
33 | + @SuppressWarnings("unused") | |
34 | + public void exceptionMultiStrategyHandlerInTry() { | |
35 | + try { | |
36 | + txt.toString(); | |
37 | + int result = 4 / 0; | |
38 | + } catch (ArithmeticException e) { | |
39 | + exceptionTryCacth = true; | |
40 | + } | |
41 | + } | |
42 | + | |
43 | + @SuppressWarnings("unused") | |
44 | + public void exceptionMultiStrategyHandlerAndTry() { | |
45 | + txt.toString(); | |
46 | + try { | |
47 | + int result = 4 / 0; | |
48 | + } catch (ArithmeticException e) { | |
49 | + exceptionTryCacth = true; | |
50 | + } | |
51 | + } | |
52 | + | |
53 | + public void exceptionTwoHandler() { | |
54 | + try { | |
55 | + txt.toString(); | |
56 | + } catch (NullPointerException e) { | |
57 | + exceptionTryCacth = true; | |
58 | + } | |
59 | + } | |
60 | + | |
61 | + public void exceptionHandler() { | |
62 | + txt.toString(); | |
63 | + } | |
64 | + | |
65 | + @ExceptionHandler | |
66 | + public void handler(NullPointerException cause) { | |
67 | + exceptionHandler = true; | |
68 | + } | |
69 | + | |
70 | +} | ... | ... |
impl/core/src/test/java/exception/MultiStrategyExceptionHandlerTest.java
0 → 100644
... | ... | @@ -0,0 +1,61 @@ |
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 MultiStrategyExceptionHandlerTest { | |
17 | + | |
18 | + @Inject | |
19 | + private MultiStrategyExceptionHandler handlerTest; | |
20 | + | |
21 | + @Deployment | |
22 | + public static JavaArchive createDeployment() { | |
23 | + JavaArchive deployment = Tests.createDeployment(OneExceptionTest.class); | |
24 | + return deployment; | |
25 | + } | |
26 | + | |
27 | + @Test | |
28 | + public void testExceptionMultiStrategyTryAndHandler() { | |
29 | + handlerTest.exceptionMultiStrategyTryAndHandler(); | |
30 | + assertEquals(true, handlerTest.isExceptionTryCacth()); | |
31 | + assertEquals(true, handlerTest.isExceptionHandler()); | |
32 | + } | |
33 | + | |
34 | + @Test | |
35 | + public void testExceptionMultiStrategyHandlerInTry() { | |
36 | + handlerTest.exceptionMultiStrategyHandlerInTry(); | |
37 | + assertEquals(true, handlerTest.isExceptionTryCacth()); | |
38 | + assertEquals(true, handlerTest.isExceptionHandler()); | |
39 | + } | |
40 | + | |
41 | + @Test | |
42 | + public void testExceptionMultiStrategyHandlerAndTry() { | |
43 | + handlerTest.exceptionMultiStrategyHandlerAndTry(); | |
44 | + assertEquals(true, handlerTest.isExceptionTryCacth()); | |
45 | + assertEquals(true, handlerTest.isExceptionHandler()); | |
46 | + } | |
47 | + | |
48 | + @Test | |
49 | + public void testSameExceptionTwoStrategyHandler() { | |
50 | + handlerTest.exceptionTwoHandler(); | |
51 | + assertEquals(true, handlerTest.isExceptionTryCacth()); | |
52 | + assertEquals(false, handlerTest.isExceptionHandler()); | |
53 | + } | |
54 | + | |
55 | + @Test | |
56 | + public void testExceptionOneStrategyHandler() { | |
57 | + handlerTest.exceptionHandler(); | |
58 | + assertEquals(false, handlerTest.isExceptionTryCacth()); | |
59 | + assertEquals(true, handlerTest.isExceptionHandler()); | |
60 | + } | |
61 | +} | ... | ... |
... | ... | @@ -0,0 +1,68 @@ |
1 | +package exception; | |
2 | + | |
3 | +import br.gov.frameworkdemoiselle.exception.ExceptionHandler; | |
4 | +import br.gov.frameworkdemoiselle.stereotype.Controller; | |
5 | + | |
6 | +@Controller | |
7 | +public class OneException { | |
8 | + | |
9 | + private boolean exceptionHandler = false; | |
10 | + | |
11 | + private boolean exceptionHandlerIllegalArgument1 = false; | |
12 | + | |
13 | + private boolean exceptionHandlerIllegalArgument2 = false; | |
14 | + | |
15 | + private boolean exceptionHandlerIllegalArgument3 = false; | |
16 | + | |
17 | + public boolean isExceptionHandler() { | |
18 | + return exceptionHandler; | |
19 | + } | |
20 | + | |
21 | + public boolean isExceptionHandlerIllegalArgument1() { | |
22 | + return exceptionHandlerIllegalArgument1; | |
23 | + } | |
24 | + | |
25 | + public boolean isExceptionHandlerIllegalArgument2() { | |
26 | + return exceptionHandlerIllegalArgument2; | |
27 | + } | |
28 | + | |
29 | + public boolean isExceptionHandlerIllegalArgument3() { | |
30 | + return exceptionHandlerIllegalArgument3; | |
31 | + } | |
32 | + | |
33 | + @SuppressWarnings("null") | |
34 | + public void throwExceptionWithHandler() { | |
35 | + String txt = null; | |
36 | + txt.toString(); | |
37 | + } | |
38 | + | |
39 | + @SuppressWarnings("unused") | |
40 | + public void throwExceptionWithoutHandler() { | |
41 | + int result = 4/0; | |
42 | + } | |
43 | + | |
44 | + public void throwExceptionIllegalArgument() { | |
45 | + throw new IllegalArgumentException(); | |
46 | + } | |
47 | + | |
48 | + @ExceptionHandler | |
49 | + public void handler(NullPointerException cause) { | |
50 | + exceptionHandler = true; | |
51 | + } | |
52 | + | |
53 | + | |
54 | + @ExceptionHandler | |
55 | + public void handler1(IllegalArgumentException cause) { | |
56 | + exceptionHandlerIllegalArgument1 = true; | |
57 | + } | |
58 | + | |
59 | + @ExceptionHandler | |
60 | + public void handler3(IllegalArgumentException cause) { | |
61 | + exceptionHandlerIllegalArgument3 = true; | |
62 | + } | |
63 | + | |
64 | + @ExceptionHandler | |
65 | + public void handler2(IllegalArgumentException cause) { | |
66 | + exceptionHandlerIllegalArgument2 = true; | |
67 | + } | |
68 | +} | ... | ... |
... | ... | @@ -0,0 +1,51 @@ |
1 | +package exception; | |
2 | + | |
3 | +import javax.inject.Inject; | |
4 | + | |
5 | +import static junit.framework.Assert.fail; | |
6 | +import static junit.framework.Assert.assertEquals; | |
7 | + | |
8 | +import org.jboss.arquillian.container.test.api.Deployment; | |
9 | +import org.jboss.arquillian.junit.Arquillian; | |
10 | +import org.jboss.shrinkwrap.api.spec.JavaArchive; | |
11 | +import org.junit.Test; | |
12 | +import org.junit.runner.RunWith; | |
13 | + | |
14 | +import test.Tests; | |
15 | + | |
16 | +@RunWith(Arquillian.class) | |
17 | +public class OneExceptionTest { | |
18 | + | |
19 | + @Inject | |
20 | + private OneException oneException; | |
21 | + | |
22 | + @Deployment | |
23 | + public static JavaArchive createDeployment() { | |
24 | + JavaArchive deployment = Tests.createDeployment(OneExceptionTest.class); | |
25 | + return deployment; | |
26 | + } | |
27 | + | |
28 | + @Test | |
29 | + public void testExceptionWithHandler() { | |
30 | + oneException.throwExceptionWithHandler(); | |
31 | + assertEquals(true, oneException.isExceptionHandler()); | |
32 | + } | |
33 | + | |
34 | + @Test | |
35 | + public void testExceptionWithoutHandler() { | |
36 | + try { | |
37 | + oneException.throwExceptionWithoutHandler(); | |
38 | + fail(); | |
39 | + } catch (Exception cause) { | |
40 | + assertEquals(ArithmeticException.class, cause.getClass()); | |
41 | + } | |
42 | + } | |
43 | + | |
44 | + @Test | |
45 | + public void testExceptionWithMultiHandler() { | |
46 | + oneException.throwExceptionIllegalArgument(); | |
47 | + assertEquals(false, oneException.isExceptionHandlerIllegalArgument1()); | |
48 | + assertEquals(true, oneException.isExceptionHandlerIllegalArgument2()); | |
49 | + assertEquals(false, oneException.isExceptionHandlerIllegalArgument3()); | |
50 | + } | |
51 | +} | ... | ... |
impl/core/src/test/java/test/Tests.java
... | ... | @@ -57,7 +57,7 @@ public final class Tests { |
57 | 57 | .create(JavaArchive.class) |
58 | 58 | .addClass(LocaleProducer.class) |
59 | 59 | .addPackages(true, "br") |
60 | - .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml") | |
60 | + .addAsResource(Tests.createFileAsset("src/test/resources/test/beans.xml"), "beans.xml") | |
61 | 61 | .addAsManifestResource( |
62 | 62 | new File("src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension"), |
63 | 63 | "services/javax.enterprise.inject.spi.Extension"); | ... | ... |
... | ... | @@ -0,0 +1,11 @@ |
1 | +<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
2 | + xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"> | |
3 | + | |
4 | + <interceptors> | |
5 | + <class>br.gov.frameworkdemoiselle.exception.ExceptionHandlerInterceptor</class> | |
6 | + <class>br.gov.frameworkdemoiselle.internal.interceptor.RequiredPermissionInterceptor</class> | |
7 | + <class>br.gov.frameworkdemoiselle.internal.interceptor.RequiredRoleInterceptor</class> | |
8 | + <class>br.gov.frameworkdemoiselle.internal.interceptor.TransactionalInterceptor</class> | |
9 | + </interceptors> | |
10 | + | |
11 | +</beans> | ... | ... |