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.ext;
20  
21  import java.io.IOException;
22  
23  import javax.faces.application.FacesMessage;
24  import javax.faces.component.html.HtmlForm;
25  import javax.faces.component.html.HtmlInputText;
26  import javax.faces.component.html.HtmlOutputLabel;
27  
28  import junit.framework.Test;
29  import junit.framework.TestSuite;
30  
31  import org.apache.myfaces.component.html.ext.HtmlMessage;
32  import org.apache.myfaces.test.AbstractTomahawkViewControllerTestCase;
33  import org.apache.myfaces.test.mock.MockResponseWriter;
34  import org.apache.myfaces.test.utils.HtmlCheckAttributesUtil;
35  import org.apache.myfaces.test.utils.HtmlRenderedAttr;
36  
37  public class HtmlMessageRendererTest extends AbstractTomahawkViewControllerTestCase
38  {
39      private static final String ERROR_CLASS = "errorClass";
40      private static final String WARN_CLASS = "warnClass";
41      private static final String INFO_CLASS = "infoClass";
42      
43      private HtmlMessage message;
44      private HtmlForm form;
45      private HtmlInputText inputText;
46  
47      public HtmlMessageRendererTest(String name)
48      {
49          super(name);
50      }
51      
52      public static Test suite() {
53          return new TestSuite(HtmlMessageRendererTest.class);
54      }
55  
56      public void setUp() throws Exception
57      {
58          super.setUp();
59  
60          form = new HtmlForm();
61          form.setId("myFormId");
62          inputText = new HtmlInputText();
63          inputText.setParent(form);
64          inputText.setId("myInputId");
65          
66          message = new HtmlMessage();
67          message.setErrorClass(ERROR_CLASS);
68          message.setWarnClass(WARN_CLASS);
69          message.setInfoClass(INFO_CLASS);
70          message.setParent(form);
71          
72          form.getChildren().add(inputText);
73          form.getChildren().add(message);
74          
75          facesContext.getViewRoot().getChildren().add(form);
76      }
77  
78      public void tearDown() throws Exception
79      {
80          super.tearDown();
81      }    
82      
83      public void testHtmlPropertyPassTru() throws Exception
84      {
85          facesContext.addMessage(inputText.getClientId(facesContext), 
86                  new FacesMessage("Validation message here."));
87  
88      	HtmlRenderedAttr[] attrs = {
89              //_UniversalProperties
90              new HtmlRenderedAttr("dir"), 
91              new HtmlRenderedAttr("lang"), 
92              new HtmlRenderedAttr("title"),
93              //_EventProperties
94              new HtmlRenderedAttr("onclick"), 
95              new HtmlRenderedAttr("ondblclick"), 
96              new HtmlRenderedAttr("onkeydown"), 
97              new HtmlRenderedAttr("onkeypress"),
98              new HtmlRenderedAttr("onkeyup"), 
99              new HtmlRenderedAttr("onmousedown"), 
100             new HtmlRenderedAttr("onmousemove"), 
101             new HtmlRenderedAttr("onmouseout"),
102             new HtmlRenderedAttr("onmouseover"), 
103             new HtmlRenderedAttr("onmouseup"),
104             //_StyleProperties
105             new HtmlRenderedAttr("style"), 
106             new HtmlRenderedAttr("styleClass", "styleClass", "class=\"infoClass\""),
107         };
108         
109         facesContext.addMessage("test1", new FacesMessage(FacesMessage.SEVERITY_WARN, "warnSumary", "detailWarnSummary"));
110         message.setStyle("left: 48px; top: 432px; position: absolute");
111         message.setFor("myInputId");
112         
113         MockResponseWriter writer = (MockResponseWriter)facesContext.getResponseWriter();
114         HtmlCheckAttributesUtil.checkRenderedAttributes(
115                 message, facesContext, writer, attrs);
116         if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) {
117             fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
118         }
119     }
120     
121     public void testLabelClientIdSubstitution() throws IOException {
122         final String ERROR_MESSAGE = "Please enter a number.";
123         final String LABEL_TEXT = "My Numeric Input Field";
124 
125         // This is the kind of message that will get generated by a JSF 1.2 converter
126         // Note that the full client id is used, instead of just the id
127         //
128         facesContext.addMessage(inputText.getClientId(facesContext), new FacesMessage(FacesMessage.SEVERITY_ERROR,
129                 "myFormId:myInputId: " + ERROR_MESSAGE, 
130                 "myFormId:myInputId: " + ERROR_MESSAGE));
131 
132         // This is the label for our text field
133         //
134         HtmlOutputLabel label = new HtmlOutputLabel();
135         label.setValue(LABEL_TEXT);
136         label.setFor("myInputId");
137         form.getChildren().add(label);
138 
139         // Render the HTML
140         //
141         message.setFor("myInputId");
142         message.encodeAll(facesContext);
143 
144         MockResponseWriter writer = (MockResponseWriter) facesContext.getResponseWriter();
145         String resultingHtml = writer.getWriter().toString();
146 
147         // Check that the full client id was properly replaced with the label text
148         //
149         assertTrue(("<span class=\"errorClass\">" + LABEL_TEXT + ": " + ERROR_MESSAGE + "</span>")
150                 .equals(resultingHtml));
151     }
152 }