KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > za > org > coefficient > util > testing > CoefficientTestingContext


1 /*
2  * Coefficient - facilitates project based collaboration
3  * Copyright (C) 2003, Dylan Etkin, CSIR icomtek
4  * PO Box 395
5  * Pretoria 0001, RSA
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package za.org.coefficient.util.testing;
20
21 import java.io.IOException JavaDoc;
22 import java.io.Serializable JavaDoc;
23
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import org.apache.commons.collections.ExtendedProperties;
28 import org.apache.commons.fileupload.FileUploadException;
29
30 import za.org.coefficient.authentication.CoefficientUser;
31 import za.org.coefficient.core.BaseCoefficientContext;
32 import za.org.coefficient.core.Constants;
33 import za.org.coefficient.util.common.BaseMultipartRequest;
34 import za.org.coefficient.util.common.InvokerFactory;
35
36 /**
37  * <p>Project: coefficient</p>
38  * <p>Description: This is an implementation of the CoefficientContext
39  * that is backed by a Map for the session data, and 2 more
40  * for the request and request attribute data - this makes it useful
41  * for running unit tests.</p>
42  * <p>Copyright: Copyright (c) 2003</p>
43  * <p>Company: CSIR</p>
44  * @author tfogwill
45  * @version 1.0
46  */

47 public class CoefficientTestingContext extends BaseCoefficientContext
48     implements Serializable JavaDoc
49 {
50     
51     private static final String JavaDoc REQUEST_DATA_KEY = "request.data";
52     private static final String JavaDoc REQUEST_ATTR_KEY = "request.attribute";
53     private static final String JavaDoc SESSION_KEY = "session.data";
54     private static final String JavaDoc USERNAME_KEY = "user.username";
55     
56     /**
57      * CoefficientTestingContext constructor
58      * @param filename of the properties file containing the request and session data
59      * @throws IOException if the file cannot be read
60      */

61     public CoefficientTestingContext(String JavaDoc filename) throws IOException JavaDoc{
62         this(new ExtendedProperties(filename));
63     }
64     
65     /**
66      * CoefficientTestingContext constructor
67      * @param filename of the properties file containing the request and session data
68      * @param fileUploadData The map containing the fileupload data (String name/uploadedfile fileData)
69      * @throws IOException if the file cannot be read
70      */

71     public CoefficientTestingContext(String JavaDoc filename, Map JavaDoc fileUploadData) throws IOException JavaDoc{
72         this(new ExtendedProperties(filename), fileUploadData);
73     }
74     
75     /**
76      * CoefficientTestingContext constructor
77      * @param properties properties object containing the request and session data
78      */

79     public CoefficientTestingContext(ExtendedProperties properties){
80         this(properties.subset(SESSION_KEY), properties.subset(REQUEST_DATA_KEY), properties.subset(REQUEST_ATTR_KEY), new HashMap JavaDoc());
81         setupUser(properties);
82     }
83     
84     /**
85      * CoefficientTestingContext constructor
86      * @param properties properties object containing the request and session data
87      * @param fileUploadData The map containing the fileupload data (String name/uploadedfile fileData)
88      */

89     public CoefficientTestingContext(ExtendedProperties properties, Map JavaDoc fileUploadData){
90         this(properties.subset(SESSION_KEY), properties.subset(REQUEST_DATA_KEY), properties.subset(REQUEST_ATTR_KEY), fileUploadData);
91         setupUser(properties);
92     }
93     
94     /**
95      * constructor that includes fileupload data
96      * @param session Map containing session data
97      * @param requestData Map containing reqest paramter data
98      * @param requestAttributes Map containing request attribute data
99      * @param fileUploadData Map containing file upload data data
100      */

101     public CoefficientTestingContext(Map JavaDoc session, Map JavaDoc requestData, Map JavaDoc requestAttributes){
102         this(session, requestData, requestAttributes, new HashMap JavaDoc());
103     }
104     
105     /**
106      * constructor that includes fileupload data
107      * @param session Map containing session data
108      * @param requestData Map containing reqest paramter data
109      * @param requestAttributes Map containing request attribute data
110      * @param fileUploadData Map containing file upload data data
111      */

112     public CoefficientTestingContext(Map JavaDoc session, Map JavaDoc requestData, Map JavaDoc requestAttributes, Map JavaDoc fileUploadData){
113         super();
114         this.session = session;
115         this.requestData = requestData;
116         this.requestAttributes = requestAttributes;
117         this.fileUploadData = fileUploadData;
118     }
119     
120     /*
121      * @see za.org.coefficient.interfaces.CoefficientContext#getMultipartRequest()
122      */

123     public BaseMultipartRequest getMultipartRequest() throws FileUploadException {
124         return new TestingMultipartRequest(fileUploadData, requestData);
125     }
126
127     /*
128      * @see za.org.coefficient.interfaces.CoefficientContext#getRequestURL()
129      */

130     public String JavaDoc getRequestURL() {
131         return "TESTING";
132     }
133
134     private void setupUser(ExtendedProperties properties){
135         String JavaDoc username = properties.getString(USERNAME_KEY);
136         if (username != null && username.length() > 0){
137             CoefficientUser user = null;
138             try {
139                 user = (CoefficientUser)InvokerFactory
140                     .getRemoteInvoker().invokeMethodOnModule("UserAdmin",
141                                                              "findUserForName", new Object JavaDoc[] {username});
142             } catch (Exception JavaDoc e) {
143                 e.printStackTrace();
144             }
145
146             if (user == null){
147                 throw new RuntimeException JavaDoc("INVALID USER: User " + username
148                                            + " not found");
149             }
150             setSessionAttribute(Constants.USER_SESSION_STRING, user);
151         }
152     }
153
154 }
155
Popular Tags