From d132a1f626b8209ceeb6b8c414a973b59dab7fdd Mon Sep 17 00:00:00 2001 From: Ednara Oliveira Date: Fri, 3 May 2013 17:54:32 -0300 Subject: [PATCH] Testes de Exception --- impl/core/src/test/java/exception/basic/ExceptionClassNotAnnotated.java | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ impl/core/src/test/java/exception/basic/ExceptionHandlerTest.java | 36 ++++++++++++++++++++++++++++-------- impl/core/src/test/java/exception/basic/ExceptionNested.java | 31 +++++++++++++++++++++++++++++++ impl/core/src/test/java/exception/basic/MultiExceptionHandler.java | 19 ------------------- 4 files changed, 116 insertions(+), 27 deletions(-) create mode 100644 impl/core/src/test/java/exception/basic/ExceptionClassNotAnnotated.java create mode 100644 impl/core/src/test/java/exception/basic/ExceptionNested.java diff --git a/impl/core/src/test/java/exception/basic/ExceptionClassNotAnnotated.java b/impl/core/src/test/java/exception/basic/ExceptionClassNotAnnotated.java new file mode 100644 index 0000000..9cafa55 --- /dev/null +++ b/impl/core/src/test/java/exception/basic/ExceptionClassNotAnnotated.java @@ -0,0 +1,57 @@ +/* + * Demoiselle Framework + * Copyright (C) 2010 SERPRO + * ---------------------------------------------------------------------------- + * This file is part of Demoiselle Framework. + * + * Demoiselle Framework is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License version 3 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License version 3 + * along with this program; if not, see + * or write to the Free Software Foundation, Inc., 51 Franklin Street, + * Fifth Floor, Boston, MA 02110-1301, USA. + * ---------------------------------------------------------------------------- + * Este arquivo é parte do Framework Demoiselle. + * + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação + * do Software Livre (FSF). + * + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português + * para maiores detalhes. + * + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título + * "LICENCA.txt", junto com esse programa. Se não, acesse + * ou escreva para a Fundação do Software Livre (FSF) Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. + */ +package exception.basic; + +import br.gov.frameworkdemoiselle.exception.ExceptionHandler; + +public class ExceptionClassNotAnnotated { + + private boolean nullPointerExceptionHandler = false; + + public boolean isNullPointerExceptionHandler() { + return nullPointerExceptionHandler; + } + + public void throwNullPointerException() { + throw new NullPointerException(); + } + + @ExceptionHandler + public void handler(NullPointerException cause) { + nullPointerExceptionHandler = true; + } +} diff --git a/impl/core/src/test/java/exception/basic/ExceptionHandlerTest.java b/impl/core/src/test/java/exception/basic/ExceptionHandlerTest.java index aa8fdb4..d6be493 100644 --- a/impl/core/src/test/java/exception/basic/ExceptionHandlerTest.java +++ b/impl/core/src/test/java/exception/basic/ExceptionHandlerTest.java @@ -41,6 +41,8 @@ import static junit.framework.Assert.fail; import javax.inject.Inject; +import junit.framework.Assert; + import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit.Arquillian; import org.jboss.shrinkwrap.api.spec.JavaArchive; @@ -55,13 +57,19 @@ public class ExceptionHandlerTest { @Inject private SimpleExceptionHandler exceptionHandler; - + @Inject private MultiExceptionHandler multiExceptionHandler; @Inject private ExceptionHandlerTwoParameter exceptionTwoParameter; + @Inject + private ExceptionNested nested; + + @Inject + private ExceptionClassNotAnnotated classNotAnnotated; + @Deployment public static JavaArchive createDeployment() { JavaArchive deployment = Tests.createDeployment(ExceptionHandlerTest.class); @@ -75,7 +83,7 @@ public class ExceptionHandlerTest { assertEquals(true, exceptionHandler.isArithmeticExceptionHandler()); assertEquals(true, exceptionHandler.isArithmeticExceptionHandler()); } - + @Test public void exceptionWithoutHandler() { try { @@ -84,7 +92,7 @@ public class ExceptionHandlerTest { } catch (Exception cause) { assertEquals(IllegalArgumentException.class, cause.getClass()); } - } + } @Test public void twoExceptionOneMethod() { @@ -102,21 +110,33 @@ public class ExceptionHandlerTest { } @Test + public void exceptionHandlerWithTwoParameter() { + try { + exceptionTwoParameter.throwIllegalPathException(); + fail(); + } catch (Exception e) { + assertEquals(DemoiselleException.class, e.getClass()); + } + } + + @Test public void exceptionHandlerWithException() { try { - multiExceptionHandler.throwNoSuchElementException(); + nested.throwNoSuchElementException(); + fail(); } catch (Exception e) { assertEquals(ArithmeticException.class, e.getClass()); + assertEquals(false, nested.isExceptionHandler()); } } @Test - public void exceptionHandlerWithTwoParameter() { + public void exceptionClassNotAnnotatedController() { try { - exceptionTwoParameter.throwIllegalPathException(); + classNotAnnotated.throwNullPointerException(); fail(); } catch (Exception e) { - assertEquals(DemoiselleException.class, e.getClass()); + assertEquals(NullPointerException.class, e.getClass()); } - } + } } diff --git a/impl/core/src/test/java/exception/basic/ExceptionNested.java b/impl/core/src/test/java/exception/basic/ExceptionNested.java new file mode 100644 index 0000000..75b98c4 --- /dev/null +++ b/impl/core/src/test/java/exception/basic/ExceptionNested.java @@ -0,0 +1,31 @@ +package exception.basic; + +import java.util.NoSuchElementException; + +import br.gov.frameworkdemoiselle.exception.ExceptionHandler; +import br.gov.frameworkdemoiselle.stereotype.Controller; + +@Controller +public class ExceptionNested { + + private boolean exceptionHandler = false; + + public boolean isExceptionHandler() { + return exceptionHandler; + } + + public void throwNoSuchElementException() { + throw new NoSuchElementException(); + } + + @ExceptionHandler + @SuppressWarnings("unused") + public void handlerWithError(NoSuchElementException cause) { + int a = 2 / 0; + } + + @ExceptionHandler + public void handler(ArithmeticException cause) { + exceptionHandler = true; + } +} diff --git a/impl/core/src/test/java/exception/basic/MultiExceptionHandler.java b/impl/core/src/test/java/exception/basic/MultiExceptionHandler.java index 41c77ca..5ffb529 100644 --- a/impl/core/src/test/java/exception/basic/MultiExceptionHandler.java +++ b/impl/core/src/test/java/exception/basic/MultiExceptionHandler.java @@ -36,8 +36,6 @@ */ package exception.basic; -import java.util.NoSuchElementException; - import br.gov.frameworkdemoiselle.exception.ExceptionHandler; import br.gov.frameworkdemoiselle.stereotype.Controller; @@ -62,22 +60,10 @@ public class MultiExceptionHandler { return exceptionHandlerIllegalArgument3; } - @SuppressWarnings("null") - public void throwExceptionWithHandler() { - String txt = null; - txt.toString(); - } - - - public void throwIllegalArgumentException() { throw new IllegalArgumentException(); } - public void throwNoSuchElementException() { - throw new NoSuchElementException(); - } - @ExceptionHandler public void handler1(IllegalArgumentException cause) { exceptionHandlerIllegalArgument1 = true; @@ -93,9 +79,4 @@ public class MultiExceptionHandler { exceptionHandlerIllegalArgument2 = true; } - @ExceptionHandler - @SuppressWarnings("unused") - public void handlerWithError(NoSuchElementException cause) { - int a = 2 / 0; - } } -- libgit2 0.21.2