Commit 8cccaa742d765b6b96e782be7eec79947dd90c80

Authored by Emerson Oliveira
1 parent e3d78555
Exists in master

IN PROGRESS - issue FWK-120: Testes da extensão JSF (segurança +

mensagem + exceção) 
https://demoiselle.atlassian.net/browse/FWK-120
Adição de testes para exceções tratadas com
AuthorizationExceptionHandler
impl/extension/jsf/pom.xml
@@ -76,6 +76,12 @@ @@ -76,6 +76,12 @@
76 <artifactId>jsf-api</artifactId> 76 <artifactId>jsf-api</artifactId>
77 </dependency> 77 </dependency>
78 <dependency> 78 <dependency>
  79 + <groupId>org.glassfish.web</groupId>
  80 + <artifactId>el-impl</artifactId>
  81 + <scope>provided</scope>
  82 + </dependency>
  83 + <!-- For Tests -->
  84 + <dependency>
79 <groupId>com.sun.faces</groupId> 85 <groupId>com.sun.faces</groupId>
80 <artifactId>jsf-impl</artifactId> 86 <artifactId>jsf-impl</artifactId>
81 <scope>test</scope> 87 <scope>test</scope>
@@ -87,9 +93,10 @@ @@ -87,9 +93,10 @@
87 <scope>test</scope> 93 <scope>test</scope>
88 </dependency> 94 </dependency>
89 <dependency> 95 <dependency>
90 - <groupId>org.glassfish.web</groupId>  
91 - <artifactId>el-impl</artifactId>  
92 - <scope>provided</scope> 96 + <groupId>com.ocpsoft</groupId>
  97 + <artifactId>prettyfaces-jsf2</artifactId>
  98 + <version>3.3.0</version>
  99 + <scope>test</scope>
93 </dependency> 100 </dependency>
94 </dependencies> 101 </dependencies>
95 102
impl/extension/jsf/src/test/java/exception/handler/authorization/AuthorizationBean.java 0 → 100644
@@ -0,0 +1,25 @@ @@ -0,0 +1,25 @@
  1 +package exception.handler.authorization;
  2 +
  3 +import br.gov.frameworkdemoiselle.security.AuthorizationException;
  4 +import br.gov.frameworkdemoiselle.stereotype.ViewController;
  5 +
  6 +@ViewController
  7 +public class AuthorizationBean {
  8 +
  9 + private String correctMessage = "Authorization Message.";
  10 +
  11 + private String exceptionMessage = "Authorization Exception!";
  12 +
  13 + public String getCorrectMessage() {
  14 + return correctMessage;
  15 + }
  16 +
  17 + public String getExceptionMessage() {
  18 + throw new AuthorizationException(exceptionMessage);
  19 + }
  20 +
  21 + public void loadExceptionMessage() {
  22 + throw new AuthorizationException(exceptionMessage);
  23 + }
  24 +
  25 +}
impl/extension/jsf/src/test/java/exception/handler/authorization/AuthorizationHandledExceptionTest.java 0 → 100644
@@ -0,0 +1,59 @@ @@ -0,0 +1,59 @@
  1 +package exception.handler.authorization;
  2 +
  3 +import static org.junit.Assert.assertNotSame;
  4 +import static org.junit.Assert.assertTrue;
  5 +
  6 +import java.io.IOException;
  7 +import java.net.URL;
  8 +
  9 +import org.apache.commons.httpclient.HttpClient;
  10 +import org.apache.commons.httpclient.HttpException;
  11 +import org.apache.commons.httpclient.HttpStatus;
  12 +import org.apache.commons.httpclient.methods.GetMethod;
  13 +import org.jboss.arquillian.container.test.api.Deployment;
  14 +import org.jboss.arquillian.junit.Arquillian;
  15 +import org.jboss.arquillian.test.api.ArquillianResource;
  16 +import org.jboss.shrinkwrap.api.spec.WebArchive;
  17 +import org.junit.Test;
  18 +import org.junit.runner.RunWith;
  19 +
  20 +import test.Tests;
  21 +
  22 +@RunWith(Arquillian.class)
  23 +public class AuthorizationHandledExceptionTest {
  24 +
  25 + @ArquillianResource
  26 + private URL deploymentUrl;
  27 +
  28 + private static final String PATH = "src/test/resources/exception-handler-authorization";
  29 +
  30 + @Deployment(testable = false)
  31 + public static WebArchive createDeployment() {
  32 + return Tests.createDeployment().addClass(AuthorizationHandledExceptionTest.class).addClass(AuthorizationBean.class)
  33 + .addAsWebResource(Tests.createFileAsset(PATH + "/index.xhtml"), "index.xhtml")
  34 + .addAsWebInfResource(Tests.createFileAsset(PATH + "/web.xml"), "web.xml")
  35 + .addAsWebInfResource(Tests.createFileAsset(PATH + "/pretty-config.xml"), "pretty-config.xml");
  36 +
  37 + }
  38 +
  39 + @Test
  40 + public void authorizationHandledException() {
  41 + HttpClient client = new HttpClient();
  42 + GetMethod method = new GetMethod(deploymentUrl + "/index");
  43 +
  44 + try {
  45 + int status = client.executeMethod(method);
  46 + String message = method.getResponseBodyAsString();
  47 + System.out.println("MESAGE: " + message);
  48 +
  49 + assertNotSame(HttpStatus.SC_INTERNAL_SERVER_ERROR, status);
  50 + assertTrue(message.contains("Authorization Message."));
  51 + assertTrue(message.contains("Authorization Exception!"));
  52 +
  53 + } catch (HttpException e) {
  54 + e.printStackTrace();
  55 + } catch (IOException e) {
  56 + e.printStackTrace();
  57 + }
  58 + }
  59 +}
