From: <asa...@us...> - 2013-11-23 07:22:46
|
Revision: 8798 http://sourceforge.net/p/htmlunit/code/8798 Author: asashour Date: 2013-11-23 07:22:39 +0000 (Sat, 23 Nov 2013) Log Message: ----------- WebClient: .closeAllWindows() to delete all temporary files. Modified Paths: -------------- trunk/htmlunit/src/changes/changes.xml trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/WebResponse.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HTMLParser.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleSheet.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlPage4Test.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleSheet2Test.java Modified: trunk/htmlunit/src/changes/changes.xml =================================================================== --- trunk/htmlunit/src/changes/changes.xml 2013-11-22 20:29:54 UTC (rev 8797) +++ trunk/htmlunit/src/changes/changes.xml 2013-11-23 07:22:39 UTC (rev 8798) @@ -8,6 +8,9 @@ <body> <release version="2.14" date="???" description="Bugfixes"> + <action type="fix" dev="asashour"> + WebClient: .closeAllWindows() to delete all temporary files. + </action> <action type="add" dev="asashour"> JavaScript: add missing MessageEvent properties. </action> Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/WebResponse.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/WebResponse.java 2013-11-22 20:29:54 UTC (rev 8797) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/WebResponse.java 2013-11-23 07:22:39 UTC (rev 8798) @@ -146,13 +146,18 @@ * or <tt>null</tt> if none was specified */ public String getContentCharsetOrNull() { + InputStream is = null; try { - return EncodingSniffer.sniffEncoding(getResponseHeaders(), getContentAsStream()); + is = getContentAsStream(); + return EncodingSniffer.sniffEncoding(getResponseHeaders(), is); } catch (final IOException e) { LOG.warn("Error trying to sniff encoding.", e); return null; } + finally { + IOUtils.closeQuietly(is); + } } /** @@ -198,8 +203,9 @@ * @return the response content as a string */ public String getContentAsString(final String encoding) { + InputStream in = null; try { - final InputStream in = responseData_.getInputStream(); + in = responseData_.getInputStream(); if (null == in) { return null; } @@ -221,6 +227,9 @@ LOG.warn(e); return null; } + finally { + IOUtils.closeQuietly(in); + } } /** Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HTMLParser.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HTMLParser.java 2013-11-22 20:29:54 UTC (rev 8797) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HTMLParser.java 2013-11-23 07:22:39 UTC (rev 8798) @@ -40,6 +40,7 @@ import net.sourceforge.htmlunit.corejs.javascript.Scriptable; import net.sourceforge.htmlunit.corejs.javascript.ScriptableObject; +import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.apache.xerces.parsers.AbstractSAXParser; import org.apache.xerces.util.DefaultErrorHandler; @@ -248,6 +249,7 @@ throw new RuntimeException("Failed parsing content from " + url, origin); } finally { + IOUtils.closeQuietly(content); page.registerParsingEnd(); } Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java 2013-11-22 20:29:54 UTC (rev 8797) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java 2013-11-23 07:22:39 UTC (rev 8798) @@ -1143,6 +1143,7 @@ } final String scriptCode = response.getContentAsString(scriptEncoding); + response.cleanUp(); if (null != scriptCode) { final JavaScriptEngine javaScriptEngine = client.getJavaScriptEngine(); final Script script = javaScriptEngine.compile(this, scriptCode, url.toExternalForm(), 1); Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleSheet.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleSheet.java 2013-11-22 20:29:54 UTC (rev 8797) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleSheet.java 2013-11-23 07:22:39 UTC (rev 8798) @@ -322,6 +322,7 @@ source.setEncoding(response.getContentCharset()); sheet = new CSSStyleSheet(element, source, uri); cache.cacheIfPossible(request, response, sheet.getWrappedSheet()); + response.cleanUp(); } } catch (final FailingHttpStatusCodeException e) { Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlPage4Test.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlPage4Test.java 2013-11-22 20:29:54 UTC (rev 8797) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlPage4Test.java 2013-11-23 07:22:39 UTC (rev 8798) @@ -14,8 +14,11 @@ */ package com.gargoylesoftware.htmlunit.html; +import java.io.File; +import java.io.FilenameFilter; import java.io.IOException; import java.io.Writer; +import java.lang.reflect.Field; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; @@ -29,8 +32,13 @@ import org.junit.runner.RunWith; import com.gargoylesoftware.htmlunit.BrowserRunner; +import com.gargoylesoftware.htmlunit.BrowserRunner.Alerts; +import com.gargoylesoftware.htmlunit.CollectingAlertHandler; +import com.gargoylesoftware.htmlunit.HttpWebConnection; import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.WebServerTestCase; +import com.gargoylesoftware.htmlunit.javascript.host.html.HTMLBodyElement; +import com.gargoylesoftware.htmlunit.util.ServletContentWrapper; /** * Tests for {@link HtmlPage}. @@ -108,4 +116,99 @@ resp.getWriter().write(builder.toString()); } } + + /** + * @exception Exception if an error occurs + */ + @Test + @Alerts("hello") + public void bigJavaScript() throws Exception { + final StringBuilder html + = new StringBuilder("<html><head>\n" + + "<script src='two.js'></script>\n" + + "<link rel='stylesheet' type='text/css' href='three.css'/>\n" + + "</head>\n" + + "<body onload='test()'></body></html>"); + + final StringBuilder javaScript = new StringBuilder("function test() {\n" + + "alert('hello');\n" + + "}"); + + final StringBuilder css = new StringBuilder("body {color: blue}"); + + final Field field = HttpWebConnection.class.getDeclaredField("MAX_IN_MEMORY"); + field.setAccessible(true); + final long maxInMemory = (Long) field.get(null); + + for (int i = 0; i < maxInMemory; i++) { + html.append(' '); + javaScript.append(' '); + css.append(' '); + } + + BigJavaScriptServlet1.CONTENT_ = html.toString(); + BigJavaScriptServlet2.CONTENT_ = javaScript.toString(); + BigJavaScriptServlet3.CONTENT_ = css.toString(); + + final int initialTempFiles = getTempFiles(); + final Map<String, Class<? extends Servlet>> map = new HashMap<String, Class<? extends Servlet>>(); + map.put("/one.html", BigJavaScriptServlet1.class); + map.put("/two.js", BigJavaScriptServlet2.class); + map.put("/three.css", BigJavaScriptServlet3.class); + startWebServer(".", null, map); + final WebClient client = getWebClient(); + final CollectingAlertHandler alertHandler = new CollectingAlertHandler(); + client.setAlertHandler(alertHandler); + final HtmlPage page = client.getPage("http://localhost:" + PORT + "/one.html"); + ((HTMLBodyElement) page.getBody().getScriptObject()).getCurrentStyle(); + + assertEquals(getExpectedAlerts(), alertHandler.getCollectedAlerts()); + assertEquals(initialTempFiles + 1, getTempFiles()); + client.closeAllWindows(); + assertEquals(initialTempFiles, getTempFiles()); + } + + /** + * The HTML servlet for {@link #bigJavaScript()}. + */ + public static class BigJavaScriptServlet1 extends ServletContentWrapper { + private static String CONTENT_; + /** The constructor. */ + public BigJavaScriptServlet1() { + super(CONTENT_); + } + } + + /** + * The JavaScript servlet for {@link #bigJavaScript()}. + */ + public static class BigJavaScriptServlet2 extends ServletContentWrapper { + private static String CONTENT_; + /** The constructor. */ + public BigJavaScriptServlet2() { + super(CONTENT_); + } + } + + /** + * The CSS servlet for {@link #bigJavaScript()}. + */ + public static class BigJavaScriptServlet3 extends ServletContentWrapper { + private static String CONTENT_; + /** The constructor. */ + public BigJavaScriptServlet3() { + super(CONTENT_); + } + } + + private int getTempFiles() { + final File file = new File(System.getProperty("java.io.tmpdir")); + final String[] list = file.list(new FilenameFilter() { + @Override + public boolean accept(final File dir, final String name) { + return name.startsWith("htmlunit"); + } + }); + return list.length; + } } Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleSheet2Test.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleSheet2Test.java 2013-11-22 20:29:54 UTC (rev 8797) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleSheet2Test.java 2013-11-23 07:22:39 UTC (rev 8798) @@ -225,7 +225,7 @@ assertEquals(selectSpanS, sheet.selects(selector, page.getHtmlElementById("s"))); } -/** + /** * Test for 3325124. * @throws Exception if the test fails */ |