001: /******************************************************************************
002: * jWebUnit project (http://jwebunit.sourceforge.net) *
003: * Distributed open-source, see full license under LICENCE.txt *
004: ******************************************************************************/package net.sourceforge.jwebunit.util;
005:
006: import javax.servlet.http.Cookie;
007:
008: import java.net.MalformedURLException;
009: import java.net.URL;
010: import java.util.ArrayList;
011: import java.util.HashMap;
012: import java.util.List;
013: import java.util.Locale;
014: import java.util.Map;
015:
016: /**
017: * Establish context for tests (things such as locale, base url for the application, cookies, authorization). The
018: * context can be accessed through the {@link net.sourceforge.jwebunit.WebTestCase}or
019: * {@link net.sourceforge.jwebunit.junit.WebTester}.
020: *
021: * @author Wilkes Joiner
022: * @author Jim Weaver
023: * @author Julien Henry
024: */
025: public class TestContext {
026: private String user;
027:
028: private String passwd;
029:
030: private String domain;
031:
032: private List cookies;
033:
034: private boolean hasAuth = false;
035:
036: private boolean hasNTLMAuth = false;
037:
038: private Locale locale = Locale.getDefault();
039:
040: private String resourceBundleName;
041:
042: private URL baseUrl;
043:
044: private String userAgent;
045:
046: private Map requestHeaders = new HashMap();
047:
048: private String proxyUser = null;
049:
050: private String proxyPasswd = null;
051:
052: private String proxyHost = null;
053:
054: private int proxyPort = -1;
055:
056: private boolean hasProxyAuth = false;
057:
058: /**
059: * Construct a test client context.
060: */
061: public TestContext() {
062: cookies = new ArrayList();
063: try {
064: baseUrl = new URL("http://localhost:8080");
065: } catch (MalformedURLException e) {
066: // Should not be invalid
067: e.printStackTrace();
068: }
069: }
070:
071: /**
072: * Clear all authorizations (basic, digest, ntlm, proxy).
073: *
074: */
075: public void clearAuthorizations() {
076: hasAuth = false;
077: hasNTLMAuth = false;
078: hasProxyAuth = false;
079: }
080:
081: /**
082: * Set basic authentication information for the test context.
083: *
084: * @param user user name
085: * @param passwd password
086: */
087: public void setAuthorization(String user, String passwd) {
088: this .user = user;
089: this .passwd = passwd;
090: hasAuth = true;
091: }
092:
093: /**
094: * Set NTLM authentication information for the test context.
095: *
096: * @param user user name
097: * @param passwd password
098: */
099: public void setNTLMAuthorization(String user, String passwd,
100: String domain) {
101: this .user = user;
102: this .passwd = passwd;
103: this .domain = domain;
104: hasNTLMAuth = true;
105: }
106:
107: /**
108: * Set proxy authentication information for the test context.
109: *
110: * @param user user name (null if none)
111: * @param passwd password (null if none)
112: * @param host proxy host name (null if applicable to any host).
113: * @param port proxy port (negative if applicable to any port).
114: */
115: public void setProxyAuthorization(String user, String passwd,
116: String host, int port) {
117: this .proxyUser = user;
118: this .proxyPasswd = passwd;
119: this .proxyHost = host;
120: this .proxyPort = port;
121: hasProxyAuth = true;
122: }
123:
124: /**
125: * Add a cookie to the test context. These cookies are set on the conversation when you use a {WebTester#beginAt}.
126: *
127: * @param name cookie name.
128: * @param value cookie value.
129: * @param domain cookie domain (ie localhost or www.foo.bar).
130: */
131: public void addCookie(String name, String value, String domain) {
132: Cookie c = new Cookie(name, value);
133: c.setDomain(domain);
134: cookies.add(c);
135: }
136:
137: /**
138: * Add a cookie to the test context. These cookies are set on the conversation when you use a {WebTester#beginAt}.
139: *
140: * @param cookie a cookie.
141: */
142: public void addCookie(Cookie cookie) {
143: cookies.add(cookie);
144: }
145:
146: /**
147: * Return true if a basic authentication has been set on the context via {@link #setAuthorization}.
148: */
149: public boolean hasAuthorization() {
150: return hasAuth;
151: }
152:
153: /**
154: * Return true if a NTLM authentication has been set on the context via {@link #setNTLMAuthorization}.
155: */
156: public boolean hasNTLMAuthorization() {
157: return hasNTLMAuth;
158: }
159:
160: /**
161: * Return true if a proxy authentication has been set on the context via {@link #setProxyAuthorization}.
162: */
163: public boolean hasProxyAuthorization() {
164: return hasProxyAuth;
165: }
166:
167: /**
168: * Return true if one or more cookies have been added to the test context.
169: */
170: public boolean hasCookies() {
171: return cookies.size() > 0;
172: }
173:
174: /**
175: * Return the authorized user for the test context.
176: */
177: public String getUser() {
178: return user;
179: }
180:
181: /**
182: * Return the user password.
183: */
184: public String getPassword() {
185: return passwd;
186: }
187:
188: /**
189: * Return the user domain.
190: */
191: public String getDomain() {
192: return domain;
193: }
194:
195: /**
196: * Return the cookies which have been added to the test context.
197: */
198: public List getCookies() {
199: return cookies;
200: }
201:
202: public String getUserAgent() {
203: return userAgent;
204: }
205:
206: public void setUserAgent(String userAgent) {
207: this .userAgent = userAgent;
208: }
209:
210: public boolean hasUserAgent() {
211: return userAgent != null;
212: }
213:
214: /**
215: * Return the locale established for the test context. If the locale has not been explicitly set,
216: * Locale.getDefault() will be returned.
217: */
218: public Locale getLocale() {
219: return locale;
220: }
221:
222: /**
223: * Set the locale for the test context.
224: */
225: public void setLocale(Locale locale) {
226: this .locale = locale;
227: }
228:
229: /**
230: * Set a resource bundle to use for the test context (will be used to lookup expected values by key in WebTester).
231: *
232: * @param name path name of the resource bundle.
233: */
234: public void setResourceBundleName(String name) {
235: resourceBundleName = name;
236: }
237:
238: /**
239: * Return the test context resource bundle for expected value lookups.
240: */
241: public String getResourceBundleName() {
242: return resourceBundleName;
243: }
244:
245: /**
246: * Return the proxy server name
247: */
248: public String getProxyHost() {
249: return proxyHost;
250: }
251:
252: /**
253: * Return the proxy server port
254: */
255: public int getProxyPort() {
256: return proxyPort;
257: }
258:
259: /**
260: * Return the proxy user name
261: */
262: public String getProxyUser() {
263: return proxyUser;
264: }
265:
266: /**
267: * Return the proxy password
268: */
269: public String getProxyPasswd() {
270: return proxyPasswd;
271: }
272:
273: /**
274: * Return the base URL for the test context. The default base URL is port 8080 on localhost.
275: */
276: public URL getBaseUrl() {
277: return baseUrl;
278: }
279:
280: /**
281: * Set the base url for the test context.
282: *
283: * @param url Base url value - A trailing "/" is appended if not provided.
284: */
285: public void setBaseUrl(String url) {
286: try {
287: baseUrl = new URL(url.endsWith("/") ? url : url + "/");
288: } catch (MalformedURLException e) {
289: throw new RuntimeException(e);
290: }
291: }
292:
293: /**
294: * Set the base url for the test context.
295: *
296: * @param url Base url value. Anything after trailing "/" will be skipped.
297: */
298: public void setBaseUrl(URL url) {
299: baseUrl = url;
300: }
301:
302: /**
303: * Add a custom request header.
304: * @param name header name.
305: * @param value header value.
306: */
307: public void addRequestHeader(final String name, final String value) {
308: requestHeaders.put(name, value);
309: }
310:
311: /**
312: * Remove a custom request header.
313: * @param name header name.
314: */
315: public void removeRequestHeader(final String name) {
316: requestHeaders.remove(name);
317: }
318:
319: /**
320: * Get custom request headers.
321: * @param name header name.
322: * @param value header value.
323: */
324: public Map getRequestHeaders() {
325: return requestHeaders;
326: }
327:
328: /**
329: * Clear custom request headers.
330: *
331: */
332: public void clearRequestHeaders() {
333: requestHeaders = new HashMap();
334: }
335:
336: }
|