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.io.Serializable; 20 import java.util.Collections; 21 import java.util.HashMap; 22 import java.util.Iterator; 23 import java.util.LinkedHashMap; 24 import java.util.Map; 25 import javax.portlet.PortalContext; 26 import javax.portlet.PortletMode; 27 import javax.portlet.PortletModeException; 28 import javax.portlet.StateAwareResponse; 29 import javax.portlet.WindowState; 30 import javax.portlet.WindowStateException; 31 import javax.xml.namespace.QName; 32 33 import org.springframework.util.Assert; 34 import org.springframework.util.CollectionUtils; 35 36 /** 37 * Mock implementation of the {@link javax.portlet.StateAwareResponse} interface. 38 * 39 * @author Juergen Hoeller 40 * @since 3.0 41 */ 42 public class MockStateAwareResponse extends MockPortletResponse implements StateAwareResponse { 43 44 private WindowState windowState; 45 46 private PortletMode portletMode; 47 48 private final Map<String, String[]> renderParameters = new LinkedHashMap<String, String[]>(); 49 50 private final Map<QName, Serializable> events = new HashMap<QName, Serializable>(); 51 52 53 /** 54 * Create a new MockActionResponse with a default {@link MockPortalContext}. 55 * @see org.springframework.mock.web.portlet.MockPortalContext 56 */ MockStateAwareResponse()57 public MockStateAwareResponse() { 58 super(); 59 } 60 61 /** 62 * Create a new MockActionResponse. 63 * @param portalContext the PortalContext defining the supported 64 * PortletModes and WindowStates 65 */ MockStateAwareResponse(PortalContext portalContext)66 public MockStateAwareResponse(PortalContext portalContext) { 67 super(portalContext); 68 } 69 70 setWindowState(WindowState windowState)71 public void setWindowState(WindowState windowState) throws WindowStateException { 72 if (!CollectionUtils.contains(getPortalContext().getSupportedWindowStates(), windowState)) { 73 throw new WindowStateException("WindowState not supported", windowState); 74 } 75 this.windowState = windowState; 76 } 77 getWindowState()78 public WindowState getWindowState() { 79 return this.windowState; 80 } 81 setPortletMode(PortletMode portletMode)82 public void setPortletMode(PortletMode portletMode) throws PortletModeException { 83 if (!CollectionUtils.contains(getPortalContext().getSupportedPortletModes(), portletMode)) { 84 throw new PortletModeException("PortletMode not supported", portletMode); 85 } 86 this.portletMode = portletMode; 87 } 88 getPortletMode()89 public PortletMode getPortletMode() { 90 return this.portletMode; 91 } 92 setRenderParameters(Map<String, String[]> parameters)93 public void setRenderParameters(Map<String, String[]> parameters) { 94 Assert.notNull(parameters, "Parameters Map must not be null"); 95 this.renderParameters.clear(); 96 this.renderParameters.putAll(parameters); 97 } 98 setRenderParameter(String key, String value)99 public void setRenderParameter(String key, String value) { 100 Assert.notNull(key, "Parameter key must not be null"); 101 Assert.notNull(value, "Parameter value must not be null"); 102 this.renderParameters.put(key, new String[] {value}); 103 } 104 setRenderParameter(String key, String[] values)105 public void setRenderParameter(String key, String[] values) { 106 Assert.notNull(key, "Parameter key must not be null"); 107 Assert.notNull(values, "Parameter values must not be null"); 108 this.renderParameters.put(key, values); 109 } 110 getRenderParameter(String key)111 public String getRenderParameter(String key) { 112 Assert.notNull(key, "Parameter key must not be null"); 113 String[] arr = this.renderParameters.get(key); 114 return (arr != null && arr.length > 0 ? arr[0] : null); 115 } 116 getRenderParameterValues(String key)117 public String[] getRenderParameterValues(String key) { 118 Assert.notNull(key, "Parameter key must not be null"); 119 return this.renderParameters.get(key); 120 } 121 getRenderParameterNames()122 public Iterator<String> getRenderParameterNames() { 123 return this.renderParameters.keySet().iterator(); 124 } 125 getRenderParameterMap()126 public Map<String, String[]> getRenderParameterMap() { 127 return Collections.unmodifiableMap(this.renderParameters); 128 } 129 removePublicRenderParameter(String name)130 public void removePublicRenderParameter(String name) { 131 this.renderParameters.remove(name); 132 } 133 setEvent(QName name, Serializable value)134 public void setEvent(QName name, Serializable value) { 135 this.events.put(name, value); 136 } 137 setEvent(String name, Serializable value)138 public void setEvent(String name, Serializable value) { 139 this.events.put(new QName(name), value); 140 } 141 getEventNames()142 public Iterator<QName> getEventNames() { 143 return this.events.keySet().iterator(); 144 } 145 getEvent(QName name)146 public Serializable getEvent(QName name) { 147 return this.events.get(name); 148 } 149 getEvent(String name)150 public Serializable getEvent(String name) { 151 return this.events.get(new QName(name)); 152 } 153 154 }