From 7cdc195331638bdb030e67ef9065bf0ff9732572 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 14 Jan 2009 09:34:07 +0000 Subject: [PATCH] Portlet mocks support Portlet API 2.0 --- org.springframework.test/.classpath | 2 +- org.springframework.test/ivy.xml | 2 +- .../org/springframework/mock/web/package.html | 2 +- .../mock/web/portlet/MockActionRequest.java | 79 ++----- .../mock/web/portlet/MockActionResponse.java | 94 +++----- .../mock/web/portlet/MockBaseURL.java | 152 ++++++++++++ .../mock/web/portlet/MockCacheControl.java | 70 ++++++ .../web/portlet/MockClientDataRequest.java | 129 ++++++++++ .../mock/web/portlet/MockEventRequest.java | 86 +++++++ .../mock/web/portlet/MockEventResponse.java | 34 +++ .../mock/web/portlet/MockMimeResponse.java | 222 ++++++++++++++++++ .../mock/web/portlet/MockPortletConfig.java | 73 +++++- .../mock/web/portlet/MockPortletContext.java | 13 +- .../mock/web/portlet/MockPortletRequest.java | 67 +++++- .../portlet/MockPortletRequestDispatcher.java | 10 + .../mock/web/portlet/MockPortletResponse.java | 91 ++++++- .../mock/web/portlet/MockPortletSession.java | 18 +- .../mock/web/portlet/MockPortletURL.java | 95 +------- .../mock/web/portlet/MockRenderRequest.java | 12 +- .../mock/web/portlet/MockRenderResponse.java | 153 +++--------- .../mock/web/portlet/MockResourceRequest.java | 128 ++++++++++ .../web/portlet/MockResourceResponse.java | 40 ++++ .../mock/web/portlet/MockResourceURL.java | 69 ++++++ .../web/portlet/MockStateAwareResponse.java | 156 ++++++++++++ .../mock/web/portlet/package.html | 2 +- org.springframework.test/template.mf | 2 +- org.springframework.test/test.iml | 6 +- 27 files changed, 1454 insertions(+), 353 deletions(-) create mode 100644 org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockBaseURL.java create mode 100644 org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockCacheControl.java create mode 100644 org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockClientDataRequest.java create mode 100644 org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockEventRequest.java create mode 100644 org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockEventResponse.java create mode 100644 org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockMimeResponse.java create mode 100644 org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockResourceRequest.java create mode 100644 org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockResourceResponse.java create mode 100644 org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockResourceURL.java create mode 100644 org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockStateAwareResponse.java diff --git a/org.springframework.test/.classpath b/org.springframework.test/.classpath index f35ec27145a..842dca8139c 100644 --- a/org.springframework.test/.classpath +++ b/org.springframework.test/.classpath @@ -15,7 +15,7 @@ - + diff --git a/org.springframework.test/ivy.xml b/org.springframework.test/ivy.xml index 37daf4b40d4..76c8b6b08c3 100644 --- a/org.springframework.test/ivy.xml +++ b/org.springframework.test/ivy.xml @@ -22,7 +22,7 @@ - + diff --git a/org.springframework.test/src/main/java/org/springframework/mock/web/package.html b/org.springframework.test/src/main/java/org/springframework/mock/web/package.html index 1bff67eed1f..e4262d69863 100644 --- a/org.springframework.test/src/main/java/org/springframework/mock/web/package.html +++ b/org.springframework.test/src/main/java/org/springframework/mock/web/package.html @@ -1,7 +1,7 @@ -A comprehensive set of Servlet API mock objects, +A comprehensive set of Servlet API 2.5 mock objects, targeted at usage with Spring's web MVC framework. Useful for testing web contexts and controllers. diff --git a/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockActionRequest.java b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockActionRequest.java index e85da537d15..3d8f3ec081f 100644 --- a/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockActionRequest.java +++ b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockActionRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 the original author or authors. + * Copyright 2002-2008 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,14 +16,6 @@ package org.springframework.mock.web.portlet; -import java.io.BufferedReader; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.Reader; -import java.io.UnsupportedEncodingException; - import javax.portlet.ActionRequest; import javax.portlet.PortalContext; import javax.portlet.PortletContext; @@ -36,25 +28,28 @@ import javax.portlet.PortletMode; * @author Juergen Hoeller * @since 2.0 */ -public class MockActionRequest extends MockPortletRequest implements ActionRequest { - - private String characterEncoding; - - private byte[] content; - - private String contentType; - +public class MockActionRequest extends MockClientDataRequest implements ActionRequest { /** * Create a new MockActionRequest with a default {@link MockPortalContext} * and a default {@link MockPortletContext}. - * @see MockPortalContext - * @see MockPortletContext + * @see org.springframework.mock.web.portlet.MockPortalContext + * @see org.springframework.mock.web.portlet.MockPortletContext */ public MockActionRequest() { super(); } + /** + * Create a new MockActionRequest with a default {@link MockPortalContext} + * and a default {@link MockPortletContext}. + * @param actionName the name of the action to trigger + */ + public MockActionRequest(String actionName) { + super(); + setParameter(ActionRequest.ACTION_NAME, actionName); + } + /** * Create a new MockActionRequest with a default {@link MockPortalContext} * and a default {@link MockPortletContext}. @@ -83,49 +78,9 @@ public class MockActionRequest extends MockPortletRequest implements ActionReque } - public void setContent(byte[] content) { - this.content = content; - } - - public InputStream getPortletInputStream() throws IOException { - if (this.content != null) { - return new ByteArrayInputStream(this.content); - } - else { - return null; - } - } - - public void setCharacterEncoding(String characterEncoding) { - this.characterEncoding = characterEncoding; - } - - public BufferedReader getReader() throws UnsupportedEncodingException { - if (this.content != null) { - InputStream sourceStream = new ByteArrayInputStream(this.content); - Reader sourceReader = (this.characterEncoding != null) ? - new InputStreamReader(sourceStream, this.characterEncoding) : new InputStreamReader(sourceStream); - return new BufferedReader(sourceReader); - } - else { - return null; - } - } - - public String getCharacterEncoding() { - return characterEncoding; - } - - public void setContentType(String contentType) { - this.contentType = contentType; - } - - public String getContentType() { - return contentType; - } - - public int getContentLength() { - return (this.content != null ? content.length : -1); + @Override + protected String getLifecyclePhase() { + return ACTION_PHASE; } } diff --git a/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockActionResponse.java b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockActionResponse.java index e608e81c49f..3b7a4ee1a14 100644 --- a/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockActionResponse.java +++ b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockActionResponse.java @@ -17,7 +17,9 @@ package org.springframework.mock.web.portlet; import java.io.IOException; +import java.io.Serializable; import java.util.Collections; +import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; @@ -27,6 +29,7 @@ import javax.portlet.PortletMode; import javax.portlet.PortletModeException; import javax.portlet.WindowState; import javax.portlet.WindowStateException; +import javax.xml.namespace.QName; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; @@ -38,16 +41,12 @@ import org.springframework.util.CollectionUtils; * @author Juergen Hoeller * @since 2.0 */ -public class MockActionResponse extends MockPortletResponse implements ActionResponse { +public class MockActionResponse extends MockStateAwareResponse implements ActionResponse { - private WindowState windowState; - - private PortletMode portletMode; + private boolean redirectAllowed = true; private String redirectedUrl; - private final Map renderParameters = new LinkedHashMap(); - /** * Create a new MockActionResponse with a default {@link MockPortalContext}. @@ -71,93 +70,60 @@ public class MockActionResponse extends MockPortletResponse implements ActionRes if (this.redirectedUrl != null) { throw new IllegalStateException("Cannot set WindowState after sendRedirect has been called"); } - if (!CollectionUtils.contains(getPortalContext().getSupportedWindowStates(), windowState)) { - throw new WindowStateException("WindowState not supported", windowState); - } - this.windowState = windowState; - } - - public WindowState getWindowState() { - return windowState; + super.setWindowState(windowState); + this.redirectAllowed = false; } public void setPortletMode(PortletMode portletMode) throws PortletModeException { if (this.redirectedUrl != null) { throw new IllegalStateException("Cannot set PortletMode after sendRedirect has been called"); } - if (!CollectionUtils.contains(getPortalContext().getSupportedPortletModes(), portletMode)) { - throw new PortletModeException("PortletMode not supported", portletMode); - } - this.portletMode = portletMode; + super.setPortletMode(portletMode); + this.redirectAllowed = false; } - public PortletMode getPortletMode() { - return portletMode; - } - - public void sendRedirect(String url) throws IOException { - if (this.windowState != null || this.portletMode != null || !this.renderParameters.isEmpty()) { - throw new IllegalStateException( - "Cannot call sendRedirect after windowState, portletMode, or renderParameters have been set"); - } - Assert.notNull(url, "Redirect URL must not be null"); - this.redirectedUrl = url; - } - - public String getRedirectedUrl() { - return redirectedUrl; - } - - public void setRenderParameters(Map parameters) { + public void setRenderParameters(Map parameters) { if (this.redirectedUrl != null) { throw new IllegalStateException("Cannot set render parameters after sendRedirect has been called"); } - Assert.notNull(parameters, "Parameters Map must not be null"); - this.renderParameters.clear(); - for (Iterator it = parameters.entrySet().iterator(); it.hasNext();) { - Map.Entry entry = (Map.Entry) it.next(); - Assert.isTrue(entry.getKey() instanceof String, "Key must be of type String"); - Assert.isTrue(entry.getValue() instanceof String[], "Value must be of type String[]"); - this.renderParameters.put((String) entry.getKey(), (String[]) entry.getValue()); - } + super.setRenderParameters(parameters); + this.redirectAllowed = false; } public void setRenderParameter(String key, String value) { if (this.redirectedUrl != null) { throw new IllegalStateException("Cannot set render parameters after sendRedirect has been called"); } - Assert.notNull(key, "Parameter key must not be null"); - Assert.notNull(value, "Parameter value must not be null"); - this.renderParameters.put(key, new String[] {value}); - } - - public String getRenderParameter(String key) { - Assert.notNull(key, "Parameter key must not be null"); - String[] arr = this.renderParameters.get(key); - return (arr != null && arr.length > 0 ? arr[0] : null); + super.setRenderParameter(key, value); + this.redirectAllowed = false; } public void setRenderParameter(String key, String[] values) { if (this.redirectedUrl != null) { throw new IllegalStateException("Cannot set render parameters after sendRedirect has been called"); } - Assert.notNull(key, "Parameter key must not be null"); - Assert.notNull(values, "Parameter values must not be null"); - this.renderParameters.put(key, values); + super.setRenderParameter(key, values); + this.redirectAllowed = false; } - public String[] getRenderParameterValues(String key) { - Assert.notNull(key, "Parameter key must not be null"); - return this.renderParameters.get(key); + public void sendRedirect(String location) throws IOException { + if (!this.redirectAllowed) { + throw new IllegalStateException( + "Cannot call sendRedirect after windowState, portletMode, or renderParameters have been set"); + } + Assert.notNull(location, "Redirect URL must not be null"); + this.redirectedUrl = location; } - public Iterator getRenderParameterNames() { - return this.renderParameters.keySet().iterator(); + public void sendRedirect(String location, String renderUrlParamName) throws IOException { + sendRedirect(location); + if (renderUrlParamName != null) { + setRenderParameter(renderUrlParamName, location); + } } - public Map getRenderParameterMap() { - return Collections.unmodifiableMap(this.renderParameters); + public String getRedirectedUrl() { + return this.redirectedUrl; } - } diff --git a/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockBaseURL.java b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockBaseURL.java new file mode 100644 index 00000000000..0339d9341c8 --- /dev/null +++ b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockBaseURL.java @@ -0,0 +1,152 @@ +/* + * Copyright 2002-2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.mock.web.portlet; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.io.Writer; +import java.net.URLEncoder; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Set; +import javax.portlet.BaseURL; +import javax.portlet.PortletSecurityException; + +import org.springframework.util.Assert; +import org.springframework.util.StringUtils; + +/** + * Mock implementation of the {@link javax.portlet.BaseURL} interface. + * + * @author Juergen Hoeller + * @since 3.0 + */ +public abstract class MockBaseURL implements BaseURL { + + public static final String URL_TYPE_RENDER = "render"; + + public static final String URL_TYPE_ACTION = "action"; + + private static final String ENCODING = "UTF-8"; + + + protected final Map parameters = new LinkedHashMap(); + + private boolean secure = false; + + private final Map properties = new LinkedHashMap(); + + + //--------------------------------------------------------------------- + // BaseURL methods + //--------------------------------------------------------------------- + + public void setParameter(String key, String value) { + Assert.notNull(key, "Parameter key must be null"); + Assert.notNull(value, "Parameter value must not be null"); + this.parameters.put(key, new String[] {value}); + } + + public void setParameter(String key, String[] values) { + Assert.notNull(key, "Parameter key must be null"); + Assert.notNull(values, "Parameter values must not be null"); + this.parameters.put(key, values); + } + + public void setParameters(Map parameters) { + Assert.notNull(parameters, "Parameters Map must not be null"); + this.parameters.clear(); + this.parameters.putAll(parameters); + } + + public Set getParameterNames() { + return this.parameters.keySet(); + } + + public String getParameter(String name) { + String[] arr = this.parameters.get(name); + return (arr != null && arr.length > 0 ? arr[0] : null); + } + + public String[] getParameterValues(String name) { + return this.parameters.get(name); + } + + public Map getParameterMap() { + return Collections.unmodifiableMap(this.parameters); + } + + public void setSecure(boolean secure) throws PortletSecurityException { + this.secure = secure; + } + + public boolean isSecure() { + return this.secure; + } + + public void write(Writer out) throws IOException { + out.write(toString()); + } + + public void write(Writer out, boolean escapeXML) throws IOException { + out.write(toString()); + } + + public void addProperty(String key, String value) { + String[] values = this.properties.get(key); + if (values != null) { + this.properties.put(key, StringUtils.addStringToArray(values, value)); + } + else { + this.properties.put(key, new String[] {value}); + } + } + + public void setProperty(String key, String value) { + this.properties.put(key, new String[] {value}); + } + + public Map getProperties() { + return Collections.unmodifiableMap(this.properties); + } + + + protected String encodeParameter(String name, String value) { + try { + return URLEncoder.encode(name, ENCODING) + "=" + URLEncoder.encode(value, ENCODING); + } + catch (UnsupportedEncodingException ex) { + return null; + } + } + + protected String encodeParameter(String name, String[] values) { + try { + StringBuilder sb = new StringBuilder(); + for (int i = 0, n = values.length; i < n; i++) { + sb.append(i > 0 ? ";" : "").append(URLEncoder.encode(name, ENCODING)).append("=") + .append(URLEncoder.encode(values[i], ENCODING)); + } + return sb.toString(); + } + catch (UnsupportedEncodingException ex) { + return null; + } + } + +} diff --git a/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockCacheControl.java b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockCacheControl.java new file mode 100644 index 00000000000..5a96a43c0fc --- /dev/null +++ b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockCacheControl.java @@ -0,0 +1,70 @@ +/* + * Copyright 2002-2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.mock.web.portlet; + +import javax.portlet.CacheControl; + +/** + * Mock implementation of the {@link javax.portlet.CacheControl} interface. + * + * @author Juergen Hoeller + * @since 3.0 + */ +public class MockCacheControl implements CacheControl { + + private int expirationTime = 0; + + private boolean publicScope = false; + + private String etag; + + private boolean useCachedContent = false; + + + public int getExpirationTime() { + return this.expirationTime; + } + + public void setExpirationTime(int time) { + this.expirationTime = time; + } + + public boolean isPublicScope() { + return this.publicScope; + } + + public void setPublicScope(boolean publicScope) { + this.publicScope = publicScope; + } + + public String getETag() { + return this.etag; + } + + public void setETag(String token) { + this.etag = token; + } + + public boolean useCachedContent() { + return this.useCachedContent; + } + + public void setUseCachedContent(boolean useCachedContent) { + this.useCachedContent = useCachedContent; + } + +} diff --git a/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockClientDataRequest.java b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockClientDataRequest.java new file mode 100644 index 00000000000..3ed0489f59f --- /dev/null +++ b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockClientDataRequest.java @@ -0,0 +1,129 @@ +/* + * Copyright 2002-2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.mock.web.portlet; + +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.io.UnsupportedEncodingException; +import javax.portlet.ClientDataRequest; +import javax.portlet.PortalContext; +import javax.portlet.PortletContext; +import javax.portlet.PortletMode; + +/** + * Mock implementation of the {@link javax.portlet.ClientDataRequest} interface. + * + * @author Juergen Hoeller + * @since 3.0 + */ +public class MockClientDataRequest extends MockPortletRequest implements ClientDataRequest { + + private String characterEncoding; + + private byte[] content; + + private String contentType; + + private String method; + + + /** + * Create a new MockClientDataRequest with a default {@link MockPortalContext} + * and a default {@link MockPortletContext}. + * @see org.springframework.mock.web.portlet.MockPortalContext + * @see org.springframework.mock.web.portlet.MockPortletContext + */ + public MockClientDataRequest() { + super(); + } + + /** + * Create a new MockClientDataRequest with a default {@link MockPortalContext}. + * @param portletContext the PortletContext that the request runs in + */ + public MockClientDataRequest(PortletContext portletContext) { + super(portletContext); + } + + /** + * Create a new MockClientDataRequest. + * @param portalContext the PortalContext that the request runs in + * @param portletContext the PortletContext that the request runs in + */ + public MockClientDataRequest(PortalContext portalContext, PortletContext portletContext) { + super(portalContext, portletContext); + } + + + public void setContent(byte[] content) { + this.content = content; + } + + public InputStream getPortletInputStream() throws IOException { + if (this.content != null) { + return new ByteArrayInputStream(this.content); + } + else { + return null; + } + } + + public void setCharacterEncoding(String characterEncoding) { + this.characterEncoding = characterEncoding; + } + + public BufferedReader getReader() throws UnsupportedEncodingException { + if (this.content != null) { + InputStream sourceStream = new ByteArrayInputStream(this.content); + Reader sourceReader = (this.characterEncoding != null) ? + new InputStreamReader(sourceStream, this.characterEncoding) : new InputStreamReader(sourceStream); + return new BufferedReader(sourceReader); + } + else { + return null; + } + } + + public String getCharacterEncoding() { + return this.characterEncoding; + } + + public void setContentType(String contentType) { + this.contentType = contentType; + } + + public String getContentType() { + return this.contentType; + } + + public int getContentLength() { + return (this.content != null ? content.length : -1); + } + + public void setMethod(String method) { + this.method = method; + } + + public String getMethod() { + return this.method; + } + +} \ No newline at end of file diff --git a/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockEventRequest.java b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockEventRequest.java new file mode 100644 index 00000000000..090627617f8 --- /dev/null +++ b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockEventRequest.java @@ -0,0 +1,86 @@ +/* + * Copyright 2002-2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.mock.web.portlet; + +import javax.portlet.Event; +import javax.portlet.EventRequest; +import javax.portlet.PortalContext; +import javax.portlet.PortletContext; + +/** + * Mock implementation of the {@link javax.portlet.RenderRequest} interface. + * + * @author Juergen Hoeller + * @since 3.0 + */ +public class MockEventRequest extends MockPortletRequest implements EventRequest { + + private final Event event; + + private String method; + + + /** + * Create a new MockRenderRequest with a default {@link MockPortalContext} + * and a default {@link MockPortletContext}. + * @param event the event that this request wraps + */ + public MockEventRequest(Event event) { + super(); + this.event = event; + } + + /** + * Create a new MockRenderRequest with a default {@link MockPortalContext}. + * @param event the event that this request wraps + * @param portletContext the PortletContext that the request runs in + */ + public MockEventRequest(Event event, PortletContext portletContext) { + super(portletContext); + this.event = event; + } + + /** + * Create a new MockRenderRequest. + * @param event the event that this request wraps + * @param portalContext the PortletContext that the request runs in + * @param portletContext the PortletContext that the request runs in + */ + public MockEventRequest(Event event, PortalContext portalContext, PortletContext portletContext) { + super(portalContext, portletContext); + this.event = event; + } + + + @Override + protected String getLifecyclePhase() { + return EVENT_PHASE; + } + + public Event getEvent() { + return this.event; + } + + public void setMethod(String method) { + this.method = method; + } + + public String getMethod() { + return this.method; + } + +} \ No newline at end of file diff --git a/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockEventResponse.java b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockEventResponse.java new file mode 100644 index 00000000000..2f81528b319 --- /dev/null +++ b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockEventResponse.java @@ -0,0 +1,34 @@ +/* + * Copyright 2002-2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.mock.web.portlet; + +import javax.portlet.EventResponse; +import javax.portlet.EventRequest; + +/** + * Mock implementation of the {@link javax.portlet.EventResponse} interface. + * + * @author Juergen Hoeller + * @since 3.0 + */ +public class MockEventResponse extends MockStateAwareResponse implements EventResponse { + + public void setRenderParameters(EventRequest request) { + setRenderParameters(request.getParameterMap()); + } + +} diff --git a/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockMimeResponse.java b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockMimeResponse.java new file mode 100644 index 00000000000..639a1a4b660 --- /dev/null +++ b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockMimeResponse.java @@ -0,0 +1,222 @@ +/* + * Copyright 2002-2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.mock.web.portlet; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.io.UnsupportedEncodingException; +import java.io.Writer; +import java.util.Collection; +import java.util.Collections; +import java.util.Enumeration; +import java.util.Locale; +import javax.portlet.CacheControl; +import javax.portlet.PortalContext; +import javax.portlet.PortletMode; +import javax.portlet.PortletURL; +import javax.portlet.MimeResponse; +import javax.portlet.ResourceURL; +import javax.portlet.PortletRequest; + +import org.springframework.util.CollectionUtils; +import org.springframework.web.util.WebUtils; + +/** + * Mock implementation of the {@link javax.portlet.MimeResponse} interface. + * + * @author Juergen Hoeller + * @since 3.0 + */ +public class MockMimeResponse extends MockPortletResponse implements MimeResponse { + + private PortletRequest request; + + private String contentType; + + private String characterEncoding = WebUtils.DEFAULT_CHARACTER_ENCODING; + + private PrintWriter writer; + + private Locale locale = Locale.getDefault(); + + private int bufferSize = 4096; + + private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + + private final CacheControl cacheControl = new MockCacheControl(); + + private boolean committed; + + private String includedUrl; + + + /** + * Create a new MockMimeResponse with a default {@link MockPortalContext}. + * @see org.springframework.mock.web.portlet.MockPortalContext + */ + public MockMimeResponse() { + super(); + } + + /** + * Create a new MockMimeResponse. + * @param portalContext the PortalContext defining the supported + * PortletModes and WindowStates + */ + public MockMimeResponse(PortalContext portalContext) { + super(portalContext); + } + + /** + * Create a new MockMimeResponse. + * @param portalContext the PortalContext defining the supported + * PortletModes and WindowStates + * @param request the corresponding render/resource request that this response + * is being generated for + */ + public MockMimeResponse(PortalContext portalContext, PortletRequest request) { + super(portalContext); + this.request = request; + } + + + //--------------------------------------------------------------------- + // RenderResponse methods + //--------------------------------------------------------------------- + + public void setContentType(String contentType) { + if (this.request != null) { + Enumeration supportedTypes = this.request.getResponseContentTypes(); + if (!CollectionUtils.contains(supportedTypes, contentType)) { + throw new IllegalArgumentException("Content type [" + contentType + "] not in supported list: " + + Collections.list(supportedTypes)); + } + } + this.contentType = contentType; + } + + public String getContentType() { + return this.contentType; + } + + public void setCharacterEncoding(String characterEncoding) { + this.characterEncoding = characterEncoding; + } + + public String getCharacterEncoding() { + return this.characterEncoding; + } + + public PrintWriter getWriter() throws UnsupportedEncodingException { + if (this.writer == null) { + Writer targetWriter = (this.characterEncoding != null + ? new OutputStreamWriter(this.outputStream, this.characterEncoding) + : new OutputStreamWriter(this.outputStream)); + this.writer = new PrintWriter(targetWriter); + } + return this.writer; + } + + public byte[] getContentAsByteArray() { + flushBuffer(); + return this.outputStream.toByteArray(); + } + + public String getContentAsString() throws UnsupportedEncodingException { + flushBuffer(); + return (this.characterEncoding != null) + ? this.outputStream.toString(this.characterEncoding) + : this.outputStream.toString(); + } + + public void setLocale(Locale locale) { + this.locale = locale; + } + + public Locale getLocale() { + return this.locale; + } + + public void setBufferSize(int bufferSize) { + this.bufferSize = bufferSize; + } + + public int getBufferSize() { + return this.bufferSize; + } + + public void flushBuffer() { + if (this.writer != null) { + this.writer.flush(); + } + if (this.outputStream != null) { + try { + this.outputStream.flush(); + } + catch (IOException ex) { + throw new IllegalStateException("Could not flush OutputStream: " + ex.getMessage()); + } + } + this.committed = true; + } + + public void resetBuffer() { + if (this.committed) { + throw new IllegalStateException("Cannot reset buffer - response is already committed"); + } + this.outputStream.reset(); + } + + public void setCommitted(boolean committed) { + this.committed = committed; + } + + public boolean isCommitted() { + return this.committed; + } + + public void reset() { + resetBuffer(); + this.characterEncoding = null; + this.contentType = null; + this.locale = null; + } + + public OutputStream getPortletOutputStream() throws IOException { + return this.outputStream; + } + + public PortletURL createRenderURL() { + return new MockPortletURL(getPortalContext(), MockPortletURL.URL_TYPE_RENDER); + } + + public PortletURL createActionURL() { + return new MockPortletURL(getPortalContext(), MockPortletURL.URL_TYPE_ACTION); + } + + public ResourceURL createResourceURL() { + return new MockResourceURL(); + } + + public CacheControl getCacheControl() { + return this.cacheControl; + } + +} diff --git a/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockPortletConfig.java b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockPortletConfig.java index 249429e1a13..304fb6777cc 100644 --- a/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockPortletConfig.java +++ b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockPortletConfig.java @@ -16,16 +16,19 @@ package org.springframework.mock.web.portlet; +import java.util.Collections; import java.util.Enumeration; import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; import java.util.Locale; import java.util.Map; -import java.util.Properties; import java.util.ResourceBundle; -import java.util.LinkedHashMap; -import java.util.Collections; +import java.util.Set; import javax.portlet.PortletConfig; import javax.portlet.PortletContext; +import javax.xml.XMLConstants; +import javax.xml.namespace.QName; import org.springframework.util.Assert; @@ -46,6 +49,18 @@ public class MockPortletConfig implements PortletConfig { private final Map initParameters = new LinkedHashMap(); + private final Set publicRenderParameterNames = new LinkedHashSet(); + + private String defaultNamespace = XMLConstants.NULL_NS_URI; + + private final Set publishingEventQNames = new LinkedHashSet(); + + private final Set processingEventQNames = new LinkedHashSet(); + + private final Set supportedLocales = new LinkedHashSet(); + + private final Map containerRuntimeOptions = new LinkedHashMap(); + /** * Create a new MockPortletConfig with a default {@link MockPortletContext}. @@ -113,4 +128,56 @@ public class MockPortletConfig implements PortletConfig { return Collections.enumeration(this.initParameters.keySet()); } + public void addPublicRenderParameterName(String name) { + this.publicRenderParameterNames.add(name); + } + + public Enumeration getPublicRenderParameterNames() { + return Collections.enumeration(this.publicRenderParameterNames); + } + + public void setDefaultNamespace(String defaultNamespace) { + this.defaultNamespace = defaultNamespace; + } + + public String getDefaultNamespace() { + return this.defaultNamespace; + } + + public void addPublishingEventQName(QName name) { + this.publishingEventQNames.add(name); + } + + public Enumeration getPublishingEventQNames() { + return Collections.enumeration(this.publishingEventQNames); + } + + public void addProcessingEventQName(QName name) { + this.processingEventQNames.add(name); + } + + public Enumeration getProcessingEventQNames() { + return Collections.enumeration(this.processingEventQNames); + } + + public void addSupportedLocale(Locale locale) { + this.supportedLocales.add(locale); + } + + public Enumeration getSupportedLocales() { + return Collections.enumeration(this.supportedLocales); + } + + public void addContainerRuntimeOption(String key, String value) { + this.containerRuntimeOptions.put(key, new String[] {value}); + } + + public void addContainerRuntimeOption(String key, String[] values) { + this.containerRuntimeOptions.put(key, values); + } + + public Map getContainerRuntimeOptions() { + return Collections.unmodifiableMap(this.containerRuntimeOptions); + } + } diff --git a/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockPortletContext.java b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockPortletContext.java index 336b21e95b4..e28715b6cf9 100644 --- a/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockPortletContext.java +++ b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockPortletContext.java @@ -25,6 +25,7 @@ import java.util.Collections; import java.util.Enumeration; import java.util.HashSet; import java.util.LinkedHashMap; +import java.util.LinkedHashSet; import java.util.Map; import java.util.Set; import javax.portlet.PortletContext; @@ -63,6 +64,8 @@ public class MockPortletContext implements PortletContext { private String portletContextName = "MockPortletContext"; + private Set containerRuntimeOptions = new LinkedHashSet(); + /** * Create a new MockPortletContext with no base path and a @@ -248,7 +251,15 @@ public class MockPortletContext implements PortletContext { } public String getPortletContextName() { - return portletContextName; + return this.portletContextName; + } + + public void addContainerRuntimeOption(String key) { + this.containerRuntimeOptions.add(key); + } + + public Enumeration getContainerRuntimeOptions() { + return Collections.enumeration(this.containerRuntimeOptions); } } diff --git a/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockPortletRequest.java b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockPortletRequest.java index 84c8af733f3..b1c710a554f 100644 --- a/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockPortletRequest.java +++ b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockPortletRequest.java @@ -33,6 +33,7 @@ import javax.portlet.PortletPreferences; import javax.portlet.PortletRequest; import javax.portlet.PortletSession; import javax.portlet.WindowState; +import javax.servlet.http.Cookie; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; @@ -90,6 +91,12 @@ public class MockPortletRequest implements PortletRequest { private int serverPort = 80; + private String windowID; + + private Cookie[] cookies; + + private final Set publicParameterNames = new HashSet(); + /** * Create a new MockPortletRequest with a default {@link MockPortalContext} @@ -120,6 +127,7 @@ public class MockPortletRequest implements PortletRequest { this.portletContext = (portletContext != null ? portletContext : new MockPortletContext()); this.responseContentTypes.add("text/html"); this.locales.add(Locale.ENGLISH); + this.parameters.put(LIFECYCLE_PHASE, new String[] {getLifecyclePhase()}); } @@ -127,6 +135,13 @@ public class MockPortletRequest implements PortletRequest { // Lifecycle methods //--------------------------------------------------------------------- + /** + * Return the Portlet 2.0 lifecycle id for the current phase. + */ + protected String getLifecyclePhase() { + return null; + } + /** * Return whether this request is still active (that is, not completed yet). */ @@ -363,7 +378,7 @@ public class MockPortletRequest implements PortletRequest { return this.parameters.get(name); } - public Map getParameterMap() { + public Map getParameterMap() { return Collections.unmodifiableMap(this.parameters); } @@ -459,4 +474,54 @@ public class MockPortletRequest implements PortletRequest { return this.serverPort; } + public void setWindowID(String windowID) { + this.windowID = windowID; + } + + public String getWindowID() { + return this.windowID; + } + + public void setCookies(Cookie[] cookies) { + this.cookies = cookies; + } + + public Cookie[] getCookies() { + return this.cookies; + } + + public Map getPrivateParameterMap() { + if (!this.publicParameterNames.isEmpty()) { + Map filtered = new LinkedHashMap(); + for (String key : this.parameters.keySet()) { + if (!this.publicParameterNames.contains(key)) { + filtered.put(key, this.parameters.get(key)); + } + } + return filtered; + } + else { + return Collections.unmodifiableMap(this.parameters); + } + } + + public Map getPublicParameterMap() { + if (!this.publicParameterNames.isEmpty()) { + Map filtered = new LinkedHashMap(); + for (String key : this.parameters.keySet()) { + if (this.publicParameterNames.contains(key)) { + filtered.put(key, this.parameters.get(key)); + } + } + return filtered; + } + else { + return Collections.emptyMap(); + } + } + + public void registerPublicParameter(String name) { + this.publicParameterNames.add(name); + } + } diff --git a/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockPortletRequestDispatcher.java b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockPortletRequestDispatcher.java index 2703c1920c2..1a05db6114b 100644 --- a/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockPortletRequestDispatcher.java +++ b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockPortletRequestDispatcher.java @@ -22,6 +22,8 @@ import javax.portlet.PortletException; import javax.portlet.PortletRequestDispatcher; import javax.portlet.RenderRequest; import javax.portlet.RenderResponse; +import javax.portlet.PortletRequest; +import javax.portlet.PortletResponse; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -61,7 +63,15 @@ public class MockPortletRequestDispatcher implements PortletRequestDispatcher { ((MockRenderResponse) response).setIncludedUrl(this.url); if (logger.isDebugEnabled()) { logger.debug("MockPortletRequestDispatcher: including URL [" + this.url + "]"); + } } + + public void include(PortletRequest request, PortletResponse response) throws PortletException, IOException { + // TODO + } + + public void forward(PortletRequest request, PortletResponse response) throws PortletException, IOException { + // TODO } } diff --git a/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockPortletResponse.java b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockPortletResponse.java index 2c94ddda4a5..6d1eb699f1b 100644 --- a/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockPortletResponse.java +++ b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockPortletResponse.java @@ -16,12 +16,20 @@ package org.springframework.mock.web.portlet; +import java.util.Collections; import java.util.LinkedHashMap; +import java.util.LinkedHashSet; import java.util.Map; import java.util.Set; - import javax.portlet.PortalContext; import javax.portlet.PortletResponse; +import javax.servlet.http.Cookie; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.DOMException; +import org.w3c.dom.Document; +import org.w3c.dom.Element; import org.springframework.util.Assert; @@ -38,6 +46,14 @@ public class MockPortletResponse implements PortletResponse { private final Map properties = new LinkedHashMap(); + private String namespace = ""; + + private final Set cookies = new LinkedHashSet(); + + private final Map xmlProperties = new LinkedHashMap(); + + private Document xmlDocument; + /** * Create a new MockPortletResponse with a default {@link MockPortalContext}. @@ -88,8 +104,8 @@ public class MockPortletResponse implements PortletResponse { this.properties.put(key, new String[] {value}); } - public Set getPropertyNames() { - return this.properties.keySet(); + public Set getPropertyNames() { + return Collections.unmodifiableSet(this.properties.keySet()); } public String getProperty(String key) { @@ -107,4 +123,73 @@ public class MockPortletResponse implements PortletResponse { return path; } + public void setNamespace(String namespace) { + this.namespace = namespace; + } + + public String getNamespace() { + return this.namespace; + } + + public void addProperty(Cookie cookie) { + Assert.notNull(cookie, "Cookie must not be null"); + this.cookies.add(cookie); + } + + public Cookie[] getCookies() { + return this.cookies.toArray(new Cookie[this.cookies.size()]); + } + + public Cookie getCookie(String name) { + Assert.notNull(name, "Cookie name must not be null"); + for (Cookie cookie : this.cookies) { + if (name.equals(cookie.getName())) { + return cookie; + } + } + return null; + } + + public void addProperty(String key, Element value) { + Assert.notNull(key, "Property key must not be null"); + Element[] oldArr = this.xmlProperties.get(key); + if (oldArr != null) { + Element[] newArr = new Element[oldArr.length + 1]; + System.arraycopy(oldArr, 0, newArr, 0, oldArr.length); + newArr[oldArr.length] = value; + this.xmlProperties.put(key, newArr); + } + else { + this.xmlProperties.put(key, new Element[] {value}); + } + } + + + public Set getXmlPropertyNames() { + return Collections.unmodifiableSet(this.xmlProperties.keySet()); + } + + public Element getXmlProperty(String key) { + Assert.notNull(key, "Property key must not be null"); + Element[] arr = this.xmlProperties.get(key); + return (arr != null && arr.length > 0 ? arr[0] : null); + } + + public Element[] getXmlProperties(String key) { + Assert.notNull(key, "Property key must not be null"); + return this.xmlProperties.get(key); + } + + public Element createElement(String tagName) throws DOMException { + if (this.xmlDocument == null) { + try { + this.xmlDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); + } + catch (ParserConfigurationException ex) { + throw new DOMException(DOMException.INVALID_STATE_ERR, ex.toString()); + } + } + return this.xmlDocument.createElement(tagName); + } + } diff --git a/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockPortletSession.java b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockPortletSession.java index ab27defe000..b0845c07113 100644 --- a/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockPortletSession.java +++ b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockPortletSession.java @@ -184,7 +184,23 @@ public class MockPortletSession implements PortletSession { } public PortletContext getPortletContext() { - return portletContext; + return this.portletContext; + } + + public Map getAttributeMap() { + return Collections.unmodifiableMap(this.portletAttributes); + } + + public Map getAttributeMap(int scope) { + if (scope == PortletSession.PORTLET_SCOPE) { + return Collections.unmodifiableMap(this.portletAttributes); + } + else if (scope == PortletSession.APPLICATION_SCOPE) { + return Collections.unmodifiableMap(this.applicationAttributes); + } + else { + return Collections.emptyMap(); + } } } diff --git a/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockPortletURL.java b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockPortletURL.java index edf889cf93e..c030f00f311 100644 --- a/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockPortletURL.java +++ b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockPortletURL.java @@ -16,18 +16,10 @@ package org.springframework.mock.web.portlet; -import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; -import java.util.Collections; -import java.util.Iterator; -import java.util.LinkedHashMap; import java.util.Map; -import java.util.Set; - import javax.portlet.PortalContext; import javax.portlet.PortletMode; import javax.portlet.PortletModeException; -import javax.portlet.PortletSecurityException; import javax.portlet.PortletURL; import javax.portlet.WindowState; import javax.portlet.WindowStateException; @@ -42,14 +34,12 @@ import org.springframework.util.CollectionUtils; * @author Juergen Hoeller * @since 2.0 */ -public class MockPortletURL implements PortletURL { +public class MockPortletURL extends MockBaseURL implements PortletURL { public static final String URL_TYPE_RENDER = "render"; public static final String URL_TYPE_ACTION = "action"; - private static final String ENCODING = "UTF-8"; - private final PortalContext portalContext; @@ -59,10 +49,6 @@ public class MockPortletURL implements PortletURL { private PortletMode portletMode; - private final Map parameters = new LinkedHashMap(); - - private boolean secure = false; - /** * Create a new MockPortletURL for the given URL type. @@ -90,6 +76,10 @@ public class MockPortletURL implements PortletURL { this.windowState = windowState; } + public WindowState getWindowState() { + return this.windowState; + } + public void setPortletMode(PortletMode portletMode) throws PortletModeException { if (!CollectionUtils.contains(this.portalContext.getSupportedPortletModes(), portletMode)) { throw new PortletModeException("PortletMode not supported", portletMode); @@ -97,77 +87,12 @@ public class MockPortletURL implements PortletURL { this.portletMode = portletMode; } - public void setParameter(String key, String value) { - Assert.notNull(key, "Parameter key must be null"); - Assert.notNull(value, "Parameter value must not be null"); - this.parameters.put(key, new String[] {value}); + public PortletMode getPortletMode() { + return this.portletMode; } - public void setParameter(String key, String[] values) { - Assert.notNull(key, "Parameter key must be null"); - Assert.notNull(values, "Parameter values must not be null"); - this.parameters.put(key, values); - } - - public void setParameters(Map parameters) { - Assert.notNull(parameters, "Parameters Map must not be null"); - this.parameters.clear(); - for (Iterator it = parameters.entrySet().iterator(); it.hasNext();) { - Map.Entry entry = (Map.Entry) it.next(); - Assert.isTrue(entry.getKey() instanceof String, "Key must be of type String"); - Assert.isTrue(entry.getValue() instanceof String[], "Value must be of type String[]"); - this.parameters.put((String) entry.getKey(), (String[]) entry.getValue()); - } - } - - public Set getParameterNames() { - return this.parameters.keySet(); - } - - public String getParameter(String name) { - String[] arr = this.parameters.get(name); - return (arr != null && arr.length > 0 ? arr[0] : null); - } - - public String[] getParameterValues(String name) { - return this.parameters.get(name); - } - - public Map getParameterMap() { - return Collections.unmodifiableMap(this.parameters); - } - - public void setSecure(boolean secure) throws PortletSecurityException { - this.secure = secure; - } - - public boolean isSecure() { - return this.secure; - } - - - private String encodeParameter(String name, String value) { - try { - return URLEncoder.encode(name, ENCODING) + "=" + URLEncoder.encode(value, ENCODING); - } - catch (UnsupportedEncodingException ex) { - return null; - } - } - - private String encodeParameter(String name, String[] values) { - try { - StringBuilder sb = new StringBuilder(); - for (int i = 0, n = values.length; i < n; i++) { - sb.append((i > 0 ? ";" : "") + - URLEncoder.encode(name, ENCODING) + "=" + - URLEncoder.encode(values[i], ENCODING)); - } - return sb.toString(); - } - catch (UnsupportedEncodingException ex) { - return null; - } + public void removePublicRenderParameter(String name) { + this.parameters.remove(name); } @@ -183,7 +108,7 @@ public class MockPortletURL implements PortletURL { for (Map.Entry entry : this.parameters.entrySet()) { sb.append(";").append(encodeParameter("param_" + entry.getKey(), entry.getValue())); } - return (this.secure ? "https:" : "http:") + + return (isSecure() ? "https:" : "http:") + "//localhost/mockportlet?" + sb.toString(); } diff --git a/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockRenderRequest.java b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockRenderRequest.java index 5bb8c54e42d..5331b084186 100644 --- a/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockRenderRequest.java +++ b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockRenderRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 the original author or authors. + * Copyright 2002-2008 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -67,4 +67,14 @@ public class MockRenderRequest extends MockPortletRequest implements RenderReque super(portalContext, portletContext); } + + @Override + protected String getLifecyclePhase() { + return RENDER_PHASE; + } + + public String getETag() { + return getProperty(RenderRequest.ETAG); + } + } diff --git a/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockRenderResponse.java b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockRenderResponse.java index c63e9a44599..298f3760aa0 100644 --- a/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockRenderResponse.java +++ b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockRenderResponse.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2006 the original author or authors. + * Copyright 2002-2008 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,12 +23,19 @@ import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.io.Writer; +import java.util.Collection; +import java.util.Collections; +import java.util.Enumeration; import java.util.Locale; - +import javax.portlet.CacheControl; import javax.portlet.PortalContext; +import javax.portlet.PortletMode; import javax.portlet.PortletURL; +import javax.portlet.RenderRequest; import javax.portlet.RenderResponse; +import javax.portlet.ResourceURL; +import org.springframework.util.CollectionUtils; import org.springframework.web.util.WebUtils; /** @@ -38,25 +45,11 @@ import org.springframework.web.util.WebUtils; * @author Juergen Hoeller * @since 2.0 */ -public class MockRenderResponse extends MockPortletResponse implements RenderResponse { - - private String contentType; - - private String namespace = "MockPortlet"; +public class MockRenderResponse extends MockMimeResponse implements RenderResponse { private String title; - private String characterEncoding = WebUtils.DEFAULT_CHARACTER_ENCODING; - - private PrintWriter writer; - - private Locale locale = Locale.getDefault(); - - private int bufferSize = 4096; - - private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - - private boolean committed; + private Collection nextPossiblePortletModes; private String includedUrl; @@ -78,126 +71,36 @@ public class MockRenderResponse extends MockPortletResponse implements RenderRes super(portalContext); } + /** + * Create a new MockRenderResponse. + * @param portalContext the PortalContext defining the supported + * PortletModes and WindowStates + * @param request the corresponding render request that this response + * is generated for + */ + public MockRenderResponse(PortalContext portalContext, RenderRequest request) { + super(portalContext, request); + } + //--------------------------------------------------------------------- // RenderResponse methods //--------------------------------------------------------------------- - public String getContentType() { - return this.contentType; - } - - public PortletURL createRenderURL() { - PortletURL url = new MockPortletURL(getPortalContext(), MockPortletURL.URL_TYPE_RENDER); - return url; - } - - public PortletURL createActionURL() { - PortletURL url = new MockPortletURL(getPortalContext(), MockPortletURL.URL_TYPE_ACTION); - return url; - } - - public String getNamespace() { - return this.namespace; - } - public void setTitle(String title) { this.title = title; } public String getTitle() { - return title; + return this.title; } - public void setContentType(String contentType) { - this.contentType = contentType; + public void setNextPossiblePortletModes(Collection portletModes) { + this.nextPossiblePortletModes = portletModes; } - public void setCharacterEncoding(String characterEncoding) { - this.characterEncoding = characterEncoding; - } - - public String getCharacterEncoding() { - return this.characterEncoding; - } - - public PrintWriter getWriter() throws UnsupportedEncodingException { - if (this.writer == null) { - Writer targetWriter = (this.characterEncoding != null - ? new OutputStreamWriter(this.outputStream, this.characterEncoding) - : new OutputStreamWriter(this.outputStream)); - this.writer = new PrintWriter(targetWriter); - } - return this.writer; - } - - public byte[] getContentAsByteArray() { - flushBuffer(); - return this.outputStream.toByteArray(); - } - - public String getContentAsString() throws UnsupportedEncodingException { - flushBuffer(); - return (this.characterEncoding != null) - ? this.outputStream.toString(this.characterEncoding) - : this.outputStream.toString(); - } - - public void setLocale(Locale locale) { - this.locale = locale; - } - - public Locale getLocale() { - return this.locale; - } - - public void setBufferSize(int bufferSize) { - this.bufferSize = bufferSize; - } - - public int getBufferSize() { - return this.bufferSize; - } - - public void flushBuffer() { - if (this.writer != null) { - this.writer.flush(); - } - if (this.outputStream != null) { - try { - this.outputStream.flush(); - } - catch (IOException ex) { - throw new IllegalStateException("Could not flush OutputStream: " + ex.getMessage()); - } - } - this.committed = true; - } - - public void resetBuffer() { - if (this.committed) { - throw new IllegalStateException("Cannot reset buffer - response is already committed"); - } - this.outputStream.reset(); - } - - public void setCommitted(boolean committed) { - this.committed = committed; - } - - public boolean isCommitted() { - return this.committed; - } - - public void reset() { - resetBuffer(); - this.characterEncoding = null; - this.contentType = null; - this.locale = null; - } - - public OutputStream getPortletOutputStream() throws IOException { - return this.outputStream; + public Collection getNextPossiblePortletModes() { + return this.nextPossiblePortletModes; } @@ -210,7 +113,7 @@ public class MockRenderResponse extends MockPortletResponse implements RenderRes } public String getIncludedUrl() { - return includedUrl; + return this.includedUrl; } } diff --git a/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockResourceRequest.java b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockResourceRequest.java new file mode 100644 index 00000000000..f8a94dc7380 --- /dev/null +++ b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockResourceRequest.java @@ -0,0 +1,128 @@ +/* + * Copyright 2002-2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.mock.web.portlet; + +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; +import javax.portlet.PortalContext; +import javax.portlet.PortletContext; +import javax.portlet.RenderRequest; +import javax.portlet.ResourceRequest; + +/** + * Mock implementation of the {@link javax.portlet.ActionRequest} interface. + * + * @author Juergen Hoeller + * @since 3.0 + */ +public class MockResourceRequest extends MockClientDataRequest implements ResourceRequest { + + private String resourceID; + + private String cacheability; + + private final Map privateRenderParameterMap = new LinkedHashMap(); + + + /** + * Create a new MockResourceRequest with a default {@link MockPortalContext} + * and a default {@link MockPortletContext}. + * @see org.springframework.mock.web.portlet.MockPortalContext + * @see org.springframework.mock.web.portlet.MockPortletContext + */ + public MockResourceRequest() { + super(); + } + + /** + * Create a new MockResourceRequest with a default {@link MockPortalContext} + * and a default {@link MockPortletContext}. + * @param resourceID the resource id for this request + */ + public MockResourceRequest(String resourceID) { + super(); + this.resourceID = resourceID; + } + + /** + * Create a new MockResourceRequest with a default {@link MockPortalContext} + * and a default {@link MockPortletContext}. + * @param url the resource URL for this request + */ + public MockResourceRequest(MockResourceURL url) { + super(); + this.resourceID = url.getResourceID(); + this.cacheability = url.getCacheability(); + } + + /** + * Create a new MockResourceRequest with a default {@link MockPortalContext}. + * @param portletContext the PortletContext that the request runs in + */ + public MockResourceRequest(PortletContext portletContext) { + super(portletContext); + } + + /** + * Create a new MockResourceRequest. + * @param portalContext the PortalContext that the request runs in + * @param portletContext the PortletContext that the request runs in + */ + public MockResourceRequest(PortalContext portalContext, PortletContext portletContext) { + super(portalContext, portletContext); + } + + + @Override + protected String getLifecyclePhase() { + return RESOURCE_PHASE; + } + + public void setResourceID(String resourceID) { + this.resourceID = resourceID; + } + + public String getResourceID() { + return this.resourceID; + } + + public void setCacheability(String cacheLevel) { + this.cacheability = cacheLevel; + } + + public String getCacheability() { + return this.cacheability; + } + + public String getETag() { + return getProperty(RenderRequest.ETAG); + } + + public void addPrivateRenderParameter(String key, String value) { + this.privateRenderParameterMap.put(key, new String[] {value}); + } + + public void addPrivateRenderParameter(String key, String[] values) { + this.privateRenderParameterMap.put(key, values); + } + + public Map getPrivateRenderParameterMap() { + return Collections.unmodifiableMap(this.privateRenderParameterMap); + } + +} diff --git a/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockResourceResponse.java b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockResourceResponse.java new file mode 100644 index 00000000000..1f8ea709afb --- /dev/null +++ b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockResourceResponse.java @@ -0,0 +1,40 @@ +/* + * Copyright 2002-2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.mock.web.portlet; + +import javax.portlet.ResourceResponse; + +/** + * Mock implementation of the {@link javax.portlet.ResourceResponse} interface. + * + * @author Juergen Hoeller + * @since 3.0 + */ +public class MockResourceResponse extends MockMimeResponse implements ResourceResponse { + + private int contentLength = 0; + + + public void setContentLength(int len) { + this.contentLength = len; + } + + public int getContentLength() { + return this.contentLength; + } + +} diff --git a/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockResourceURL.java b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockResourceURL.java new file mode 100644 index 00000000000..5b46d248692 --- /dev/null +++ b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockResourceURL.java @@ -0,0 +1,69 @@ +/* + * Copyright 2002-2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.mock.web.portlet; + +import java.util.Map; +import javax.portlet.ResourceURL; + +/** + * Mock implementation of the {@link javax.portlet.ResourceURL} interface. + * + * @author Juergen Hoeller + * @since 3.0 + */ +public class MockResourceURL extends MockBaseURL implements ResourceURL { + + private String resourceID; + + private String cacheability; + + + //--------------------------------------------------------------------- + // ResourceURL methods + //--------------------------------------------------------------------- + + public void setResourceID(String resourceID) { + this.resourceID = resourceID; + } + + public String getResourceID() { + return this.resourceID; + } + + public void setCacheability(String cacheLevel) { + this.cacheability = cacheLevel; + } + + public String getCacheability() { + return this.cacheability; + } + + + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(encodeParameter("resourceID", this.resourceID)); + if (this.cacheability != null) { + sb.append(";").append(encodeParameter("cacheability", this.cacheability)); + } + for (Map.Entry entry : this.parameters.entrySet()) { + sb.append(";").append(encodeParameter("param_" + entry.getKey(), entry.getValue())); + } + return (isSecure() ? "https:" : "http:") + + "//localhost/mockportlet?" + sb.toString(); + } + +} diff --git a/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockStateAwareResponse.java b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockStateAwareResponse.java new file mode 100644 index 00000000000..39c6885b126 --- /dev/null +++ b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockStateAwareResponse.java @@ -0,0 +1,156 @@ +/* + * Copyright 2002-2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.mock.web.portlet; + +import java.io.IOException; +import java.io.Serializable; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.Map; +import javax.portlet.ActionResponse; +import javax.portlet.PortalContext; +import javax.portlet.PortletMode; +import javax.portlet.PortletModeException; +import javax.portlet.WindowState; +import javax.portlet.WindowStateException; +import javax.portlet.StateAwareResponse; +import javax.xml.namespace.QName; + +import org.springframework.util.Assert; +import org.springframework.util.CollectionUtils; + +/** + * Mock implementation of the {@link javax.portlet.StateAwareResponse} interface. + * + * @author Juergen Hoeller + * @since 3.0 + */ +public class MockStateAwareResponse extends MockPortletResponse implements StateAwareResponse { + + private WindowState windowState; + + private PortletMode portletMode; + + private final Map renderParameters = new LinkedHashMap(); + + private final Map events = new HashMap(); + + + /** + * Create a new MockActionResponse with a default {@link MockPortalContext}. + * @see org.springframework.mock.web.portlet.MockPortalContext + */ + public MockStateAwareResponse() { + super(); + } + + /** + * Create a new MockActionResponse. + * @param portalContext the PortalContext defining the supported + * PortletModes and WindowStates + */ + public MockStateAwareResponse(PortalContext portalContext) { + super(portalContext); + } + + + public void setWindowState(WindowState windowState) throws WindowStateException { + if (!CollectionUtils.contains(getPortalContext().getSupportedWindowStates(), windowState)) { + throw new WindowStateException("WindowState not supported", windowState); + } + this.windowState = windowState; + } + + public WindowState getWindowState() { + return this.windowState; + } + + public void setPortletMode(PortletMode portletMode) throws PortletModeException { + if (!CollectionUtils.contains(getPortalContext().getSupportedPortletModes(), portletMode)) { + throw new PortletModeException("PortletMode not supported", portletMode); + } + this.portletMode = portletMode; + } + + public PortletMode getPortletMode() { + return this.portletMode; + } + + public void setRenderParameters(Map parameters) { + Assert.notNull(parameters, "Parameters Map must not be null"); + this.renderParameters.clear(); + this.renderParameters.putAll(parameters); + } + + public void setRenderParameter(String key, String value) { + Assert.notNull(key, "Parameter key must not be null"); + Assert.notNull(value, "Parameter value must not be null"); + this.renderParameters.put(key, new String[] {value}); + } + + public void setRenderParameter(String key, String[] values) { + Assert.notNull(key, "Parameter key must not be null"); + Assert.notNull(values, "Parameter values must not be null"); + this.renderParameters.put(key, values); + } + + public String getRenderParameter(String key) { + Assert.notNull(key, "Parameter key must not be null"); + String[] arr = this.renderParameters.get(key); + return (arr != null && arr.length > 0 ? arr[0] : null); + } + + public String[] getRenderParameterValues(String key) { + Assert.notNull(key, "Parameter key must not be null"); + return this.renderParameters.get(key); + } + + public Iterator getRenderParameterNames() { + return this.renderParameters.keySet().iterator(); + } + + public Map getRenderParameterMap() { + return Collections.unmodifiableMap(this.renderParameters); + } + + public void removePublicRenderParameter(String name) { + this.renderParameters.remove(name); + } + + public void setEvent(QName name, Serializable value) { + this.events.put(name, value); + } + + public void setEvent(String name, Serializable value) { + this.events.put(new QName(name), value); + } + + public Iterator getEventNames() { + return this.events.keySet().iterator(); + } + + public Serializable getEvent(QName name) { + return this.events.get(name); + } + + public Serializable getEvent(String name) { + return this.events.get(new QName(name)); + } + +} \ No newline at end of file diff --git a/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/package.html b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/package.html index 3fa19b640c7..18595a68389 100644 --- a/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/package.html +++ b/org.springframework.test/src/main/java/org/springframework/mock/web/portlet/package.html @@ -1,7 +1,7 @@ -A comprehensive set of Portlet API mock objects, +A comprehensive set of Portlet API 2.0 mock objects, targeted at usage with Spring's web MVC framework. Useful for testing web contexts and controllers. diff --git a/org.springframework.test/template.mf b/org.springframework.test/template.mf index 5f48b761661..a5548d5759f 100644 --- a/org.springframework.test/template.mf +++ b/org.springframework.test/template.mf @@ -5,7 +5,7 @@ Bundle-ManifestVersion: 2 Import-Template: javax.activation.*;version="[1.1.0, 2.0.0)";resolution:=optional, javax.persistence.*;version="[1.0.0, 2.0.0)";resolution:=optional, - javax.portlet.*;version="[1.0.0, 2.0.0)";resolution:=optional, + javax.portlet.*;version="[2.0.0, 3.0.0)";resolution:=optional, javax.servlet;version="[2.4.0, 3.0.0)";resolution:=optional, javax.servlet.http;version="[2.4.0, 3.0.0)";resolution:=optional, javax.servlet.jsp.*;version="[2.0.0, 3.0.0)";resolution:=optional, diff --git a/org.springframework.test/test.iml b/org.springframework.test/test.iml index 17d6b2c5d3c..f7e7efccf6d 100644 --- a/org.springframework.test/test.iml +++ b/org.springframework.test/test.iml @@ -45,10 +45,12 @@ - + - + + +