huber 2004/07/04 10:53:35
Modified: src/test/org/apache/cocoon/environment/mock
MockEnvironment.java MockRequest.java
MockResponse.java MockSession.java
Log:
added more methods for setting various fields during junit tests
CVS: ----------------------------------------------------------------------
CVS: PR:
CVS: If this change addresses a PR in the problem report tracking
CVS: database, then enter the PR number(s) here.
CVS: Obtained from:
CVS: If this change has been taken from another system, such as NCSA,
CVS: then name the system in this line, otherwise delete it.
CVS: Submitted by:
CVS: If this code has been contributed to Apache by someone else; i.e.,
CVS: they sent us a patch or a new module, then include their name/email
CVS: address here. If this is your work then delete this line.
CVS: Reviewed by:
CVS: If we are doing pre-commit code reviews and someone else has
CVS: reviewed your changes, include their name(s) here.
CVS: If you have not had it reviewed then delete this line.
Revision Changes Path
1.11 +1 -4
cocoon-2.1/src/test/org/apache/cocoon/environment/mock/MockEnvironment.java
Index: MockEnvironment.java
===================================================================
RCS file:
/home/cvs/cocoon-2.1/src/test/org/apache/cocoon/environment/mock/MockEnvironment.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- MockEnvironment.java 26 May 2004 01:31:06 -0000 1.10
+++ MockEnvironment.java 4 Jul 2004 17:53:34 -0000 1.11
@@ -19,12 +19,9 @@
import java.io.IOException;
import java.io.OutputStream;
import java.util.Enumeration;
-import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
-
import junit.framework.AssertionFailedError;
-
import org.apache.cocoon.environment.Environment;
public class MockEnvironment implements Environment {
@@ -37,7 +34,7 @@
private int contentlength;
private int status;
private ByteArrayOutputStream outputstream;
- private HashMap objectmodel;
+ private Map objectmodel;
private Hashtable attributes = new Hashtable();
public MockEnvironment() {
1.11 +91 -69
cocoon-2.1/src/test/org/apache/cocoon/environment/mock/MockRequest.java
Index: MockRequest.java
===================================================================
RCS file:
/home/cvs/cocoon-2.1/src/test/org/apache/cocoon/environment/mock/MockRequest.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- MockRequest.java 5 Mar 2004 13:03:03 -0000 1.10
+++ MockRequest.java 4 Jul 2004 17:53:34 -0000 1.11
@@ -1,12 +1,12 @@
/*
* Copyright 1999-2004 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.
@@ -33,7 +33,7 @@
import org.apache.cocoon.environment.Session;
public class MockRequest implements Request {
-
+
private Hashtable attributes = new Hashtable();
private String scheme;
private String protocol = "HTTP/1.1";
@@ -55,64 +55,68 @@
private String charEncoding;
private String serverName;
private int port = 80;
-
+
private Hashtable parameters = new Hashtable();
private Hashtable headers = new Hashtable();
- private HashMap cookies = new HashMap();
-
+ private Map cookies = new HashMap();
+
private MockSession session;
-
+
+ private boolean isRequestedSessionIdFromCookie = true;
+ private boolean isRequestedSessionIdFromURL = false;
+
public Object get(String name) {
return getAttribute(name);
}
-
+
public Object getAttribute(String name) {
return attributes.get(name);
}
-
+
public Enumeration getAttributeNames() {
return attributes.keys();
}
-
+
public void setAttribute(String name, Object o) {
- if (o == null)
+ if (o == null) {
attributes.remove(name);
- else
+ } else {
attributes.put(name, o);
+ }
}
-
+
public void removeAttribute(String name) {
attributes.remove(name);
}
-
+
public String getAuthType() {
return authType;
}
-
+
public String getCharacterEncoding() {
return charEncoding;
}
-
+
public void setCharacterEncoding(String enc) throws
java.io.UnsupportedEncodingException {
charEncoding = enc;
}
-
+
public int getContentLength() {
return -1;
}
-
+
public String getContentType() {
return contentType;
}
-
+
public String getParameter(String name) {
return (String)parameters.get(name);
}
-
+
public Enumeration getParameterNames() {
return parameters.keys();
}
-
+
public String[] getParameterValues(String name) {
Object param = parameters.get(name);
if( null == param )
@@ -125,43 +129,43 @@
}
}
}
-
+
public void addParameter(String name, String value) {
parameters.put(name, value);
}
-
+
public String getProtocol() {
return protocol;
}
-
+
public String getScheme() {
return scheme;
}
-
+
public String getServerName() {
return serverName;
}
-
+
public int getServerPort() {
return port;
}
-
+
public String getRemoteAddr() {
return remoteAddr;
}
-
+
public String getRemoteHost() {
return remoteHost;
}
-
+
public Locale getLocale() {
return locale;
}
-
+
public Enumeration getLocales() {
return Collections.enumeration(Collections.singleton(getLocale()));
}
-
+
public boolean isSecure() {
if(scheme==null){
return false;
@@ -169,7 +173,7 @@
return scheme.equalsIgnoreCase("HTTPS");
}
}
-
+
public Cookie[] getCookies() {
if (cookies.isEmpty())
return null;
@@ -178,17 +182,17 @@
return (Cookie []) cookies.values().toArray(cookieArray);
}
}
-
+
public Map getCookieMap() {
return cookies;
}
-
+
public long getDateHeader(String name) {
String s1 = getHeader(name);
- if(s1 == null)
+ if (s1 == null) {
return -1L;
- try
- {
+ }
+ try {
DateFormat dateFormat = new SimpleDateFormat();
return dateFormat.parse(s1).getTime();
}
@@ -196,94 +200,96 @@
throw new IllegalArgumentException("Cannot parse date: " + s1);
}
}
-
+
public String getHeader(String name) {
return (String) headers.get(name);
}
-
+
public Enumeration getHeaders(String name) {
throw new AssertionFailedError("Not implemented");
}
-
+
public Enumeration getHeaderNames() {
return headers.keys();
}
-
+
public String getMethod() {
return method;
}
-
+
public String getPathInfo() {
return pathInfo;
}
-
+
public String getPathTranslated() {
throw new AssertionFailedError("Not implemented");
}
-
+
public String getContextPath() {
return contextPath;
}
-
+
public void setContextPath(String path) {
contextPath = path;
}
-
+
public String getQueryString() {
return queryString;
}
-
+
public void setQueryString(String string) {
queryString = string;
}
-
+
public String getRemoteUser() {
return remoteUser;
}
-
+
public Principal getUserPrincipal() {
return principal;
}
-
+
public boolean isUserInRole(String role) {
return userRole.equals(role);
}
-
+
public String getRequestedSessionId() {
return reqSessionId;
}
-
+
public String getRequestURI() {
return requestURI;
}
-
+
public void setRequestURI(String uri) {
requestURI = uri;
}
-
+
public String getSitemapURI() {
return requestURI;
}
-
+
public String getServletPath() {
return servletPath;
}
-
+
public Session getSession(boolean create) {
- if ((session == null) && (create))
+ if ((session == null) && (create)) {
this.session = new MockSession();
- else if ((session != null) && (!(session).isValid()) && (create))
+ } else if ((session != null) && (!(session).isValid()) && (create)) {
this.session = new MockSession();
- if ((session != null) && ((session).isValid()))
+ }
+ if ((session != null) && ((session).isValid())) {
return this.session;
- else
+ } else {
return null;
+ }
}
-
+
public Session getSession() {
return getSession(true);
}
-
+
public boolean isRequestedSessionIdValid() {
if (session != null) {
try {
@@ -295,15 +301,13 @@
} else
return false;
}
-
+
public boolean isRequestedSessionIdFromCookie() {
- return true;
+ return isRequestedSessionIdFromCookie;
}
-
public boolean isRequestedSessionIdFromURL() {
- return false;
+ return isRequestedSessionIdFromURL;
}
-
public void reset() {
attributes.clear();
scheme = null;
@@ -326,8 +330,26 @@
charEncoding = null;
serverName = null;
port = 80;
-
+
parameters.clear();
headers.clear();
+ }
+
+ public void setHeader( String key, String value ) {
+ this.headers.put(key, value );
+ }
+
+ public void setMethod( String method ) {
+ this.method = method;
+ }
+
+ public void clearSession() {
+ this.session = null;
+ }
+ public void setIsRequestedSessionIdFromURL( boolean
isRequestedSessionIdFromURL ) {
+ this.isRequestedSessionIdFromURL = isRequestedSessionIdFromURL;
+ }
+ public void setIsRequestedSessionIdFromCooki( boolean
isRequestedSessionIdFromCookie ) {
+ this.isRequestedSessionIdFromCookie = isRequestedSessionIdFromCookie;
}
}
1.4 +17 -6
cocoon-2.1/src/test/org/apache/cocoon/environment/mock/MockResponse.java
Index: MockResponse.java
===================================================================
RCS file:
/home/cvs/cocoon-2.1/src/test/org/apache/cocoon/environment/mock/MockResponse.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- MockResponse.java 5 Mar 2004 13:03:03 -0000 1.3
+++ MockResponse.java 4 Jul 2004 17:53:34 -0000 1.4
@@ -20,19 +20,19 @@
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
-
-import junit.framework.AssertionFailedError;
-
import org.apache.cocoon.environment.Cookie;
import org.apache.cocoon.environment.Response;
+import org.apache.cocoon.environment.Session;
public class MockResponse implements Response {
private String encoding;
private Locale locale;
- private HashSet cookies = new HashSet();
- private HashMap header = new HashMap();
+ private Set cookies = new HashSet();
+ private Map header = new HashMap();
+ private Session session;
+
public void setCharacterEncoding(String encoding) {
this.encoding = encoding;
}
@@ -69,7 +69,14 @@
}
public String encodeURL(String url) {
- throw new AssertionFailedError("Not implemented");
+ //throw new AssertionFailedError("Not implemented");
+ StringBuffer sb = new StringBuffer();
+ sb.append( url );
+ if (session != null) {
+ sb.append( "?JSESSIONID=");
+ sb.append( session.getId() );
+ }
+ return sb.toString();
}
public void setDateHeader(String name, long date) {
@@ -105,5 +112,9 @@
locale = null;
cookies.clear();
header.clear();
+ }
+
+ public void setSession( Session session ) {
+ this.session = session;
}
}
1.3 +8 -2
cocoon-2.1/src/test/org/apache/cocoon/environment/mock/MockSession.java
Index: MockSession.java
===================================================================
RCS file:
/home/cvs/cocoon-2.1/src/test/org/apache/cocoon/environment/mock/MockSession.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- MockSession.java 5 Mar 2004 13:03:03 -0000 1.2
+++ MockSession.java 4 Jul 2004 17:53:34 -0000 1.3
@@ -30,6 +30,7 @@
private int maxinactiveinterval = -1;
private Hashtable attributes = new Hashtable();
private boolean valid = true;
+ private boolean isNew = false;
public long getCreationTime() {
checkValid();
@@ -87,16 +88,21 @@
public boolean isNew() {
checkValid();
- return false;
+ return isNew;
}
private void checkValid() throws IllegalStateException {
- if (!valid)
+ if (!valid) {
throw new AssertionFailedError("session has been invalidated!");
+ }
}
public boolean isValid() {
return valid;
+ }
+
+ public void setIsNew(boolean isNew ) {
+ this.isNew = isNew;
}
}