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 javax.faces.component;
20  
21  import java.util.List;
22  import java.util.Map;
23  
24  /**
25   * A generic framework less testcase for our _DeltaStateHelper class!
26   *
27   * @author Werner Punz (latest modification by $Author: lu4242 $)
28   * @version $Rev: 980922 $ $Date: 2010-07-30 13:50:40 -0500 (Fri, 30 Jul 2010) $
29   */
30  public class _DeltaStateHelperTest extends AbstractComponentTest
31  {
32  
33      private static final String KEY3 = "key3";
34      private static final String KEY5 = "key5";
35      private static final String KEY_2_1 = "key_2_1";
36      private static final String VAL1 = "val1";
37      private static final String VAL2 = "val2";
38      private static final String VAL3 = "val3";
39      private static final String KEY1 = "key1";
40      private static final String KEY2 = "key2";
41      private static final String KEY_2_2 = "key_2_2";
42      private static final String VAL5 = "val5";
43      ProbeDeltaStateHelper _instance = null;
44  
45      private void assertStructure()
46      {
47          assertTrue("check for key1", _instance.get(KEY1).equals(VAL1));
48          assertTrue("check for key2", _instance.get(KEY2) instanceof Map);
49          assertTrue("check for key3", _instance.get(KEY3) instanceof List);
50  
51          assertTrue("check for list size",
52                  ((List) _instance.get(KEY3)).size() >= 1);
53          assertTrue("check for map entries", ((Map) _instance.get(KEY2)).get(
54                  KEY_2_2).equals(VAL3));
55          assertTrue("check for map entries", ((Map) _instance.get(KEY2)).get(
56                  KEY_2_1).equals(VAL2));
57  
58      }
59  
60      /**
61       * class needed to get a jsf less behavior
62       * so that we can add a jsf less testcase here!
63       */
64      class ProbeDeltaStateHelper extends _DeltaStateHelper
65      {
66  
67          boolean _initialStateMarked = true;
68  
69          public ProbeDeltaStateHelper()
70          {
71              super(null);
72          }
73  
74          @Override
75          protected boolean isInitialStateMarked()
76          {
77              return _initialStateMarked;
78          }
79  
80          public void setInitialStateMarked(boolean initState)
81          {
82              _initialStateMarked = initState;
83          }
84      }
85  
86      public _DeltaStateHelperTest(String testName)
87      {
88          super(testName);
89      }
90  
91      @Override
92      protected void setUp() throws Exception
93      {
94  
95          super.setUp();
96  
97          _instance = new ProbeDeltaStateHelper();
98          _instance.setInitialStateMarked(true);
99      }
100 
101     @Override
102     protected void tearDown() throws Exception
103     {
104         super.tearDown();
105 
106         _instance = null;
107     }
108 
109     /**
110      * Test of isInitalStateMarked method, of class _DeltaStateHelper.
111      */
112     public void testIsInitalStateMarked()
113     {
114         assertTrue("Initial state must be marked", _instance
115                 .isInitialStateMarked());
116         _instance.setInitialStateMarked(false);
117         assertFalse("Initial state must be false", _instance
118                 .isInitialStateMarked());
119     }
120 
121     /**
122      * Test of add method, of class _DeltaStateHelper.
123      */
124     public void testAdd()
125     {
126         _instance.add(KEY1, VAL1);
127         Object val = _instance.get(KEY1);
128         assertTrue("Value must be list", val instanceof List);
129         assertTrue("Value size must be one", ((List) val).size() == 1);
130 
131         _instance.add(KEY1, new Integer(2));
132         _instance.add(KEY2, new Integer(2));
133 
134         val = _instance.get(KEY1);
135         assertTrue("Value must be list", val instanceof List);
136         assertTrue("Value size must be one", ((List) val).size() == 2);
137 
138         assertTrue("Value msut be of type string and must equal val1",
139                 ((List) val).get(0).equals(VAL1));
140 
141         assertTrue("Value msut be of type int and must equal 2", ((List) val)
142                 .get(1).equals(new Integer(2)));
143 
144         val = _instance.get(KEY2);
145         assertTrue("Value must be list", val instanceof List);
146         assertTrue("Value size must be one", ((List) val).size() == 1);
147     }
148 
149     /**
150      * specialized setup for our get tests
151      */
152     private void _setupGetTests()
153     {
154 
155         _instance.put(KEY1, VAL1);
156         _instance.put(KEY2, KEY_2_1, VAL2);
157         _instance.put(KEY2, KEY_2_2, VAL3);
158 
159         _instance.add(KEY3, VAL3);
160     }
161 
162     /**
163      * Test of get method, of class _DeltaStateHelper.
164      */
165     public void testGet()
166     {
167         _setupGetTests();
168         assertStructure();
169     }
170 
171     /**
172      * Test of put method, of class _DeltaStateHelper.
173      */
174     public void testPut_Serializable_Object()
175     {
176         _setupGetTests();
177 
178         assertTrue("check for key1", _instance.get(KEY1).equals(VAL1));
179 
180         Map entry = (Map) _instance.get(KEY2);
181         assertTrue("check for key2", _instance.get(KEY2) instanceof Map);
182 
183         assertTrue("check for key2 structure", entry.size() == 2
184                 && entry.get(KEY_2_1).equals(VAL2)
185                 && entry.get(KEY_2_2).equals(VAL3));
186     }
187 
188     public void testPut_null()
189     {
190         _instance.put(KEY1, null);
191         _instance.put(KEY2, null);
192 
193         assertNull("key1 is not null", _instance.get(KEY1));
194         assertNull("key2 is not null", _instance.get(KEY2));
195 
196         _setupGetTests();
197         assertTrue("check for key1", _instance.get(KEY1).equals(VAL1));
198 
199         Map entry = (Map) _instance.get(KEY2);
200         assertTrue("check for key2", _instance.get(KEY2) instanceof Map);
201 
202         assertTrue("check for key2 structure", entry.size() == 2
203                 && entry.get(KEY_2_1).equals(VAL2)
204                 && entry.get(KEY_2_2).equals(VAL3));
205 
206         _instance.put(KEY1, null);
207         assertNull("key1 is not null", _instance.get(KEY1));
208     }
209 
210     /**
211      * Test of put method, of class _DeltaStateHelper.
212      */
213     public void testPut_3args()
214     {
215         //covered already by testPut_Serializable_Object()
216     }
217 
218     /**
219      * Test of remove method, of class _DeltaStateHelper.
220      */
221     public void testRemove_Serializable()
222     {
223         _setupGetTests();
224         _instance.remove(KEY1);
225         assertTrue("key 1 should not exist anymore",
226                 _instance.get(KEY1) == null);
227         //TODO check the deleted data structure for further internal structural tests
228     }
229 
230     /**
231      * Test of remove method, of class _DeltaStateHelper.
232      */
233     public void testRemove_Serializable_Object()
234     {
235         _setupGetTests();
236         _instance.remove(KEY2, KEY_2_1);
237         _instance.remove(KEY2, KEY_2_2);
238 
239         _instance.remove(KEY3, VAL3);
240 
241         assertTrue("no key2 should exist anymore", _instance.get(KEY2) == null);
242         assertTrue("key3 also should not exist anymore",
243                 _instance.get(KEY3) == null);
244     }
245 
246     /**
247      * Test of saveState method, of class _DeltaStateHelper.
248      */
249     public void testSaveState()
250     {
251 
252         _instance.setInitialStateMarked(false);
253         _setupGetTests();
254 
255         //save stating does not need a facesContext for now!
256         Object retVal = _instance.saveState(facesContext);
257 
258         assertTrue("retVal must be an array", retVal instanceof Object[]);
259         assertTrue("arraylength must be given", ((Object[]) retVal).length > 0);
260         //only basic structural tests are done here
261         //the more important ones are the ones in restorestate
262 
263         //now lets do some structural tests
264         //theoretically there should be almot no data in the delta state if the full state already has been stored!
265         _instance.setInitialStateMarked(true);
266         _instance.put(KEY5, VAL5);
267         Object[] deltaSaveState = (Object[]) _instance.saveState(null);
268         //only the new value should be saved as delta
269         assertTrue("Delta Savestate structure", deltaSaveState.length == 2
270                 && deltaSaveState[0].equals(KEY5)
271                 && deltaSaveState[1].equals(VAL5));
272 
273     }
274 
275     /**
276      * Test of restoreState method, of class _DeltaStateHelper.
277      */
278     public void testRestoreState()
279     {
280         _setupGetTests();
281         _instance.setInitialStateMarked(false);
282         Object serializedState = _instance.saveState(facesContext);
283         _instance.restoreState(facesContext, serializedState);
284         assertStructure();
285 
286         _setupGetTests();
287         _instance.setInitialStateMarked(true);
288         serializedState = _instance.saveState(facesContext);
289         _instance.restoreState(facesContext, serializedState);
290         assertStructure();
291 
292         _instance.setInitialStateMarked(true);
293         _setupGetTests();
294         serializedState = _instance.saveState(facesContext);
295         _instance.restoreState(facesContext, serializedState);
296         assertStructure();
297     }
298 
299     /**
300      * Test of isTransient method, of class _DeltaStateHelper.
301      */
302     public void testIsTransient()
303     {
304         _instance.setTransient(true);
305         assertTrue(_instance.isTransient());
306     }
307 }