From c20d0c4b06bc45ca0608873fbc4009aa5be1406e Mon Sep 17 00:00:00 2001 From: Ednara Oliveira Date: Thu, 18 Apr 2013 18:21:20 -0300 Subject: [PATCH] Testes de Exceptions --- impl/core/src/test/java/exception/MultiException.java | 45 +++++++++++++++++++++++++++++++++++++++++++++ impl/core/src/test/java/exception/MultiExceptionTest.java | 46 ++++++++++++++++++++++++++++++++++++++++++++++ impl/core/src/test/java/exception/MultiStrategyExceptionHandler.java | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ impl/core/src/test/java/exception/MultiStrategyExceptionHandlerTest.java | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ impl/core/src/test/java/exception/OneException.java | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ impl/core/src/test/java/exception/OneExceptionTest.java | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ impl/core/src/test/java/test/Tests.java | 2 +- impl/core/src/test/resources/test/beans.xml | 11 +++++++++++ 8 files changed, 353 insertions(+), 1 deletion(-) create mode 100644 impl/core/src/test/java/exception/MultiException.java create mode 100644 impl/core/src/test/java/exception/MultiExceptionTest.java create mode 100644 impl/core/src/test/java/exception/MultiStrategyExceptionHandler.java create mode 100644 impl/core/src/test/java/exception/MultiStrategyExceptionHandlerTest.java create mode 100644 impl/core/src/test/java/exception/OneException.java create mode 100644 impl/core/src/test/java/exception/OneExceptionTest.java create mode 100644 impl/core/src/test/resources/test/beans.xml diff --git a/impl/core/src/test/java/exception/MultiException.java b/impl/core/src/test/java/exception/MultiException.java new file mode 100644 index 0000000..ceac771 --- /dev/null +++ b/impl/core/src/test/java/exception/MultiException.java @@ -0,0 +1,45 @@ +package exception; + +import br.gov.frameworkdemoiselle.exception.ExceptionHandler; +import br.gov.frameworkdemoiselle.stereotype.Controller; + +@Controller +public class MultiException { + + private boolean nullPointerExceptionHandler = false; + + private boolean arithmeticExceptionHandler = false; + + public boolean isNullPointerExceptionHandler() { + return nullPointerExceptionHandler; + } + + public boolean isArithmeticExceptionHandler() { + return arithmeticExceptionHandler; + } + + public void throwExceptionNullPointer() { + throw new NullPointerException(); + } + + public void throwExceptionArithmetic() { + throw new ArithmeticException(); + } + + @SuppressWarnings({ "null", "unused" }) + public void throwTwoException() { + String txt = null; + txt.toString(); + int result = 4 / 0; + } + + @ExceptionHandler + public void handler(NullPointerException cause) { + nullPointerExceptionHandler = true; + } + + @ExceptionHandler + public void handler(ArithmeticException cause) { + arithmeticExceptionHandler = true; + } +} diff --git a/impl/core/src/test/java/exception/MultiExceptionTest.java b/impl/core/src/test/java/exception/MultiExceptionTest.java new file mode 100644 index 0000000..5c2728d --- /dev/null +++ b/impl/core/src/test/java/exception/MultiExceptionTest.java @@ -0,0 +1,46 @@ +package exception; + +import javax.inject.Inject; + +import static junit.framework.Assert.fail; +import static junit.framework.Assert.assertEquals; + +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.Test; +import org.junit.runner.RunWith; + +import test.Tests; + +@RunWith(Arquillian.class) +public class MultiExceptionTest { + + @Inject + private MultiException multiException; + + @Deployment + public static JavaArchive createDeployment() { + JavaArchive deployment = Tests.createDeployment(MultiExceptionTest.class); + return deployment; + } + + @Test + public void testNullPointerExceptionHandler() { + multiException.throwExceptionNullPointer(); + assertEquals(true, multiException.isNullPointerExceptionHandler()); + } + + @Test + public void testArithmeticExceptionHandler() { + multiException.throwExceptionArithmetic(); + assertEquals(true, multiException.isArithmeticExceptionHandler()); + } + + @Test + public void testMultiExceptionHandler() { + multiException.throwTwoException(); + assertEquals(true, multiException.isNullPointerExceptionHandler()); + assertEquals(true, multiException.isArithmeticExceptionHandler()); + } +} diff --git a/impl/core/src/test/java/exception/MultiStrategyExceptionHandler.java b/impl/core/src/test/java/exception/MultiStrategyExceptionHandler.java new file mode 100644 index 0000000..0333795 --- /dev/null +++ b/impl/core/src/test/java/exception/MultiStrategyExceptionHandler.java @@ -0,0 +1,70 @@ +package exception; + +import br.gov.frameworkdemoiselle.exception.ExceptionHandler; +import br.gov.frameworkdemoiselle.stereotype.Controller; + +@Controller +public class MultiStrategyExceptionHandler { + + private boolean exceptionHandler = false; + + private boolean exceptionTryCacth = false; + + String txt = null; + + public boolean isExceptionHandler() { + return exceptionHandler; + } + + public boolean isExceptionTryCacth() { + return exceptionTryCacth; + } + + @SuppressWarnings("unused") + public void exceptionMultiStrategyTryAndHandler() { + try { + int result = 4 / 0; + } catch (ArithmeticException e) { + exceptionTryCacth = true; + } + txt.toString(); + } + + @SuppressWarnings("unused") + public void exceptionMultiStrategyHandlerInTry() { + try { + txt.toString(); + int result = 4 / 0; + } catch (ArithmeticException e) { + exceptionTryCacth = true; + } + } + + @SuppressWarnings("unused") + public void exceptionMultiStrategyHandlerAndTry() { + txt.toString(); + try { + int result = 4 / 0; + } catch (ArithmeticException e) { + exceptionTryCacth = true; + } + } + + public void exceptionTwoHandler() { + try { + txt.toString(); + } catch (NullPointerException e) { + exceptionTryCacth = true; + } + } + + public void exceptionHandler() { + txt.toString(); + } + + @ExceptionHandler + public void handler(NullPointerException cause) { + exceptionHandler = true; + } + +} diff --git a/impl/core/src/test/java/exception/MultiStrategyExceptionHandlerTest.java b/impl/core/src/test/java/exception/MultiStrategyExceptionHandlerTest.java new file mode 100644 index 0000000..bc7a05c --- /dev/null +++ b/impl/core/src/test/java/exception/MultiStrategyExceptionHandlerTest.java @@ -0,0 +1,61 @@ +package exception; + +import static junit.framework.Assert.assertEquals; + +import javax.inject.Inject; + +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.Test; +import org.junit.runner.RunWith; + +import test.Tests; + +@RunWith(Arquillian.class) +public class MultiStrategyExceptionHandlerTest { + + @Inject + private MultiStrategyExceptionHandler handlerTest; + + @Deployment + public static JavaArchive createDeployment() { + JavaArchive deployment = Tests.createDeployment(OneExceptionTest.class); + return deployment; + } + + @Test + public void testExceptionMultiStrategyTryAndHandler() { + handlerTest.exceptionMultiStrategyTryAndHandler(); + assertEquals(true, handlerTest.isExceptionTryCacth()); + assertEquals(true, handlerTest.isExceptionHandler()); + } + + @Test + public void testExceptionMultiStrategyHandlerInTry() { + handlerTest.exceptionMultiStrategyHandlerInTry(); + assertEquals(true, handlerTest.isExceptionTryCacth()); + assertEquals(true, handlerTest.isExceptionHandler()); + } + + @Test + public void testExceptionMultiStrategyHandlerAndTry() { + handlerTest.exceptionMultiStrategyHandlerAndTry(); + assertEquals(true, handlerTest.isExceptionTryCacth()); + assertEquals(true, handlerTest.isExceptionHandler()); + } + + @Test + public void testSameExceptionTwoStrategyHandler() { + handlerTest.exceptionTwoHandler(); + assertEquals(true, handlerTest.isExceptionTryCacth()); + assertEquals(false, handlerTest.isExceptionHandler()); + } + + @Test + public void testExceptionOneStrategyHandler() { + handlerTest.exceptionHandler(); + assertEquals(false, handlerTest.isExceptionTryCacth()); + assertEquals(true, handlerTest.isExceptionHandler()); + } +} diff --git a/impl/core/src/test/java/exception/OneException.java b/impl/core/src/test/java/exception/OneException.java new file mode 100644 index 0000000..f784f14 --- /dev/null +++ b/impl/core/src/test/java/exception/OneException.java @@ -0,0 +1,68 @@ +package exception; + +import br.gov.frameworkdemoiselle.exception.ExceptionHandler; +import br.gov.frameworkdemoiselle.stereotype.Controller; + +@Controller +public class OneException { + + private boolean exceptionHandler = false; + + private boolean exceptionHandlerIllegalArgument1 = false; + + private boolean exceptionHandlerIllegalArgument2 = false; + + private boolean exceptionHandlerIllegalArgument3 = false; + + public boolean isExceptionHandler() { + return exceptionHandler; + } + + public boolean isExceptionHandlerIllegalArgument1() { + return exceptionHandlerIllegalArgument1; + } + + public boolean isExceptionHandlerIllegalArgument2() { + return exceptionHandlerIllegalArgument2; + } + + public boolean isExceptionHandlerIllegalArgument3() { + return exceptionHandlerIllegalArgument3; + } + + @SuppressWarnings("null") + public void throwExceptionWithHandler() { + String txt = null; + txt.toString(); + } + + @SuppressWarnings("unused") + public void throwExceptionWithoutHandler() { + int result = 4/0; + } + + public void throwExceptionIllegalArgument() { + throw new IllegalArgumentException(); + } + + @ExceptionHandler + public void handler(NullPointerException cause) { + exceptionHandler = true; + } + + + @ExceptionHandler + public void handler1(IllegalArgumentException cause) { + exceptionHandlerIllegalArgument1 = true; + } + + @ExceptionHandler + public void handler3(IllegalArgumentException cause) { + exceptionHandlerIllegalArgument3 = true; + } + + @ExceptionHandler + public void handler2(IllegalArgumentException cause) { + exceptionHandlerIllegalArgument2 = true; + } +} diff --git a/impl/core/src/test/java/exception/OneExceptionTest.java b/impl/core/src/test/java/exception/OneExceptionTest.java new file mode 100644 index 0000000..b44e7ec --- /dev/null +++ b/impl/core/src/test/java/exception/OneExceptionTest.java @@ -0,0 +1,51 @@ +package exception; + +import javax.inject.Inject; + +import static junit.framework.Assert.fail; +import static junit.framework.Assert.assertEquals; + +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.Test; +import org.junit.runner.RunWith; + +import test.Tests; + +@RunWith(Arquillian.class) +public class OneExceptionTest { + + @Inject + private OneException oneException; + + @Deployment + public static JavaArchive createDeployment() { + JavaArchive deployment = Tests.createDeployment(OneExceptionTest.class); + return deployment; + } + + @Test + public void testExceptionWithHandler() { + oneException.throwExceptionWithHandler(); + assertEquals(true, oneException.isExceptionHandler()); + } + + @Test + public void testExceptionWithoutHandler() { + try { + oneException.throwExceptionWithoutHandler(); + fail(); + } catch (Exception cause) { + assertEquals(ArithmeticException.class, cause.getClass()); + } + } + + @Test + public void testExceptionWithMultiHandler() { + oneException.throwExceptionIllegalArgument(); + assertEquals(false, oneException.isExceptionHandlerIllegalArgument1()); + assertEquals(true, oneException.isExceptionHandlerIllegalArgument2()); + assertEquals(false, oneException.isExceptionHandlerIllegalArgument3()); + } +} diff --git a/impl/core/src/test/java/test/Tests.java b/impl/core/src/test/java/test/Tests.java index d38a4c3..95ae861 100644 --- a/impl/core/src/test/java/test/Tests.java +++ b/impl/core/src/test/java/test/Tests.java @@ -57,7 +57,7 @@ public final class Tests { .create(JavaArchive.class) .addClass(LocaleProducer.class) .addPackages(true, "br") - .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml") + .addAsResource(Tests.createFileAsset("src/test/resources/test/beans.xml"), "beans.xml") .addAsManifestResource( new File("src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension"), "services/javax.enterprise.inject.spi.Extension"); diff --git a/impl/core/src/test/resources/test/beans.xml b/impl/core/src/test/resources/test/beans.xml new file mode 100644 index 0000000..72cfc7c --- /dev/null +++ b/impl/core/src/test/resources/test/beans.xml @@ -0,0 +1,11 @@ + + + + br.gov.frameworkdemoiselle.exception.ExceptionHandlerInterceptor + br.gov.frameworkdemoiselle.internal.interceptor.RequiredPermissionInterceptor + br.gov.frameworkdemoiselle.internal.interceptor.RequiredRoleInterceptor + br.gov.frameworkdemoiselle.internal.interceptor.TransactionalInterceptor + + + -- libgit2 0.21.2