001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.pluto.testsuite.test;
018:
019: import org.apache.commons.logging.Log;
020: import org.apache.commons.logging.LogFactory;
021: import org.apache.pluto.testsuite.TestResult;
022: import org.apache.pluto.testsuite.TestUtils;
023:
024: import java.util.Enumeration;
025: import java.util.Iterator;
026: import java.util.Map;
027:
028: import javax.portlet.PortletPreferences;
029: import javax.portlet.PortletRequest;
030: import javax.portlet.ReadOnlyException;
031:
032: /**
033: * Common portlet preferences test.
034: */
035: public class PreferenceCommonTest extends AbstractReflectivePortletTest {
036:
037: /** Logger. */
038: private static final Log LOG = LogFactory
039: .getLog(PreferenceCommonTest.class);
040:
041: protected static final String BOGUS_KEY = "org.apache.pluto.testsuite.BOGUS_KEY";
042:
043: protected static final String READ_ONLY_PREF_NAME = "readonly";
044:
045: protected static final String NO_VALUE_PREF_NAME = "nameWithNoValue";
046:
047: protected static final String PREF_NAME = "dummyName";
048: protected static final String PREF_VALUE = "dummyValue";
049:
050: protected static final String DEF_VALUE = "Default";
051: protected static final String NEW_VALUE = "notTheOriginal";
052:
053: // Test Methods ------------------------------------------------------------
054:
055: protected TestResult checkGetEmptyPreference(PortletRequest request) {
056: return doCheckDefaultPreference(request, "nonexistence!");
057: }
058:
059: protected TestResult checkGetNoValuePreference(
060: PortletRequest request) {
061: return doCheckDefaultPreference(request, NO_VALUE_PREF_NAME);
062: }
063:
064: /**
065: * Private method that checks if a preference is not defined or has no
066: * value in <code>portlet.xml</code>, the default values are returned.
067: * @param request the portlet request.
068: * @param preferenceName the preference name which is not defined or has no
069: * value in <code>portlet.xml</code>.
070: * @return the test result.
071: */
072: private TestResult doCheckDefaultPreference(PortletRequest request,
073: String preferenceName) {
074: TestResult result = new TestResult();
075: result
076: .setDescription("Ensure proper default is returned when "
077: + "a non-existing/value-undefined preference is requested.");
078: result.setSpecPLT("14.1");
079:
080: PortletPreferences preferences = request.getPreferences();
081: String value = preferences.getValue(preferenceName, DEF_VALUE);
082: String[] values = preferences.getValues(preferenceName,
083: new String[] { DEF_VALUE });
084: if (DEF_VALUE.equals(value) && values != null
085: && values.length == 1 && DEF_VALUE.equals(values[0])) {
086: result.setReturnCode(TestResult.PASSED);
087: } else if (!DEF_VALUE.equals(value)) {
088: TestUtils.failOnAssertion("preference value", value,
089: DEF_VALUE, result);
090: } else {
091: TestUtils.failOnAssertion("preference values", values,
092: new String[] { DEF_VALUE }, result);
093: }
094: return result;
095: }
096:
097: protected TestResult checkGetPreferences(PortletRequest request) {
098: TestResult result = new TestResult();
099: result.setDescription("Ensure that preferences defined "
100: + "in the deployment descriptor may be retrieved.");
101: result.setSpecPLT("14.1");
102:
103: PortletPreferences preferences = request.getPreferences();
104: String value = preferences.getValue(PREF_NAME, DEF_VALUE);
105: if (value != null && value.equals(PREF_VALUE)) {
106: result.setReturnCode(TestResult.PASSED);
107: } else {
108: TestUtils.failOnAssertion("preference value", value,
109: PREF_VALUE, result);
110: }
111: return result;
112: }
113:
114: protected TestResult checkSetPreferenceSingleValue(
115: PortletRequest request) {
116: TestResult result = new TestResult();
117: result
118: .setDescription("Ensure a single preference value can be set.");
119: result.setSpecPLT("14.1");
120:
121: PortletPreferences preferences = request.getPreferences();
122: try {
123: preferences.setValue("TEST", "TEST_VALUE");
124: } catch (ReadOnlyException ex) {
125: TestUtils.failOnException(
126: "Unable to set preference value.", ex, result);
127: return result;
128: }
129:
130: String value = preferences.getValue("TEST", DEF_VALUE);
131: if (value != null && value.equals("TEST_VALUE")) {
132: result.setReturnCode(TestResult.PASSED);
133: } else {
134: TestUtils.failOnAssertion("preference value", value,
135: "TEST_VALUE", result);
136: }
137: return result;
138: }
139:
140: protected TestResult checkSetPreferenceMultiValues(
141: PortletRequest request) {
142: TestResult result = new TestResult();
143: result
144: .setDescription("Ensure multiple preference values can be set.");
145: result.setSpecPLT("14.1");
146:
147: PortletPreferences preferences = request.getPreferences();
148: try {
149: preferences.setValues("TEST", new String[] { "ONE",
150: "ANOTHER" });
151: } catch (ReadOnlyException ex) {
152: TestUtils.failOnException(
153: "Unable to set preference values.", ex, result);
154: return result;
155: }
156:
157: String[] values = preferences.getValues("TEST",
158: new String[] { DEF_VALUE });
159: if (values != null && values.length == 2
160: && values[0].equals("ONE")
161: && values[1].equals("ANOTHER")) {
162: result.setReturnCode(TestResult.PASSED);
163: } else if (values == null) {
164: TestUtils.failOnAssertion("preference values", values,
165: new String[] { "ONE", "ANOTHER" }, result);
166: } else if (values.length != 2) {
167: TestUtils.failOnAssertion("length of preference values",
168: String.valueOf(values.length), String.valueOf(2),
169: result);
170: } else {
171: TestUtils.failOnAssertion("preference values", values,
172: new String[] { "ONE", "ANOTHER" }, result);
173: }
174: return result;
175: }
176:
177: protected TestResult checkSetPreferenceNull(PortletRequest request) {
178: TestResult result = new TestResult();
179: result
180: .setDescription("Ensure a preference value can be set to null.");
181: result.setSpecPLT("14.1");
182:
183: PortletPreferences preferences = request.getPreferences();
184: try {
185: preferences.setValue("TEST", null);
186: } catch (ReadOnlyException ex) {
187: TestUtils.failOnException(
188: "Unable to set preference value.", ex, result);
189: return result;
190: }
191:
192: String value = preferences.getValue("TEST", DEF_VALUE);
193: if (DEF_VALUE.equals(value)) {
194: result.setReturnCode(TestResult.PASSED);
195: } else {
196: TestUtils.failOnAssertion("preference value", value,
197: DEF_VALUE, result);
198: }
199: return result;
200: }
201:
202: protected TestResult checkSetPreferencesReturnsFirst(
203: PortletRequest request) {
204: TestResult result = new TestResult();
205: result
206: .setDescription("Ensure the first value set to a given "
207: + "preference is returned first by PortletPreferences.getValue().");
208: result.setSpecPLT("14.1");
209:
210: PortletPreferences preferences = request.getPreferences();
211: try {
212: preferences.setValues("TEST", new String[] { "FIRST",
213: "SECOND" });
214: } catch (ReadOnlyException ex) {
215: TestUtils.failOnException(
216: "Unable to set preference values.", ex, result);
217: return result;
218: }
219:
220: String value = preferences.getValue("TEST", DEF_VALUE);
221: if (value != null && value.equals("FIRST")) {
222: result.setReturnCode(TestResult.PASSED);
223: } else {
224: TestUtils.failOnAssertion("preference value", value,
225: "FIRST", result);
226: }
227: return result;
228: }
229:
230: protected TestResult checkResetPreferenceToDefault(
231: PortletRequest request) {
232: TestResult result = new TestResult();
233: result.setDescription("Ensure preferences are properly reset.");
234: result.setSpecPLT("14.1");
235:
236: PortletPreferences preferences = request.getPreferences();
237: boolean setOccured = false;
238: boolean resetOccured = false;
239:
240: try {
241: // Set new value to overwrite the default value.
242: preferences.setValue(PREF_NAME, NEW_VALUE);
243: String value = preferences.getValue(PREF_NAME, DEF_VALUE);
244: if (NEW_VALUE.equals(value)) {
245: setOccured = true;
246: }
247: // Reset the preference so that default value is restored.
248: preferences.reset(PREF_NAME);
249: value = preferences.getValue(PREF_NAME, DEF_VALUE);
250: if (PREF_VALUE.equals(value)) {
251: resetOccured = true;
252: }
253: } catch (ReadOnlyException ex) {
254: TestUtils.failOnException(
255: "Unable to set preference value.", ex, result);
256: return result;
257: }
258:
259: // Everything is OK.
260: if (setOccured && resetOccured) {
261: result.setReturnCode(TestResult.PASSED);
262: }
263: // Error occurred when setting or storing preferences.
264: else if (!setOccured) {
265: result.setReturnCode(TestResult.WARNING);
266: result
267: .setResultMessage("A function upon which the reset test "
268: + "depends failed to execute as expected. "
269: + "Check the other test results in this test suite.");
270: }
271: // Error occurred when resetting preference.
272: else {
273: result.setReturnCode(TestResult.FAILED);
274: result
275: .setResultMessage("Preferences value was not successfully reset after store");
276: }
277: return result;
278: }
279:
280: protected TestResult checkResetPreferenceWithoutDefault(
281: PortletRequest request) {
282: TestResult result = new TestResult();
283: result
284: .setDescription("Ensure preferences are properly reset (removed) "
285: + "when the default value is not defined.");
286: result.setSpecPLT("14.1");
287:
288: PortletPreferences preferences = request.getPreferences();
289: boolean setOccured = false;
290: boolean resetOccured = false;
291:
292: try {
293: // Set preference value to overwrite the original (null).
294: preferences.setValue(BOGUS_KEY, NEW_VALUE);
295: String value = preferences.getValue(BOGUS_KEY, DEF_VALUE);
296: if (NEW_VALUE.equals(value)) {
297: setOccured = true;
298: }
299: // Reset preference value to null.
300: preferences.reset(BOGUS_KEY);
301: value = preferences.getValue(BOGUS_KEY, DEF_VALUE);
302: if (DEF_VALUE.equals(value)) {
303: resetOccured = true;
304: }
305: } catch (ReadOnlyException ex) {
306: TestUtils.failOnException(
307: "Unable to set preference value.", ex, result);
308: return result;
309: }
310:
311: // Everything is OK.
312: if (setOccured && resetOccured) {
313: result.setReturnCode(TestResult.PASSED);
314: }
315: // Error occurred when setting or storing preferences.
316: else if (!setOccured) {
317: result.setReturnCode(TestResult.WARNING);
318: result
319: .setResultMessage("A function upon which the reset test "
320: + "depends failed to execute as expected. "
321: + "Check the other test results in this test suite.");
322: }
323: // Error occurred when resetting preference value.
324: else {
325: result.setReturnCode(TestResult.FAILED);
326: result
327: .setResultMessage("Preferences value was not successfully "
328: + "reset after store.");
329: }
330: return result;
331: }
332:
333: protected TestResult checkModifyReadOnlyPreferences(
334: PortletRequest request) {
335: TestResult result = new TestResult();
336: result
337: .setDescription("Ensure that setValue() / setValues() / reset() "
338: + "methods throw ReadOnlyException when invoked "
339: + "on read-only preferences.");
340: result.setSpecPLT("14.1");
341:
342: PortletPreferences preferences = request.getPreferences();
343: if (!preferences.isReadOnly(READ_ONLY_PREF_NAME)) {
344: result.setReturnCode(TestResult.WARNING);
345: result.setResultMessage("Preference " + READ_ONLY_PREF_NAME
346: + " is not a read-only preference. "
347: + "This may be due to misconfiuration.");
348: return result;
349: }
350:
351: boolean setValueOK = false;
352: boolean setValuesOK = false;
353: boolean resetOK = false;
354:
355: // Check setValue() method.
356: try {
357: preferences.setValue(READ_ONLY_PREF_NAME, "written");
358: } catch (ReadOnlyException ex) {
359: setValueOK = true;
360: }
361:
362: // Check setValues() method.
363: try {
364: preferences.setValues(READ_ONLY_PREF_NAME,
365: new String[] { "written" });
366: } catch (ReadOnlyException ex) {
367: setValuesOK = true;
368: }
369:
370: // Check reset() method.
371: try {
372: preferences.reset(READ_ONLY_PREF_NAME);
373: } catch (ReadOnlyException ex) {
374: resetOK = true;
375: }
376:
377: if (setValueOK && setValuesOK && resetOK) {
378: result.setReturnCode(TestResult.PASSED);
379: } else {
380: result.setReturnCode(TestResult.FAILED);
381: StringBuffer buffer = new StringBuffer();
382: if (!setValueOK) {
383: buffer.append("setValue(..), ");
384: }
385: if (!setValuesOK) {
386: buffer.append("setValues(..), ");
387: }
388: if (!resetOK) {
389: buffer.append("reset(..), ");
390: }
391: result.setResultMessage("Method(s) [" + buffer.toString()
392: + "] invoked on read-only preference ("
393: + READ_ONLY_PREF_NAME
394: + ") without ReadOnlyException.");
395: }
396: return result;
397: }
398:
399: protected TestResult checkGetPreferenceNames(PortletRequest request) {
400: TestResult result = new TestResult();
401: result.setDescription("Ensure returned enumeration is valid.");
402: result.setSpecPLT("14.1");
403:
404: PortletPreferences preferences = request.getPreferences();
405: Map prefMap = preferences.getMap();
406: boolean hasAll = true;
407: for (Enumeration en = preferences.getNames(); en
408: .hasMoreElements();) {
409: if (!prefMap.containsKey(en.nextElement())) {
410: hasAll = false;
411: break;
412: }
413: }
414:
415: if (hasAll) {
416: result.setReturnCode(TestResult.PASSED);
417: } else {
418: result.setReturnCode(TestResult.FAILED);
419: result.setResultMessage("At least one name is not found "
420: + "in the preference map.");
421: }
422: return result;
423: }
424:
425: /**
426: * FIXME:
427: */
428: protected TestResult checkGetPreferenceMap(PortletRequest request) {
429: TestResult result = checkGetPreferenceNames(request);
430: result.setDescription("Ensure returned map is valid.");
431: result.setSpecPLT("14.1");
432: return result;
433: }
434:
435: /**
436: * Check (xci) SPEC 91, PLT 14.1: Preferences values are not modified
437: * if the values in the Map are altered.
438: */
439: protected TestResult checkPreferenceValueNotModified(
440: PortletRequest request) {
441: TestResult result = new TestResult();
442: result
443: .setDescription("Preferences values are not modified if "
444: + "the values in the returned preference Map are altered.");
445: result.setSpecPLT("14.1");
446:
447: PortletPreferences preferences = request.getPreferences();
448: if (LOG.isDebugEnabled()) {
449: LOG.debug("Original Preferences:");
450: logPreferences(preferences);
451: }
452:
453: // Modify the returned preference map.
454: Map prefMap = preferences.getMap();
455: String[] values = (String[]) prefMap.get(PREF_NAME);
456: String originalValue = null;
457: String modifiedValue = "Value modified in preferences map.";
458: if (values != null && values.length == 1) {
459: originalValue = values[0];
460: values[0] = modifiedValue;
461: }
462:
463: // Check if the value held by portlet preferences is modified.
464: if (LOG.isDebugEnabled()) {
465: LOG.debug("Modified Preferences:");
466: logPreferences(preferences);
467: }
468: String newValue = preferences.getValue(PREF_NAME, DEF_VALUE);
469: if (newValue != null && newValue.equals(originalValue)) {
470: result.setReturnCode(TestResult.PASSED);
471: } else {
472: result.setReturnCode(TestResult.FAILED);
473: result
474: .setResultMessage("Preference value modified according to "
475: + "the preference map.");
476: }
477: return result;
478: }
479:
480: // Debug Methods -----------------------------------------------------------
481:
482: /**
483: * Logs out the portlet preferences.
484: * @param preferences PortletPreferences to log.
485: */
486: protected void logPreferences(PortletPreferences preferences) {
487: StringBuffer buffer = new StringBuffer();
488: Map map = preferences.getMap();
489: for (Iterator it = map.keySet().iterator(); it.hasNext();) {
490: String key = (String) it.next();
491: String[] values = (String[]) map.get(key);
492: buffer.append(key).append("=");
493: if (values != null) {
494: buffer.append("{");
495: for (int i = 0; i < values.length; i++) {
496: buffer.append(values[i]);
497: if (i < values.length - 1) {
498: buffer.append(",");
499: }
500: }
501: buffer.append("}");
502: } else {
503: // Spec allows null values.
504: buffer.append("NULL");
505: }
506: buffer.append(";");
507: }
508: LOG.debug("PortletPreferences: " + buffer.toString());
509: }
510:
511: }
|