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.context.servlet;
20  
21  import org.apache.myfaces.test.base.junit4.AbstractJsfTestCase;
22  import org.junit.After;
23  import org.junit.Assert;
24  import org.junit.Before;
25  import org.junit.Test;
26  import org.junit.runner.RunWith;
27  import org.junit.runners.JUnit4;
28  
29  import java.io.IOException;
30  import java.io.StringWriter;
31  import java.util.Arrays;
32  import java.util.Collection;
33  import java.util.HashMap;
34  import java.util.HashSet;
35  import java.util.List;
36  import java.util.Map;
37  import java.util.Set;
38  
39  import javax.faces.component.UIViewRoot;
40  import javax.faces.context.FacesContext;
41  
42  import org.apache.myfaces.view.facelets.FaceletTestCase;
43  import org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage;
44  
45  /**
46   * Test cases for ServletExternalContextImpl.
47   * 
48   * @author Bill Lucy (latest modification by $Author$)
49   * @version $Revision$ $Date$
50   */
51  @RunWith(JUnit4.class)
52  public class FacesContextImplBaseTest extends FaceletTestCase {
53  
54      @Before
55      @Override
56      public void setUp() throws Exception {
57          super.setUp();
58      }
59  
60      @After
61      @Override
62      public void tearDown() throws Exception {
63          super.tearDown();
64      }
65  
66      /**
67       * Verify that the Map returned by UIViewRoot.getViewMap() is cleared when a new view root is set,
68       * as required in 4.1.19.2
69       */
70      @Test
71      public void testViewMapCleared() throws Exception
72      {
73          // don't use MockFacesServletContext here
74          FacesContextImpl fci = new  FacesContextImpl(facesContext.getExternalContext(), null, null);
75          UIViewRoot firstRoot = new UIViewRoot();
76          UIViewRoot secondRoot = new UIViewRoot();
77          fci.setViewRoot(firstRoot);
78          Map<String, Object> viewMap = firstRoot.getViewMap();
79          viewMap.put("entry", Boolean.TRUE);
80  
81          // set a new view root, which will cause the old view root's ViewMap to be cleared if BUILDING_VIEW_METADATA is not set
82          fci.setViewRoot(secondRoot);
83          Assert.assertEquals("The ViewMap was not cleared as expected", 0, viewMap.size());
84      }
85  
86      /**
87       * Make sure the Map returned by UIViewRoot.getViewMap() is NOT cleared when a new view root is set while the 
88       * BUILDING_VIEW_METADATA attribute is set.  See MYFACES-4282.
89       */
90      @Test
91      public void testViewMapNotClearedWhileBuildingViewMetadata() throws Exception
92      {
93          FacesContextImpl fci = new  FacesContextImpl(facesContext.getExternalContext(), null, null);
94          UIViewRoot firstRoot = new UIViewRoot();
95          UIViewRoot secondRoot = new UIViewRoot();
96          fci.getAttributes().put(FaceletViewDeclarationLanguage.BUILDING_VIEW_METADATA, Boolean.TRUE);
97          fci.setViewRoot(firstRoot);
98          Map<String, Object> viewMap = firstRoot.getViewMap();
99          viewMap.put("entry", Boolean.TRUE);
100 
101         // set a new view root - the old view root's ViewMap should NOT be cleared
102         fci.setViewRoot(secondRoot);
103         Assert.assertEquals("The ViewMap was incorrectly cleared while the BUILDING_VIEW_METADATA attribute was set", 1, viewMap.size());
104     }
105 }