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.ActionTest;
022: import org.apache.pluto.testsuite.TestResult;
023: import org.apache.pluto.testsuite.TestUtils;
024: import org.apache.pluto.testsuite.validator.PreferencesValidatorImpl;
025:
026: import java.io.IOException;
027:
028: import javax.portlet.PortletPreferences;
029: import javax.portlet.PortletRequest;
030: import javax.portlet.ReadOnlyException;
031: import javax.portlet.ValidatorException;
032:
033: /**
034: */
035: public class PreferenceInActionTest extends PreferenceCommonTest
036: implements ActionTest {
037:
038: /** Logger. */
039: private static final Log LOG = LogFactory
040: .getLog(PreferenceInActionTest.class);
041:
042: // Test Methods ------------------------------------------------------------
043:
044: protected TestResult checkPreferenceValidator(PortletRequest request) {
045: TestResult result = new TestResult();
046: result
047: .setDescription("Ensure the validator catches invalid preferences.");
048: result.setSpecPLT("14.4");
049:
050: PortletPreferences preferences = request.getPreferences();
051: if (LOG.isDebugEnabled()) {
052: LOG.debug("Original preferences:");
053: logPreferences(preferences);
054: }
055: boolean exceptionThrown = false;
056: try {
057: preferences.setValue("TEST", " Spaces removed by trim ");
058: if (LOG.isDebugEnabled()) {
059: LOG.debug("Modified VALIDATION_TEST_KEY preference:");
060: logPreferences(preferences);
061: }
062: // Call store() method to invoke the validator.
063: preferences.store();
064:
065: } catch (ReadOnlyException ex) {
066: TestUtils.failOnException(
067: "Unable to set preference value.", ex, result);
068: return result;
069:
070: } catch (IOException ex) {
071: TestUtils.failOnException(
072: "Unable to store preference value.", ex, result);
073: return result;
074:
075: } catch (ValidatorException ex) {
076: // We are expecting this exception!
077: exceptionThrown = true;
078: // FIXME: what is going on below?
079: try {
080: //get rid of spaces because it causes problems with reset() call.
081: preferences.setValue("TEST", "OK");
082: preferences.reset("TEST");
083: } catch (Throwable th) {
084: LOG.error(th);
085: }
086: }
087:
088: if (exceptionThrown) {
089: result.setReturnCode(TestResult.PASSED);
090: } else {
091: result.setReturnCode(TestResult.FAILED);
092: result
093: .setResultMessage("Illegal value not caught by validator.");
094: }
095: return result;
096: }
097:
098: protected TestResult checkOnePreferenceValidatorPerPortletDefinition(
099: PortletRequest request) {
100: TestResult result = new TestResult();
101: result
102: .setDescription("Ensure only one validator instance is created "
103: + "per portlet definition.");
104: result.setSpecPLT("14.4");
105:
106: PortletPreferences preferences = request.getPreferences();
107: try {
108: preferences.setValue(
109: PreferencesValidatorImpl.CHECK_VALIDATOR_COUNT,
110: "true");
111: // Call store() method to invoke the validator.
112: preferences.store();
113: result.setReturnCode(TestResult.PASSED);
114: } catch (ReadOnlyException ex) {
115: TestUtils.failOnException(
116: "Unable to set preference value.", ex, result);
117: } catch (IOException ex) {
118: TestUtils.failOnException(
119: "Unable to store preference value.", ex, result);
120: } catch (ValidatorException ex) {
121: TestUtils.failOnException(
122: "Unable to store preference value.", ex, result);
123: } finally {
124: try {
125: preferences
126: .reset(PreferencesValidatorImpl.CHECK_VALIDATOR_COUNT);
127: preferences.store();
128: } catch (Exception ex) {
129: TestUtils
130: .failOnException(
131: "Unable to reset preference value for "
132: + PreferencesValidatorImpl.CHECK_VALIDATOR_COUNT,
133: ex, result);
134: }
135: }
136: return result;
137: }
138:
139: protected TestResult checkStorePreferences(PortletRequest request) {
140: TestResult result = new TestResult();
141: result
142: .setDescription("Ensure storage works for portlet preferences.");
143: result.setSpecPLT("14.1");
144:
145: PortletPreferences preferences = request.getPreferences();
146: if (LOG.isDebugEnabled()) {
147: LOG.debug("Preferences to store: " + preferences);
148: }
149:
150: boolean setOccured = false;
151: boolean storeOccured = false;
152:
153: try {
154: // Set new value for preference "dummyName".
155: preferences.setValue(PREF_NAME, NEW_VALUE);
156: String value = preferences.getValue(PREF_NAME, DEF_VALUE);
157: if (NEW_VALUE.equals(value)) {
158: setOccured = true;
159: }
160: // Store the preference and get value.
161: preferences.store();
162: value = preferences.getValue(PREF_NAME, DEF_VALUE);
163: if (NEW_VALUE.equals(value)) {
164: storeOccured = true;
165: }
166: } catch (ReadOnlyException ex) {
167: TestUtils.failOnException(
168: "Unable to set preference value.", ex, result);
169: return result;
170: } catch (ValidatorException ex) {
171: TestUtils.failOnException(
172: "Unable to store preference value.", ex, result);
173: return result;
174: } catch (IOException ex) {
175: TestUtils.failOnException(
176: "Unable to store preference value.", ex, result);
177: return result;
178: } finally {
179: // Reset preference to default value, and store!
180: try {
181: preferences.reset(PREF_NAME);
182: preferences.store();
183: } catch (Exception ex) {
184: TestUtils.failOnException(
185: "Unable to set preference value.", ex, result);
186: return result;
187: }
188: }
189:
190: // Everything is OK.
191: if (setOccured && storeOccured) {
192: result.setReturnCode(TestResult.PASSED);
193: }
194: // Error occurred when setting preference value.
195: else if (!setOccured) {
196: result.setReturnCode(TestResult.WARNING);
197: result
198: .setResultMessage("A function upon which the reset test "
199: + "depends failed to execute as expected. "
200: + "Check the other test results in this test suite.");
201: }
202: // Error occurred when storing preference value.
203: else {
204: result.setReturnCode(TestResult.FAILED);
205: result
206: .setResultMessage("Preferences not successfully stored.");
207: }
208: return result;
209: }
210:
211: }
|