Portlet mocks support Portlet API 2.0

This commit is contained in:
Juergen Hoeller 2009-01-14 09:34:07 +00:00
parent 46f99640ee
commit 7cdc195331
27 changed files with 1454 additions and 353 deletions

View File

@ -15,7 +15,7 @@
<classpathentry combineaccessrules="false" kind="src" path="/org.springframework.web.servlet"/> <classpathentry combineaccessrules="false" kind="src" path="/org.springframework.web.servlet"/>
<classpathentry kind="var" path="IVY_CACHE/javax.activation/com.springsource.javax.activation/1.1.0/com.springsource.javax.activation-1.1.0.jar" sourcepath="/IVY_CACHE/javax.activation/com.springsource.javax.activation/1.1.0/com.springsource.javax.activation-sources-1.1.0.jar"/> <classpathentry kind="var" path="IVY_CACHE/javax.activation/com.springsource.javax.activation/1.1.0/com.springsource.javax.activation-1.1.0.jar" sourcepath="/IVY_CACHE/javax.activation/com.springsource.javax.activation/1.1.0/com.springsource.javax.activation-sources-1.1.0.jar"/>
<classpathentry kind="var" path="IVY_CACHE/javax.persistence/com.springsource.javax.persistence/1.0.0/com.springsource.javax.persistence-1.0.0.jar" sourcepath="/IVY_CACHE/javax.persistence/com.springsource.javax.persistence/1.0.0/com.springsource.javax.persistence-sources-1.0.0.jar"/> <classpathentry kind="var" path="IVY_CACHE/javax.persistence/com.springsource.javax.persistence/1.0.0/com.springsource.javax.persistence-1.0.0.jar" sourcepath="/IVY_CACHE/javax.persistence/com.springsource.javax.persistence/1.0.0/com.springsource.javax.persistence-sources-1.0.0.jar"/>
<classpathentry kind="var" path="IVY_CACHE/javax.portlet/com.springsource.javax.portlet/1.0.0/com.springsource.javax.portlet-1.0.0.jar"/> <classpathentry kind="var" path="IVY_CACHE/javax.portlet/com.springsource.javax.portlet/2.0.0/com.springsource.javax.portlet-2.0.0.jar"/>
<classpathentry kind="var" path="IVY_CACHE/javax.servlet/com.springsource.javax.servlet/2.5.0/com.springsource.javax.servlet-2.5.0.jar" sourcepath="/IVY_CACHE/javax.servlet/com.springsource.javax.servlet/2.5.0/com.springsource.javax.servlet-sources-2.5.0.jar"/> <classpathentry kind="var" path="IVY_CACHE/javax.servlet/com.springsource.javax.servlet/2.5.0/com.springsource.javax.servlet-2.5.0.jar" sourcepath="/IVY_CACHE/javax.servlet/com.springsource.javax.servlet/2.5.0/com.springsource.javax.servlet-sources-2.5.0.jar"/>
<classpathentry kind="var" path="IVY_CACHE/javax.servlet/com.springsource.javax.servlet.jsp/2.1.0/com.springsource.javax.servlet.jsp-2.1.0.jar" sourcepath="/IVY_CACHE/javax.servlet/com.springsource.javax.servlet.jsp/2.1.0/com.springsource.javax.servlet.jsp-sources-2.1.0.jar"/> <classpathentry kind="var" path="IVY_CACHE/javax.servlet/com.springsource.javax.servlet.jsp/2.1.0/com.springsource.javax.servlet.jsp-2.1.0.jar" sourcepath="/IVY_CACHE/javax.servlet/com.springsource.javax.servlet.jsp/2.1.0/com.springsource.javax.servlet.jsp-sources-2.1.0.jar"/>
<classpathentry kind="var" path="IVY_CACHE/org.junit/com.springsource.junit/3.8.2/com.springsource.junit-3.8.2.jar" sourcepath="/IVY_CACHE/org.junit/com.springsource.junit/3.8.2/com.springsource.junit-sources-3.8.2.jar"/> <classpathentry kind="var" path="IVY_CACHE/org.junit/com.springsource.junit/3.8.2/com.springsource.junit-3.8.2.jar" sourcepath="/IVY_CACHE/org.junit/com.springsource.junit/3.8.2/com.springsource.junit-sources-3.8.2.jar"/>

