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.lifecycle;
20  
21  import javax.faces.component.UIViewRoot;
22  
23  import junit.framework.Assert;
24  
25  import org.apache.myfaces.test.base.junit4.AbstractJsfTestCase;
26  import org.junit.Test;
27  
28  public class RenderResponseExecutorTest extends AbstractJsfTestCase
29  {
30  
31      @Test
32      public void testNavigationCondition() throws Exception
33      {
34          UIViewRoot a = new UIViewRoot();
35          UIViewRoot b = new UIViewRoot();
36          UIViewRoot c = new UIViewRoot();
37          UIViewRoot d = new UIViewRoot();
38          a.setViewId("/a.xhtml");
39          b.setViewId("/a.xhtml");
40          c.setViewId("/c.xhtml");
41  
42          //If the view was not changed continue (return false)
43          Assert.assertFalse(checkCondition(a, a));
44          
45          //If the view is different instance but same viewId iterate again (return true) 
46          Assert.assertTrue(checkCondition(a, b));
47          
48          //If the view is different instance and different id iterate again
49          Assert.assertTrue(checkCondition(a, c));
50          
51          //If the view is different instance and id is null iterate again
52          Assert.assertTrue(checkCondition(a, d));
53          
54          //If the view is different instance and id is not null iterate again
55          Assert.assertTrue(checkCondition(d, a));
56  
57          //If the view was not change continue (return false)
58          Assert.assertFalse(checkCondition(d, d));
59      }
60      
61      protected boolean checkCondition(UIViewRoot previousRoot, UIViewRoot root)
62      {
63          String viewId = previousRoot.getViewId();
64          String newViewId = (root == null) ? null : root.getViewId();
65          
66          boolean isNotSameRoot = !( (newViewId == null ? newViewId == viewId : newViewId.equals(viewId) ) && 
67                  previousRoot.equals(root) );
68          
69          if ((newViewId == null && viewId != null) 
70                  || (newViewId != null && (!newViewId.equals(viewId) || isNotSameRoot ) ))
71          {
72              return true;
73          }
74          else
75          {
76              return false;
77          }
78      }
79  }