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