View File

@ -22,7 +22,7 @@
<dependency org="javax.activation" name="com.springsource.javax.activation" rev="1.1.0" conf="provided->compile"/> <dependency org="javax.activation" name="com.springsource.javax.activation" rev="1.1.0" conf="provided->compile"/>
<dependency org="javax.el" name="com.springsource.javax.el" rev="2.1.0" conf="provided->compile"/> <dependency org="javax.el" name="com.springsource.javax.el" rev="2.1.0" conf="provided->compile"/>
<dependency org="javax.persistence" name="com.springsource.javax.persistence" rev="1.0.0" conf="provided->compile"/> <dependency org="javax.persistence" name="com.springsource.javax.persistence" rev="1.0.0" conf="provided->compile"/>
<dependency org="javax.portlet" name="com.springsource.javax.portlet" rev="1.0.0" conf="provided->compile"/> <dependency org="javax.portlet" name="com.springsource.javax.portlet" rev="2.0.0" conf="provided->compile"/>
<dependency org="javax.servlet" name="com.springsource.javax.servlet" rev="2.5.0" conf="provided->compile"/> <dependency org="javax.servlet" name="com.springsource.javax.servlet" rev="2.5.0" conf="provided->compile"/>
<dependency org="javax.servlet" name="com.springsource.javax.servlet.jsp" rev="2.1.0" conf="provided->compile"/> <dependency org="javax.servlet" name="com.springsource.javax.servlet.jsp" rev="2.1.0" conf="provided->compile"/>
<dependency org="org.junit" name="com.springsource.junit" rev="3.8.2" conf="optional->compile"/> <dependency org="org.junit" name="com.springsource.junit" rev="3.8.2" conf="optional->compile"/>

View File

@ -1,7 +1,7 @@
<html> <html>
<body> <body>
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. targeted at usage with Spring's web MVC framework.
Useful for testing web contexts and controllers. Useful for testing web contexts and controllers.

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,14 +16,6 @@
package org.springframework.mock.web.portlet; 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.ActionRequest;
import javax.portlet.PortalContext; import javax.portlet.PortalContext;
import javax.portlet.PortletContext; import javax.portlet.PortletContext;
@ -36,25 +28,28 @@ import javax.portlet.PortletMode;
* @author Juergen Hoeller * @author Juergen Hoeller
* @since 2.0 * @since 2.0
*/ */
public class MockActionRequest extends MockPortletRequest implements ActionRequest { public class MockActionRequest extends MockClientDataRequest implements ActionRequest {
private String characterEncoding;
private byte[] content;
private String contentType;
/** /**
* Create a new MockActionRequest with a default {@link MockPortalContext} * Create a new MockActionRequest with a default {@link MockPortalContext}
* and a default {@link MockPortletContext}. * and a default {@link MockPortletContext}.
* @see MockPortalContext * @see org.springframework.mock.web.portlet.MockPortalContext
* @see MockPortletContext * @see org.springframework.mock.web.portlet.MockPortletContext
*/ */
public MockActionRequest() { public MockActionRequest() {
super(); 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} * Create a new MockActionRequest with a default {@link MockPortalContext}
* and a default {@link MockPortletContext}. * and a default {@link MockPortletContext}.
@ -83,49 +78,9 @@ public class MockActionRequest extends MockPortletRequest implements ActionReque
} }
public void setContent(byte[] content) { @Override
this.content = content; protected String getLifecyclePhase() {
} return ACTION_PHASE;
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);
} }
} }

View File

