KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > jwebunit > TestContext


1 /********************************************************************************
2  * Copyright (c) 2001, ThoughtWorks, Inc.
3  * Distributed open-source, see full license under licenses/jwebunit_license.txt
4  **********************************/

5 package net.sourceforge.jwebunit;
6
7 import com.meterware.httpunit.ClientProperties;
8 import com.meterware.httpunit.WebClient;
9 import com.meterware.httpunit.WebConversation;
10
11 import javax.servlet.http.Cookie JavaDoc;
12 import java.io.UnsupportedEncodingException JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Locale JavaDoc;
17
18 /**
19  * Establish context for tests (things such as locale, base url for the
20  * application, cookies, authorization). The context can be accessed through
21  * the {@link net.sourceforge.jwebunit.WebTestCase}or
22  * {@link net.sourceforge.jwebunit.WebTester}.
23  *
24  * @author Wilkes Joiner
25  * @author Jim Weaver
26  */

27 public class TestContext {
28     private WebClient client;
29     private String JavaDoc user;
30     private String JavaDoc passwd;
31     private List JavaDoc cookies;
32     private boolean hasAuth;
33     private Locale JavaDoc locale = Locale.getDefault();
34     private String JavaDoc encodingScheme = "ISO-8859-1";
35     private String JavaDoc resourceBundleName;
36     private String JavaDoc baseUrl = "http://localhost:8080";
37     private String JavaDoc userAgent;
38     private String JavaDoc proxyName;
39     private int proxyPort = 80;
40
41     /**
42      * Construct a test client context.
43      */

44     public TestContext() {
45         cookies = new ArrayList JavaDoc();
46     }
47
48     /**
49      * Set authentication information for the test context. This information is
50      * used by {@link HttpUnitDialog}to set authorization on the
51      * WebConversation when the dialog is begun.
52      *
53      * @param user
54      * user name
55      * @param passwd
56      * password
57      */

58     public void setAuthorization(String JavaDoc user, String JavaDoc passwd) {
59         this.user = user;
60         this.passwd = passwd;
61         hasAuth = true;
62     }
63
64     /**
65      * Add a cookie to the test context. These cookies are set on the
66      * WebConversation when an {@link HttpUnitDialog}is begun.
67      *
68      * @param name
69      * cookie name.
70      * @param value
71      * cookie value.
72      */

73     public void addCookie(String JavaDoc name, String JavaDoc value) {
74         cookies.add(new Cookie JavaDoc(name, value));
75     }
76
77     /**
78      * Return true if a user / password has been set on the context via
79      * {@link #setAuthorization}.
80      */

81     public boolean hasAuthorization() {
82         return hasAuth;
83     }
84
85     /**
86      * Return true if one or more cookies have been added to the test context.
87      */

88     public boolean hasCookies() {
89         return cookies.size() > 0;
90     }
91
92     /**
93      * Return the authorized user for the test context.
94      */

95     public String JavaDoc getUser() {
96         return user;
97     }
98
99     /**
100      * Return the user password.
101      */

102     public String JavaDoc getPassword() {
103         return passwd;
104     }
105
106     /**
107      * Return the cookies which have been added to the test context.
108      */

109     public List JavaDoc getCookies() {
110         return cookies;
111     }
112
113     public String JavaDoc getUserAgent() {
114         return userAgent;
115     }
116
117     public void setUserAgent(String JavaDoc userAgent) {
118         this.userAgent = userAgent;
119     }
120
121     public boolean hasUserAgent() {
122         return userAgent != null;
123     }
124
125     /**
126      * Return the locale established for the test context. If the locale has
127      * not been explicitly set, Locale.getDefault() will be returned.
128      */

129     public Locale JavaDoc getLocale() {
130         return locale;
131     }
132
133     /**
134      * Set the locale for the test context.
135      */

136     public void setLocale(Locale JavaDoc locale) {
137         this.locale = locale;
138     }
139
140     /**
141      * Return the encoding scheme for the test context. The default encoding
142      * scheme is ISO-8859-1.
143      */

144     public String JavaDoc getEncodingScheme() {
145         return encodingScheme;
146     }
147
148     /**
149      * Set the encoding scheme for the test context which is applied to
150      * response text.
151      */

152     public void setEncodingScheme(String JavaDoc encodingScheme) {
153         this.encodingScheme = encodingScheme;
154     }
155
156     /**
157      * Return the value of a String in the encoding specified by the test
158      * context.
159      *
160      * @param text
161      * input text.
162      * @return String representing bytes of text converted by context's
163      * encoding scheme.
164      */

165     public String JavaDoc toEncodedString(String JavaDoc text) {
166         try {
167             return new String JavaDoc(text.getBytes(), encodingScheme);
168         } catch (UnsupportedEncodingException JavaDoc e) {
169             e.printStackTrace();
170             return text;
171         }
172     }
173
174     /**
175      * Set a resource bundle to use for the test context (will be used to
176      * lookup expected values by key in WebTester).
177      *
178      * @param name
179      * path name of the resource bundle.
180      */

181     public void setResourceBundleName(String JavaDoc name) {
182         resourceBundleName = name;
183     }
184
185     /**
186      * Return the test context resource bundle for expected value lookups.
187      */

188     public String JavaDoc getResourceBundleName() {
189         return resourceBundleName;
190     }
191
192     /**
193      * Return the proxy server name Contributed by Jack Chen
194      */

195     public String JavaDoc getProxyName() {
196         return proxyName;
197     }
198
199     /**
200      * Set the proxy server name for the test context. Contributed by Jack Chen
201      */

202     public void setProxyName(String JavaDoc proxyName) {
203         this.proxyName = proxyName;
204     }
205
206     /**
207      * Return the proxy server port Contributed by Jack Chen
208      */

209     public int getProxyPort() {
210         return proxyPort;
211     }
212
213     /**
214      * Set the proxy server port for the test context. Contributed by Jack Chen
215      */

216     public void setProxyPort(int proxyPort) {
217         this.proxyPort = proxyPort;
218     }
219
220     /**
221      * Return true if a proxy name is set {@link #setProxyName}. Contributed
222      * by Jack Chen
223      */

224     public boolean hasProxy() {
225         return proxyName != null && proxyName.trim().length() > 0;
226     }
227
228     /**
229      * Return the base URL for the test context. The default base URL is port
230      * 8080 on localhost.
231      */

232     public String JavaDoc getBaseUrl() {
233         return baseUrl;
234     }
235
236     /**
237      * Set the base url for the test context.
238      *
239      * @param url
240      * Base url value - A trailing "/" is appended if not provided.
241      */

242     public void setBaseUrl(String JavaDoc url) {
243         baseUrl = url.endsWith("/") ? url : url + "/";
244     }
245
246     /**
247      * Set the WebClient to use for testing. If not set, jwebunit will create
248      * an httpunit WebConversation.
249      */

250     public void setWebClient(WebClient client) {
251         this.client = client;
252     }
253
254     /**
255      * This returns a WebClient with all the appropraite settings for this
256      * TestContext.
257      */

258     public WebClient getWebClient() {
259         if (client == null) {
260             client = new WebConversation();
261         }
262
263         if (hasAuthorization()) {
264             client.setAuthorization(getUser(), getPassword());
265         }
266         if (hasProxy()) {
267             client.setProxyServer(getProxyName(), getProxyPort());
268         }
269     
270         if (hasCookies()) {
271             List JavaDoc cookies = getCookies();
272             for (Iterator JavaDoc iter = cookies.iterator(); iter.hasNext();) {
273                 Cookie JavaDoc c = (Cookie JavaDoc) iter.next();
274                 client.addCookie(c.getName(), c.getValue());
275             }
276         }
277         if (hasUserAgent()) {
278             ClientProperties properties = client.getClientProperties();
279             properties.setUserAgent(getUserAgent());
280         }
281         return client;
282     }
283
284 }
285
Popular Tags