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.faces.component.UIViewRoot;
22  import javax.faces.context.ResponseWriter;
23  
24  import org.apache.myfaces.view.facelets.FaceletTestCase;
25  import org.apache.myfaces.view.facelets.util.FastWriter;
26  import org.junit.Assert;
27  import org.junit.Test;
28  
29  /**
30   * Test cases related to the Faceletes templating mechanism.
31   * 
32   * @author Jakob Korherr (latest modification by $Author: jakobk $)
33   * @version $Revision: 980006 $ $Date: 2010-07-28 04:43:17 -0500 (Wed, 28 Jul 2010) $
34   */
35  public class TemplateTestCase extends FaceletTestCase
36  {
37  
38      private FastWriter _writer;
39  
40      public void setUp() throws Exception
41      {
42          super.setUp();
43  
44          // install the FastWriter
45          _writer = new FastWriter();
46          ResponseWriter rw = facesContext.getResponseWriter();
47          rw = rw.cloneWithWriter(_writer);
48          facesContext.setResponseWriter(rw);
49      }
50  
51      @Override
52      public void tearDown() throws Exception
53      {
54          _writer = null;
55  
56          super.tearDown();
57      }
58  
59      /**
60       * Tests the case when <ui:insert> is used without a name attribute.
61       * In this case the body of the surrounding <ui:composition> must be inserted.
62       * @throws Exception
63       */
64      @Test
65      public void testInsertWithoutNameComposition() throws Exception
66      {
67          _testInsertWithoutName("s_page_composition.xhtml");
68      }
69  
70      /**
71       * Tests the case when <ui:insert> is used without a name attribute.
72       * In this case the body of the surrounding <ui:decorate> must be inserted.
73       * @throws Exception
74       */
75      @Test
76      public void testInsertWithoutNameDecorate() throws Exception
77      {
78          _testInsertWithoutName("s_page_decorate.xhtml");
79      }
80  
81      private void _testInsertWithoutName(String page) throws Exception
82      {
83          UIViewRoot root = facesContext.getViewRoot();
84          vdl.buildView(facesContext, root, page);
85  
86          // render view
87          root.encodeAll(facesContext);
88          final String output = _writer.toString();
89  
90          // assertions
91          Assert.assertTrue(output.contains("<span id=\"body\">BODY</span>"));
92          Assert.assertTrue(output.contains("<span id=\"popupSpan\"><p>[POPUPCONTENT]</p></span>"));
93      }
94  
95      /**
96       * Tests the case when there's a <ui:decorate> with a template that inserts
97       * a property defined by the <ui:decorate>.
98       * @throws Exception
99       */
100     @Test
101     public void testInsertWithDefinedNameDecorate() throws Exception
102     {
103         _testInsertWithDefinedName("decorate_with_template_and_define.xhtml");
104     }
105 
106     /**
107      * Tests the case when there's a <ui:composition> with a template that inserts
108      * a property defined by the <ui:composition>.
109      * @throws Exception
110      */
111     @Test
112     public void testInsertWithDefinedNameComposition() throws Exception
113     {
114         _testInsertWithDefinedName("composition_with_template_and_define.xhtml");
115     }
116 
117     private void _testInsertWithDefinedName(String page) throws Exception
118     {
119         UIViewRoot root = facesContext.getViewRoot();
120         vdl.buildView(facesContext, root, page);
121 
122         // render view
123         root.encodeAll(facesContext);
124         final String output = _writer.toString();
125 
126         // assertions
127         Assert.assertTrue(output.contains("<span>defined content</span>"));
128     }
129 
130     /**
131      * Tests the case when there's a <ui:decorate> with a template that wants to
132      * insert a property defined by the <ui:decorate>, but the property was not
133      * defined. In this case the default value (= body of <ui:insert>) must be rendered.
134      * @throws Exception
135      */
136     @Test
137     public void testInsertWithoutDefinedNameDecorate() throws Exception
138     {
139         _testInsertWithoutDefinedName("decorate_with_template.xhtml");
140     }
141 
142     /**
143      * Tests the case when there's a <ui:composition> with a template that wants to
144      * insert a property defined by the <ui:composition>, but the property was not
145      * defined. In this case the default value (= body of <ui:insert>) must be rendered.
146      * @throws Exception
147      */
148     @Test
149     public void testInsertWithoutDefinedNameComposition() throws Exception
150     {
151         _testInsertWithoutDefinedName("composition_with_template.xhtml");
152     }
153 
154     private void _testInsertWithoutDefinedName(String page) throws Exception
155     {
156         UIViewRoot root = facesContext.getViewRoot();
157         vdl.buildView(facesContext, root, page);
158 
159         // render view
160         root.encodeAll(facesContext);
161         final String output = _writer.toString();
162 
163         // assertions
164         Assert.assertTrue(output
165                 .contains("<span>default ui:insert content</span>"));
166     }
167 
168     /**
169      * Tests multilevel templating. In this case multilevel_template_OuterClient_with_define.xhtml
170      * is a <ui:decorate> which has a template and a defined content of a <ui:include> of
171      * multilevel_template_InnerClient_with_define.xhtml, which itself has a template and a defined
172      * content of "inner content".
173      * @throws Exception
174      */
175     @Test
176     public void _testMultilevelTemplating() throws Exception
177     {
178         UIViewRoot root = facesContext.getViewRoot();
179         vdl.buildView(facesContext, root,
180                 "multilevel_template_OuterClient_with_define.xhtml");
181 
182         // render view
183         root.encodeAll(facesContext);
184         String output = _writer.toString();
185         output = output.replaceAll("(?s)<!--.*?-->", ""); // remove license headers for clarity
186 
187         // assertions
188         Assert.assertTrue(output.contains("<body>inner content</body>"));
189     }
190 
191     /**
192      * Tests multilevel templating. In this case multilevel_template_OuterClient_with_define.xhtml
193      * is a <ui:decorate> which has a template and a defined content of a <ui:include> of
194      * multilevel_template_InnerClient_with_define.xhtml, which itself has a template, but not a
195      * defined content. However it inserts content and thus it must use the fallback content of
196      * the innerTemplate.
197      * @throws Exception
198      */
199     @Test
200     public void _testMultilevelTemplatingInnerFallback() throws Exception
201     {
202         UIViewRoot root = facesContext.getViewRoot();
203         vdl.buildView(facesContext, root,
204                 "multilevel_template_OuterClient.xhtml");
205 
206         // render view
207         root.encodeAll(facesContext);
208         String output = _writer.toString();
209         output = output.replaceAll("(?s)<!--.*?-->", ""); // remove license headers for clarity
210 
211         // assertions
212         Assert.assertTrue(output
213                 .contains("<body>inner fallback content</body>"));
214     }
215 
216 }