Source Code Cross Referenced for WebTestContext.java in  » Testing » slimdog » org » jzonic » webtester » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » slimdog » org.jzonic.webtester 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.