1 /* 2 * Copyright 2002-2009 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.springframework.mock.web.portlet; 18 19 import java.util.Collections; 20 import java.util.Enumeration; 21 import java.util.HashMap; 22 import java.util.LinkedHashMap; 23 import java.util.LinkedHashSet; 24 import java.util.Locale; 25 import java.util.Map; 26 import java.util.ResourceBundle; 27 import java.util.Set; 28 import javax.portlet.PortletConfig; 29 import javax.portlet.PortletContext; 30 import javax.xml.XMLConstants; 31 import javax.xml.namespace.QName; 32 33 import org.springframework.util.Assert; 34 35 /** 36 * Mock implementation of the {@link javax.portlet.PortletConfig} interface. 37 * 38 * @author John A. Lewis 39 * @author Juergen Hoeller 40 * @since 2.0 41 */ 42 public class MockPortletConfig implements PortletConfig { 43 44 private final PortletContext portletContext; 45 46 private final String portletName; 47 48 private final Map<Locale, ResourceBundle> resourceBundles = new HashMap<Locale, ResourceBundle>(); 49 50 private final Map<String, String> initParameters = new LinkedHashMap<String, String>(); 51 52 private final Set<String> publicRenderParameterNames = new LinkedHashSet<String>(); 53 54 private String defaultNamespace = XMLConstants.NULL_NS_URI; 55 56 private final Set<QName> publishingEventQNames = new LinkedHashSet<QName>(); 57 58 private final Set<QName> processingEventQNames = new LinkedHashSet<QName>(); 59 60 private final Set<Locale> supportedLocales = new LinkedHashSet<Locale>(); 61 62 private final Map<String, String[]> containerRuntimeOptions = new LinkedHashMap<String, String[]>(); 63 64 65 /** 66 * Create a new MockPortletConfig with a default {@link MockPortletContext}. 67 */ MockPortletConfig()68 public MockPortletConfig() { 69 this(null, ""); 70 } 71 72 /** 73 * Create a new MockPortletConfig with a default {@link MockPortletContext}. 74 * @param portletName the name of the portlet 75 */ MockPortletConfig(String portletName)76 public MockPortletConfig(String portletName) { 77 this(null, portletName); 78 } 79 80 /** 81 * Create a new MockPortletConfig. 82 * @param portletContext the PortletContext that the portlet runs in 83 */ MockPortletConfig(PortletContext portletContext)84 public MockPortletConfig(PortletContext portletContext) { 85 this(portletContext, ""); 86 } 87 88 /** 89 * Create a new MockPortletConfig. 90 * @param portletContext the PortletContext that the portlet runs in 91 * @param portletName the name of the portlet 92 */ MockPortletConfig(PortletContext portletContext, String portletName)93 public MockPortletConfig(PortletContext portletContext, String portletName) { 94 this.portletContext = (portletContext != null ? portletContext : new MockPortletContext()); 95 this.portletName = portletName; 96 } 97 98 getPortletName()99 public String getPortletName() { 100 return this.portletName; 101 } 102 getPortletContext()103 public PortletContext getPortletContext() { 104 return this.portletContext; 105 } 106 setResourceBundle(Locale locale, ResourceBundle resourceBundle)107 public void setResourceBundle(Locale locale, ResourceBundle resourceBundle) { 108 Assert.notNull(locale, "Locale must not be null"); 109 this.resourceBundles.put(locale, resourceBundle); 110 } 111 getResourceBundle(Locale locale)112 public ResourceBundle getResourceBundle(Locale locale) { 113 Assert.notNull(locale, "Locale must not be null"); 114 return this.resourceBundles.get(locale); 115 } 116 addInitParameter(String name, String value)117 public void addInitParameter(String name, String value) { 118 Assert.notNull(name, "Parameter name must not be null"); 119 this.initParameters.put(name, value); 120 } 121 getInitParameter(String name)122 public String getInitParameter(String name) { 123 Assert.notNull(name, "Parameter name must not be null"); 124 return this.initParameters.get(name); 125 } 126 getInitParameterNames()127 public Enumeration<String> getInitParameterNames() { 128 return Collections.enumeration(this.initParameters.keySet()); 129 } 130 addPublicRenderParameterName(String name)131 public void addPublicRenderParameterName(String name) { 132 this.publicRenderParameterNames.add(name); 133 } 134 getPublicRenderParameterNames()135 public Enumeration<String> getPublicRenderParameterNames() { 136 return Collections.enumeration(this.publicRenderParameterNames); 137 } 138 setDefaultNamespace(String defaultNamespace)139 public void setDefaultNamespace(String defaultNamespace) { 140 this.defaultNamespace = defaultNamespace; 141 } 142 getDefaultNamespace()143 public String getDefaultNamespace() { 144 return this.defaultNamespace; 145 } 146 addPublishingEventQName(QName name)147 public void addPublishingEventQName(QName name) { 148 this.publishingEventQNames.add(name); 149 } 150 getPublishingEventQNames()151 public Enumeration<QName> getPublishingEventQNames() { 152 return Collections.enumeration(this.publishingEventQNames); 153 } 154 addProcessingEventQName(QName name)155 public void addProcessingEventQName(QName name) { 156 this.processingEventQNames.add(name); 157 } 158 getProcessingEventQNames()159 public Enumeration<QName> getProcessingEventQNames() { 160 return Collections.enumeration(this.processingEventQNames); 161 } 162 addSupportedLocale(Locale locale)163 public void addSupportedLocale(Locale locale) { 164 this.supportedLocales.add(locale); 165 } 166 getSupportedLocales()167 public Enumeration<Locale> getSupportedLocales() { 168 return Collections.enumeration(this.supportedLocales); 169 } 170 addContainerRuntimeOption(String key, String value)171 public void addContainerRuntimeOption(String key, String value) { 172 this.containerRuntimeOptions.put(key, new String[] {value}); 173 } 174 addContainerRuntimeOption(String key, String[] values)175 public void addContainerRuntimeOption(String key, String[] values) { 176 this.containerRuntimeOptions.put(key, values); 177 } 178 getContainerRuntimeOptions()179 public Map<String, String[]> getContainerRuntimeOptions() { 180 return Collections.unmodifiableMap(this.containerRuntimeOptions); 181 } 182 183 } 184