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