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;
20  
21  import java.io.StringWriter;
22  
23  import javax.el.ValueExpression;
24  import javax.faces.component.UIViewRoot;
25  import javax.faces.component.html.HtmlOutputText;
26  import javax.validation.constraints.AssertTrue;
27  
28  import junit.framework.Test;
29  import junit.framework.TestSuite;
30  
31  import org.apache.myfaces.renderkit.html.HtmlTextRenderer;
32  import org.apache.myfaces.shared.util.StateUtils;
33  import org.apache.myfaces.shared_impl.util.serial.DefaultSerialFactory;
34  import org.apache.myfaces.test.base.AbstractJsfTestCase;
35  import org.apache.myfaces.test.mock.MockRenderKitFactory;
36  import org.apache.myfaces.test.mock.MockResponseWriter;
37  
38  /**
39   * @author Bruno Aranda (latest modification by $Author: struberg $)
40   * @version $Revision: 1188235 $ $Date: 2011-10-24 13:09:33 -0400 (Mon, 24 Oct 2011) $
41   */
42  public class ErrorPageWriterTest extends AbstractJsfTestCase
43  {
44      public static Test suite()
45      {
46          return new TestSuite(ErrorPageWriterTest.class); // needed in maven
47      }
48  
49      private MockResponseWriter writer ;
50      private HtmlOutputText outputText;
51  
52      public ErrorPageWriterTest(String name)
53      {
54          super(name);
55      }
56  
57      public void setUp() throws Exception
58      {
59          super.setUp();
60  
61          outputText = new HtmlOutputText();
62  
63          writer = new MockResponseWriter(new StringWriter(), null, null);
64          facesContext.setResponseWriter(writer);
65          // TODO remove these two lines once myfaces-test goes alpha, see MYFACES-1155
66          facesContext.getViewRoot().setRenderKitId(MockRenderKitFactory.HTML_BASIC_RENDER_KIT);
67          facesContext.getRenderKit().addRenderer(
68                  outputText.getFamily(),
69                  outputText.getRendererType(),
70                  new HtmlTextRenderer());
71          servletContext.setAttribute(StateUtils.SERIAL_FACTORY, new DefaultSerialFactory());
72  
73          facesContext.getAttributes().put("org.apache.myfaces.RENDERED_JSF_JS", Boolean.TRUE);
74      }
75  
76      public void tearDown() throws Exception
77      {
78          super.tearDown();
79          outputText = null;
80          writer = null;
81      }
82  
83      public void testValueExpressionGetExpressionStringReturnsNull()
84      {
85          //See MYFACES-3413 for details
86          UIViewRoot root = facesContext.getViewRoot();
87  //        UIForm form = new UIForm();
88  //        form.setId("formId");
89  //        
90  //        form.getChildren().add(inputText);
91          root.getChildren().add(outputText);
92  
93          ValueExpression ve = new NullReturningGetExpressionStringValueExpression();
94          
95          outputText.setValueExpression("rendered", ve);
96          String id = "testValueExpressionGetExpressionStringReturnsNullOutputComponent";
97          outputText.setId(id);
98          try 
99          {
100             StringWriter w = new StringWriter();
101             Throwable t = new Throwable("Placeholder throwable");
102             ErrorPageWriter.debugHtml(w, facesContext, t);
103             String output = w.toString();
104             int indexOfOutputComponentId = output.indexOf(id);
105             String surroundingText = "output component not found.";
106             if (-1 != indexOfOutputComponentId) {
107                 surroundingText = output.substring(Math.max(0, indexOfOutputComponentId - 20), Math.min(output.length(), indexOfOutputComponentId + 280));
108             }
109             int indexOfHasRenderedAttribute = output.indexOf("rendered=\"\"");
110             boolean hasRenderedAttribute = (-1 != indexOfHasRenderedAttribute);
111             assertTrue("rendered attribute wasn't written correctly: " + surroundingText, hasRenderedAttribute);
112         }
113         catch (Exception e)
114         {
115             fail(e.getMessage());
116         }
117     }
118     
119 }