impl/extension/jsf/src/test/java/exception/handler/authorization/AuthorizationNotHandledExceptionTest.java 0 → 100644
@@ -0,0 +1,61 @@ @@ -0,0 +1,61 @@
  1 +package exception.handler.authorization;
  2 +
  3 +import static org.junit.Assert.assertEquals;
  4 +import static org.junit.Assert.assertFalse;
  5 +import static org.junit.Assert.assertNotSame;
  6 +import static org.junit.Assert.assertTrue;
  7 +
  8 +import java.io.IOException;
  9 +import java.net.URL;
  10 +
  11 +import junit.framework.Assert;
  12 +
  13 +import org.apache.commons.httpclient.HttpClient;
  14 +import org.apache.commons.httpclient.HttpException;
  15 +import org.apache.commons.httpclient.HttpStatus;
  16 +import org.apache.commons.httpclient.methods.GetMethod;
  17 +import org.jboss.arquillian.container.test.api.Deployment;
  18 +import org.jboss.arquillian.junit.Arquillian;
  19 +import org.jboss.arquillian.test.api.ArquillianResource;
  20 +import org.jboss.shrinkwrap.api.spec.WebArchive;
  21 +import org.junit.Test;
  22 +import org.junit.runner.RunWith;
  23 +
  24 +import test.Tests;
  25 +
  26 +@RunWith(Arquillian.class)
  27 +public class AuthorizationNotHandledExceptionTest {
  28 +
  29 + @ArquillianResource
  30 + private URL deploymentUrl;
  31 +
  32 + private static final String PATH = "src/test/resources/exception-handler-authorization";
  33 +
  34 + @Deployment(testable = false)
  35 + public static WebArchive createDeployment() {
  36 + return Tests.createDeployment().addClass(AuthorizationNotHandledExceptionTest.class).addClass(AuthorizationBean.class)
  37 + .addAsWebResource(Tests.createFileAsset(PATH + "/page.xhtml"), "page.xhtml")
  38 + .addAsWebInfResource(Tests.createFileAsset(PATH + "/web.xml"), "web.xml");
  39 + }
  40 +
  41 + @Test
  42 + public void authorizationNotHandledException() {
  43 + HttpClient client = new HttpClient();
  44 + GetMethod method = new GetMethod(deploymentUrl + "/page.jsf");
  45 +
  46 + try {
  47 + int status = client.executeMethod(method);
  48 + String message = method.getResponseBodyAsString();
  49 + System.out.println("MESAGE: " + message);
  50 +
  51 + assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, status);
  52 + assertTrue(message.contains("Authorization Exception!"));
  53 + assertFalse(message.contains("Authorization Message."));
  54 +
  55 + } catch (HttpException e) {
  56 + e.printStackTrace();
  57 + } catch (IOException e) {
  58 + e.printStackTrace();
  59 + }
  60 + }
  61 +}
impl/extension/jsf/src/test/resources/exception-handler-authorization/index.xhtml 0 → 100644
@@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
  1 +<html xmlns:h="http://java.sun.com/jsf/html"
  2 + xmlns:ui="http://java.sun.com/jsf/facelets">
  3 +
  4 + <h:body>
  5 + #{authorizationBean.correctMessage}
  6 + <h:messages />
  7 + </h:body>
  8 +
  9 +</html>
