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.Serializable;
020import java.util.Collections;
021import java.util.HashMap;
022import java.util.Iterator;
023import java.util.LinkedHashMap;
024import java.util.Map;
025import javax.portlet.PortalContext;
026import javax.portlet.PortletMode;
027import javax.portlet.PortletModeException;
028import javax.portlet.StateAwareResponse;
029import javax.portlet.WindowState;
030import javax.portlet.WindowStateException;
031import javax.xml.namespace.QName;
032
033import org.springframework.util.Assert;
034import org.springframework.util.CollectionUtils;
035
036/**
037 * Mock implementation of the {@link javax.portlet.StateAwareResponse} interface.
038 *
039 * @author Juergen Hoeller
040 * @since 3.0
041 */
042public class MockStateAwareResponse extends MockPortletResponse implements StateAwareResponse {
043
044        private WindowState windowState;
045
046        private PortletMode portletMode;
047
048        private final Map<String, String[]> renderParameters = new LinkedHashMap<String, String[]>();
049
050        private final Map<QName, Serializable> events = new HashMap<QName, Serializable>();
051
052
053        /**
054         * Create a new MockActionResponse with a default {@link MockPortalContext}.
055         * @see org.springframework.mock.web.portlet.MockPortalContext
056         */
057        public MockStateAwareResponse() {
058                super();
059        }
060
061        /**
062         * Create a new MockActionResponse.
063         * @param portalContext the PortalContext defining the supported
064         * PortletModes and WindowStates
065         */
066        public MockStateAwareResponse(PortalContext portalContext) {
067                super(portalContext);
068        }
069
070
071        @Override
072        public void setWindowState(WindowState windowState) throws WindowStateException {
073                if (!CollectionUtils.contains(getPortalContext().getSupportedWindowStates(), windowState)) {
074                        throw new WindowStateException("WindowState not supported", windowState);
075                }
076                this.windowState = windowState;
077        }
078
079        @Override
080        public WindowState getWindowState() {
081                return this.windowState;
082        }
083
084        @Override
085        public void setPortletMode(PortletMode portletMode) throws PortletModeException {
086                if (!CollectionUtils.contains(getPortalContext().getSupportedPortletModes(), portletMode)) {
087                        throw new PortletModeException("PortletMode not supported", portletMode);
088                }
089                this.portletMode = portletMode;
090        }
091
092        @Override
093        public PortletMode getPortletMode() {
094                return this.portletMode;
095        }
096
097        @Override
098        public void setRenderParameters(Map<String, String[]> parameters) {
099                Assert.notNull(parameters, "Parameters Map must not be null");
100                this.renderParameters.clear();
101                this.renderParameters.putAll(parameters);
102        }
103
104        @Override
105        public void setRenderParameter(String key, String value) {
106                Assert.notNull(key, "Parameter key must not be null");
107                Assert.notNull(value, "Parameter value must not be null");
108                this.renderParameters.put(key, new String[] {value});
109        }
110
111        @Override
112        public void setRenderParameter(String key, String[] values) {
113                Assert.notNull(key, "Parameter key must not be null");
114                Assert.notNull(values, "Parameter values must not be null");
115                this.renderParameters.put(key, values);
116        }
117
118        public String getRenderParameter(String key) {
119                Assert.notNull(key, "Parameter key must not be null");
120                String[] arr = this.renderParameters.get(key);
121                return (arr != null && arr.length > 0 ? arr[0] : null);
122        }
123
124        public String[] getRenderParameterValues(String key) {
125                Assert.notNull(key, "Parameter key must not be null");
126                return this.renderParameters.get(key);
127        }
128
129        public Iterator<String> getRenderParameterNames() {
130                return this.renderParameters.keySet().iterator();
131        }
132
133        @Override
134        public Map<String, String[]> getRenderParameterMap() {
135                return Collections.unmodifiableMap(this.renderParameters);
136        }
137
138        @Override
139        public void removePublicRenderParameter(String name) {
140                this.renderParameters.remove(name);
141        }
142
143        @Override
144        public void setEvent(QName name, Serializable value) {
145                this.events.put(name, value);
146        }
147
148        @Override
149        public void setEvent(String name, Serializable value) {
150                this.events.put(new QName(name), value);
151        }
152
153        public Iterator<QName> getEventNames() {
154                return this.events.keySet().iterator();
155        }
156
157        public Serializable getEvent(QName name) {
158                return this.events.get(name);
159        }
160
161        public Serializable getEvent(String name) {
162                return this.events.get(new QName(name));
163        }
164
165}