001/*
002 * Copyright 2002-2012 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 *      https://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
017package org.springframework.mock.web.portlet;
018
019import java.io.IOException;
020import java.util.Collections;
021import java.util.Enumeration;
022import java.util.HashSet;
023import java.util.LinkedHashMap;
024import java.util.Map;
025import java.util.Set;
026import javax.portlet.PortletPreferences;
027import javax.portlet.PreferencesValidator;
028import javax.portlet.ReadOnlyException;
029import javax.portlet.ValidatorException;
030
031import org.springframework.util.Assert;
032
033/**
034 * Mock implementation of the {@link javax.portlet.PortletPreferences} interface.
035 *
036 * @author John A. Lewis
037 * @author Juergen Hoeller
038 * @since 2.0
039 */
040public class MockPortletPreferences implements PortletPreferences {
041
042        private PreferencesValidator preferencesValidator;
043
044        private final Map<String, String[]> preferences = new LinkedHashMap<String, String[]>();
045
046        private final Set<String> readOnly = new HashSet<String>();
047
048
049        public void setReadOnly(String key, boolean readOnly) {
050                Assert.notNull(key, "Key must not be null");
051                if (readOnly) {
052                        this.readOnly.add(key);
053                }
054                else {
055                        this.readOnly.remove(key);
056                }
057        }
058
059        @Override
060        public boolean isReadOnly(String key) {
061                Assert.notNull(key, "Key must not be null");
062                return this.readOnly.contains(key);
063        }
064
065        @Override
066        public String getValue(String key, String def) {
067                Assert.notNull(key, "Key must not be null");
068                String[] values = this.preferences.get(key);
069                return (values != null && values.length > 0 ? values[0] : def);
070        }
071
072        @Override
073        public String[] getValues(String key, String[] def) {
074                Assert.notNull(key, "Key must not be null");
075                String[] values = this.preferences.get(key);
076                return (values != null && values.length > 0 ? values : def);
077        }
078
079        @Override
080        public void setValue(String key, String value) throws ReadOnlyException {
081                setValues(key, new String[] {value});
082        }
083
084        @Override
085        public void setValues(String key, String[] values) throws ReadOnlyException {
086                Assert.notNull(key, "Key must not be null");
087                if (isReadOnly(key)) {
088                        throw new ReadOnlyException("Preference '" + key + "' is read-only");
089                }
090                this.preferences.put(key, values);
091        }
092
093        @Override
094        public Enumeration<String> getNames() {
095                return Collections.enumeration(this.preferences.keySet());
096        }
097
098        @Override
099        public Map<String, String[]> getMap() {
100                return Collections.unmodifiableMap(this.preferences);
101        }
102
103        @Override
104        public void reset(String key) throws ReadOnlyException {
105                Assert.notNull(key, "Key must not be null");
106                if (isReadOnly(key)) {
107                        throw new ReadOnlyException("Preference '" + key + "' is read-only");
108                }
109                this.preferences.remove(key);
110        }
111
112        public void setPreferencesValidator(PreferencesValidator preferencesValidator) {
113                this.preferencesValidator = preferencesValidator;
114        }
115
116        @Override
117        public void store() throws IOException, ValidatorException {
118                if (this.preferencesValidator != null) {
119                        this.preferencesValidator.validate(this);
120                }
121        }
122
123}