1 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 ; 12 import java.io.UnsupportedEncodingException ; 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 import java.util.Locale ; 17 18 27 public class TestContext { 28 private WebClient client; 29 private String user; 30 private String passwd; 31 private List cookies; 32 private boolean hasAuth; 33 private Locale locale = Locale.getDefault(); 34 private String encodingScheme = "ISO-8859-1"; 35 private String resourceBundleName; 36 private String baseUrl = "http://localhost:8080"; 37 private String userAgent; 38 private String proxyName; 39 private int proxyPort = 80; 40 41 44 public TestContext() { 45 cookies = new ArrayList (); 46 } 47 48 58 public void setAuthorization(String user, String passwd) { 59 this.user = user; 60 this.passwd = passwd; 61 hasAuth = true; 62 } 63 64 73 public void addCookie(String name, String value) { 74 cookies.add(new Cookie (name, value)); 75 } 76 77 81 public boolean hasAuthorization() { 82 return hasAuth; 83 } 84 85 88 public boolean hasCookies() { 89 return cookies.size() > 0; 90 } 91 92 95 public String getUser() { 96 return user; 97 } 98 99 102 public String getPassword() { 103 return passwd; 104 } 105 106 109 public List getCookies() { 110 return cookies; 111 } 112 113 public String getUserAgent() { 114 return userAgent; 115 } 116 117 public void setUserAgent(String userAgent) { 118 this.userAgent = userAgent; 119 } 120 121 public boolean hasUserAgent() { 122 return userAgent != null; 123 } 124 125 129 public Locale getLocale() { 130 return locale; 131 } 132 133 136 public void setLocale(Locale locale) { 137 this.locale = locale; 138 } 139 140 144 public String getEncodingScheme() { 145 return encodingScheme; 146 } 147 148 152 public void setEncodingScheme(String encodingScheme) { 153 this.encodingScheme = encodingScheme; 154 } 155 156 165 public String toEncodedString(String text) { 166 try { 167 return new String (text.getBytes(), encodingScheme); 168 } catch (UnsupportedEncodingException e) { 169 e.printStackTrace(); 170 return text; 171 } 172 } 173 174 181 public void setResourceBundleName(String name) { 182 resourceBundleName = name; 183 } 184 185 188 public String getResourceBundleName() { 189 return resourceBundleName; 190 } 191 192 195 public String getProxyName() { 196 return proxyName; 197 } 198 199 202 public void setProxyName(String proxyName) { 203 this.proxyName = proxyName; 204 } 205 206 209 public int getProxyPort() { 210 return proxyPort; 211 } 212 213 216 public void setProxyPort(int proxyPort) { 217 this.proxyPort = proxyPort; 218 } 219 220 224 public boolean hasProxy() { 225 return proxyName != null && proxyName.trim().length() > 0; 226 } 227 228 232 public String getBaseUrl() { 233 return baseUrl; 234 } 235 236 242 public void setBaseUrl(String url) { 243 baseUrl = url.endsWith("/") ? url : url + "/"; 244 } 245 246 250 public void setWebClient(WebClient client) { 251 this.client = client; 252 } 253 254 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 cookies = getCookies(); 272 for (Iterator iter = cookies.iterator(); iter.hasNext();) { 273 Cookie c = (Cookie ) 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 |