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.view.facelets.tag.ui;
20  
21  import javax.el.ExpressionFactory;
22  import javax.faces.application.ViewHandler;
23  import javax.faces.component.UIOutput;
24  import javax.faces.component.UIViewRoot;
25  import javax.faces.component.html.HtmlOutputText;
26  import javax.faces.context.ResponseWriter;
27  
28  import org.apache.myfaces.config.RuntimeConfig;
29  import org.apache.myfaces.renderkit.html.HtmlTextRenderer;
30  import org.apache.myfaces.test.mock.MockExternalContext;
31  import org.apache.myfaces.test.mock.MockResponseWriter;
32  import org.apache.myfaces.view.facelets.FaceletTestCase;
33  import org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage;
34  import org.apache.myfaces.view.facelets.util.FastWriter;
35  import org.junit.Assert;
36  import org.junit.Test;
37  
38  public class TestUserTags extends FaceletTestCase {
39  
40      @Override
41      protected void setUpServletObjects() throws Exception
42      {
43          super.setUpServletObjects();
44          servletContext.addInitParameter(ViewHandler.FACELETS_SKIP_COMMENTS_PARAM_NAME, "true");
45          servletContext.addInitParameter(ViewHandler.FACELETS_LIBRARIES_PARAM_NAME, "/user.taglib.xml");
46      }
47  
48      @Override
49      protected ExpressionFactory createExpressionFactory()
50      {
51          // For this test we need the a real one, because the Mock does not
52          // handle VariableMapper stuff properly and ui:param logic will not work
53          return new org.apache.el.ExpressionFactoryImpl();
54      }
55      
56      @Override
57      protected void setupComponents() throws Exception
58      {
59          application.addComponent(UIViewRoot.COMPONENT_TYPE,
60                  UIViewRoot.class.getName());
61          application.addComponent(HtmlOutputText.COMPONENT_TYPE,
62                  HtmlOutputText.class.getName());
63      }
64  
65      @Override
66      protected void setupConvertersAndValidators() throws Exception
67      {
68      }
69  
70      @Override
71      protected void setupRenderers() throws Exception
72      {
73          renderKit.addRenderer(UIOutput.COMPONENT_FAMILY,
74                  "javax.faces.Text", new HtmlTextRenderer());
75      }
76      
77      @Test
78      public void testClientClient() throws Exception {
79          request.setAttribute("test", "foo");
80          
81          UIViewRoot root = facesContext.getViewRoot();
82          vdl.buildView(facesContext, root, "test-tags.xml");
83          
84          FastWriter fw = new FastWriter();
85          MockResponseWriter mrw = new MockResponseWriter(fw);
86          facesContext.setResponseWriter(mrw);
87          root.encodeAll(facesContext);
88          //System.out.println(fw);
89      }
90  
91      /**
92       * Simple attribute passing should only be available on target xhtml source
93       * 
94       * @throws Exception
95       */
96      @Test
97      public void testUserTag1() throws Exception 
98      {
99          UIViewRoot root = facesContext.getViewRoot();
100         vdl.buildView(facesContext, root, "usertagtest1.xhtml");
101         
102         FastWriter fw = new FastWriter();
103         ResponseWriter rw = facesContext.getResponseWriter();
104         rw = rw.cloneWithWriter(fw);
105         facesContext.setResponseWriter(rw);
106         root.encodeAll(facesContext);
107         rw.flush();
108         
109         String result = fw.toString();
110         Assert.assertTrue("Output should contain 'value1'", result.contains("value1"));
111         Assert.assertFalse("Output should not contain 'value2'", result.contains("value2"));
112         Assert.assertFalse("Output should not contain 'value3'", result.contains("value3"));
113     }
114     
115     /**
116      * Attributes should not pass to other user tags if they are not declared explicitly.
117      * 
118      * @throws Exception
119      */
120     @Test
121     public void testUserTag2() throws Exception 
122     {
123         UIViewRoot root = facesContext.getViewRoot();
124         vdl.buildView(facesContext, root, "usertagtest2.xhtml");
125         
126         FastWriter fw = new FastWriter();
127         ResponseWriter rw = facesContext.getResponseWriter();
128         rw = rw.cloneWithWriter(fw);
129         facesContext.setResponseWriter(rw);
130         root.encodeAll(facesContext);
131         rw.flush();
132         
133         String result = fw.toString();
134         Assert.assertTrue("Output should contain 'value2'", result.contains("value2"));
135         Assert.assertFalse("Output should not contain 'value3'", result.contains("value3"));
136         Assert.assertFalse("Output should not contain 'value1'", result.contains("value1"));
137     } 
138 }