View Javadoc

1   /*
2    * Copyright 2003,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.pluto.portalImpl.portlet.test;
17  
18  import java.util.Map;
19  import java.util.Enumeration;
20  import java.io.IOException;
21  
22  import javax.portlet.PortletRequest;
23  import javax.portlet.PortletPreferences;
24  import javax.portlet.ReadOnlyException;
25  import javax.portlet.ValidatorException;
26  
27  /***
28   * @author <a href="ddewolf@apache.org">David H. DeWolf</a>
29   */
30  public class SimplePreferenceTest
31   extends ActionAbstractReflectivePortletTest
32   implements ActionTest {
33  
34      private static final String BOGUS_KEY = "org.apache.pluto.testsuite.BOGUS_KEY";
35  
36      public String getTestSuiteName() {
37          return "Simple Preferences Test";
38      }
39  
40      protected TestResult checkGetEmptyPreference(PortletRequest req) {
41          TestResult res = new TestResult();
42          res.setName("Get Empty Preference Test");
43          res.setDesc("Ensure proper default is returned when a non-existing preference is requested.");
44          PortletPreferences preferences = req.getPreferences();
45          String preference =  preferences.getValue("nonexistence!", "Default");
46          if(preference.equals("Default")) {
47              res.setReturnCode(TestResult.PASSED);
48          }
49          else {
50              res.setReturnCode(TestResult.FAILED);
51              res.setResults("Preferences value was '"+preference+"' expected 'permanent'.");
52          }
53          return res;
54      }
55  
56      protected TestResult checkGetEmptyPreferences(PortletRequest req) {
57          TestResult res = new TestResult();
58          res.setName("Get Empty Preferences Test");
59          res.setDesc("Ensure proper defaults are returned when a non-existent preference set is requested.");
60          PortletPreferences preferences = req.getPreferences();
61          String[] preference = preferences.getValues("nonexistence!", new String[] {"Default"});
62          if(preference.length == 1 && preference[0].equals("Default")) {
63              res.setReturnCode(TestResult.PASSED);
64          }
65          else {
66              res.setReturnCode(TestResult.FAILED);
67              res.setResults("Preferences value was '"+preference[0]+"' expected 'permanent'.");
68          }
69          return res;
70      }
71  
72  
73      protected TestResult checkGetPreferences(PortletRequest req) {
74          TestResult res = new TestResult();
75          res.setName("Get Preference Test");
76          res.setDesc("Tests that preferences defined in the deployment descriptor may be retrieved.");
77          PortletPreferences preferences = req.getPreferences();
78          String preference =  preferences.getValue("dummyName", "defaultValue");
79          if(preference.equals("dummyValue")) {
80              res.setReturnCode(TestResult.PASSED);
81          }
82          else {
83              res.setReturnCode(TestResult.FAILED);
84              res.setResults("Preferences value was '"+preference+"' expected 'dummyValue'.");
85          }
86          return res;
87      }
88  
89      protected TestResult checkSetPreference(PortletRequest req) {
90          TestResult res = new TestResult();
91          res.setName("Set Preference  Test");
92          res.setDesc("Ensure a single preference can be set");
93  
94          PortletPreferences preferences = req.getPreferences();
95          try {
96              preferences.setValue("TEST", "TEST_VALUE");
97          }
98          catch(ReadOnlyException roe) {
99              res.setReturnCode(TestResult.FAILED);
100             res.setResults(roe.getMessage());
101             return res;
102         }
103 
104         String preference = preferences.getValue("TEST", "Error!");
105         if(preference.equals("TEST_VALUE")) {
106             res.setReturnCode(TestResult.PASSED);
107         }
108         else {
109             res.setReturnCode(TestResult.FAILED);
110             res.setResults("Preferences value was '"+preference+"' expected 'permanent'.");
111         }
112         return res;
113     }
114 
115     protected TestResult checkSetPreferences(PortletRequest req) {
116         TestResult res = new TestResult();
117         res.setName("Set Preferences Test");
118         res.setDesc("Ensure preferences can be set.");
119 
120         PortletPreferences preferences = req.getPreferences();
121         try {
122             preferences.setValues("TEST", new String[] {"TEST_VALUE", "ANOTHER"});
123         }
124         catch(ReadOnlyException roe) {
125             res.setReturnCode(TestResult.FAILED);
126             res.setResults(roe.getMessage());
127             return res;
128         }
129 
130         String[] preference = preferences.getValues("TEST", new String[] { "Error!" });
131         if(preference.length == 2 && preference[0].equals("TEST_VALUE") &&
132            preference[1].equals("ANOTHER")) {
133             res.setReturnCode(TestResult.PASSED);
134         }
135         else {
136             res.setReturnCode(TestResult.FAILED);
137             res.setResults("Preferences value was '"+preference+"' expected 'permanent'.");
138         }
139         return res;
140     }
141 
142     protected TestResult checkSetPreferencesReturnsFirst(PortletRequest req) {
143         TestResult res = new TestResult();
144         res.setName("Set Preferences Test");
145         res.setDesc("Ensure preferences can be set.");
146 
147         PortletPreferences preferences = req.getPreferences();
148         try {
149             preferences.setValues("TEST", new String[] {"TEST_VALUE", "ANOTHER"});
150         }
151         catch(ReadOnlyException roe) {
152             res.setReturnCode(TestResult.FAILED);
153             res.setResults(roe.getMessage());
154             return res;
155         }
156 
157         String preference = preferences.getValue("TEST", "Error!");
158         if(preference.equals("TEST_VALUE")) {
159             res.setReturnCode(TestResult.PASSED);
160         }
161         else {
162             res.setReturnCode(TestResult.FAILED);
163             res.setResults("Preferences value was '"+preference+"' expected 'permanent'.");
164         }
165         return res;
166     }
167 
168     protected TestResult checkPreferenceValidator(PortletRequest req) {
169         TestResult res = new TestResult();
170         res.setName("Preference Validator Test");
171         res.setDesc("Check to make sure that the validator catches invalid preferences.");
172 
173         PortletPreferences preferences = req.getPreferences();
174         boolean exceptionThrown = false;
175         try {
176             preferences.setValue("VALIDATION_TEST_KEY", " Spaces removed by trim ");
177             preferences.store();
178         }
179         catch (ReadOnlyException roe) {
180 
181         }
182         catch (ValidatorException e) {
183             exceptionThrown = true;
184             try { preferences.reset("VALIDATION_TEST_KEY"); }catch(Throwable t) {}
185         }
186         catch (IOException io) {
187 
188         }
189 
190         if(exceptionThrown) {
191             res.setReturnCode(TestResult.PASSED);
192         }
193         else {
194             res.setReturnCode(TestResult.FAILED);
195             res.setResults("Illegal value not caught by validator.");
196         }
197         return res;
198     }
199 
200     protected TestResult checkStorePreferences(PortletRequest req) {
201         TestResult res = new TestResult();
202         res.setName("Preference Store Test");
203         res.setDesc("Ensure storage works.");
204 
205         PortletPreferences preferences = req.getPreferences();
206         boolean setOccured = false;
207         boolean storeOccured = false;
208         try {
209             preferences.setValue("dummyName", "notTheOriginal");
210             String pref = preferences.getValue("dummyName", "Default");
211             if("notTheOriginal".equals(pref)) {
212                 setOccured = true;
213             }
214 
215             preferences.store();
216             if("notTheOriginal".equals(preferences.getValue("dummyName", "Default"))) {
217                 storeOccured = true;
218             }
219 
220             preferences.reset("dummyName");
221         }
222         catch(ReadOnlyException roe) {
223 
224         }
225         catch(ValidatorException ve) {
226 
227         }
228         catch(IOException io) {
229 
230         }
231 
232         if(setOccured && storeOccured) {
233             res.setReturnCode(TestResult.PASSED);
234         }
235         else if (!setOccured) {
236             res.setReturnCode(TestResult.WARNING);
237             res.setResults("A function upon which the reset test depends failed to execute as expected. Check the other test results in this test suite.");
238         }
239         else {
240             res.setReturnCode(TestResult.FAILED);
241             res.setResults("Preferences not successfully stored.");
242         }
243         return res;
244     }
245 
246     protected TestResult checkResetPreferences(PortletRequest req) {
247         TestResult res = new TestResult();
248         res.setName("Reset Preference Test");
249         res.setDesc("Tests that preferences are properly reset.");
250         PortletPreferences preferences = req.getPreferences();
251         boolean setOccured = false;
252         boolean storeOccured = false;
253         boolean resetOccured = false;
254         try {
255             preferences.setValue("dummyName", "notTheOriginal");
256             String pref = preferences.getValue("dummyName", "Default");
257             if("notTheOriginal".equals(pref)) {
258                 setOccured = true;
259             }
260 
261             preferences.store();
262             if("notTheOriginal".equals(preferences.getValue("dummyName", "Default"))) {
263                 storeOccured = true;
264             }
265 
266             preferences.reset("dummyName");
267 
268             String preference =  preferences.getValue("dummyName", "defaultValue");
269             if(preference.equals("dummyValue")) {
270                 resetOccured = true;
271             }
272         }
273         catch(ReadOnlyException roe) {
274             roe.printStackTrace();
275         }
276         catch(ValidatorException ve) {
277             ve.printStackTrace();
278         }
279         catch(IOException io) {
280             io.printStackTrace();
281         }
282 
283         if(setOccured && storeOccured && resetOccured) {
284             res.setReturnCode(TestResult.PASSED);
285         }
286         else if(!setOccured || !storeOccured) {
287             res.setReturnCode(TestResult.WARNING);
288             res.setResults("A function upon which the reset test depends failed to execute as expected. Check the other test results in this test suite.");
289         }
290         else {
291             res.setReturnCode(TestResult.FAILED);
292             res.setResults("Preferences value was not successfully reset after store");
293         }
294 
295         return res;
296     }
297 
298     protected TestResult checkResetToNullPreferences(PortletRequest req) {
299         TestResult res = new TestResult();
300         res.setName("Reset to Null Preference Test");
301         res.setDesc("Tests that preferences are properly reset when originally null.");
302         PortletPreferences preferences = req.getPreferences();
303         boolean setOccured = false;
304         boolean storeOccured = false;
305         boolean resetOccured = false;
306         try {
307             preferences.setValue(BOGUS_KEY, "notTheOriginal");
308             String pref = preferences.getValue(BOGUS_KEY, "Default");
309             if("notTheOriginal".equals(pref)) {
310                 setOccured = true;
311             }
312 
313             preferences.store();
314             if("notTheOriginal".equals(preferences.getValue(BOGUS_KEY, "Default"))) {
315                 storeOccured = true;
316             }
317 
318             preferences.reset(BOGUS_KEY);
319 
320             String preference =  preferences.getValue(BOGUS_KEY, "defaultValue");
321             if("defaultValue".equals(preference)) {
322                 resetOccured = true;
323             }
324         }
325         catch(ReadOnlyException roe) {
326             roe.printStackTrace();
327         }
328         catch(ValidatorException ve) {
329             ve.printStackTrace();
330         }
331         catch(IOException io) {
332             io.printStackTrace();
333         }
334 
335         if(setOccured && storeOccured && resetOccured) {
336             res.setReturnCode(TestResult.PASSED);
337         }
338         else if(!setOccured || !storeOccured) {
339             res.setReturnCode(TestResult.WARNING);
340             res.setResults("A function upon which the reset test depends failed to execute as expected. Check the other test results in this test suite.");
341         }
342         else {
343             res.setReturnCode(TestResult.FAILED);
344             res.setResults("Preferences value was not successfully reset after store.");
345         }
346         return res;
347     }
348 
349     protected TestResult checkReadOnlyPreferences(PortletRequest req) {
350         TestResult res = new TestResult();
351         res.setName("Preference Read Only Test");
352         res.setDesc("Check to make sure that read only preferences may not be set.");
353 
354         PortletPreferences preferences = req.getPreferences();
355         boolean exceptionThrown = false;
356         try {
357             preferences.setValue("readonly", "written");
358         }
359         catch (ReadOnlyException roe) {
360             exceptionThrown = true;
361         }
362 
363         if(exceptionThrown) {
364             res.setReturnCode(TestResult.PASSED);
365         }
366         else {
367             res.setReturnCode(TestResult.FAILED);
368             res.setResults("Read Only Preference was written.");
369         }
370 
371         return res;
372     }
373 
374     protected TestResult checkGetPreferenceMap(PortletRequest req) {
375         TestResult res = checkGetPreferenceNames(req);
376         res.setName("Preference Map Test.");
377         res.setDesc("Ensure returned map is valid");
378         return res;
379     }
380 
381     protected TestResult checkGetPreferenceNames(PortletRequest req) {
382         TestResult res = new TestResult();
383         res.setName("Preference Names Enumeration Test.");
384         res.setDesc("Ensure returned enumeration is valid");
385 
386         PortletPreferences prefs = req.getPreferences();
387         Map map = prefs.getMap();
388         Enumeration enum = prefs.getNames();
389         boolean hasAll = true;
390         while(enum.hasMoreElements()) {
391             if(!map.containsKey(enum.nextElement())) {
392                 hasAll = false;
393                 break;
394             }
395         }
396 
397         if(hasAll) {
398             res.setReturnCode(TestResult.PASSED);
399         }
400         else {
401             res.setReturnCode(TestResult.FAILED);
402             res.setResults("All names not found as preferences.");
403         }
404         return res;
405     }
406 
407 
408 }