001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.mock.web.portlet;
018:
019: import java.io.IOException;
020: import java.util.Collections;
021: import java.util.Enumeration;
022: import java.util.HashSet;
023: import java.util.Map;
024: import java.util.Set;
025:
026: import javax.portlet.PortletPreferences;
027: import javax.portlet.PreferencesValidator;
028: import javax.portlet.ReadOnlyException;
029: import javax.portlet.ValidatorException;
030:
031: import org.springframework.core.CollectionFactory;
032: import org.springframework.util.Assert;
033:
034: /**
035: * Mock implementation of the {@link javax.portlet.PortletPreferences} interface.
036: *
037: * @author John A. Lewis
038: * @author Juergen Hoeller
039: * @since 2.0
040: */
041: public class MockPortletPreferences implements PortletPreferences {
042:
043: private PreferencesValidator preferencesValidator;
044:
045: private final Map preferences = CollectionFactory
046: .createLinkedMapIfPossible(16);
047:
048: private final Set readOnly = new HashSet();
049:
050: public void setReadOnly(String key, boolean readOnly) {
051: Assert.notNull(key, "Key must not be null");
052: if (readOnly) {
053: this .readOnly.add(key);
054: } else {
055: this .readOnly.remove(key);
056: }
057: }
058:
059: public boolean isReadOnly(String key) {
060: Assert.notNull(key, "Key must not be null");
061: return this .readOnly.contains(key);
062: }
063:
064: public String getValue(String key, String def) {
065: Assert.notNull(key, "Key must not be null");
066: String[] values = (String[]) this .preferences.get(key);
067: return (values != null && values.length > 0 ? values[0] : def);
068: }
069:
070: public String[] getValues(String key, String[] def) {
071: Assert.notNull(key, "Key must not be null");
072: String[] values = (String[]) this .preferences.get(key);
073: return (values != null && values.length > 0 ? values : def);
074: }
075:
076: public void setValue(String key, String value)
077: throws ReadOnlyException {
078: setValues(key, new String[] { value });
079: }
080:
081: public void setValues(String key, String[] values)
082: throws ReadOnlyException {
083: Assert.notNull(key, "Key must not be null");
084: if (isReadOnly(key)) {
085: throw new ReadOnlyException("Preference '" + key
086: + "' is read-only");
087: }
088: this .preferences.put(key, values);
089: }
090:
091: public Enumeration getNames() {
092: return Collections.enumeration(this .preferences.keySet());
093: }
094:
095: public Map getMap() {
096: return Collections.unmodifiableMap(this .preferences);
097: }
098:
099: public void reset(String key) throws ReadOnlyException {
100: Assert.notNull(key, "Key must not be null");
101: if (isReadOnly(key)) {
102: throw new ReadOnlyException("Preference '" + key
103: + "' is read-only");
104: }
105: this .preferences.remove(key);
106: }
107:
108: public void setPreferencesValidator(
109: PreferencesValidator preferencesValidator) {
110: this .preferencesValidator = preferencesValidator;
111: }
112:
113: public void store() throws IOException, ValidatorException {
114: if (this.preferencesValidator != null) {
115: this.preferencesValidator.validate(this);
116: }
117: }
118:
119: }
|