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.trinidad.component;
20  
21  import javax.faces.context.FacesContext;
22  
23  import junit.framework.Test;
24  import junit.framework.TestSuite;
25  import junit.textui.TestRunner;
26  
27  import org.apache.myfaces.trinidad.component.UIComponentTestCase;
28  import org.apache.myfaces.trinidad.component.UIXTableTest.DoNotCallBinding;
29  import org.apache.myfaces.trinidad.context.MockRequestContext;
30  
31  /**
32   * Unit tests for UIXTree
33   *
34   */
35  public class UIXTreeTest extends UIComponentTestCase
36  {
37    /**
38     * @param testName  the unit test name
39     */
40    public UIXTreeTest(
41      String testName)
42    {
43      super(testName);
44    }
45  
46  
47  
48    private MockRequestContext _mafct;
49  
50    @Override
51    public void setUp() throws Exception
52    {
53      super.setUp();
54      _mafct = new MockRequestContext();
55    }
56  
57    @Override
58    public void tearDown() throws Exception
59    {
60      _mafct.release();
61      _mafct = null;
62      super.tearDown();
63    }
64  
65  
66    public static Test suite()
67    {
68      return new TestSuite(UIXTreeTest.class);
69    }
70  
71  
72    /**
73     * make sure that the model is not executed at invalid or unnecessary times.
74     * valueBindings cannot be called during restoreState.
75     * Also table model must not be executed if rendered="false".
76     * However, saveState is called even if rendered="false" on a component.
77     * Therefore, saveState should not call getValue() on the table.
78     */
79    public void testSaveRestoreStateGetValue()
80    {
81      // make sure that getValue() is not called during restoreState:
82      DoNotCallBinding doNotCall = new DoNotCallBinding();
83      doNotCall.doNotCall = true;
84      final Object state;
85      {
86        UIXTree tree = _createTree();
87        tree.setValueBinding("value", doNotCall);
88        state = tree.processSaveState(facesContext);
89      }
90  
91      UIXTree tree = _createTree();
92      // this value binding should be restored during processRestoreState;
93      // however, set it anyway just to catch any getValue() calls prior to
94      // that.
95      tree.setValueBinding("value", doNotCall);
96      tree.processRestoreState(facesContext, state);
97  
98      assertTrue(tree.getValueBinding("value") instanceof DoNotCallBinding);
99  
100   }
101 
102   @Override
103   public void setCurrentContext(FacesContext fc)
104   {
105     // prevent removal of facesContext before we are done testing:
106     if (fc != null)
107       super.setCurrentContext(fc);
108   }
109 
110 
111   public static void main(String[] args)
112   {
113     TestRunner.run(UIXTreeTest.class);
114   }
115 
116   @Override
117   protected boolean isRendererUsed()
118   {
119     // we use our own MockRenderer; not the one created by our super class:
120     return false;
121   }
122 
123   private UIXTree _createTree()
124   {
125     UIXTree tree = new UIXTree();
126     return tree;
127   }
128 
129 }