Commit a0cae561c78071ce2c97378b7e887f4e849c88ff

Authored by Cleverson Sacramento
1 parent 1b9eeaf8
Exists in master

IN PROGRESS - issue JUN-6: MessageContext + jUnit

https://demoiselle.atlassian.net/browse/JUN-6
impl/extension/jsf/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/FacesMessageAppender.java
... ... @@ -37,18 +37,26 @@
37 37 package br.gov.frameworkdemoiselle.internal.implementation;
38 38  
39 39 import static br.gov.frameworkdemoiselle.annotation.Priority.L3_PRIORITY;
  40 +
  41 +import javax.enterprise.context.ContextNotActiveException;
  42 +
40 43 import br.gov.frameworkdemoiselle.annotation.Priority;
41 44 import br.gov.frameworkdemoiselle.message.Message;
42   -import br.gov.frameworkdemoiselle.message.MessageAppender;
43 45 import br.gov.frameworkdemoiselle.util.Faces;
44 46  
45 47 @Priority(L3_PRIORITY)
46   -public class FacesMessageAppender implements MessageAppender {
  48 +public class FacesMessageAppender extends LoggerMessageAppender {
47 49  
48 50 private static final long serialVersionUID = 1L;
49 51  
50 52 @Override
51 53 public void append(Message message) {
52   - Faces.addMessage(message);
  54 + try {
  55 + Faces.addMessage(message);
  56 + } catch (ContextNotActiveException cause) {
  57 + // TODO Logar warning...
  58 +
  59 + super.append(message);
  60 + }
53 61 }
54 62 }
... ...
impl/extension/jsf/src/main/java/br/gov/frameworkdemoiselle/internal/proxy/FacesContextProxy.java
... ... @@ -5,7 +5,9 @@ import java.util.Iterator;
5 5 import java.util.List;
6 6 import java.util.Map;
7 7  
  8 +import javax.annotation.PostConstruct;
8 9 import javax.el.ELContext;
  10 +import javax.enterprise.context.ContextNotActiveException;
9 11 import javax.enterprise.inject.Default;
10 12 import javax.faces.application.Application;
11 13 import javax.faces.application.FacesMessage;
... ... @@ -26,8 +28,16 @@ public class FacesContextProxy extends FacesContext implements Serializable {
26 28  
27 29 private static final long serialVersionUID = 1L;
28 30  
29   - private FacesContext getDelegate() {
30   - return FacesContext.getCurrentInstance();
  31 + @PostConstruct
  32 + public FacesContext getDelegate() {
  33 + FacesContext facesContext = FacesContext.getCurrentInstance();
  34 +
  35 + if (facesContext == null) {
  36 + // TODO Colocar a mensagem correta
  37 + throw new ContextNotActiveException();
  38 + }
  39 +
  40 + return facesContext;
31 41 }
32 42  
33 43 public int hashCode() {
... ...
impl/extension/jsf/src/main/java/br/gov/frameworkdemoiselle/util/Faces.java
... ... @@ -59,7 +59,7 @@ import br.gov.frameworkdemoiselle.message.SeverityType;
59 59 * Utility class to insert messages in the FacesContext.
60 60 *
61 61 * @author SERPRO
62   - * */
  62 + */
63 63 public class Faces {
64 64  
65 65 private Faces() {
... ...
impl/extension/jsf/src/test/java/proxy/FacesContextProxyBean.java 0 → 100644
... ... @@ -0,0 +1,61 @@
  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 +package proxy;
  38 +
  39 +import static javax.servlet.http.HttpServletResponse.SC_ACCEPTED;
  40 +
  41 +import javax.faces.context.FacesContext;
  42 +import javax.inject.Inject;
  43 +import javax.inject.Named;
  44 +import javax.servlet.http.HttpServletResponse;
  45 +
  46 +import br.gov.frameworkdemoiselle.util.Beans;
  47 +
  48 +@Named
  49 +public class FacesContextProxyBean {
  50 +
  51 + @Inject
  52 + private HttpServletResponse response;
  53 +
  54 + public String getOk() {
  55 + FacesContext facesContext = Beans.getReference(FacesContext.class);
  56 + facesContext.responseComplete();
  57 +
  58 + response.setStatus(SC_ACCEPTED);
  59 + return null;
  60 + }
  61 +}
... ...
impl/extension/jsf/src/test/java/proxy/FacesContextProxyServlet.java
... ... @@ -36,8 +36,12 @@
36 36 */
37 37 package proxy;
38 38  
  39 +import static javax.servlet.http.HttpServletResponse.SC_EXPECTATION_FAILED;
  40 +import static javax.servlet.http.HttpServletResponse.SC_OK;
  41 +
39 42 import java.io.IOException;
40 43  
  44 +import javax.enterprise.context.ContextNotActiveException;
41 45 import javax.faces.context.FacesContext;
42 46 import javax.servlet.ServletException;
43 47 import javax.servlet.annotation.WebServlet;
... ... @@ -45,9 +49,6 @@ import javax.servlet.http.HttpServlet;
45 49 import javax.servlet.http.HttpServletRequest;
46 50 import javax.servlet.http.HttpServletResponse;
47 51  
48   -import org.apache.commons.httpclient.HttpStatus;
49   -
50   -import br.gov.frameworkdemoiselle.internal.proxy.FacesContextProxy;
51 52 import br.gov.frameworkdemoiselle.util.Beans;
52 53  
53 54 @WebServlet("/index")
... ... @@ -55,15 +56,14 @@ public class FacesContextProxyServlet extends HttpServlet {
55 56  
56 57 private static final long serialVersionUID = 1L;
57 58  
58   - private FacesContext facesContext;
59   -
60 59 @Override
61 60 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
62   - facesContext = Beans.getReference(FacesContext.class);
63   - if (facesContext.getClass() == FacesContextProxy.class) {
64   - response.setStatus(HttpStatus.SC_OK);
65   - } else {
66   - response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
  61 + try {
  62 + Beans.getReference(FacesContext.class);
  63 + response.setStatus(SC_OK);
  64 +
  65 + } catch (ContextNotActiveException cause) {
  66 + response.setStatus(SC_EXPECTATION_FAILED);
67 67 }
68 68 }
69 69 }
... ...
impl/extension/jsf/src/test/java/proxy/FacesContextProxyTest.java
... ... @@ -36,6 +36,8 @@
36 36 */
37 37 package proxy;
38 38  
  39 +import static javax.servlet.http.HttpServletResponse.SC_ACCEPTED;
  40 +import static javax.servlet.http.HttpServletResponse.SC_EXPECTATION_FAILED;
39 41 import static org.junit.Assert.assertEquals;
40 42  
41 43 import java.io.IOException;
... ... @@ -43,7 +45,6 @@ import java.net.URL;
43 45  
44 46 import org.apache.commons.httpclient.HttpClient;
45 47 import org.apache.commons.httpclient.HttpException;
46   -import org.apache.commons.httpclient.HttpStatus;
47 48 import org.apache.commons.httpclient.methods.GetMethod;
48 49 import org.jboss.arquillian.container.test.api.Deployment;
49 50 import org.jboss.arquillian.junit.Arquillian;
... ... @@ -64,16 +65,26 @@ public class FacesContextProxyTest {
64 65  
65 66 @Deployment(testable = false)
66 67 public static WebArchive createDeployment() {
67   - return Tests.createDeployment().addClass(FacesContextProxyServlet.class)
  68 + return Tests.createDeployment().addClasses(FacesContextProxyServlet.class, FacesContextProxyBean.class)
  69 + .addAsWebResource(Tests.createFileAsset(PATH + "/index.xhtml"), "index.xhtml")
68 70 .addAsWebInfResource(Tests.createFileAsset(PATH + "/web.xml"), "web.xml");
69 71 }
70 72  
71 73 @Test
72   - public void facesContextProxy() throws HttpException, IOException {
  74 + public void validProxyCreated() throws HttpException, IOException {
  75 + HttpClient client = new HttpClient();
  76 + GetMethod method = new GetMethod(deploymentUrl + "/index.jsf");
  77 +
  78 + int status = client.executeMethod(method);
  79 + assertEquals(SC_ACCEPTED, status);
  80 + }
  81 +
  82 + @Test
  83 + public void inValidProxyCreatedCausedByContextNotActiveException() throws HttpException, IOException {
73 84 HttpClient client = new HttpClient();
74 85 GetMethod method = new GetMethod(deploymentUrl + "/index");
75 86  
76 87 int status = client.executeMethod(method);
77   - assertEquals(HttpStatus.SC_OK, status);
  88 + assertEquals(SC_EXPECTATION_FAILED, status);
78 89 }
79 90 }
... ...
impl/extension/jsf/src/test/resources/proxy/index.xhtml 0 → 100644
... ... @@ -0,0 +1,43 @@
  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 +<html xmlns:h="http://java.sun.com/jsf/html">
  38 +
  39 + <h:body>
  40 + #{facesContextProxyBean.ok}
  41 + </h:body>
  42 +
  43 +</html>
... ...
impl/extension/jsf/src/test/resources/proxy/web.xml
... ... @@ -48,7 +48,17 @@
48 48 <filter-name>Demoiselle Servlet Filter</filter-name>
49 49 <url-pattern>/*</url-pattern>
50 50 </filter-mapping>
51   -
  51 +
  52 + <servlet>
  53 + <servlet-name>Faces Servlet</servlet-name>
  54 + <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
  55 + <load-on-startup>1</load-on-startup>
  56 + </servlet>
  57 + <servlet-mapping>
  58 + <servlet-name>Faces Servlet</servlet-name>
  59 + <url-pattern>*.jsf</url-pattern>
  60 + </servlet-mapping>
  61 +
52 62 <servlet>
53 63 <servlet-name>Servlet Class</servlet-name>
54 64 <servlet-class>proxy.FacesContextProxyServlet</servlet-class>
... ... @@ -56,6 +66,6 @@
56 66 <servlet-mapping>
57 67 <servlet-name>Servlet Class</servlet-name>
58 68 <url-pattern>/index</url-pattern>
59   - </servlet-mapping>
  69 + </servlet-mapping>
60 70  
61 71 </web-app>
62 72 \ No newline at end of file
... ...