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.validator;
018:
019: import javax.portlet.PortletPreferences;
020: import javax.portlet.PreferencesValidator;
021: import javax.portlet.ValidatorException;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025:
026: import java.util.ArrayList;
027: import java.util.Collection;
028: import java.util.Enumeration;
029: import java.util.HashMap;
030: import java.util.Map;
031:
032: /**
033: * Implementation of the portlet preferences validator.
034: */
035: public class PreferencesValidatorImpl implements PreferencesValidator {
036:
037: /** Logger. */
038: private static final Log LOG = LogFactory
039: .getLog(PreferencesValidatorImpl.class);
040:
041: public static final String CHECK_VALIDATOR_COUNT = "checkValidatorCount";
042:
043: /** Count of instances created. */
044: private static final Map INSTANCE_COUNTER = new HashMap();
045:
046: /** Count of invocation number of method <code>validate()</code>. */
047: private int validateInvoked = 0;
048:
049: // Constructor -------------------------------------------------------------
050:
051: /**
052: * Default no-arg constructor.
053: */
054: public PreferencesValidatorImpl() {
055: if (LOG.isDebugEnabled()) {
056: LOG.debug("Creating validator instance: "
057: + getClass().getName());
058: }
059: Integer count = (Integer) INSTANCE_COUNTER.get(getClass()
060: .getName());
061: if (count == null) {
062: count = new Integer(1);
063: } else {
064: count = new Integer(count.intValue() + 1);
065: }
066: INSTANCE_COUNTER.put(getClass().getName(), count);
067: }
068:
069: // PreferencesValidator Impl -----------------------------------------------
070:
071: public void validate(PortletPreferences preferences)
072: throws ValidatorException {
073: validateInvoked++;
074: String value = preferences
075: .getValue(CHECK_VALIDATOR_COUNT, null);
076: if (value != null && value.equalsIgnoreCase("true")) {
077: checkValidatorCount();
078: }
079:
080: //
081: // TODO: Determine why we use this - I seem to remember it's a
082: // spec requirement, and fix it so that we don't have issues
083: // anymore. When enabled, all preferences fail in testsuite.
084: //
085: final String[] DEFAULT_VALUES = new String[] { "no values" };
086: Collection failedNames = new ArrayList();
087: for (Enumeration en = preferences.getNames(); en
088: .hasMoreElements();) {
089: String name = (String) en.nextElement();
090: String[] values = preferences.getValues(name,
091: DEFAULT_VALUES);
092: if (values != null) { // null values are allowed
093: for (int i = 0; i < values.length; i++) {
094: if (values[i] != null) { // null values are allowed
095: // Validate that the preferences do not
096: // start or end with white spaces.
097: if (!values[i].equals(values[i].trim())) {
098: if (LOG.isDebugEnabled()) {
099: LOG.debug("Validation failed: "
100: + "value has white spaces: "
101: + "name=" + name + "; value=|"
102: + values[i] + "|");
103: }
104: failedNames.add(name);
105: }
106: }
107: }
108: }
109: }
110:
111: if (!failedNames.isEmpty()) {
112: throw new ValidatorException(
113: "One or more preferences do not pass the validation.",
114: failedNames);
115: }
116: }
117:
118: private void checkValidatorCount() throws ValidatorException {
119: if (LOG.isDebugEnabled()) {
120: LOG.debug("Checking validator count...");
121: }
122: Integer instanceCreated = (Integer) INSTANCE_COUNTER
123: .get(getClass().getName());
124: if (LOG.isDebugEnabled()) {
125: LOG.debug("Method validate() invoked " + validateInvoked
126: + " times.");
127: LOG.debug("Validator created " + instanceCreated.intValue()
128: + " times.");
129: }
130: if (instanceCreated.intValue() != 1) {
131: throw new ValidatorException(
132: instanceCreated.toString()
133: + " validator instances were created, "
134: + "expected 1 validator instance per portlet definition.",
135: null);
136: }
137: }
138:
139: }
|