Revision: 650
          http://svn.sourceforge.net/jwebunit/?rev=650&view=rev
Author:   henryju
Date:     2007-01-11 01:23:17 -0800 (Thu, 11 Jan 2007)

Log Message:
-----------
Improve proxy support.
Temporary hack for ImmediateRefreshHandler until HtmlUnit official fix.

Modified Paths:
--------------
    
branches/1.x/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/JWebUnitAPITestCase.java
    
branches/1.x/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/util/RedirectServlet.java
    
branches/1.x/jwebunit-commons-tests/src/main/resources/testcases/RedirectionTest/redirect.html
    
branches/1.x/jwebunit-core/src/main/java/net/sourceforge/jwebunit/util/TestContext.java
    
branches/1.x/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitDialog.java

Added Paths:
-----------
    
branches/1.x/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/ImmediateRefreshHandler.java

Modified: 
branches/1.x/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/JWebUnitAPITestCase.java
===================================================================
--- 
branches/1.x/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/JWebUnitAPITestCase.java
   2007-01-11 08:54:31 UTC (rev 649)
+++ 
branches/1.x/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/JWebUnitAPITestCase.java
   2007-01-11 09:23:17 UTC (rev 650)
@@ -10,59 +10,61 @@
 import net.sourceforge.jwebunit.junit.WebTestCase;
 import net.sourceforge.jwebunit.tests.util.reflect.MethodInvoker;
 
