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