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.util.Collections;
020import java.util.Enumeration;
021import java.util.HashMap;
022import java.util.LinkedHashMap;
023import java.util.LinkedHashSet;
024import java.util.Locale;
025import java.util.Map;
026import java.util.ResourceBundle;
027import java.util.Set;
028import javax.portlet.PortletConfig;
029import javax.portlet.PortletContext;
030import javax.xml.XMLConstants;
031import javax.xml.namespace.QName;
032
033import org.springframework.util.Assert;
034
035/**
036 * Mock implementation of the {@link javax.portlet.PortletConfig} interface.
037 *
038 * @author John A. Lewis
039 * @author Juergen Hoeller
040 * @since 2.0
041 */
042public class MockPortletConfig implements PortletConfig {
043
044        private final PortletContext portletContext;
045
046        private final String portletName;
047
048        private final Map<Locale, ResourceBundle> resourceBundles = new HashMap<Locale, ResourceBundle>();
049
050        private final Map<String, String> initParameters = new LinkedHashMap<String, String>();
051
052        private final Set<String> publicRenderParameterNames = new LinkedHashSet<String>();
053
054        private String defaultNamespace = XMLConstants.NULL_NS_URI;
055
056        private final Set<QName> publishingEventQNames = new LinkedHashSet<QName>();
057
058        private final Set<QName> processingEventQNames = new LinkedHashSet<QName>();
059
060        private final Set<Locale> supportedLocales = new LinkedHashSet<Locale>();
061
062        private final Map<String, String[]> containerRuntimeOptions = new LinkedHashMap<String, String[]>();
063
064
065        /**
066         * Create a new MockPortletConfig with a default {@link MockPortletContext}.
067         */
068        public MockPortletConfig() {
069                this(null, "");
070        }
071
072        /**
073         * Create a new MockPortletConfig with a default {@link MockPortletContext}.
074         * @param portletName the name of the portlet
075         */
076        public MockPortletConfig(String portletName) {
077                this(null, portletName);
078        }
079
080        /**
081         * Create a new MockPortletConfig.
082         * @param portletContext the PortletContext that the portlet runs in
083         */
084        public MockPortletConfig(PortletContext portletContext) {
085                this(portletContext, "");
086        }
087
088        /**
089         * Create a new MockPortletConfig.
090         * @param portletContext the PortletContext that the portlet runs in
091         * @param portletName the name of the portlet
092         */
093        public MockPortletConfig(PortletContext portletContext, String portletName) {
094                this.portletContext = (portletContext != null ? portletContext : new MockPortletContext());
095                this.portletName = portletName;
096        }
097
098
099        @Override
100        public String getPortletName() {
101                return this.portletName;
102        }
103
104        @Override
105        public PortletContext getPortletContext() {
106                return this.portletContext;
107        }
108
109        public void setResourceBundle(Locale locale, ResourceBundle resourceBundle) {
110                Assert.notNull(locale, "Locale must not be null");
111                this.resourceBundles.put(locale, resourceBundle);
112        }
113
114        @Override
115        public ResourceBundle getResourceBundle(Locale locale) {
116                Assert.notNull(locale, "Locale must not be null");
117                return this.resourceBundles.get(locale);
118        }
119
120        public void addInitParameter(String name, String value) {
121                Assert.notNull(name, "Parameter name must not be null");
122                this.initParameters.put(name, value);
123        }
124
125        @Override
126        public String getInitParameter(String name) {
127                Assert.notNull(name, "Parameter name must not be null");
128                return this.initParameters.get(name);
129        }
130
131        @Override
132        public Enumeration<String> getInitParameterNames() {
133                return Collections.enumeration(this.initParameters.keySet());
134        }
135
136        public void addPublicRenderParameterName(String name) {
137                this.publicRenderParameterNames.add(name);
138        }
139
140        @Override
141        public Enumeration<String> getPublicRenderParameterNames() {
142                return Collections.enumeration(this.publicRenderParameterNames);
143        }
144
145        public void setDefaultNamespace(String defaultNamespace) {
146                this.defaultNamespace = defaultNamespace;
147        }
148
149        @Override
150        public String getDefaultNamespace() {
151                return this.defaultNamespace;
152        }
153
154        public void addPublishingEventQName(QName name) {
155                this.publishingEventQNames.add(name);
156        }
157
158        @Override
159        public Enumeration<QName> getPublishingEventQNames() {
160                return Collections.enumeration(this.publishingEventQNames);
161        }
162
163        public void addProcessingEventQName(QName name) {
164                this.processingEventQNames.add(name);
165        }
166
167        @Override
168        public Enumeration<QName> getProcessingEventQNames() {
169                return Collections.enumeration(this.processingEventQNames);
170        }
171
172        public void addSupportedLocale(Locale locale) {
173                this.supportedLocales.add(locale);
174        }
175
176        @Override
177        public Enumeration<Locale> getSupportedLocales() {
178                return Collections.enumeration(this.supportedLocales);
179        }
180
181        public void addContainerRuntimeOption(String key, String value) {
182                this.containerRuntimeOptions.put(key, new String[] {value});
183        }
184
185        public void addContainerRuntimeOption(String key, String[] values) {
186                this.containerRuntimeOptions.put(key, values);
187        }
188
189        @Override
190        public Map<String, String[]> getContainerRuntimeOptions() {
191                return Collections.unmodifiableMap(this.containerRuntimeOptions);
192        }
193
194}