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.impl;
20  
21  import java.io.IOException;
22  import java.util.List;
23  import javax.faces.component.UIComponent;
24  import javax.faces.component.UIViewRoot;
25  import org.apache.myfaces.view.facelets.FaceletTestCase;
26  import org.junit.Assert;
27  import org.junit.Test;
28  
29  public class ResourceMarkDeleteTest  extends FaceletTestCase {
30  
31      @Test
32      public void test_only_ajs_is_included() throws IOException
33      {
34          UIViewRoot root = facesContext.getViewRoot();
35  
36          // Building the view
37          // Because 'includeResource' is not set the second resource should not be included
38          vdl.buildView(facesContext, root, "test_conditional_include_resources.xhtml");
39  
40          List<UIComponent> resources = root.getComponentResources(facesContext, "head");
41  
42          Assert.assertTrue("Only one script is included.", resources.size() == 1);
43          Assert.assertTrue("a.js is included.", resources.get(0).getAttributes().get("name").equals("a.js"));
44      }
45  
46      @Test
47      public void test_ajs_and_xjs_are_included() throws IOException
48      {
49          facesContext.getAttributes().put("includeResource", Boolean.TRUE);
50          UIViewRoot root = facesContext.getViewRoot();
51  
52          // Building the view
53          // Because 'includeResource' is now set the second resource should be included
54          vdl.buildView(facesContext, root, "test_conditional_include_resources.xhtml");
55  
56          List<UIComponent> resources = root.getComponentResources(facesContext, "head");
57  
58          Assert.assertTrue("Two scripts are included.", resources.size() == 2);
59      }
60  
61      @Test
62      public void test_only_ajs_after_refresh_view_is_included() throws IOException
63      {
64      	UIViewRoot view = facesContext.getViewRoot();
65  
66          // Building the initial view
67          // Because 'includeResource' is not set the second resource should not be included
68          vdl.buildView(facesContext, view, "test_conditional_include_resources.xhtml");
69  
70          {
71              List<UIComponent> resources = view.getComponentResources(facesContext, "head");
72  
73              Assert.assertTrue("Only one script is included.", resources.size() == 1);
74          }
75  
76          // reset 'isFilledView'
77          facesContext.getAttributes().remove(view);
78  
79          // Building the view a second time
80          // Because 'includeResource' is now set the second resource should be included
81          facesContext.getAttributes().put("includeResource", Boolean.TRUE);
82          vdl.buildView(facesContext, view);
83  
84          {
85              List<UIComponent> resources = view.getComponentResources(facesContext, "head");
86  
87              Assert.assertTrue("Two scripts are included.", resources.size() == 2);
88          }
89  
90          // reset 'isFilledView'
91          facesContext.getAttributes().remove(view);
92          // Building the view a third time
93          // Because 'includeResource' is now removed the second resource should not be included
94  
95          facesContext.getAttributes().remove("includeResource");
96          vdl.buildView(facesContext, view);
97  
98          {
99              List<UIComponent> resources = view.getComponentResources(facesContext, "head");
100 
101             Assert.assertTrue("Only one script is included.", resources.size() == 1);
102         }
103     }
104 }