From c298b4fa63617e63d37d03010e9e9e9028fd1069 Mon Sep 17 00:00:00 2001 From: Ednara Oliveira Date: Fri, 19 Apr 2013 18:01:19 -0300 Subject: [PATCH] Testes de Exceção --- impl/core/src/test/java/exception/CustomException.java | 11 +++++++++++ impl/core/src/test/java/exception/CustomExceptionHandler.java | 27 +++++++++++++++++++++++++++ impl/core/src/test/java/exception/CustomExceptionTest.java | 32 ++++++++++++++++++++++++++++++++ impl/core/src/test/java/exception/ExceptionInheritance.java | 39 +++++++++++++++++++++++++++++++++++++++ impl/core/src/test/java/exception/ExceptionInheritanceTest.java | 39 +++++++++++++++++++++++++++++++++++++++ impl/core/src/test/java/exception/MultiException.java | 14 ++++++++------ impl/core/src/test/java/exception/MultiExceptionOneHandler.java | 18 ++++++++++++++++++ impl/core/src/test/java/exception/MultiExceptionTest.java | 19 +++++++++++++++++-- 8 files changed, 191 insertions(+), 8 deletions(-) create mode 100644 impl/core/src/test/java/exception/CustomException.java create mode 100644 impl/core/src/test/java/exception/CustomExceptionHandler.java create mode 100644 impl/core/src/test/java/exception/CustomExceptionTest.java create mode 100644 impl/core/src/test/java/exception/ExceptionInheritance.java create mode 100644 impl/core/src/test/java/exception/ExceptionInheritanceTest.java create mode 100644 impl/core/src/test/java/exception/MultiExceptionOneHandler.java diff --git a/impl/core/src/test/java/exception/CustomException.java b/impl/core/src/test/java/exception/CustomException.java new file mode 100644 index 0000000..eefc046 --- /dev/null +++ b/impl/core/src/test/java/exception/CustomException.java @@ -0,0 +1,11 @@ +package exception; + +import br.gov.frameworkdemoiselle.exception.ApplicationException; + + +@ApplicationException +public class CustomException extends RuntimeException { + + private static final long serialVersionUID = 1L; + +} \ No newline at end of file diff --git a/impl/core/src/test/java/exception/CustomExceptionHandler.java b/impl/core/src/test/java/exception/CustomExceptionHandler.java new file mode 100644 index 0000000..f6dfc18 --- /dev/null +++ b/impl/core/src/test/java/exception/CustomExceptionHandler.java @@ -0,0 +1,27 @@ +package exception; + +import br.gov.frameworkdemoiselle.exception.ExceptionHandler; +import br.gov.frameworkdemoiselle.stereotype.Controller; + +@Controller +public class CustomExceptionHandler { + + private boolean exceptionHandler = false; + + public boolean isExceptionHandler() { + return exceptionHandler; + } + + public void setExceptionHandler(boolean exceptionHandler) { + this.exceptionHandler = exceptionHandler; + } + + public void throwExceptionWithMessage() { + throw new CustomException(); + } + + @ExceptionHandler + public void handler(CustomException exception) { + setExceptionHandler(true); + } +} diff --git a/impl/core/src/test/java/exception/CustomExceptionTest.java b/impl/core/src/test/java/exception/CustomExceptionTest.java new file mode 100644 index 0000000..e9dc73d --- /dev/null +++ b/impl/core/src/test/java/exception/CustomExceptionTest.java @@ -0,0 +1,32 @@ +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 CustomExceptionTest { + + @Inject + private CustomExceptionHandler exception; + + @Deployment + public static JavaArchive createDeployment() { + JavaArchive deployment = Tests.createDeployment(CustomExceptionTest.class); + return deployment; + } + + @Test + public void testCustomExceptionHandler() { + exception.throwExceptionWithMessage(); + assertEquals(true, exception.isExceptionHandler()); + } +} diff --git a/impl/core/src/test/java/exception/ExceptionInheritance.java b/impl/core/src/test/java/exception/ExceptionInheritance.java new file mode 100644 index 0000000..175be23 --- /dev/null +++ b/impl/core/src/test/java/exception/ExceptionInheritance.java @@ -0,0 +1,39 @@ +package exception; + +import br.gov.frameworkdemoiselle.exception.ExceptionHandler; +import br.gov.frameworkdemoiselle.stereotype.Controller; + +@Controller +public class ExceptionInheritance { + + private boolean handlerSuperClass = false; + + private boolean handlerClass = false; + + public boolean isHandlerSuperClass() { + return handlerSuperClass; + } + + public boolean isHandlerClass() { + return handlerClass; + } + + public void throwNullPointerException() { + throw new NullPointerException(); + } + + public void throwArithmeticException() { + throw new ArithmeticException(); + } + + @ExceptionHandler + public void handle(ArithmeticException e) { + handlerClass = true; + } + + @ExceptionHandler + public void handle(RuntimeException e) { + handlerSuperClass = true; + } + +} diff --git a/impl/core/src/test/java/exception/ExceptionInheritanceTest.java b/impl/core/src/test/java/exception/ExceptionInheritanceTest.java new file mode 100644 index 0000000..1a91834 --- /dev/null +++ b/impl/core/src/test/java/exception/ExceptionInheritanceTest.java @@ -0,0 +1,39 @@ +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 ExceptionInheritanceTest { + + @Inject + private ExceptionInheritance exceptionInheritance; + + @Deployment + public static JavaArchive createDeployment() { + JavaArchive deployment = Tests.createDeployment(ExceptionInheritanceTest.class); + return deployment; + } + + @Test + public void testExceptionInheritanceSuperClass() { + exceptionInheritance.throwNullPointerException(); + assertEquals(true, exceptionInheritance.isHandlerSuperClass()); + } + + @Test + public void testExceptionInheritanceClass() { + exceptionInheritance.throwArithmeticException(); + assertEquals(false, exceptionInheritance.isHandlerSuperClass()); + assertEquals(true, exceptionInheritance.isHandlerClass()); + } +} diff --git a/impl/core/src/test/java/exception/MultiException.java b/impl/core/src/test/java/exception/MultiException.java index ceac771..2d54256 100644 --- a/impl/core/src/test/java/exception/MultiException.java +++ b/impl/core/src/test/java/exception/MultiException.java @@ -1,5 +1,7 @@ package exception; +import java.awt.geom.IllegalPathStateException; + import br.gov.frameworkdemoiselle.exception.ExceptionHandler; import br.gov.frameworkdemoiselle.stereotype.Controller; @@ -9,7 +11,7 @@ public class MultiException { private boolean nullPointerExceptionHandler = false; private boolean arithmeticExceptionHandler = false; - + public boolean isNullPointerExceptionHandler() { return nullPointerExceptionHandler; } @@ -17,15 +19,15 @@ public class MultiException { public boolean isArithmeticExceptionHandler() { return arithmeticExceptionHandler; } - - public void throwExceptionNullPointer() { + + public void throwNullPointerException() { throw new NullPointerException(); } - public void throwExceptionArithmetic() { + public void throwArithmeticException() { throw new ArithmeticException(); } - + @SuppressWarnings({ "null", "unused" }) public void throwTwoException() { String txt = null; @@ -41,5 +43,5 @@ public class MultiException { @ExceptionHandler public void handler(ArithmeticException cause) { arithmeticExceptionHandler = true; - } + } } diff --git a/impl/core/src/test/java/exception/MultiExceptionOneHandler.java b/impl/core/src/test/java/exception/MultiExceptionOneHandler.java new file mode 100644 index 0000000..af13f22 --- /dev/null +++ b/impl/core/src/test/java/exception/MultiExceptionOneHandler.java @@ -0,0 +1,18 @@ +package exception; + +import java.awt.geom.IllegalPathStateException; + +import br.gov.frameworkdemoiselle.exception.ExceptionHandler; +import br.gov.frameworkdemoiselle.stereotype.Controller; + +@Controller +public class MultiExceptionOneHandler { + + public void throwIllegalPathException() { + throw new IllegalPathStateException(); + } + + @ExceptionHandler + public void handler(IllegalPathStateException cause, IllegalStateException cause2) { + } +} diff --git a/impl/core/src/test/java/exception/MultiExceptionTest.java b/impl/core/src/test/java/exception/MultiExceptionTest.java index 5c2728d..2415115 100644 --- a/impl/core/src/test/java/exception/MultiExceptionTest.java +++ b/impl/core/src/test/java/exception/MultiExceptionTest.java @@ -11,6 +11,8 @@ import org.jboss.shrinkwrap.api.spec.JavaArchive; import org.junit.Test; import org.junit.runner.RunWith; +import br.gov.frameworkdemoiselle.DemoiselleException; + import test.Tests; @RunWith(Arquillian.class) @@ -18,6 +20,9 @@ public class MultiExceptionTest { @Inject private MultiException multiException; + + @Inject + private MultiExceptionOneHandler multiExceptionOneHandler; @Deployment public static JavaArchive createDeployment() { @@ -27,13 +32,13 @@ public class MultiExceptionTest { @Test public void testNullPointerExceptionHandler() { - multiException.throwExceptionNullPointer(); + multiException.throwNullPointerException(); assertEquals(true, multiException.isNullPointerExceptionHandler()); } @Test public void testArithmeticExceptionHandler() { - multiException.throwExceptionArithmetic(); + multiException.throwArithmeticException(); assertEquals(true, multiException.isArithmeticExceptionHandler()); } @@ -43,4 +48,14 @@ public class MultiExceptionTest { assertEquals(true, multiException.isNullPointerExceptionHandler()); assertEquals(true, multiException.isArithmeticExceptionHandler()); } + + @Test + public void testExceptionHandlerWithTwoException() { + try { + multiExceptionOneHandler.throwIllegalPathException(); + fail(); + } catch (Exception e) { + assertEquals(DemoiselleException.class, e.getClass()); + } + } } -- libgit2 0.21.2