@ -17,7 +17,9 @@
package org.springframework.mock.web.portlet; package org.springframework.mock.web.portlet;
import java.io.IOException; import java.io.IOException;
import java.io.Serializable;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
@ -27,6 +29,7 @@ import javax.portlet.PortletMode;
import javax.portlet.PortletModeException; import javax.portlet.PortletModeException;
import javax.portlet.WindowState; import javax.portlet.WindowState;
import javax.portlet.WindowStateException; import javax.portlet.WindowStateException;
import javax.xml.namespace.QName;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
@ -38,16 +41,12 @@ import org.springframework.util.CollectionUtils;
* @author Juergen Hoeller * @author Juergen Hoeller
* @since 2.0 * @since 2.0
*/ */
public class MockActionResponse extends MockPortletResponse implements ActionResponse { public class MockActionResponse extends MockStateAwareResponse implements ActionResponse {
private WindowState windowState; private boolean redirectAllowed = true;
private PortletMode portletMode;
private String redirectedUrl; private String redirectedUrl;
private final Map<String, String[]> renderParameters = new LinkedHashMap<String, String[]>();
/** /**
* Create a new MockActionResponse with a default {@link MockPortalContext}. * Create a new MockActionResponse with a default {@link MockPortalContext}.
@ -71,93 +70,60 @@ public class MockActionResponse extends MockPortletResponse implements ActionRes
if (this.redirectedUrl != null) { if (this.redirectedUrl != null) {
throw new IllegalStateException("Cannot set WindowState after sendRedirect has been called"); throw new IllegalStateException("Cannot set WindowState after sendRedirect has been called");
} }
if (!CollectionUtils.contains(getPortalContext().getSupportedWindowStates(), windowState)) { super.setWindowState(windowState);
throw new WindowStateException("WindowState not supported", windowState); this.redirectAllowed = false;
}
this.windowState = windowState;
}
public WindowState getWindowState() {
return windowState;
} }
public void setPortletMode(PortletMode portletMode) throws PortletModeException { public void setPortletMode(PortletMode portletMode) throws PortletModeException {
if (this.redirectedUrl != null) { if (this.redirectedUrl != null) {
throw new IllegalStateException("Cannot set PortletMode after sendRedirect has been called"); throw new IllegalStateException("Cannot set PortletMode after sendRedirect has been called");
} }
if (!CollectionUtils.contains(getPortalContext().getSupportedPortletModes(), portletMode)) { super.setPortletMode(portletMode);
throw new PortletModeException("PortletMode not supported", portletMode); this.redirectAllowed = false;
}
this.portletMode = portletMode;
} }
public PortletMode getPortletMode() { public void setRenderParameters(Map<String, String[]> parameters) {
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) {
if (this.redirectedUrl != null) { if (this.redirectedUrl != null) {
throw new IllegalStateException("Cannot set render parameters after sendRedirect has been called"); throw new IllegalStateException("Cannot set render parameters after sendRedirect has been called");
} }
Assert.notNull(parameters, "Parameters Map must not be null"); super.setRenderParameters(parameters);
this.renderParameters.clear(); this.redirectAllowed = false;
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());
}
} }
public void setRenderParameter(String key, String value) { public void setRenderParameter(String key, String value) {
if (this.redirectedUrl != null) { if (this.redirectedUrl != null) {
throw new IllegalStateException("Cannot set render parameters after sendRedirect has been called"); throw new IllegalStateException("Cannot set render parameters after sendRedirect has been called");
} }
Assert.notNull(key, "Parameter key must not be null"); super.setRenderParameter(key, value);
Assert.notNull(value, "Parameter value must not be null"); this.redirectAllowed = false;
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);
} }
public void setRenderParameter(String key, String[] values) { public void setRenderParameter(String key, String[] values) {
if (this.redirectedUrl != null) { if (this.redirectedUrl != null) {
throw new IllegalStateException("Cannot set render parameters after sendRedirect has been called"); throw new IllegalStateException("Cannot set render parameters after sendRedirect has been called");
} }
Assert.notNull(key, "Parameter key must not be null"); super.setRenderParameter(key, values);
Assert.notNull(values, "Parameter values must not be null"); this.redirectAllowed = false;
this.renderParameters.put(key, values);
} }
public String[] getRenderParameterValues(String key) { public void sendRedirect(String location) throws IOException {
Assert.notNull(key, "Parameter key must not be null"); if (!this.redirectAllowed) {
return this.renderParameters.get(key); 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() { public void sendRedirect(String location, String renderUrlParamName) throws IOException {
return this.renderParameters.keySet().iterator(); sendRedirect(location);
if (renderUrlParamName != null) {
setRenderParameter(renderUrlParamName, location);
}
} }
public Map getRenderParameterMap() { public String getRedirectedUrl() {
return Collections.unmodifiableMap(this.renderParameters); return this.redirectedUrl;
} }
} }

View File

@ -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<String, String[]> parameters = new LinkedHashMap<String, String[]>();
private boolean secure = false;
private final Map<String, String[]> properties = new LinkedHashMap<String, String[]>();
//---------------------------------------------------------------------
// 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<String, String[]> parameters) {
Assert.notNull(parameters, "Parameters Map must not be null");
this.parameters.clear();
this.parameters.putAll(parameters);
}
public Set<String> 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<String, String[]> 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<String, String[]> 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;
}
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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());
}
}

View File

@ -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<String> 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;
}
}

View File

@ -16,16 +16,19 @@
package org.springframework.mock.web.portlet; package org.springframework.mock.web.portlet;
import java.util.Collections;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import java.util.LinkedHashMap; import java.util.Set;
import java.util.Collections;
import javax.portlet.PortletConfig; import javax.portlet.PortletConfig;
import javax.portlet.PortletContext; import javax.portlet.PortletContext;
import javax.xml.XMLConstants;
import javax.xml.namespace.QName;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@ -46,6 +49,18 @@ public class MockPortletConfig implements PortletConfig {
private final Map<String, String> initParameters = new LinkedHashMap<String, String>(); private final Map<String, String> initParameters = new LinkedHashMap<String, String>();
private final Set<String> publicRenderParameterNames = new LinkedHashSet<String>();
private String defaultNamespace = XMLConstants.NULL_NS_URI;
private final Set<QName> publishingEventQNames = new LinkedHashSet<QName>();
private final Set<QName> processingEventQNames = new LinkedHashSet<QName>();
private final Set<Locale> supportedLocales = new LinkedHashSet<Locale>();
private final Map<String, String[]> containerRuntimeOptions = new LinkedHashMap<String, String[]>();
/** /**
* Create a new MockPortletConfig with a default {@link MockPortletContext}. * Create a new MockPortletConfig with a default {@link MockPortletContext}.
@ -113,4 +128,56 @@ public class MockPortletConfig implements PortletConfig {
return Collections.enumeration(this.initParameters.keySet()); return Collections.enumeration(this.initParameters.keySet());
} }
public void addPublicRenderParameterName(String name) {
this.publicRenderParameterNames.add(name);
}
public Enumeration<String> 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<QName> getPublishingEventQNames() {
return Collections.enumeration(this.publishingEventQNames);
}
public void addProcessingEventQName(QName name) {
this.processingEventQNames.add(name);
}
public Enumeration<QName> getProcessingEventQNames() {
return Collections.enumeration(this.processingEventQNames);
}
public void addSupportedLocale(Locale locale) {
this.supportedLocales.add(locale);
}
public Enumeration<Locale> 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<String, String[]> getContainerRuntimeOptions() {
return Collections.unmodifiableMap(this.containerRuntimeOptions);
}
} }

View File

@ -25,6 +25,7 @@ import java.util.Collections;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import javax.portlet.PortletContext; import javax.portlet.PortletContext;
@ -63,6 +64,8 @@ public class MockPortletContext implements PortletContext {
private String portletContextName = "MockPortletContext"; private String portletContextName = "MockPortletContext";
private Set<String> containerRuntimeOptions = new LinkedHashSet<String>();
/** /**
* Create a new MockPortletContext with no base path and a * Create a new MockPortletContext with no base path and a
@ -248,7 +251,15 @@ public class MockPortletContext implements PortletContext {
} }
public String getPortletContextName() { public String getPortletContextName() {
return portletContextName; return this.portletContextName;
}
public void addContainerRuntimeOption(String key) {
this.containerRuntimeOptions.add(key);
}
public Enumeration<String> getContainerRuntimeOptions() {
return Collections.enumeration(this.containerRuntimeOptions);
} }
} }

View File

@ -33,6 +33,7 @@ import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequest; import javax.portlet.PortletRequest;
import javax.portlet.PortletSession; import javax.portlet.PortletSession;
import javax.portlet.WindowState; import javax.portlet.WindowState;
import javax.servlet.http.Cookie;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
@ -90,6 +91,12 @@ public class MockPortletRequest implements PortletRequest {
private int serverPort = 80; private int serverPort = 80;
private String windowID;
private Cookie[] cookies;
private final Set<String> publicParameterNames = new HashSet<String>();
/** /**
* Create a new MockPortletRequest with a default {@link MockPortalContext} * 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.portletContext = (portletContext != null ? portletContext : new MockPortletContext());
this.responseContentTypes.add("text/html"); this.responseContentTypes.add("text/html");
this.locales.add(Locale.ENGLISH); this.locales.add(Locale.ENGLISH);
this.parameters.put(LIFECYCLE_PHASE, new String[] {getLifecyclePhase()});
} }
@ -127,6 +135,13 @@ public class MockPortletRequest implements PortletRequest {
// Lifecycle methods // 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). * 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); return this.parameters.get(name);
} }
public Map getParameterMap() { public Map<String, String[]> getParameterMap() {
return Collections.unmodifiableMap(this.parameters); return Collections.unmodifiableMap(this.parameters);
} }
@ -459,4 +474,54 @@ public class MockPortletRequest implements PortletRequest {
return this.serverPort; 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<String, String[]> getPrivateParameterMap() {
if (!this.publicParameterNames.isEmpty()) {
Map<String, String[]> filtered = new LinkedHashMap<String, String[]>();
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<String, String[]> getPublicParameterMap() {
if (!this.publicParameterNames.isEmpty()) {
Map<String, String[]> filtered = new LinkedHashMap<String, String[]>();
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);
}
} }

View File

@ -22,6 +22,8 @@ import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher; import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest; import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse; import javax.portlet.RenderResponse;
import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -64,4 +66,12 @@ public class MockPortletRequestDispatcher implements PortletRequestDispatcher {
} }
} }
public void include(PortletRequest request, PortletResponse response) throws PortletException, IOException {
// TODO
}
public void forward(PortletRequest request, PortletResponse response) throws PortletException, IOException {
// TODO
}
} }

View File

@ -16,12 +16,20 @@
package org.springframework.mock.web.portlet; package org.springframework.mock.web.portlet;
import java.util.Collections;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import javax.portlet.PortalContext; import javax.portlet.PortalContext;
import javax.portlet.PortletResponse; 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; import org.springframework.util.Assert;
@ -38,6 +46,14 @@ public class MockPortletResponse implements PortletResponse {
private final Map<String, String[]> properties = new LinkedHashMap<String, String[]>(); private final Map<String, String[]> properties = new LinkedHashMap<String, String[]>();
private String namespace = "";
private final Set<Cookie> cookies = new LinkedHashSet<Cookie>();
private final Map<String, Element[]> xmlProperties = new LinkedHashMap<String, Element[]>();
private Document xmlDocument;
/** /**
* Create a new MockPortletResponse with a default {@link MockPortalContext}. * 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}); this.properties.put(key, new String[] {value});
} }
public Set getPropertyNames() { public Set<String> getPropertyNames() {
return this.properties.keySet(); return Collections.unmodifiableSet(this.properties.keySet());
} }
public String getProperty(String key) { public String getProperty(String key) {
@ -107,4 +123,73 @@ public class MockPortletResponse implements PortletResponse {
return path; 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<String> 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);
}
} }

View File

@ -184,7 +184,23 @@ public class MockPortletSession implements PortletSession {
} }
public PortletContext getPortletContext() { public PortletContext getPortletContext() {
return portletContext; return this.portletContext;
}
public Map<String, Object> getAttributeMap() {
return Collections.unmodifiableMap(this.portletAttributes);
}
public Map<String, Object> 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();
}
} }
} }

View File

@ -16,18 +16,10 @@
package org.springframework.mock.web.portlet; 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.Map;
import java.util.Set;
import javax.portlet.PortalContext; import javax.portlet.PortalContext;
import javax.portlet.PortletMode; import javax.portlet.PortletMode;
import javax.portlet.PortletModeException; import javax.portlet.PortletModeException;
import javax.portlet.PortletSecurityException;
import javax.portlet.PortletURL; import javax.portlet.PortletURL;
import javax.portlet.WindowState; import javax.portlet.WindowState;
import javax.portlet.WindowStateException; import javax.portlet.WindowStateException;
@ -42,14 +34,12 @@ import org.springframework.util.CollectionUtils;
* @author Juergen Hoeller * @author Juergen Hoeller
* @since 2.0 * @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_RENDER = "render";
public static final String URL_TYPE_ACTION = "action"; public static final String URL_TYPE_ACTION = "action";
private static final String ENCODING = "UTF-8";
private final PortalContext portalContext; private final PortalContext portalContext;
@ -59,10 +49,6 @@ public class MockPortletURL implements PortletURL {
private PortletMode portletMode; private PortletMode portletMode;
private final Map<String, String[]> parameters = new LinkedHashMap<String, String[]>();
private boolean secure = false;
/** /**
* Create a new MockPortletURL for the given URL type. * Create a new MockPortletURL for the given URL type.
@ -90,6 +76,10 @@ public class MockPortletURL implements PortletURL {
this.windowState = windowState; this.windowState = windowState;
} }
public WindowState getWindowState() {
return this.windowState;
}
public void setPortletMode(PortletMode portletMode) throws PortletModeException { public void setPortletMode(PortletMode portletMode) throws PortletModeException {
if (!CollectionUtils.contains(this.portalContext.getSupportedPortletModes(), portletMode)) { if (!CollectionUtils.contains(this.portalContext.getSupportedPortletModes(), portletMode)) {
throw new PortletModeException("PortletMode not supported", portletMode); throw new PortletModeException("PortletMode not supported", portletMode);
@ -97,77 +87,12 @@ public class MockPortletURL implements PortletURL {
this.portletMode = portletMode; this.portletMode = portletMode;
} }
public void setParameter(String key, String value) { public PortletMode getPortletMode() {
Assert.notNull(key, "Parameter key must be null"); return this.portletMode;
Assert.notNull(value, "Parameter value must not be null");
this.parameters.put(key, new String[] {value});
} }
public void setParameter(String key, String[] values) { public void removePublicRenderParameter(String name) {
Assert.notNull(key, "Parameter key must be null"); this.parameters.remove(name);
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<String> 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<String, String[]> 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;
}
} }
@ -183,7 +108,7 @@ public class MockPortletURL implements PortletURL {
for (Map.Entry<String, String[]> entry : this.parameters.entrySet()) { for (Map.Entry<String, String[]> entry : this.parameters.entrySet()) {
sb.append(";").append(encodeParameter("param_" + entry.getKey(), entry.getValue())); sb.append(";").append(encodeParameter("param_" + entry.getKey(), entry.getValue()));
} }
return (this.secure ? "https:" : "http:") + return (isSecure() ? "https:" : "http:") +
"//localhost/mockportlet?" + sb.toString(); "//localhost/mockportlet?" + sb.toString();
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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); super(portalContext, portletContext);
} }
@Override
protected String getLifecyclePhase() {
return RENDER_PHASE;
}
public String getETag() {
return getProperty(RenderRequest.ETAG);
}
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.PrintWriter;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.io.Writer; import java.io.Writer;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Locale; import java.util.Locale;
import javax.portlet.CacheControl;
import javax.portlet.PortalContext; import javax.portlet.PortalContext;
import javax.portlet.PortletMode;
import javax.portlet.PortletURL; import javax.portlet.PortletURL;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse; import javax.portlet.RenderResponse;
import javax.portlet.ResourceURL;
import org.springframework.util.CollectionUtils;
import org.springframework.web.util.WebUtils; import org.springframework.web.util.WebUtils;
/** /**
@ -38,25 +45,11 @@ import org.springframework.web.util.WebUtils;
* @author Juergen Hoeller * @author Juergen Hoeller
* @since 2.0 * @since 2.0
*/ */
public class MockRenderResponse extends MockPortletResponse implements RenderResponse { public class MockRenderResponse extends MockMimeResponse implements RenderResponse {
private String contentType;
private String namespace = "MockPortlet";
private String title; private String title;
private String characterEncoding = WebUtils.DEFAULT_CHARACTER_ENCODING; private Collection<PortletMode> nextPossiblePortletModes;
private PrintWriter writer;
private Locale locale = Locale.getDefault();
private int bufferSize = 4096;
private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
private boolean committed;
private String includedUrl; private String includedUrl;
@ -78,126 +71,36 @@ public class MockRenderResponse extends MockPortletResponse implements RenderRes
super(portalContext); 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 // 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) { public void setTitle(String title) {
this.title = title; this.title = title;
} }
public String getTitle() { public String getTitle() {
return title; return this.title;
} }
public void setContentType(String contentType) { public void setNextPossiblePortletModes(Collection<PortletMode> portletModes) {
this.contentType = contentType; this.nextPossiblePortletModes = portletModes;
} }
public void setCharacterEncoding(String characterEncoding) { public Collection<PortletMode> getNextPossiblePortletModes() {
this.characterEncoding = characterEncoding; return this.nextPossiblePortletModes;
}
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;
} }
@ -210,7 +113,7 @@ public class MockRenderResponse extends MockPortletResponse implements RenderRes
} }
public String getIncludedUrl() { public String getIncludedUrl() {
return includedUrl; return this.includedUrl;
} }
} }

