001: /*
002: * Coefficient - facilitates project based collaboration
003: * Copyright (C) 2003, Dylan Etkin, CSIR icomtek
004: * PO Box 395
005: * Pretoria 0001, RSA
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package za.org.coefficient.util.testing;
020:
021: import java.io.IOException;
022: import java.io.Serializable;
023:
024: import java.util.HashMap;
025: import java.util.Map;
026:
027: import org.apache.commons.collections.ExtendedProperties;
028: import org.apache.commons.fileupload.FileUploadException;
029:
030: import za.org.coefficient.authentication.CoefficientUser;
031: import za.org.coefficient.core.BaseCoefficientContext;
032: import za.org.coefficient.core.Constants;
033: import za.org.coefficient.util.common.BaseMultipartRequest;
034: import za.org.coefficient.util.common.InvokerFactory;
035:
036: /**
037: * <p>Project: coefficient</p>
038: * <p>Description: This is an implementation of the CoefficientContext
039: * that is backed by a Map for the session data, and 2 more
040: * for the request and request attribute data - this makes it useful
041: * for running unit tests.</p>
042: * <p>Copyright: Copyright (c) 2003</p>
043: * <p>Company: CSIR</p>
044: * @author tfogwill
045: * @version 1.0
046: */
047: public class CoefficientTestingContext extends BaseCoefficientContext
048: implements Serializable {
049:
050: private static final String REQUEST_DATA_KEY = "request.data";
051: private static final String REQUEST_ATTR_KEY = "request.attribute";
052: private static final String SESSION_KEY = "session.data";
053: private static final String USERNAME_KEY = "user.username";
054:
055: /**
056: * CoefficientTestingContext constructor
057: * @param filename of the properties file containing the request and session data
058: * @throws IOException if the file cannot be read
059: */
060: public CoefficientTestingContext(String filename)
061: throws IOException {
062: this (new ExtendedProperties(filename));
063: }
064:
065: /**
066: * CoefficientTestingContext constructor
067: * @param filename of the properties file containing the request and session data
068: * @param fileUploadData The map containing the fileupload data (String name/uploadedfile fileData)
069: * @throws IOException if the file cannot be read
070: */
071: public CoefficientTestingContext(String filename, Map fileUploadData)
072: throws IOException {
073: this (new ExtendedProperties(filename), fileUploadData);
074: }
075:
076: /**
077: * CoefficientTestingContext constructor
078: * @param properties properties object containing the request and session data
079: */
080: public CoefficientTestingContext(ExtendedProperties properties) {
081: this (properties.subset(SESSION_KEY), properties
082: .subset(REQUEST_DATA_KEY), properties
083: .subset(REQUEST_ATTR_KEY), new HashMap());
084: setupUser(properties);
085: }
086:
087: /**
088: * CoefficientTestingContext constructor
089: * @param properties properties object containing the request and session data
090: * @param fileUploadData The map containing the fileupload data (String name/uploadedfile fileData)
091: */
092: public CoefficientTestingContext(ExtendedProperties properties,
093: Map fileUploadData) {
094: this (properties.subset(SESSION_KEY), properties
095: .subset(REQUEST_DATA_KEY), properties
096: .subset(REQUEST_ATTR_KEY), fileUploadData);
097: setupUser(properties);
098: }
099:
100: /**
101: * constructor that includes fileupload data
102: * @param session Map containing session data
103: * @param requestData Map containing reqest paramter data
104: * @param requestAttributes Map containing request attribute data
105: * @param fileUploadData Map containing file upload data data
106: */
107: public CoefficientTestingContext(Map session, Map requestData,
108: Map requestAttributes) {
109: this (session, requestData, requestAttributes, new HashMap());
110: }
111:
112: /**
113: * constructor that includes fileupload data
114: * @param session Map containing session data
115: * @param requestData Map containing reqest paramter data
116: * @param requestAttributes Map containing request attribute data
117: * @param fileUploadData Map containing file upload data data
118: */
119: public CoefficientTestingContext(Map session, Map requestData,
120: Map requestAttributes, Map fileUploadData) {
121: super ();
122: this .session = session;
123: this .requestData = requestData;
124: this .requestAttributes = requestAttributes;
125: this .fileUploadData = fileUploadData;
126: }
127:
128: /*
129: * @see za.org.coefficient.interfaces.CoefficientContext#getMultipartRequest()
130: */
131: public BaseMultipartRequest getMultipartRequest()
132: throws FileUploadException {
133: return new TestingMultipartRequest(fileUploadData, requestData);
134: }
135:
136: /*
137: * @see za.org.coefficient.interfaces.CoefficientContext#getRequestURL()
138: */
139: public String getRequestURL() {
140: return "TESTING";
141: }
142:
143: private void setupUser(ExtendedProperties properties) {
144: String username = properties.getString(USERNAME_KEY);
145: if (username != null && username.length() > 0) {
146: CoefficientUser user = null;
147: try {
148: user = (CoefficientUser) InvokerFactory
149: .getRemoteInvoker().invokeMethodOnModule(
150: "UserAdmin", "findUserForName",
151: new Object[] { username });
152: } catch (Exception e) {
153: e.printStackTrace();
154: }
155:
156: if (user == null) {
157: throw new RuntimeException("INVALID USER: User "
158: + username + " not found");
159: }
160: setSessionAttribute(Constants.USER_SESSION_STRING, user);
161: }
162: }
163:
164: }
|