-
 /**
- * This class is intended be used by all "testcase" classes that are
- * used to test the functionality of the jwebunit core api.  This isn't
- * to be extended by end users of the jwebunit api.
+ * This class is intended be used by all "testcase" classes that are used to 
test the functionality of the jwebunit core
+ * api. This isn't to be extended by end users of the jwebunit api.
  * 
  * @author Nicholas Neuberger
  */
 public abstract class JWebUnitAPITestCase extends WebTestCase {
 
     protected static final Object[] NOARGS = new Object[0];
-    
+
     public static final int JETTY_PORT = 8082;
-    
+
     public static final String JETTY_URL = "/jwebunit";
 
-       public static final String HOST_PATH = 
"http://localhost:"+JETTY_PORT+JETTY_URL;
-       
-       /**
-        * @param name
-        */
-       public JWebUnitAPITestCase(String name) {
-               super(name);
-       }
+    public static final String HOST_PATH = "http://localhost:"; + JETTY_PORT
+            + JETTY_URL;
 
-       /**
-        * 
-        */
-       public JWebUnitAPITestCase() {
-               super();
-       }
-       
-       public void setUp() throws Exception {
-               super.setUp();
-               getTestContext().setBaseUrl(HOST_PATH);
+    /**
+     * @param name
+     */
+    public JWebUnitAPITestCase(String name) {
+        super(name);
+    }
+
+    /**
+     * 
+     */
+    public JWebUnitAPITestCase() {
+        super();
+    }
+
+    public void setUp() throws Exception {
+        super.setUp();
+        getTestContext().setBaseUrl(HOST_PATH);
         getTestContext().setAuthorization("admin", "admin");
-       }
+    }
 
-    public void assertPassFail(String methodName, Object passArg, Object 
failArgs) throws Throwable {
-        assertPassFail(methodName, new Object[]{passArg}, new 
Object[]{failArgs});
+    public void assertPassFail(String methodName, Object passArg,
+            Object failArgs) throws Throwable {
+        assertPassFail(methodName, new Object[] { passArg },
+                new Object[] { failArgs });
     }
 
-    public void assertPassFail(String methodName, Object[] passArgs, Object[] 
failArgs) throws Throwable {
+    public void assertPassFail(String methodName, Object[] passArgs,
+            Object[] failArgs) throws Throwable {
         assertPass(methodName, passArgs);
         assertFail(methodName, failArgs);
     }
 
     protected void assertPass(String methodName, Object arg) throws Throwable {
-        this.assertPass(methodName, new Object[] {arg});
+        this.assertPass(methodName, new Object[] { arg });
     }
 
-
-    protected void assertPass(String methodName, Object[] args) throws 
Throwable {
+    protected void assertPass(String methodName, Object[] args)
+            throws Throwable {
         MethodInvoker invoker = new MethodInvoker(this, methodName, args);
         try {
             invoker.invoke();
@@ -72,21 +74,24 @@
     }
 
     public void assertFail(String methodName, Object arg) {
-        assertFail(methodName, new Object[]{arg});
+        assertFail(methodName, new Object[] { arg });
     }
 
     public void assertFail(String methodName, Object[] args) {
         assertException(AssertionFailedError.class, methodName, args);
     }
 
-    public void assertException(Class exceptionClass, String methodName, 
Object[] args) {
+    public void assertException(Class exceptionClass, String methodName,
+            Object[] args) {
         MethodInvoker invoker = new MethodInvoker(this, methodName, args);
         try {
             invoker.invoke();
-            fail("Expected test failure did not occur for method: " + 
methodName);
+            fail("Expected test failure did not occur for method: "
+                    + methodName);
         } catch (InvocationTargetException e) {
-            assertTrue("Expected " + exceptionClass.getName() + "but was " + 
e.getTargetException().getClass().getName(),
-                       exceptionClass.isInstance(e.getTargetException()));
+            assertTrue("Expected " + exceptionClass.getName() + "but was "
+                    + e.getTargetException().getClass().getName(),
+                    exceptionClass.isInstance(e.getTargetException()));
         } catch (Exception e) {
             e.printStackTrace();
             throw new RuntimeException(e.getMessage());

Modified: 
branches/1.x/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/util/RedirectServlet.java
===================================================================
--- 
branches/1.x/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/util/RedirectServlet.java
  2007-01-11 08:54:31 UTC (rev 649)
+++ 
branches/1.x/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/util/RedirectServlet.java
  2007-01-11 09:23:17 UTC (rev 650)
@@ -17,7 +17,7 @@
 
     protected void doGet(HttpServletRequest request,
             HttpServletResponse response) throws ServletException, IOException 
{
-        response.sendRedirect("/jwebunit/RedirectionTest/redirect.html");
+        response.sendRedirect("/jwebunit/RedirectionTest/redirected.html");
     }
 
 }

Modified: 
branches/1.x/jwebunit-commons-tests/src/main/resources/testcases/RedirectionTest/redirect.html
===================================================================
--- 
branches/1.x/jwebunit-commons-tests/src/main/resources/testcases/RedirectionTest/redirect.html
      2007-01-11 08:54:31 UTC (rev 649)
+++ 
branches/1.x/jwebunit-commons-tests/src/main/resources/testcases/RedirectionTest/redirect.html
      2007-01-11 09:23:17 UTC (rev 650)
@@ -1,9 +1,8 @@
 <html>
     <head>
-        <title>Redirected</title>
+        <title>Redirection</title>
+        <meta http-equiv='refresh' content='1800;url=redirected.html'>
     </head>
     <body>
-        <h1>The redirection works</h1>
-        <p>This page shows that the redirection works like expected.</p>
     </body>
 </html>
\ No newline at end of file

Modified: 
branches/1.x/jwebunit-core/src/main/java/net/sourceforge/jwebunit/util/TestContext.java
===================================================================
--- 
branches/1.x/jwebunit-core/src/main/java/net/sourceforge/jwebunit/util/TestContext.java
     2007-01-11 08:54:31 UTC (rev 649)
+++ 
branches/1.x/jwebunit-core/src/main/java/net/sourceforge/jwebunit/util/TestContext.java
     2007-01-11 09:23:17 UTC (rev 650)
@@ -46,13 +46,13 @@
 
     private Map requestHeaders = new HashMap();
 
-    private String proxyUser;
+    private String proxyUser = null;
 
-    private String proxyPasswd;
+    private String proxyPasswd = null;
 
-    private String proxyHost;
+    private String proxyHost = null;
 
-    private int proxyPort;
+    private int proxyPort = -1;
 
     private boolean hasProxyAuth = false;
 
@@ -107,8 +107,8 @@
     /**
      * Set proxy authentication information for the test context.
      * 
-     * @param user user name
-     * @param passwd password
+     * @param user user name (null if none)
+     * @param passwd password (null if none)
      * @param host proxy host name (null if applicable to any host).
      * @param port proxy port (negative if applicable to any port).
      */

Modified: 
branches/1.x/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitDialog.java
===================================================================
--- 
branches/1.x/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitDialog.java
   2007-01-11 08:54:31 UTC (rev 649)
+++ 
branches/1.x/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitDialog.java
   2007-01-11 09:23:17 UTC (rev 650)
@@ -14,7 +14,6 @@
 import com.gargoylesoftware.htmlunit.Page;
 import com.gargoylesoftware.htmlunit.PromptHandler;
 import com.gargoylesoftware.htmlunit.TextPage;
-import com.gargoylesoftware.htmlunit.ThreadedRefreshHandler;
 import com.gargoylesoftware.htmlunit.UnexpectedPage;
 import com.gargoylesoftware.htmlunit.WebClient;
 import com.gargoylesoftware.htmlunit.WebResponse;
@@ -625,12 +624,20 @@
     }
 
     private void initWebClient() {
-        wc = new WebClient(new BrowserVersion(BrowserVersion.INTERNET_EXPLORER,
-                "4.0", testContext.getUserAgent(), "1.2", 6));
+        
+        BrowserVersion bv = new 
BrowserVersion(BrowserVersion.INTERNET_EXPLORER,
+                "4.0", testContext.getUserAgent(), "1.2", 6);
+        if (getTestContext().getProxyHost()!=null && 
getTestContext().getProxyPort()>0) {
+            //Proxy
+            wc = new WebClient(bv, getTestContext().getProxyHost(), 
getTestContext().getProxyPort());
+        }
+        else {
+            wc = new WebClient(bv);
+        }
         wc.setJavaScriptEnabled(jsEnabled);
         wc.setThrowExceptionOnScriptError(true);
         wc.setRedirectEnabled(true);
-        // wc.setRefreshHandler(new ThreadedRefreshHandler());
+        wc.setRefreshHandler(new ImmediateRefreshHandler());
         DefaultCredentialsProvider creds = new DefaultCredentialsProvider();
         if (getTestContext().hasAuthorization()) {
             creds.addCredentials(getTestContext().getUser(), getTestContext()

Added: 
branches/1.x/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/ImmediateRefreshHandler.java
===================================================================
--- 
branches/1.x/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/ImmediateRefreshHandler.java
                          (rev 0)
+++ 
branches/1.x/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/ImmediateRefreshHandler.java
  2007-01-11 09:23:17 UTC (rev 650)
@@ -0,0 +1,55 @@
+/******************************************************************************
+ * jWebUnit project (http://jwebunit.sourceforge.net)                         *
+ * Distributed open-source, see full license under LICENCE.txt                *
+ 
******************************************************************************/
+package net.sourceforge.jwebunit.htmlunit;
+
+import java.io.IOException;
+import java.net.URL;
+
+import com.gargoylesoftware.htmlunit.Page;
+import com.gargoylesoftware.htmlunit.RefreshHandler;
+import com.gargoylesoftware.htmlunit.SubmitMethod;
+import com.gargoylesoftware.htmlunit.WebClient;
+import com.gargoylesoftware.htmlunit.WebRequestSettings;
+import com.gargoylesoftware.htmlunit.WebWindow;
+
+/**
+ * Custom Handler until HtmlUnit fix. Cf Bug 1628076
+ */
+public class ImmediateRefreshHandler implements RefreshHandler {
+
+    /**
+     * Immediately refreshes the specified page using the specified URL.
+     * 
+     * @param page The page that is going to be refreshed.
+     * @param url The URL where the new page will be loaded.
+     * @param seconds The number of seconds to wait before reloading the page 
(ignored!).
+     * @throws IOException if the refresh fails
+     */
+    public void handleRefresh(final Page page, final URL url, final int 
seconds)
+            throws IOException {
+        final WebWindow window = page.getEnclosingWindow();
+        if (window == null) {
+            return;
+        }
+        final WebClient client = window.getWebClient();
+        if (page.getWebResponse().getUrl().toExternalForm().equals(
+                url.toExternalForm())
+                && SubmitMethod.GET.equals(page.getWebResponse()
+                        .getRequestMethod())) {
+            if (seconds > 0) {
+                // Do not refresh
+                return;
+            } else {
+                final String msg = "Refresh Aborted by HtmlUnit: "
+                        + "Attempted to refresh a page using an 
ImmediateRefreshHandler "
+                        + "which could have caused an OutOfMemoryError "
+                        + "Please use WaitingRefreshHandler or 
ThreadedRefreshHandler instead.";
+                throw new RuntimeException(msg);
+            }
+        }
+        client.getPage(window, new WebRequestSettings(url));
+    }
+
+}


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Jwebunit-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jwebunit-development

Reply via email to