View File

@ -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<String, String[]> privateRenderParameterMap = new LinkedHashMap<String, String[]>();
/**
* 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<String, String[]> getPrivateRenderParameterMap() {
return Collections.unmodifiableMap(this.privateRenderParameterMap);
}
}

View File

@ -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;
}
}

View File

@ -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<String, String[]> entry : this.parameters.entrySet()) {
sb.append(";").append(encodeParameter("param_" + entry.getKey(), entry.getValue()));
}
return (isSecure() ? "https:" : "http:") +
"//localhost/mockportlet?" + sb.toString();
}
}

View File

@ -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<String, String[]> renderParameters = new LinkedHashMap<String, String[]>();
private final Map<QName, Serializable> events = new HashMap<QName, Serializable>();
/**
* 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<String, String[]> 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<String> getRenderParameterNames() {
return this.renderParameters.keySet().iterator();
}
public Map<String, String[]> 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<QName> 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));
}
}

View File

@ -1,7 +1,7 @@
<html> <html>
<body> <body>
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. targeted at usage with Spring's web MVC framework.
Useful for testing web contexts and controllers. Useful for testing web contexts and controllers.

View File

@ -5,7 +5,7 @@ Bundle-ManifestVersion: 2
Import-Template: Import-Template:
javax.activation.*;version="[1.1.0, 2.0.0)";resolution:=optional, javax.activation.*;version="[1.1.0, 2.0.0)";resolution:=optional,
javax.persistence.*;version="[1.0.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;version="[2.4.0, 3.0.0)";resolution:=optional,
javax.servlet.http;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, javax.servlet.jsp.*;version="[2.0.0, 3.0.0)";resolution:=optional,

View File

@ -45,10 +45,12 @@
<orderEntry type="module-library"> <orderEntry type="module-library">
<library> <library>
<CLASSES> <CLASSES>
<root url="jar://$IVY_CACHE$/javax.portlet/com.springsource.javax.portlet/1.0.0/com.springsource.javax.portlet-1.0.0.jar!/" /> <root url="jar://$IVY_CACHE$/javax.portlet/com.springsource.javax.portlet/2.0.0/com.springsource.javax.portlet-2.0.0.jar!/" />
</CLASSES> </CLASSES>
<JAVADOC /> <JAVADOC />
<SOURCES /> <SOURCES>
<root url="jar://$IVY_CACHE$/javax.portlet/com.springsource.javax.portlet/2.0.0/com.springsource.javax.portlet-sources-2.0.0.jar!/" />
</SOURCES>
</library> </library>
</orderEntry> </orderEntry>
<orderEntry type="module-library"> <orderEntry type="module-library">