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