impl/extension/jsf/src/test/resources/exception-handler-authorization/page.xhtml 0 → 100644
@@ -0,0 +1,10 @@ @@ -0,0 +1,10 @@
  1 +<html xmlns:h="http://java.sun.com/jsf/html"
  2 + xmlns:ui="http://java.sun.com/jsf/facelets">
  3 +
  4 + <h:body>
  5 + #{authorizationBean.correctMessage}
  6 + #{authorizationBean.exceptionMessage}
  7 + <h:messages />
  8 + </h:body>
  9 +
  10 +</html>
0 \ No newline at end of file 11 \ No newline at end of file
impl/extension/jsf/src/test/resources/exception-handler-authorization/pretty-config.xml 0 → 100644
@@ -0,0 +1,13 @@ @@ -0,0 +1,13 @@
  1 +<pretty-config xmlns="http://ocpsoft.com/prettyfaces/3.3.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2 + xsi:schemaLocation="http://ocpsoft.com/prettyfaces/3.3.0
  3 + http://ocpsoft.com/xml/ns/prettyfaces/ocpsoft-pretty-faces-3.3.0.xsd">
  4 +
  5 + <rewrite match="^/*(.*)$" substitute="/$1" redirect="301" />
  6 +
  7 + <url-mapping id="index">
  8 + <pattern value="/index" />
  9 + <view-id value="/index.jsf" />
  10 + <action>#{authorizationBean.loadExceptionMessage}</action>
  11 + </url-mapping>
  12 +
  13 +</pretty-config>
0 \ No newline at end of file 14 \ No newline at end of file
impl/extension/jsf/src/test/resources/exception-handler-authorization/web.xml 0 → 100644
@@ -0,0 +1,74 @@ @@ -0,0 +1,74 @@
  1 +<!--
  2 + Demoiselle Framework
  3 + Copyright (C) 2010 SERPRO
  4 + ============================================================================
  5 + This file is part of Demoiselle Framework.
  6 +
  7 + Demoiselle Framework is free software; you can redistribute it and/or
  8 + modify it under the terms of the GNU Lesser General Public License version 3
  9 + as published by the Free Software Foundation.
  10 +
  11 + This program is distributed in the hope that it will be useful,
  12 + but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 + GNU General Public License for more details.
  15 +
  16 + You should have received a copy of the GNU Lesser General Public License version 3
  17 + along with this program; if not, see <http://www.gnu.org/licenses />
  18 + or write to the Free Software Foundation, Inc., 51 Franklin Street,
  19 + Fifth Floor, Boston, MA 02110-1301, USA.
  20 + ============================================================================
  21 + Este arquivo é parte do Framework Demoiselle.
  22 +
  23 + O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou
  24 + modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação
  25 + do Software Livre (FSF).
  26 +
  27 + Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
  28 + GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
  29 + APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português
  30 + para maiores detalhes.
  31 +
  32 + Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
  33 + "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses />
  34 + ou escreva para a Fundação do Software Livre (FSF) Inc.,
  35 + 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
  36 +-->
  37 +<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  38 + xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  39 +
  40 + <listener>
  41 + <listener-class>br.gov.frameworkdemoiselle.util.ServletListener</listener-class>
  42 + </listener>
  43 + <filter>
  44 + <filter-name>Demoiselle Servlet Filter</filter-name>
  45 + <filter-class>br.gov.frameworkdemoiselle.util.ServletFilter</filter-class>
  46 + </filter>
  47 + <filter-mapping>
  48 + <filter-name>Demoiselle Servlet Filter</filter-name>
  49 + <url-pattern>/*</url-pattern>
  50 + </filter-mapping>
  51 +
  52 + <filter>
  53 + <filter-name>Pretty Filter</filter-name>
  54 + <filter-class>com.ocpsoft.pretty.PrettyFilter</filter-class>
  55 + </filter>
  56 +
  57 + <filter-mapping>
  58 + <filter-name>Pretty Filter</filter-name>
  59 + <url-pattern>/*</url-pattern>
  60 + <dispatcher>FORWARD</dispatcher>
  61 + <dispatcher>REQUEST</dispatcher>
  62 + <dispatcher>ERROR</dispatcher>
  63 + </filter-mapping>
  64 +
  65 + <servlet>
  66 + <servlet-name>Faces Servlet</servlet-name>
  67 + <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
  68 + <load-on-startup>1</load-on-startup>
  69 + </servlet>
  70 + <servlet-mapping>
  71 + <servlet-name>Faces Servlet</servlet-name>
  72 + <url-pattern>*.jsf</url-pattern>
  73 + </servlet-mapping>
  74 +</web-app>
0 \ No newline at end of file 75 \ No newline at end of file