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: }
|