001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package de.schlund.pfixcore.util;
020:
021: import java.util.Collections;
022: import java.util.Enumeration;
023: import java.util.HashMap;
024: import java.util.Map;
025:
026: import javax.servlet.ServletContext;
027: import javax.servlet.http.HttpSession;
028:
029: import de.schlund.pfixcore.workflow.Context;
030: import de.schlund.pfixcore.workflow.ContextImpl;
031: import de.schlund.pfixcore.workflow.context.ServerContextImpl;
032: import de.schlund.pfixxml.config.ContextXMLServletConfig;
033:
034: /**
035: * Helper class that provides a dummy context instance that can
036: * be used within unit tests.
037: *
038: * @author Sebastian Marsching <sebastian.marsching@1und1.de>
039: */
040: public class DummyContextFactory {
041:
042: public static Context getDummyContext(ContextXMLServletConfig config)
043: throws Exception {
044: HttpSession session = new DummySession();
045: ServerContextImpl servercontext = new ServerContextImpl(config
046: .getContextConfig(), "Dummy");
047: ContextImpl context = new ContextImpl(servercontext, session);
048: context.prepareForRequest();
049: return context;
050: }
051:
052: private static class DummySession implements HttpSession {
053:
054: private int maxInactiveInterval;
055:
056: private Map<String, Object> atts = Collections
057: .synchronizedMap(new HashMap<String, Object>());
058:
059: public long getCreationTime() {
060: return 0;
061: }
062:
063: public String getId() {
064: return "dummysession";
065: }
066:
067: public long getLastAccessedTime() {
068: return 0;
069: }
070:
071: public ServletContext getServletContext() {
072: // There is no ServletContext
073: return null;
074: }
075:
076: public void setMaxInactiveInterval(int arg0) {
077: this .maxInactiveInterval = arg0;
078: }
079:
080: public int getMaxInactiveInterval() {
081: return this .maxInactiveInterval;
082: }
083:
084: @SuppressWarnings("deprecation")
085: public javax.servlet.http.HttpSessionContext getSessionContext() {
086: return null;
087: }
088:
089: public Object getAttribute(String arg0) {
090: return atts.get(arg0);
091: }
092:
093: public Object getValue(String arg0) {
094: return getAttribute(arg0);
095: }
096:
097: @SuppressWarnings("unchecked")
098: public Enumeration getAttributeNames() {
099: return Collections.enumeration(atts.keySet());
100: }
101:
102: public String[] getValueNames() {
103: return atts.keySet().toArray(new String[0]);
104: }
105:
106: public void setAttribute(String arg0, Object arg1) {
107: atts.put(arg0, arg1);
108:
109: }
110:
111: public void putValue(String arg0, Object arg1) {
112: setAttribute(arg0, arg1);
113: }
114:
115: public void removeAttribute(String arg0) {
116: atts.remove(arg0);
117: }
118:
119: public void removeValue(String arg0) {
120: removeAttribute(arg0);
121: }
122:
123: public void invalidate() {
124: // Does nothing here
125: }
126:
127: public boolean isNew() {
128: // Here: always false
129: return false;
130: }
131:
132: }
133: }
|