stephan 2003/04/16 03:54:00
Added: src/test/org/apache/cocoon/environment/mock MockContext.java
MockCookie.java MockEnvironment.java
MockRedirector.java MockRequest.java
MockResponse.java MockSession.java
Log:
Add mock classes for the environment, that makes possible to test
Cocoon components outside of the normal environment (Inspired by the
Strutstestcase project).
Revision Changes Path
1.1
cocoon-2.1/src/test/org/apache/cocoon/environment/mock/MockContext.java
Index: MockContext.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Apache Cocoon" and "Apache Software Foundation" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation and was originally created by
Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.environment.mock;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Hashtable;
import java.io.InputStream;
import org.apache.cocoon.environment.Context;
public class MockContext implements Context {
private Hashtable attributes = new Hashtable();
private Hashtable resources = new Hashtable();
private Hashtable mappings = new Hashtable();
private Hashtable initparameters = new Hashtable();
public Object getAttribute(String name) {
return attributes.get(name);
}
public void setAttribute(String name, Object value) {
attributes.put(name, value);
}
public void removeAttribute(String name) {
attributes.remove(name);
}
public Enumeration getAttributeNames() {
return attributes.keys();
}
public void setResource(String path, URL url) {
resources.put(path, url);
}
public URL getResource(String path) throws MalformedURLException {
return (URL)resources.get(path);
}
public String getRealPath(String path) {
return path;
}
public String getMimeType(String file) {
return (String)mappings.get(file.substring(file.lastIndexOf(".")+1));
}
public void setInitParameter(String name, String value) {
initparameters.put(name, value);
}
public String getInitParameter(String name) {
return (String)initparameters.get(name);
}
public InputStream getResourceAsStream(String path) {
return null;
}
}
1.1
cocoon-2.1/src/test/org/apache/cocoon/environment/mock/MockCookie.java
Index: MockCookie.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Apache Cocoon" and "Apache Software Foundation" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation and was originally created by
Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.environment.mock;
import org.apache.cocoon.environment.Cookie;
public class MockCookie implements Cookie {
private String comment;
private String domain;
private int maxage;
private String path;
private boolean secure;
private String name;
private String value;
private int version;
public void setComment(String comment) {
this.comment = comment;
}
public String getComment() {
return comment;
}
public void setDomain(String domain) {
this.domain = domain;
}
public String getDomain() {
return domain;
}
public void setMaxAge(int maxage) {
this.maxage = maxage;
}
public int getMaxAge() {
return maxage;
}
public void setPath(String path) {
this.path = path;
}
public String getPath() {
return path;
}
public void setSecure(boolean secure) {
this.secure = secure;
}
public boolean getSecure() {
return secure;
}
public void setName(String name) {
this.name= name;
}
public String getName() {
return name;
}
public void setValue(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
}
1.1
cocoon-2.1/src/test/org/apache/cocoon/environment/mock/MockEnvironment.java
Index: MockEnvironment.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Apache Cocoon" and "Apache Software Foundation" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation and was originally created by
Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.environment.mock;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import junit.framework.AssertionFailedError;
import org.apache.cocoon.ProcessingException;
import org.apache.cocoon.environment.Environment;
import org.apache.cocoon.environment.Source;
import org.apache.excalibur.source.SourceResolver;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
public class MockEnvironment implements Environment {
private SourceResolver resolver;
private String uri;
private String uriprefix;
private URL rootcontext;
private URL context;
private String view;
private String action;
private String contenttype;
private int contentlength;
private int status;
private ByteArrayOutputStream outputstream;
private HashMap objectmodel;
private Hashtable attributes = new Hashtable();
public MockEnvironment(SourceResolver resolver) {
this.resolver = resolver;
}
public String getURI() {
return uri;
}
public String getURIPrefix() {
return uriprefix;
}
public URL getRootContext() {
return rootcontext;
}
public URL getContext() {
return context;
}
public String getView() {
return view;
}
public String getAction() {
return action;
}
public void setContext(String prefix, String uri) {
throw new AssertionFailedError("Not implemented");
}
public void changeContext(String uriprefix, String context) throws
Exception {
throw new AssertionFailedError("Not implemented");
}
public void redirect(boolean sessionmode, String url) throws IOException {
throw new AssertionFailedError("Use Redirector.redirect instead!");
}
public void setContentType(String contenttype) {
this.contenttype = contenttype;
}
public String getContentType() {
return contenttype;
}
public void setContentLength(int length) {
this.contentlength = contentlength;
}
public int getContentLength() {
return contentlength;
}
public void setStatus(int statusCode) {
this.status = status;
}
public int getStatus() {
return status;
}
public OutputStream getOutputStream() throws IOException {
outputstream = new ByteArrayOutputStream();
return outputstream;
}
public OutputStream getOutputStream(int bufferSize) throws IOException {
outputstream = new ByteArrayOutputStream();
return outputstream;
}
public byte[] getOutput() {
return outputstream.toByteArray();
}
public Map getObjectModel() {
return objectmodel;
}
public boolean isResponseModified(long lastModified) {
throw new AssertionFailedError("Not implemented");
}
public void setResponseIsNotModified() {
throw new AssertionFailedError("Not implemented");
}
public void setAttribute(String name, Object value) {
attributes.put(name, value);
}
public Object getAttribute(String name) {
return attributes.get(name);
}
public void removeAttribute(String name) {
attributes.remove(name);
}
public Enumeration getAttributeNames() {
return attributes.keys();
}
public boolean tryResetResponse() throws IOException {
throw new AssertionFailedError("Not implemented");
}
public void commitResponse() throws IOException {
throw new AssertionFailedError("Not implemented");
}
public void startingProcessing() {
throw new AssertionFailedError("Not implemented");
}
public void finishingProcessing() {
throw new AssertionFailedError("Not implemented");
}
public Source resolve(String systemID)
throws ProcessingException, SAXException, IOException {
throw new AssertionFailedError("Not not use deprecated methods!");
}
public void toSAX(org.apache.excalibur.source.Source source,
ContentHandler handler)
throws SAXException, IOException, ProcessingException {
throw new AssertionFailedError("Not not use deprecated methods!");
}
public void toSAX(org.apache.excalibur.source.Source source,
String mimeTypeHint,
ContentHandler handler)
throws SAXException, IOException, ProcessingException {
throw new AssertionFailedError("Not not use deprecated methods!");
}
public org.apache.excalibur.source.Source resolveURI(String location)
throws MalformedURLException, IOException,
org.apache.excalibur.source.SourceException {
return resolver.resolveURI(location);
}
public org.apache.excalibur.source.Source resolveURI(String location,
String base,
Map parameters)
throws MalformedURLException, IOException,
org.apache.excalibur.source.SourceException {
return resolver.resolveURI(location, base, parameters);
}
/**
* Releases a resolved resource
*/
public void release(org.apache.excalibur.source.Source source) {
resolver.release(source);
}
}
1.1
cocoon-2.1/src/test/org/apache/cocoon/environment/mock/MockRedirector.java
Index: MockRedirector.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Apache Cocoon" and "Apache Software Foundation" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation and was originally created by
Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.environment.mock;
import org.apache.cocoon.ProcessingException;
import java.io.IOException;
import org.apache.cocoon.environment.Redirector;
public class MockRedirector implements Redirector {
protected boolean hasRedirected = false;
private String redirect;
public void redirect(boolean sessionmode, String url) throws IOException,
ProcessingException {
this.hasRedirected = true;
redirect = url;
}
public void globalRedirect(boolean sessionmode, String url) throws
IOException, ProcessingException {
redirect(sessionmode, url);
}
public String getRedirect() {
return redirect;
}
public boolean hasRedirected() {
return this.hasRedirected;
}
}
1.1
cocoon-2.1/src/test/org/apache/cocoon/environment/mock/MockRequest.java
Index: MockRequest.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Apache Cocoon" and "Apache Software Foundation" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation and was originally created by
Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.environment.mock;
import java.security.Principal;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import junit.framework.AssertionFailedError;
import org.apache.cocoon.environment.Cookie;
import org.apache.cocoon.environment.Request;
import org.apache.cocoon.environment.Session;
public class MockRequest implements Request {
private Hashtable attributes;
private String scheme;
private String protocol = "HTTP/1.1";
private String requestURI;
private String requestURL;
private String contextPath = "";
private String servletPath;
private String pathInfo;
private String queryString;
private String method;
private String contentType;
private Locale locale;
private Principal principal;
private String remoteAddr;
private String remoteHost;
private String remoteUser;
private String userRole;
private String reqSessionId;
private String authType;
private String charEncoding;
private String serverName;
private int port;
private Hashtable parameters = new Hashtable();
private Hashtable headers = new Hashtable();
private HashMap cookies = new HashMap();
private MockSession session;
public Object get(String name) {
return getAttribute(name);
}
public Object getAttribute(String name) {
return attributes.get(name);
}
public Enumeration getAttributeNames() {
return attributes.keys();
}
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 )
return null;
else {
if (param.getClass().isArray()) {
return (String[]) param;
} else {
return new String[] {(String) param};
}
}
}
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 void setAttribute(String name, Object o) {
if (o == null)
attributes.remove(name);
else
attributes.put(name, o);
}
public void removeAttribute(String name) {
attributes.remove(name);
}
public Locale getLocale() {
return locale;
}
public Enumeration getLocales() {
return Collections.enumeration(Collections.singleton(getLocale()));
}
public boolean isSecure() {
if(scheme==null){
return false;
} else{
return scheme.equalsIgnoreCase("HTTPS");
}
}
public Cookie[] getCookies() {
if (cookies.isEmpty())
return null;
else {
Cookie[] cookieArray = new Cookie[cookies.size()];
return (Cookie []) cookies.values().toArray(cookieArray);
}
}
public Map getCookieMap() {
return cookies;
}
public long getDateHeader(String name) {
String s1 = getHeader(name);
if(s1 == null)
return -1L;
try
{
DateFormat dateFormat = new SimpleDateFormat();
return dateFormat.parse(s1).getTime();
}
catch(ParseException exception) {
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 String getQueryString() {
return queryString;
}
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 String getSitemapURI() {
return requestURI;
}
public String getServletPath() {
return servletPath;
}
public Session getSession(boolean create) {
if ((session == null) && (create))
this.session = new MockSession();
else if ((session != null) && (!((MockSession) session).isValid()) &&
(create))
this.session = new MockSession();
if ((session != null) && (((MockSession) session).isValid()))
return this.session;
else
return null;
}
public Session getSession() {
return getSession(true);
}
public boolean isRequestedSessionIdValid() {
if (session != null) {
try {
session.getId();
return true;
} catch (IllegalStateException e) {
return false;
}
} else
return false;
}
public boolean isRequestedSessionIdFromCookie() {
return true;
}
public boolean isRequestedSessionIdFromURL() {
return false;
}
}
1.1
cocoon-2.1/src/test/org/apache/cocoon/environment/mock/MockResponse.java
Index: MockResponse.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Apache Cocoon" and "Apache Software Foundation" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation and was originally created by
Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.environment.mock;
import java.util.Locale;
import java.util.HashMap;
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;
public class MockResponse implements Response {
private String encoding;
private Locale locale;
private HashSet cookies = new HashSet();
private HashMap header = new HashMap();
public void setCharacterEncoding(String encoding) {
this.encoding = encoding;
}
public String getCharacterEncoding() {
return encoding;
}
public void setLocale(Locale locale) {
this.locale = locale;
}
public Locale getLocale() {
return locale;
}
public Cookie createCookie(String name, String value) {
MockCookie cookie = new MockCookie();
cookie.setName(name);
cookie.setValue(value);
return cookie;
}
public void addCookie(Cookie cookie) {
cookies.add(cookie);
}
public Set getCookies() {
return cookies;
}
public boolean containsHeader(String name) {
return header.containsKey(name);
}
public String encodeURL(String url) {
throw new AssertionFailedError("Not implemented");
}
public void setDateHeader(String name, long date) {
header.put(name, new Long(date));
}
public void addDateHeader(String name, long date) {
header.put(name, new Long(date));
}
public void setHeader(String name, String value) {
header.put(name, value);
}
public void addHeader(String name, String value) {
header.put(name, value);
}
public void setIntHeader(String name, int value) {
header.put(name, new Integer(value));
}
public void addIntHeader(String name, int value) {
header.put(name, new Integer(value));
}
public Map getHeader() {
return header;
}
}
1.1
cocoon-2.1/src/test/org/apache/cocoon/environment/mock/MockSession.java
Index: MockSession.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Apache Cocoon" and "Apache Software Foundation" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation and was originally created by
Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.environment.mock;
import java.util.Enumeration;
import java.util.Hashtable;
import junit.framework.AssertionFailedError;
import org.apache.cocoon.environment.Session;
public class MockSession implements Session {
private long creationtime = System.currentTimeMillis();
private String id = "MockSession";
private long lastaccessedtime = System.currentTimeMillis();
private int maxinactiveinterval = -1;
private Hashtable attributes = new Hashtable();
private boolean valid = true;
public long getCreationTime() {
checkValid();
return creationtime;
}
public void setId(String id) {
this.id = id;
}
public String getId() {
checkValid();
return id;
}
public long getLastAccessedTime() {
checkValid();
return lastaccessedtime;
}
public void setMaxInactiveInterval(int interval) {
checkValid();
this.maxinactiveinterval = interval;
}
public int getMaxInactiveInterval() {
checkValid();
return maxinactiveinterval;
}
public Object getAttribute(String name) {
checkValid();
return attributes.get(name);
}
public Enumeration getAttributeNames() {
checkValid();
return attributes.keys();
}
public void setAttribute(String name, Object value) {
checkValid();
attributes.put(name, value);
}
public void removeAttribute(String name) {
checkValid();
attributes.remove(name);
}
public void invalidate() {
checkValid();
this.valid = false;
}
public boolean isNew() {
checkValid();
return false;
}
private void checkValid() throws IllegalStateException {
if (!valid)
throw new AssertionFailedError("session has been invalidated!");
}
public boolean isValid() {
return valid;
}
}