001: package org.jzonic.webtester;
002:
003: import java.io.IOException;
004: import java.net.MalformedURLException;
005: import java.util.Properties;
006:
007: import org.xml.sax.SAXException;
008:
009: import com.meterware.httpunit.HttpUnitOptions;
010: import com.meterware.httpunit.WebConversation;
011: import com.meterware.httpunit.WebForm;
012: import com.meterware.httpunit.WebLink;
013: import com.meterware.httpunit.WebResponse;
014: import com.meterware.httpunit.WebTable;
015:
016: /**
017: * The WebTestContext is used during every test and keeps
018: * all informations like the current form or the content
019: * of the latest HTML page
020: *
021: * @author Mecky
022: */
023: public class WebTestContext {
024:
025: private String content;
026: private WebResponse response;
027: private String proxyHost;
028: private int proxyPort = 80;
029: private WebForm webForm;
030: private Properties vars;
031: private WebTable table;
032: private WebConversation wc;
033: private String urlPrefix = "";
034:
035: public WebTestContext() {
036: }
037:
038: public void getContent(String url) throws MalformedURLException,
039: IOException, SAXException {
040: if (wc == null)
041: wc = new WebConversation();
042: if (proxyHost != null) {
043: wc.setProxyServer(proxyHost, proxyPort);
044: }
045: response = wc.getResponse(url);
046: content = response.getText();
047: webForm = null;
048: table = null;
049: }
050:
051: protected void setContent(String content) {
052: this .content = content;
053: }
054:
055: public void selectForm(int number) throws SAXException {
056: WebForm[] forms = response.getForms();
057: if (forms != null && forms.length > number) {
058: webForm = forms[number];
059: }
060: }
061:
062: /**
063: * @return
064: */
065: public String getHtmlText() {
066: return content;
067: }
068:
069: public String getProxyHost() {
070: return proxyHost;
071: }
072:
073: public void setProxyHost(String proxyHost) {
074: this .proxyHost = proxyHost;
075: }
076:
077: public int getProxyPort() {
078: return proxyPort;
079: }
080:
081: public void setProxyPort(int proxyPort) {
082: this .proxyPort = proxyPort;
083: }
084:
085: public WebForm getWebForm() {
086: return webForm;
087: }
088:
089: public void setResponse(WebResponse resp) throws IOException {
090: response = resp;
091: content = resp.getText();
092: }
093:
094: /**
095: * @param title
096: * @return
097: * @throws SAXException
098: */
099: public String getTitle() throws SAXException {
100: return response.getTitle();
101: }
102:
103: /**
104: * @param btnName
105: * @return
106: * @throws SAXException
107: */
108: public WebLink getLinkWith(String linkName) throws SAXException {
109: return response.getLinkWith(linkName);
110: }
111:
112: public WebLink getLinkWithID(String linkName) throws SAXException {
113: return response.getLinkWithID(linkName);
114: }
115:
116: public WebLink getLinkWithImageText(String linkName)
117: throws SAXException {
118: return response.getLinkWithImageText(linkName);
119: }
120:
121: /**
122: * @param props
123: */
124: public void setVariables(Properties props) {
125: vars = props;
126: }
127:
128: public String replaceVar(String text) {
129: if (text == null) {
130: return text;
131: }
132: if (vars == null) {
133: return text;
134: }
135: // Find the start of the variable
136: int startPosition = text.indexOf("${");
137: if (startPosition == -1) {
138: return text;
139: }
140:
141: StringBuffer buffer = new StringBuffer();
142: int textLength = text.length();
143: int lastIndex = 0;
144: // Loop while there are instances of "${" and cursor position is < length -2
145: // so there are no index out of bounds on the indexOf calls.
146: while (startPosition > -1 && (startPosition + 2) < textLength) {
147: int endPosition = text.indexOf("}", startPosition + 2);
148: if (endPosition == -1) {
149: // No end tag
150: break;
151: }
152:
153: // Append previous text to this point
154: buffer.append(text.substring(lastIndex, startPosition));
155:
156: // key is ${currentKey}
157: String currentKey = text.substring(startPosition + 2,
158: endPosition);
159: String value = (String) vars.get(currentKey);
160: if (value != null) {
161: // Substitute any var's in this variable value
162: value = replaceVar(value);
163:
164: // Append the new String
165: buffer.append(value);
166: lastIndex = endPosition + 1;
167: } else {
168: // append the ${ and start at the character after { to check more possible
169: // variables.
170: buffer.append(text.charAt(startPosition));
171: buffer.append(text.charAt(startPosition + 1));
172: lastIndex = startPosition + 2;
173: }
174: // Set the 'pointer' to the end of the oldString in 'text'
175: startPosition = text.indexOf("${", lastIndex);
176: }
177: // Append the final part
178: buffer.append(text.substring(lastIndex, textLength));
179: return buffer.toString();
180: }
181:
182: /**
183: * @param tableName
184: */
185: public void selectTable(String tableName) throws SAXException {
186: table = response.getTableWithID(tableName);
187: }
188:
189: public WebTable getTable() {
190: return table;
191: }
192:
193: public String getUrlPrefix() {
194: return urlPrefix;
195: }
196:
197: public void setUrlPrefix(String urlPrefix) {
198: this .urlPrefix = urlPrefix;
199: }
200:
201: public void enableJavaScript(boolean b) {
202: HttpUnitOptions.setScriptingEnabled(b);
203: }
204:
205: }
|