View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.renderkit.html;
20  
21  import java.io.StringWriter;
22  
23  import javax.faces.application.FacesMessage;
24  import javax.faces.component.html.HtmlMessages;
25  
26  import junit.framework.Test;
27  import junit.framework.TestSuite;
28  
29  import org.apache.myfaces.test.utils.HtmlCheckAttributesUtil;
30  import org.apache.myfaces.test.utils.HtmlRenderedAttr;
31  import org.apache.myfaces.test.base.AbstractJsfTestCase;
32  import org.apache.myfaces.test.mock.MockRenderKitFactory;
33  import org.apache.myfaces.test.mock.MockResponseWriter;
34  
35  /**
36   * @author Bruno Aranda (latest modification by $Author$)
37   * @version $Revision$ $Date$
38   */
39  public class HtmlMessagesRendererTest extends AbstractJsfTestCase
40  {
41      private static final String ERROR_CLASS = "errorClass";
42      private static final String WARN_CLASS = "warnClass";
43      private static final String INFO_CLASS = "infoClass";
44  
45      private HtmlMessages messages;
46      private MockResponseWriter writer;
47  
48      public HtmlMessagesRendererTest(String name)
49      {
50          super(name);
51      }
52      
53      public static Test suite() {
54          return new TestSuite(HtmlMessagesRendererTest.class);
55      }
56  
57      public void setUp() throws Exception
58      {
59          super.setUp();
60          messages = new HtmlMessages();
61  
62          writer = new MockResponseWriter(new StringWriter(), null, null);
63          facesContext.setResponseWriter(writer);
64  
65          facesContext.getViewRoot().setRenderKitId(MockRenderKitFactory.HTML_BASIC_RENDER_KIT);
66          facesContext.getRenderKit().addRenderer(
67                  messages.getFamily(),
68                  messages.getRendererType(),
69                  new HtmlMessagesRenderer());
70          
71          facesContext.getAttributes().put("org.apache.myfaces.RENDERED_JSF_JS", Boolean.TRUE);
72      }
73  
74      public void tearDown() throws Exception
75      {
76          super.tearDown();
77          messages = null;
78          writer = null;
79      }
80      
81      public void testHtmlPropertyPassTru() throws Exception
82      {
83          HtmlRenderedAttr[] attrs = {
84              //_EventProperties
85              new HtmlRenderedAttr("onclick",2), 
86              new HtmlRenderedAttr("ondblclick",2), 
87              new HtmlRenderedAttr("onkeydown",2), 
88              new HtmlRenderedAttr("onkeypress",2),
89              new HtmlRenderedAttr("onkeyup",2), 
90              new HtmlRenderedAttr("onmousedown",2), 
91              new HtmlRenderedAttr("onmousemove",2), 
92              new HtmlRenderedAttr("onmouseout",2),
93              new HtmlRenderedAttr("onmouseover",2), 
94              new HtmlRenderedAttr("onmouseup",2),
95              //_StyleProperties
96              new HtmlRenderedAttr("styleClass", "styleClass", "class=\"styleClass\""),
97              new HtmlRenderedAttr("style"),
98              new HtmlRenderedAttr("role"),
99              new HtmlRenderedAttr("warnClass", "warnClass", "class=\"warnClass\"",2),
100             new HtmlRenderedAttr("warnStyle", "warnStyle", "style=\"warnStyle\"",2)
101         };
102         
103         facesContext.addMessage("test1", new FacesMessage(FacesMessage.SEVERITY_WARN, "warnSumary", "detailWarnSummary"));
104         facesContext.addMessage("test2", new FacesMessage(FacesMessage.SEVERITY_WARN, "warnSumary2", "detailWarnSummary2"));        
105 
106         messages.setErrorClass(ERROR_CLASS);
107         messages.setWarnClass(WARN_CLASS);
108         messages.setInfoClass(INFO_CLASS);
109         messages.setWarnStyle("warnStyle");
110         
111         messages.setLayout("table");
112         //messages.setStyle("left: 48px; top: 432px; position: absolute");
113         
114         MockResponseWriter writer = (MockResponseWriter)facesContext.getResponseWriter();
115         HtmlCheckAttributesUtil.checkRenderedAttributes(
116                 messages, facesContext, writer, attrs);
117         if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) {
118             fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
119         }
120     }
121     
122     public void testRenderSpanOnlyWhenNecessary1() throws Exception
123     {
124         facesContext.addMessage("test1", new FacesMessage(FacesMessage.SEVERITY_WARN, "warnSumary", "detailWarnSummary"));
125         messages.encodeEnd(facesContext);
126         facesContext.renderResponse();
127         String output = writer.getWriter().toString();
128         assertTrue(output.contains("warnSumary"));
129         assertTrue(!output.contains("span"));
130     }
131     
132     public void testRenderSpanOnlyWhenNecessary2() throws Exception
133     {
134         facesContext.addMessage("test1", new FacesMessage(FacesMessage.SEVERITY_WARN, "warnSumary", "detailWarnSummary"));
135         messages.setLayout("table");
136         messages.encodeEnd(facesContext);
137         facesContext.renderResponse();
138         String output = writer.getWriter().toString();
139         assertTrue(output.contains("warnSumary"));
140         assertTrue(!output.contains("span"));
141     }
142     
143     public void testRenderSpanOnlyWhenNecessary3() throws Exception
144     {
145         facesContext.addMessage("test1", new FacesMessage(FacesMessage.SEVERITY_WARN, "warnSumary", "detailWarnSummary"));
146         messages.setId("msgPanel");
147         messages.encodeEnd(facesContext);
148         facesContext.renderResponse();
149         String output = writer.getWriter().toString();
150         assertTrue(output.contains("warnSumary"));
151         assertTrue(!output.contains("span"));
152     }
153     
154     /**
155      * It should output the class on li
156      * @throws Exception
157      */
158     public void testRenderSpanOnlyWhenNecessary4() throws Exception
159     {
160         facesContext.addMessage("test1", new FacesMessage(FacesMessage.SEVERITY_FATAL, "fatalSumary", "detailFatalSummary"));
161         messages.setId("msgPanel");
162         messages.setFatalClass("fatalClass");
163         messages.encodeEnd(facesContext);
164         facesContext.renderResponse();
165         String output = writer.getWriter().toString();
166         assertTrue(output.contains("fatalSumary"));
167         assertTrue(output.contains("li class=\"fatalClass\""));
168         assertTrue(!output.contains("span"));
169     }
170     
171     /**
172      * It should output the class on td
173      * @throws Exception
174      */
175     public void testRenderSpanOnlyWhenNecessary5() throws Exception
176     {
177         facesContext.addMessage("test1", new FacesMessage(FacesMessage.SEVERITY_FATAL, "fatalSumary", "detailFatalSummary"));
178         messages.setId("msgPanel");
179         messages.setLayout("table");
180         messages.setFatalClass("fatalClass");
181         messages.encodeEnd(facesContext);
182         facesContext.renderResponse();
183         String output = writer.getWriter().toString();
184         assertTrue(output.contains("fatalSumary"));
185         assertTrue(output.contains("td class=\"fatalClass\""));
186         assertTrue(!output.contains("span"));
187     }
188     
189     public void testHtmlPropertyPassTruNotRendered() throws Exception
190     {
191         HtmlRenderedAttr[] attrs = HtmlCheckAttributesUtil.generateAttrsNotRenderedForReadOnly();
192         
193         facesContext.addMessage("test1", new FacesMessage(FacesMessage.SEVERITY_WARN, "warnSumary", "detailWarnSummary"));
194 
195         messages.setErrorClass(ERROR_CLASS);
196         messages.setWarnClass(WARN_CLASS);
197         messages.setInfoClass(INFO_CLASS);
198         messages.setWarnStyle("warnStyle");
199         
200         messages.setLayout("table");
201         messages.setStyle("left: 48px; top: 432px; position: absolute");
202         
203         MockResponseWriter writer = (MockResponseWriter)facesContext.getResponseWriter();
204         HtmlCheckAttributesUtil.checkRenderedAttributes(
205                 messages, facesContext, writer, attrs);
206         if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) {
207             fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
208         }
209     }
210 }