Author: niallp
Date: Thu Jun 8 09:12:37 2006
New Revision: 412787
URL: http://svn.apache.org/viewvc?rev=412787&view=rev
Log:
Add portlet mock objects and tests
Added:
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/MockPortletContext.java
(with props)
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/MockPortletRequest.java
(with props)
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/MockPortletSession.java
(with props)
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/PortletGetLocaleCommandTestCase.java
(with props)
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/PortletWebContextTestCase.java
(with props)
Added:
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/MockPortletContext.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/MockPortletContext.java?rev=412787&view=auto
==============================================================================
---
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/MockPortletContext.java
(added)
+++
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/MockPortletContext.java
Thu Jun 8 09:12:37 2006
@@ -0,0 +1,137 @@
+/*
+ * Copyright 2006 The Apache Software Foundation
+ *
+ * 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.apache.commons.chain.web.portlet;
+
+
+import javax.portlet.Portlet;
+import javax.portlet.PortletContext;
+import javax.portlet.PortletRequestDispatcher;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.Hashtable;
+import java.util.Set;
+
+
+// Mock Object for PortletContext
+public class MockPortletContext implements PortletContext {
+
+
+ private int majorVersion = 1;
+ private int minorVersion = 0;
+ private String portletContextName = "MockPortletContext";
+ private String serverInfo = portletContextName;
+ private Hashtable parameters = new Hashtable();
+ private Hashtable attributes = new Hashtable();
+
+
+ // --------------------------------------------------------- Public Methods
+
+
+ public void setPortletContextName(String portletContextName) {
+ this.portletContextName = portletContextName;
+ }
+
+ public void setServerInfo(String serverInfo) {
+ this.serverInfo = serverInfo;
+ }
+
+ public void addInitParameter(String name, String value) {
+ parameters.put(name, value);
+ }
+
+
+ // ------------------------------------------------- PortletContext Methods
+
+
+ public Object getAttribute(String name) {
+ return attributes.get(name);
+ }
+
+ public Enumeration getAttributeNames() {
+ return attributes.keys();
+ }
+
+ public String getInitParameter(String name) {
+ return (String)parameters.get(name);
+ }
+
+ public Enumeration getInitParameterNames() {
+ return parameters.keys();
+ }
+
+ public int getMajorVersion() {
+ return majorVersion;
+ }
+
+ public String getMimeType(String path) {
+ throw new UnsupportedOperationException();
+ }
+
+ public int getMinorVersion() {
+ return minorVersion;
+ }
+
+ public PortletRequestDispatcher getNamedDispatcher(String name) {
+ throw new UnsupportedOperationException();
+ }
+
+ public String getPortletContextName() {
+ return portletContextName;
+ }
+
+ public String getRealPath(String path) {
+ throw new UnsupportedOperationException();
+ }
+
+ public PortletRequestDispatcher getRequestDispatcher(String path) {
+ throw new UnsupportedOperationException();
+ }
+
+ public URL getResource(String path) throws MalformedURLException {
+ throw new UnsupportedOperationException();
+ }
+
+ public InputStream getResourceAsStream(String path) {
+ throw new UnsupportedOperationException();
+ }
+
+ public Set getResourcePaths(String path) {
+ throw new UnsupportedOperationException();
+ }
+
+ public String getServerInfo() {
+ return serverInfo;
+ }
+
+ public void log(String message) {
+ throw new UnsupportedOperationException();
+ }
+
+ public void log(String message, Throwable exception) {
+ throw new UnsupportedOperationException();
+ }
+
+ public void removeAttribute(String name) {
+ attributes.remove(name);
+ }
+
+ public void setAttribute(String name, Object value) {
+ attributes.put(name, value);
+ }
+
+}
Propchange:
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/MockPortletContext.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/MockPortletContext.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added:
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/MockPortletRequest.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/MockPortletRequest.java?rev=412787&view=auto
==============================================================================
---
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/MockPortletRequest.java
(added)
+++
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/MockPortletRequest.java
Thu Jun 8 09:12:37 2006
@@ -0,0 +1,314 @@
+/*
+ * Copyright 2006 The Apache Software Foundation
+ *
+ * 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.apache.commons.chain.web.portlet;
+
+
+import org.apache.commons.chain.web.MockEnumeration;
+import org.apache.commons.chain.web.MockPrincipal;
+
+import javax.portlet.PortalContext;
+import javax.portlet.PortletRequest;
+import javax.portlet.PortletSession;
+import javax.portlet.PortletContext;
+import javax.portlet.PortletMode;
+import javax.portlet.PortletPreferences;
+import javax.portlet.WindowState;
+import java.security.Principal;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Enumeration;
+import java.util.Locale;
+
+
+
+// Mock Object for PortletRequest
+public class MockPortletRequest implements PortletRequest {
+
+ private String contextPath;
+ private String authType;
+ private Locale locale;
+ private String scheme = "http";
+ private String serverName = "localhost";
+ private int serverPort = 8080;
+ private PortalContext portalContext;
+ private PortletContext context;
+ private PortletSession session;
+ private PortletMode portletMode;
+ private PortletPreferences portletPreferences;
+ private WindowState windowState;
+ private Principal principal;
+ private Map parameters = new HashMap();
+ private Map attributes = new HashMap();
+ private Map properties = new HashMap();
+
+
+ public MockPortletRequest() {
+ this(null, null, null);
+ }
+
+ public MockPortletRequest(String contextPath, PortletContext context,
PortletSession session) {
+ this.contextPath = contextPath;
+ this.context = (context == null ? new MockPortletContext() : context);
+ this.session = session;
+ }
+
+ // --------------------------------------------------------- Public Methods
+
+ public void addParameter(String name, String value) {
+ String values[] = (String[])parameters.get(name);
+ if (values == null) {
+ String results[] = new String[] { value };
+ parameters.put(name, results);
+ return;
+ }
+ String results[] = new String[values.length + 1];
+ System.arraycopy(values, 0, results, 0, values.length);
+ results[values.length] = value;
+ parameters.put(name, results);
+ }
+
+ public void addProperty(String name, String value) {
+ String values[] = (String[])properties.get(name);
+ if (values == null) {
+ String results[] = new String[] { value };
+ properties.put(name, results);
+ return;
+ }
+ String results[] = new String[values.length + 1];
+ System.arraycopy(values, 0, results, 0, values.length);
+ results[values.length] = value;
+ properties.put(name, results);
+ }
+
+ public void setAuthType(String authType) {
+ this.authType = authType;
+ }
+
+ public void setContextPath(String contextPath) {
+ this.contextPath = contextPath;
+ }
+
+ public void setLocale(Locale locale) {
+ this.locale = locale;
+ }
+
+ public void setPortalContext(PortalContext portalContext) {
+ this.portalContext = portalContext;
+ }
+
+ public void setPortletContext(PortletContext context) {
+ this.context = context;
+ }
+
+ public void setPortletMode(PortletMode portletMode) {
+ this.portletMode = portletMode;
+ }
+
+ public void setPortletPreferences(PortletPreferences portletPreferences) {
+ this.portletPreferences = portletPreferences;
+ }
+
+ public void setPortletSession(PortletSession session) {
+ this.session = session;
+ }
+
+ public void setScheme(String scheme) {
+ this.scheme = scheme;
+ }
+
+ public void setServerName(String serverName) {
+ this.serverName = serverName;
+ }
+
+ public void setServerPort(int serverPort) {
+ this.serverPort = serverPort;
+ }
+
+ public void setUserPrincipal(Principal principal) {
+ this.principal = principal;
+ }
+
+ public void setUserPrincipal(WindowState windowState) {
+ this.windowState = windowState;
+ }
+
+
+ // --------------------------------------------- PortletRequest Methods
+
+ public Object getAttribute(String name) {
+ return attributes.get(name);
+ }
+
+ public Enumeration getAttributeNames() {
+ return new MockEnumeration(attributes.keySet().iterator());
+ }
+
+ public String getAuthType() {
+ return authType;
+ }
+
+ public String getContextPath() {
+ return contextPath;
+ }
+
+ public Locale getLocale() {
+ return locale;
+ }
+
+ public Enumeration getLocales() {
+ throw new UnsupportedOperationException();
+ }
+
+ public String getParameter(String name) {
+ String values[] = (String[])parameters.get(name);
+ if (values != null) {
+ return values[0];
+ } else {
+ return null;
+ }
+ }
+
+ public Map getParameterMap() {
+ return parameters;
+ }
+
+ public Enumeration getParameterNames() {
+ return new MockEnumeration(parameters.keySet().iterator());
+ }
+
+ public String[] getParameterValues(String name) {
+ return (String[])parameters.get(name);
+ }
+
+ public PortalContext getPortalContext() {
+ return portalContext;
+ }
+
+ public PortletMode getPortletMode() {
+ return portletMode;
+ }
+
+ public PortletSession getPortletSession() {
+ return getPortletSession(true);
+ }
+
+ public PortletSession getPortletSession(boolean create) {
+ if (create && session == null) {
+ session = new MockPortletSession(context);
+ }
+ return session;
+ }
+
+ public PortletPreferences getPreferences() {
+ return portletPreferences;
+ }
+
+ public Enumeration getProperties(String name) {
+ throw new UnsupportedOperationException();
+ }
+
+ public String getProperty(String name) {
+ String values[] = (String[])properties.get(name);
+ if (values != null) {
+ return values[0];
+ } else {
+ return null;
+ }
+ }
+
+ public Enumeration getPropertyNames() {
+ return new MockEnumeration(properties.keySet().iterator());
+ }
+
+
+ public String getRemoteUser() {
+ if (principal != null) {
+ return principal.getName();
+ } else {
+ return null;
+ }
+ }
+
+ public String getRequestedSessionId() {
+ throw new UnsupportedOperationException();
+ }
+
+ public String getResponseContentType() {
+ throw new UnsupportedOperationException();
+ }
+
+ public Enumeration getResponseContentTypes() {
+ throw new UnsupportedOperationException();
+ }
+
+ public String getScheme() {
+ return scheme;
+ }
+
+ public String getServerName() {
+ return serverName;
+ }
+
+ public int getServerPort() {
+ return serverPort;
+ }
+
+ public Principal getUserPrincipal() {
+ return principal;
+ }
+
+ public WindowState getWindowState() {
+ return windowState;
+ }
+
+ public boolean isPortletModeAllowed(PortletMode mode) {
+ throw new UnsupportedOperationException();
+ }
+
+ public boolean isRequestedSessionIdValid() {
+ throw new UnsupportedOperationException();
+ }
+
+ public boolean isSecure() {
+ return false;
+ }
+
+ public boolean isUserInRole(String role) {
+ if ((principal != null) && (principal instanceof MockPrincipal)) {
+ return ((MockPrincipal)principal).isUserInRole(role);
+ } else {
+ return false;
+ }
+ }
+
+ public boolean isWindowStateAllowed(WindowState state) {
+ throw new UnsupportedOperationException();
+ }
+
+ public void removeAttribute(String name) {
+ attributes.remove(name);
+ }
+
+
+ public void setAttribute(String name, Object value) {
+ if (value == null) {
+ attributes.remove(name);
+ } else {
+ attributes.put(name, value);
+ }
+ }
+
+}
Propchange:
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/MockPortletRequest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/MockPortletRequest.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added:
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/MockPortletSession.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/MockPortletSession.java?rev=412787&view=auto
==============================================================================
---
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/MockPortletSession.java
(added)
+++
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/MockPortletSession.java
Thu Jun 8 09:12:37 2006
@@ -0,0 +1,174 @@
+/*
+ * Copyright 2006 The Apache Software Foundation
+ *
+ * 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.apache.commons.chain.web.portlet;
+
+
+import org.apache.commons.chain.web.MockEnumeration;
+
+import javax.portlet.PortletContext;
+import javax.portlet.PortletSession;
+import javax.portlet.PortletContext;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Date;
+
+
+
+// Mock Object for PortletSession
+public class MockPortletSession implements PortletSession {
+
+
+ private Date creationTime = new Date();
+ private Date lastAccessedTime = creationTime;
+
+ private PortletContext context = null;
+ private int maxInactiveInterval = 100;
+ private boolean newSession = true;
+ private String id = "mockId" + creationTime.getTime();
+ private Map portletScope = new HashMap();
+ private Map applicationScope = new HashMap();
+
+
+ public MockPortletSession() {
+ this(null);
+ }
+
+
+ public MockPortletSession(PortletContext context) {
+ this.context = (context == null ? new MockPortletContext() : context);
+ }
+
+
+ // --------------------------------------------------------- Public Methods
+
+
+ public void setPortletContext(PortletContext context) {
+ this.context = context;
+ }
+
+ public void setNew(boolean newSession) {
+ this.newSession = newSession;
+ }
+
+ public void setNew(String id) {
+ this.id = id;
+ }
+
+
+
+ // ---------------------------------------------------- PortletSession
Methods
+
+
+ public Object getAttribute(String name) {
+ accessed();
+ return getAttribute(name, PortletSession.PORTLET_SCOPE);
+ }
+
+ public Object getAttribute(String name, int scope) {
+ accessed();
+ return getScope(scope).get(name);
+ }
+
+
+ public Enumeration getAttributeNames() {
+ accessed();
+ return getAttributeNames(PortletSession.PORTLET_SCOPE);
+ }
+
+ public Enumeration getAttributeNames(int scope) {
+ accessed();
+ return new MockEnumeration(getScope(scope).keySet().iterator());
+ }
+
+
+ public long getCreationTime() {
+ accessed();
+ return creationTime.getTime();
+ }
+
+
+ public String getId() {
+ accessed();
+ return id;
+ }
+
+
+ public long getLastAccessedTime() {
+ return lastAccessedTime.getTime();
+ }
+
+
+ public int getMaxInactiveInterval() {
+ accessed();
+ return maxInactiveInterval;
+ }
+
+
+ public PortletContext getPortletContext() {
+ accessed();
+ return context;
+ }
+
+ public void invalidate() {
+ throw new UnsupportedOperationException();
+ }
+
+ public boolean isNew() {
+ accessed();
+ return newSession;
+ }
+
+ public void removeAttribute(String name) {
+ accessed();
+ removeAttribute(name, PortletSession.PORTLET_SCOPE);
+ }
+
+ public void removeAttribute(String name, int scope) {
+ accessed();
+ getScope(scope).remove(name);
+ }
+
+ public void setAttribute(String name, Object value) {
+ accessed();
+ setAttribute(name, value, PortletSession.PORTLET_SCOPE);
+ }
+
+ public void setAttribute(String name, Object value, int scope) {
+ accessed();
+ getScope(scope).put(name, value);
+ }
+
+ public void setMaxInactiveInterval(int interval) {
+ accessed();
+ this.maxInactiveInterval = interval;
+ }
+
+ private void accessed() {
+ lastAccessedTime = new Date();
+ }
+
+ private Map getScope(int scope) {
+ if (scope == PortletSession.PORTLET_SCOPE) {
+ return portletScope;
+ } else if (scope == PortletSession.APPLICATION_SCOPE) {
+ return applicationScope;
+ } else {
+ throw new IllegalArgumentException("Invalid scope: " + scope);
+ }
+ }
+
+}
Propchange:
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/MockPortletSession.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/MockPortletSession.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added:
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/PortletGetLocaleCommandTestCase.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/PortletGetLocaleCommandTestCase.java?rev=412787&view=auto
==============================================================================
---
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/PortletGetLocaleCommandTestCase.java
(added)
+++
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/PortletGetLocaleCommandTestCase.java
Thu Jun 8 09:12:37 2006
@@ -0,0 +1,155 @@
+/*
+ * Copyright 2006 The Apache Software Foundation
+ *
+ * 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.apache.commons.chain.web.portlet;
+
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+import org.apache.commons.chain.Context;
+
+import javax.portlet.PortletContext;
+import javax.portlet.PortletRequest;
+import javax.portlet.PortletResponse;
+import javax.portlet.PortletSession;
+import java.util.Locale;
+
+
+// Test case for org.apache.commons.chain.web.portlet.PortletGetLocaleCommand
+
+public class PortletGetLocaleCommandTestCase extends TestCase {
+
+
+ // ---------------------------------------------------------- Constructors
+
+ /**
+ * Construct a new instance of this test case.
+ *
+ * @param name Name of the test case
+ */
+ public PortletGetLocaleCommandTestCase(String name) {
+ super(name);
+ }
+
+
+ // ----------------------------------------------------- Instance Variables
+
+
+ protected Locale locale = null;
+
+ // Portlet API Objects
+ protected PortletContext pcontext = null;
+ protected PortletRequest request = null;
+ protected PortletResponse response = null;
+ protected PortletSession session = null;
+
+ // Chain API Objects
+ protected Context context = null;
+ protected PortletGetLocaleCommand command = null;
+
+
+ // -------------------------------------------------- Overall Test Methods
+
+
+ /**
+ * Set up instance variables required by this test case.
+ */
+ public void setUp() {
+
+ locale = new Locale("en", "US");
+
+ // Set up Portlet API Objects
+ pcontext = new MockPortletContext();
+ session = new MockPortletSession(pcontext);
+ request = new MockPortletRequest(null, pcontext, session);
+ ((MockPortletRequest) request).setLocale(locale);
+
+ // Set up Chain API Objects
+ context = new PortletWebContext(pcontext, request, response);
+ command = new PortletGetLocaleCommand();
+
+ }
+
+
+ /**
+ * Return the tests included in this test suite.
+ */
+ public static Test suite() {
+
+ return (new TestSuite(PortletGetLocaleCommandTestCase.class));
+
+ }
+
+
+ /**
+ * Tear down instance variables required by this test case.
+ */
+ public void tearDown() {
+
+ pcontext = null;
+ session = null;
+ request = null;
+ response = null;
+
+ context = null;
+ command = null;
+
+ }
+
+
+ // ------------------------------------------------- Individual Test
Methods
+
+
+ // Test configured behavior
+ public void testConfigured() throws Exception {
+
+ command.setLocaleKey("special");
+ assertEquals("special", command.getLocaleKey());
+ check(context, command);
+
+ }
+
+
+ // Test default behavior
+ public void testDefaut() throws Exception {
+
+ assertEquals("locale", command.getLocaleKey());
+ check(context, command);
+
+ }
+
+
+ // --------------------------------------------------------- Support
Methods
+
+
+ protected void check(Context context, PortletGetLocaleCommand command)
+ throws Exception {
+
+ String localeKey = command.getLocaleKey();
+ assertNotNull(localeKey);
+ Object value = context.get(localeKey);
+ assertNull(value);
+ boolean result = command.execute(context);
+ assertFalse(result);
+ value = context.get(localeKey);
+ assertNotNull(value);
+ assertTrue(value instanceof Locale);
+ assertEquals(locale, (Locale) value);
+
+ }
+
+
+}
Propchange:
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/PortletGetLocaleCommandTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/PortletGetLocaleCommandTestCase.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added:
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/PortletWebContextTestCase.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/PortletWebContextTestCase.java?rev=412787&view=auto
==============================================================================
---
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/PortletWebContextTestCase.java
(added)
+++
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/PortletWebContextTestCase.java
Thu Jun 8 09:12:37 2006
@@ -0,0 +1,631 @@
+/*
+ * Copyright 2006 The Apache Software Foundation
+ *
+ * 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.apache.commons.chain.web.portlet;
+
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import org.apache.commons.chain.Context;
+import org.apache.commons.chain.impl.ContextBaseTestCase;
+import org.apache.commons.chain.web.WebContext;
+
+import javax.portlet.PortletContext;
+import javax.portlet.PortletRequest;
+import javax.portlet.PortletResponse;
+import javax.portlet.PortletSession;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+import java.util.Collection;
+
+
+/**
+ * Extension of <code>ContextBaseTestCase</code> to validate the
+ * extra functionality of this implementation.
+ */
+
+public class PortletWebContextTestCase extends ContextBaseTestCase {
+
+
+ // ---------------------------------------------------------- Constructors
+
+ /**
+ * Construct a new instance of this test case.
+ *
+ * @param name Name of the test case
+ */
+ public PortletWebContextTestCase(String name) {
+ super(name);
+ }
+
+
+ // ----------------------------------------------------- Instance Variables
+
+
+ // Portlet API Objects
+ protected PortletContext pcontext = null;
+ protected PortletRequest request = null;
+ protected PortletResponse response = null;
+ protected PortletSession session = null;
+
+
+ // -------------------------------------------------- Overall Test Methods
+
+
+ /**
+ * Set up instance variables required by this test case.
+ */
+ public void setUp() {
+ pcontext = new MockPortletContext();
+ pcontext.setAttribute("akey1", "avalue1");
+ pcontext.setAttribute("akey2", "avalue2");
+ pcontext.setAttribute("akey3", "avalue3");
+ pcontext.setAttribute("akey4", "avalue4");
+ ((MockPortletContext) pcontext).addInitParameter("ikey1", "ivalue1");
+ ((MockPortletContext) pcontext).addInitParameter("ikey2", "ivalue2");
+ ((MockPortletContext) pcontext).addInitParameter("ikey3", "ivalue3");
+ session = new MockPortletSession(pcontext);
+ session.setAttribute("skey1", "svalue1");
+ session.setAttribute("skey2", "svalue2");
+ session.setAttribute("skey3", "svalue3");
+ request = new MockPortletRequest(null, pcontext, session);
+ request.setAttribute("rkey1", "rvalue1");
+ request.setAttribute("rkey2", "rvalue2");
+ ((MockPortletRequest) request).addParameter("pkey1", "pvalue1");
+ ((MockPortletRequest) request).addParameter("pkey2", "pvalue2a");
+ ((MockPortletRequest) request).addParameter("pkey2", "pvalue2b");
+ context = createContext();
+ }
+
+
+ /**
+ * Return the tests included in this test suite.
+ */
+ public static Test suite() {
+ return (new TestSuite(PortletWebContextTestCase.class));
+ }
+
+
+ /**
+ * Tear down instance variables required by this test case.
+ */
+ public void tearDown() {
+ pcontext = null;
+ session = null;
+ request = null;
+ response = null;
+ context = null;
+ }
+
+
+ // ------------------------------------------------ Individual Test Methods
+
+
+ // Test getApplicationScope()
+ public void testApplicationScope() {
+
+ Map map = ((WebContext) context).getApplicationScope();
+ assertNotNull(map);
+
+ // Initial contents
+ checkMapSize(map, 4);
+ assertEquals("avalue1", (String) map.get("akey1"));
+ assertEquals("avalue2", (String) map.get("akey2"));
+ assertEquals("avalue3", (String) map.get("akey3"));
+ assertEquals("avalue4", (String) map.get("akey4"));
+
+ // Transparency - entrySet()
+ checkEntrySet(map, true);
+
+ // Transparency - removal via web object
+ pcontext.removeAttribute("akey1");
+ checkMapSize(map, 3);
+ assertNull(map.get("akey1"));
+
+ // Transparency - removal via map
+ map.remove("akey2");
+ checkMapSize(map, 2);
+ assertNull(pcontext.getAttribute("akey2"));
+
+ // Transparency - addition via web object
+ pcontext.setAttribute("akeyA", "avalueA");
+ checkMapSize(map, 3);
+ assertEquals("avalueA", (String) map.get("akeyA"));
+
+ // Transparency - addition via map
+ map.put("akeyB", "avalueB");
+ checkMapSize(map, 4);
+ assertEquals("avalueB", (String) pcontext.getAttribute("akeyB"));
+
+ // Transparency - replacement via web object
+ pcontext.setAttribute("akeyA", "newvalueA");
+ checkMapSize(map, 4);
+ assertEquals("newvalueA", (String) map.get("akeyA"));
+
+ // Transparency - replacement via map
+ map.put("akeyB", "newvalueB");
+ assertEquals(4, map.size());
+ assertEquals("newvalueB", (String) pcontext.getAttribute("akeyB"));
+
+ // Clearing the map
+ map.clear();
+ checkMapSize(map, 0);
+
+ }
+
+
+ // Test equals() and hashCode()
+ // Copied from ContextBaseTestCase with customized creation of "other"
+ public void testEquals() {
+
+ // Compare to self
+ assertTrue(context.equals(context));
+ assertTrue(context.hashCode() == context.hashCode());
+
+ // Compare to equivalent instance
+ Context other = new PortletWebContext(pcontext, request, response);
+ // assertTrue(context.equals(other));
+ assertTrue(context.hashCode() == other.hashCode());
+
+ // Compare to non-equivalent instance - other modified
+ other.put("bop", "bop value");
+ // assertTrue(!context.equals(other));
+ assertTrue(context.hashCode() != other.hashCode());
+
+ // Compare to non-equivalent instance - self modified
+ other = new PortletWebContext(pcontext, request, response);
+ context.put("bop", "bop value");
+ // assertTrue(!context.equals(other));
+ assertTrue(context.hashCode() != other.hashCode());
+
+ }
+
+
+ // Test getHeader()
+ public void testHeader() {
+
+ Map map = ((WebContext) context).getHeader();
+ assertNotNull("Header Map Null", map);
+
+ // Initial contents
+ checkMapSize(map, 0);
+
+ try {
+ map.put("hkey3", "hvalue3");
+ fail("Should have thrown UnsupportedOperationException");
+ } catch (UnsupportedOperationException e) {
+ ; // expected result
+ }
+
+ }
+
+
+ // Test getHeaderValues()
+ public void testHeaderValues() {
+
+ Map map = ((WebContext) context).getHeaderValues();
+ assertNotNull("HeaderValues Map Null", map);
+
+ // Initial contents
+ checkMapSize(map, 0);
+
+ try {
+ map.put("hkey3", "ABC");
+ fail("Should have thrown UnsupportedOperationException");
+ } catch (UnsupportedOperationException e) {
+ ; // expected result
+ }
+
+ }
+
+
+ // Test getInitParam()
+ public void testInitParam() {
+
+ Map map = ((WebContext) context).getInitParam();
+ assertNotNull(map);
+
+ // Initial contents
+ checkMapSize(map, 3);
+ assertEquals("ivalue1", (String) map.get("ikey1"));
+ assertEquals("ivalue2", (String) map.get("ikey2"));
+ assertEquals("ivalue3", (String) map.get("ikey3"));
+ assertTrue(map.containsKey("ikey1"));
+ assertTrue(map.containsKey("ikey2"));
+ assertTrue(map.containsKey("ikey3"));
+ assertTrue(map.containsValue("ivalue1"));
+ assertTrue(map.containsValue("ivalue2"));
+ assertTrue(map.containsValue("ivalue3"));
+
+ // Transparency - entrySet()
+ checkEntrySet(map, false);
+
+ // Unsupported operations on read-only map
+ try {
+ map.clear();
+ fail("Should have thrown UnsupportedOperationException");
+ } catch (UnsupportedOperationException e) {
+ ; // expected result
+ }
+ try {
+ map.put("ikey4", "ivalue4");
+ fail("Should have thrown UnsupportedOperationException");
+ } catch (UnsupportedOperationException e) {
+ ; // expected result
+ }
+ try {
+ map.putAll(new HashMap());
+ fail("Should have thrown UnsupportedOperationException");
+ } catch (UnsupportedOperationException e) {
+ ; // expected result
+ }
+ try {
+ map.remove("ikey1");
+ fail("Should have thrown UnsupportedOperationException");
+ } catch (UnsupportedOperationException e) {
+ ; // expected result
+ }
+
+ }
+
+
+ // Test getParam()
+ public void testParam() {
+
+ Map map = ((WebContext) context).getParam();
+ assertNotNull(map);
+
+ // Initial contents
+ checkMapSize(map, 2);
+ assertEquals("pvalue1", (String) map.get("pkey1"));
+ assertEquals("pvalue2a", (String) map.get("pkey2"));
+ assertTrue(map.containsKey("pkey1"));
+ assertTrue(map.containsKey("pkey2"));
+ assertTrue(map.containsValue("pvalue1"));
+ assertTrue(map.containsValue("pvalue2a"));
+
+ checkEntrySet(map, false);
+
+ // Unsupported operations on read-only map
+ try {
+ map.clear();
+ fail("Should have thrown UnsupportedOperationException");
+ } catch (UnsupportedOperationException e) {
+ ; // expected result
+ }
+ try {
+ map.put("pkey3", "pvalue3");
+ fail("Should have thrown UnsupportedOperationException");
+ } catch (UnsupportedOperationException e) {
+ ; // expected result
+ }
+ try {
+ map.putAll(new HashMap());
+ fail("Should have thrown UnsupportedOperationException");
+ } catch (UnsupportedOperationException e) {
+ ; // expected result
+ }
+ try {
+ map.remove("pkey1");
+ fail("Should have thrown UnsupportedOperationException");
+ } catch (UnsupportedOperationException e) {
+ ; // expected result
+ }
+
+ }
+
+
+ // Test getParamValues()
+ public void testParamValues() {
+
+ Map map = ((WebContext) context).getParamValues();
+ assertNotNull(map);
+
+ // Initial contents
+ checkMapSize(map, 2);
+ Object value1 = map.get("pkey1");
+ assertNotNull(value1);
+ assertTrue(value1 instanceof String[]);
+ String values1[] = (String[]) value1;
+ assertEquals(1, values1.length);
+ assertEquals("pvalue1", values1[0]);
+ Object value2 = map.get("pkey2");
+ assertNotNull(value2);
+ assertTrue(value2 instanceof String[]);
+ String values2[] = (String[]) value2;
+ assertEquals(2, values2.length);
+ assertEquals("pvalue2a", values2[0]);
+ assertEquals("pvalue2b", values2[1]);
+ assertTrue(map.containsKey("pkey1"));
+ assertTrue(map.containsKey("pkey2"));
+ assertTrue(map.containsValue(values1));
+ assertTrue(map.containsValue(values2));
+
+ // Unsupported operations on read-only map
+ try {
+ map.clear();
+ fail("Should have thrown UnsupportedOperationException");
+ } catch (UnsupportedOperationException e) {
+ ; // expected result
+ }
+ try {
+ map.put("pkey3", values2);
+ fail("Should have thrown UnsupportedOperationException");
+ } catch (UnsupportedOperationException e) {
+ ; // expected result
+ }
+ try {
+ map.putAll(new HashMap());
+ fail("Should have thrown UnsupportedOperationException");
+ } catch (UnsupportedOperationException e) {
+ ; // expected result
+ }
+ try {
+ map.remove("pkey1");
+ fail("Should have thrown UnsupportedOperationException");
+ } catch (UnsupportedOperationException e) {
+ ; // expected result
+ }
+
+ }
+
+
+ // Test getCookies()
+ public void testCookies() {
+
+ Map map = ((WebContext) context).getCookies();
+ assertNotNull(map);
+
+ // Initial contents
+ checkMapSize(map, 0);
+
+ try {
+ map.put("ckey3", "XXX");
+ fail("map.put() Should have thrown UnsupportedOperationException");
+ } catch (UnsupportedOperationException e) {
+ ; // expected result
+ }
+ }
+
+ // Test state of newly created instance
+ public void testPristine() {
+
+ super.testPristine();
+ PortletWebContext pwcontext = (PortletWebContext) context;
+
+ // Properties should all be non-null
+ assertNotNull(pwcontext.getApplicationScope());
+ assertNotNull(pwcontext.getHeader());
+ assertNotNull(pwcontext.getHeaderValues());
+ assertNotNull(pwcontext.getInitParam());
+ assertNotNull(pwcontext.getParam());
+ assertNotNull(pwcontext.getParamValues());
+ assertNotNull(pwcontext.getCookies());
+ assertNotNull(pwcontext.getRequestScope());
+ assertNotNull(pwcontext.getSessionScope());
+
+ // Attribute-property transparency
+ assertTrue(pwcontext.getApplicationScope() ==
+ pwcontext.get("applicationScope"));
+ assertTrue(pwcontext.getHeader() ==
+ pwcontext.get("header"));
+ assertTrue(pwcontext.getHeaderValues() ==
+ pwcontext.get("headerValues"));
+ assertTrue(pwcontext.getInitParam() ==
+ pwcontext.get("initParam"));
+ assertTrue(pwcontext.getParam() ==
+ pwcontext.get("param"));
+ assertTrue(pwcontext.getParamValues() ==
+ pwcontext.get("paramValues"));
+ assertTrue(pwcontext.getCookies() ==
+ pwcontext.get("cookies"));
+ assertTrue(pwcontext.getRequestScope() ==
+ pwcontext.get("requestScope"));
+ assertTrue(pwcontext.getSessionScope() ==
+ pwcontext.get("sessionScope"));
+
+ }
+
+
+ // Test release()
+ public void testRelease() {
+
+ PortletWebContext pwcontext = (PortletWebContext) context;
+ pwcontext.release();
+
+ // Properties should all be null
+ assertNull("getApplicationScope()", pwcontext.getApplicationScope());
+ assertNull("getHeader()", pwcontext.getHeader());
+ assertNull("getHeaderValues()", pwcontext.getHeaderValues());
+ assertNull("getInitParam()", pwcontext.getInitParam());
+ assertNull("getParam()", pwcontext.getParam());
+ assertNull("getParamValues()", pwcontext.getParamValues());
+ assertNull("getRequestScope()", pwcontext.getRequestScope());
+ assertNull("getSessionScope()", pwcontext.getSessionScope());
+
+ // Attributes should all be null
+ assertNull("applicationScope", pwcontext.get("applicationScope"));
+ assertNull("header", pwcontext.get("header"));
+ assertNull("headerValues", pwcontext.get("headerValues"));
+ assertNull("initParam", pwcontext.get("initParam"));
+ assertNull("param", pwcontext.get("param"));
+ assertNull("paramValues", pwcontext.get("paramValues"));
+ assertNull("requestScope", pwcontext.get("requestScope"));
+ assertNull("sessionScope", pwcontext.get("sessionScope"));
+
+ }
+
+
+ // Test getRequestScope()
+ public void testRequestScope() {
+
+ Map map = ((WebContext) context).getRequestScope();
+ assertNotNull(map);
+
+ // Initial contents
+ checkMapSize(map, 2);
+ assertEquals("rvalue1", (String) map.get("rkey1"));
+ assertEquals("rvalue2", (String) map.get("rkey2"));
+
+ // Transparency - entrySet()
+ checkEntrySet(map, true);
+
+ // Transparency - removal via web object
+ request.removeAttribute("rkey1");
+ checkMapSize(map, 1);
+ assertNull(map.get("rkey1"));
+
+ // Transparency - removal via map
+ map.remove("rkey2");
+ checkMapSize(map, 0);
+ assertNull(request.getAttribute("rkey2"));
+
+ // Transparency - addition via web object
+ request.setAttribute("rkeyA", "rvalueA");
+ checkMapSize(map, 1);
+ assertEquals("rvalueA", (String) map.get("rkeyA"));
+
+ // Transparency - addition via map
+ map.put("rkeyB", "rvalueB");
+ checkMapSize(map, 2);
+ assertEquals("rvalueB", (String) request.getAttribute("rkeyB"));
+
+ // Transparency - replacement via web object
+ request.setAttribute("rkeyA", "newvalueA");
+ checkMapSize(map, 2);
+ assertEquals("newvalueA", (String) map.get("rkeyA"));
+
+ // Transparency - replacement via map
+ map.put("rkeyB", "newvalueB");
+ checkMapSize(map, 2);
+ assertEquals("newvalueB", (String) request.getAttribute("rkeyB"));
+
+ // Clearing the map
+ map.clear();
+ checkMapSize(map, 0);
+
+ }
+
+
+ // Test getSessionScope()
+ public void testSessionScope() {
+
+ Map map = ((WebContext) context).getSessionScope();
+ assertNotNull(map);
+
+ // Initial contents
+ checkMapSize(map, 3);
+ assertEquals("svalue1", (String) map.get("skey1"));
+ assertEquals("svalue2", (String) map.get("skey2"));
+ assertEquals("svalue3", (String) map.get("skey3"));
+
+ // Transparency - entrySet()
+ checkEntrySet(map, true);
+
+ // Transparency - removal via web object
+ session.removeAttribute("skey1");
+ checkMapSize(map, 2);
+ assertNull(map.get("skey1"));
+
+ // Transparency - removal via map
+ map.remove("skey2");
+ checkMapSize(map, 1);
+ assertNull(session.getAttribute("skey2"));
+
+ // Transparency - addition via web object
+ session.setAttribute("skeyA", "svalueA");
+ checkMapSize(map, 2);
+ assertEquals("svalueA", (String) map.get("skeyA"));
+
+ // Transparency - addition via map
+ map.put("skeyB", "svalueB");
+ checkMapSize(map, 3);
+ assertEquals("svalueB", (String) session.getAttribute("skeyB"));
+
+ // Transparency - replacement via web object
+ session.setAttribute("skeyA", "newvalueA");
+ checkMapSize(map, 3);
+ assertEquals("newvalueA", (String) map.get("skeyA"));
+
+ // Transparency - replacement via map
+ map.put("skeyB", "newvalueB");
+ checkMapSize(map, 3);
+ assertEquals("newvalueB", (String) session.getAttribute("skeyB"));
+
+ // Clearing the map
+ map.clear();
+ checkMapSize(map, 0);
+
+ }
+
+
+ // ------------------------------------------------------- Protected
Methods
+
+
+ protected void checkMapSize(Map map, int size) {
+ // Check reported size of the map
+ assertEquals("checkMapSize(A)", size, map.size());
+ // Iterate over key set
+ int nk = 0;
+ Iterator keys = map.keySet().iterator();
+ while (keys.hasNext()) {
+ keys.next();
+ nk++;
+ }
+ assertEquals("checkMapSize(B)", size, nk);
+ // Iterate over entry set
+ int nv = 0;
+ Iterator values = map.entrySet().iterator();
+ while (values.hasNext()) {
+ values.next();
+ nv++;
+ }
+ assertEquals("checkMapSize(C)", size, nv);
+ // Count the values
+ assertEquals("checkMapSize(D)", size, map.values().size());
+ }
+
+ // Test to ensure proper entrySet() and are modifiable optionally
+ protected void checkEntrySet(Map map, boolean modifiable) {
+ assertTrue("checkEntrySet(A)", map.size() > 1);
+ Set entries = map.entrySet();
+ assertTrue(map.size() == entries.size());
+ Object o = entries.iterator().next();
+
+ assertTrue("checkEntrySet(B)", o instanceof Map.Entry);
+
+ if (!modifiable) {
+ try {
+ ((Map.Entry)o).setValue(new Object());
+ fail("Should have thrown UnsupportedOperationException");
+ } catch (UnsupportedOperationException e) {
+ ; // expected result
+ }
+ } else {
+ // Should pass and not throw UnsupportedOperationException
+ Map.Entry e = (Map.Entry)o;
+ e.setValue(e.setValue(new Object()));
+ }
+ }
+
+ // Create a new instance of the appropriate Context type for this test case
+ protected Context createContext() {
+ return (new PortletWebContext(pcontext, request, response));
+ }
+
+
+}
Propchange:
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/PortletWebContextTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/PortletWebContextTestCase.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]