Source Code Cross Referenced for ContextHelper.java in  » Testing » webtest » com » canoo » webtest » engine » 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 » webtest » com.canoo.webtest.engine 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        // Copyright © 2002-2007 Canoo Engineering AG, Switzerland.
002:        package com.canoo.webtest.engine;
003:
004:        import java.io.BufferedInputStream;
005:        import java.io.File;
006:        import java.io.FileOutputStream;
007:        import java.io.IOException;
008:        import java.io.InputStream;
009:        import java.io.OutputStream;
010:        import java.net.URL;
011:
012:        import org.apache.commons.io.IOUtils;
013:        import org.apache.log4j.Logger;
014:
015:        import com.canoo.webtest.boundary.HtmlUnitBoundary;
016:        import com.canoo.webtest.boundary.UrlBoundary;
017:        import com.gargoylesoftware.htmlunit.MockWebConnection;
018:        import com.gargoylesoftware.htmlunit.WebClient;
019:        import com.gargoylesoftware.htmlunit.WebConnection;
020:        import com.gargoylesoftware.htmlunit.WebResponse;
021:        import com.gargoylesoftware.htmlunit.WebWindow;
022:
023:        /**
024:         * Utility class for {@link Context}.
025:         */
026:        public final class ContextHelper {
027:            private static final Logger LOG = Logger
028:                    .getLogger(ContextHelper.class);
029:
030:            /**
031:             * Writes the response to the file. If an error occurs, just logs it and returns <code>null</code>.
032:             * @param response the response to write
033:             * @param file the file to write in
034:             * @return file if everything works fine, <code>null</code> if an error occured.
035:             */
036:            public static File writeResponseFile(final WebResponse response,
037:                    final File file) {
038:                OutputStream out = null;
039:                InputStream in = null;
040:
041:                try {
042:                    out = new FileOutputStream(file);
043:                    in = new BufferedInputStream(response.getContentAsStream());
044:                    LOG.debug("Writing current response in " + file.getName());
045:                    IOUtils.copy(in, out); // once we move to commons IO 1.1
046:                } catch (final IOException e) {
047:                    LOG.error("Failed writing current response to "
048:                            + file.getName() + ". Ignoring.", e);
049:                    return null;
050:                } finally {
051:                    IOUtils.closeQuietly(in);
052:                    IOUtils.closeQuietly(out);
053:                }
054:
055:                return file;
056:            }
057:
058:            /**
059:             *
060:             * @param context
061:             * @param responseBytes
062:             * @param contentType
063:             * @param urlStr
064:             */
065:            public static void defineAsCurrentResponse(final Context context,
066:                    final byte[] responseBytes, final String contentType,
067:                    final String urlStr) {
068:                final WebClient webClient = context.getWebClient();
069:                final WebConnection originalConnection = webClient
070:                        .getWebConnection();
071:                try {
072:                    final MockWebConnection mockConnection = new MockWebConnection(
073:                            webClient);
074:                    webClient.setWebConnection(mockConnection);
075:                    mockConnection.setDefaultResponse(responseBytes, 200, "ok",
076:                            contentType);
077:
078:                    // htmlUnit does't currently follow the "current window"
079:                    // check that our faked content will be loaded in what WebTest considers as the "current window"
080:                    // cf WT-293
081:                    final WebWindow currentTopWindow = context
082:                            .getCurrentResponse().getEnclosingWindow()
083:                            .getTopWindow();
084:                    // with the <previousResponse/> it may happen that the window is not registerd anymore
085:                    if (webClient.getWebWindows().contains(currentTopWindow)) {
086:                        webClient.setCurrentWindow(currentTopWindow);
087:                    }
088:
089:                    final URL url = UrlBoundary.tryCreateUrl(urlStr);
090:                    HtmlUnitBoundary.tryGetPage(url, webClient);
091:                } finally {
092:                    // remove special connection
093:                    webClient.setWebConnection(originalConnection);
094:                }
095:            }
096:
097:            /**
098:             *
099:             * @param context
100:             * @param responseText
101:             * @param contentType
102:             * @param urlStr
103:             */
104:            public static void defineAsCurrentResponse(final Context context,
105:                    final String responseText, final String contentType,
106:                    final String urlStr) {
107:                defineAsCurrentResponse(context, responseText.getBytes(),
108:                        contentType, urlStr);
109:            }
110:
111:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.