Revision: 844
          http://jwebunit.svn.sourceforge.net/jwebunit/?rev=844&view=rev
Author:   henryju
Date:     2010-09-14 12:08:58 +0000 (Tue, 14 Sep 2010)

Log Message:
-----------
[1864365] Allow to change HtmlUnit refresh handler. Thanks to Jason McSwain for 
the patch.

Modified Paths:
--------------
    
trunk/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitTestingEngineImpl.java
    trunk/src/changes/changes.xml
    trunk/src/site/fml/faq.fml

Added Paths:
-----------
    
trunk/jwebunit-commons-tests/src/main/resources/testcases/RefreshHandlerTest/
    
trunk/jwebunit-commons-tests/src/main/resources/testcases/RefreshHandlerTest/testPage.html
    
trunk/jwebunit-htmlunit-plugin/src/test/java/net/sourceforge/jwebunit/htmlunit/RefreshHandlerTest.java

Added: 
trunk/jwebunit-commons-tests/src/main/resources/testcases/RefreshHandlerTest/testPage.html
===================================================================
--- 
trunk/jwebunit-commons-tests/src/main/resources/testcases/RefreshHandlerTest/testPage.html
                          (rev 0)
+++ 
trunk/jwebunit-commons-tests/src/main/resources/testcases/RefreshHandlerTest/testPage.html
  2010-09-14 12:08:58 UTC (rev 844)
@@ -0,0 +1,43 @@
+<!--
+
+    Copyright (c) 2010, JWebUnit team.
+
+    This file is part of JWebUnit.
+
+    JWebUnit is free software: you can redistribute it and/or modify
+    it under the terms of the GNU Lesser General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    JWebUnit is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public License
+    along with JWebUnit.  If not, see <http://www.gnu.org/licenses/>.
+
+-->
+
+<html>
+    <head>
+        <meta http-equiv="refresh" content="5">
+        <title>RefreshHandler - Test Page</title>
+    </head>
+    <body>
+        <p>Hello World</p>
+        <table>
+               <tr>
+                       <td>
+                       The Current Time is:
+                       </td>
+                       <td>
+                       <script type="text/javascript">
+                               var currentTime = new Date()
+                               document.write(currentTime);
+                       </script>
+                       </td>
+               </tr>
+        </table>        
+    </body>
+</html>

Modified: 
trunk/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitTestingEngineImpl.java
===================================================================
--- 
trunk/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitTestingEngineImpl.java
       2010-07-04 16:01:51 UTC (rev 843)
+++ 
trunk/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitTestingEngineImpl.java
       2010-09-14 12:08:58 UTC (rev 844)
@@ -68,6 +68,7 @@
 import com.gargoylesoftware.htmlunit.JavaScriptPage;
 import com.gargoylesoftware.htmlunit.Page;
 import com.gargoylesoftware.htmlunit.PromptHandler;
+import com.gargoylesoftware.htmlunit.RefreshHandler;
 import com.gargoylesoftware.htmlunit.TextPage;
 import com.gargoylesoftware.htmlunit.UnexpectedPage;
 import com.gargoylesoftware.htmlunit.WebClient;
@@ -117,6 +118,10 @@
     private final Logger logger = 
LoggerFactory.getLogger(HtmlUnitTestingEngineImpl.class);
 
     /**
+     * holder for alternative refresh handler.
+     */
+    private RefreshHandler refreshHandler;
+    /**
      * Encapsulate browser abilities.
      */
     private WebClient wc;
@@ -252,7 +257,7 @@
                // only throw exception if necessary
                if (!ignoreFailingStatusCodes) {
                    throw new TestingEngineResponseException(
-                           ex.getStatusCode(), ex);
+                           "unexpected status code ["+ex.getStatusCode()+"] at 
URL: ["+initialURL+"]", ex);
                }
         } catch (IOException ex) {
             throw new RuntimeException(ex);
@@ -806,7 +811,11 @@
         wc.setJavaScriptEnabled(jsEnabled);
         wc.setThrowExceptionOnScriptError(throwExceptionOnScriptError);
         wc.setRedirectEnabled(true);
-        wc.setRefreshHandler(new ImmediateRefreshHandler());
+        if (refreshHandler == null){
+               wc.setRefreshHandler(new ImmediateRefreshHandler());
+        } else {
+               wc.setRefreshHandler(refreshHandler);
+        }
         wc.setTimeout(timeout);
         DefaultCredentialsProvider creds = new DefaultCredentialsProvider();
         if (getTestContext().hasAuthorization()) {
@@ -2321,4 +2330,13 @@
                timeout = milliseconds;
        }
 
+       
+       public void setRefreshHandler(RefreshHandler handler){
+               this.refreshHandler = handler;
+               
+               if (wc != null){
+                       wc.setRefreshHandler(refreshHandler);
+               }
+       }
+       
 }

Added: 
trunk/jwebunit-htmlunit-plugin/src/test/java/net/sourceforge/jwebunit/htmlunit/RefreshHandlerTest.java
===================================================================
--- 
trunk/jwebunit-htmlunit-plugin/src/test/java/net/sourceforge/jwebunit/htmlunit/RefreshHandlerTest.java
                              (rev 0)
+++ 
trunk/jwebunit-htmlunit-plugin/src/test/java/net/sourceforge/jwebunit/htmlunit/RefreshHandlerTest.java
      2010-09-14 12:08:58 UTC (rev 844)
@@ -0,0 +1,126 @@
+/**
+ * Copyright (c) 2010, JWebUnit team.
+ *
+ * This file is part of JWebUnit.
+ *
+ * JWebUnit is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * JWebUnit is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with JWebUnit.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.sourceforge.jwebunit.htmlunit;
+
+import junit.framework.Assert;
+import junit.framework.AssertionFailedError;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import net.sourceforge.jwebunit.tests.JWebUnitAPITestCase;
+import net.sourceforge.jwebunit.tests.util.JettySetup;
+import net.sourceforge.jwebunit.htmlunit.HtmlUnitTestingEngineImpl;
+import com.gargoylesoftware.htmlunit.ThreadedRefreshHandler;
+import com.gargoylesoftware.htmlunit.WaitingRefreshHandler;
+
+/**
+ * Unit test to validate JWEbUnit's HtmlUnit plugin will now allow for custom
+ * RefreshHandler to be passed in prior to initialization, as well as changed 
on
+ * the fly.
+ * 
+ * @Date 09/03/2010
+ * @author Jason McSwain
+ */
+public class RefreshHandlerTest extends JWebUnitAPITestCase {
+       public RefreshHandlerTest(String name) {
+               super(name);
+       }
+
+       public static Test suite() {
+               return new JettySetup(new TestSuite(RefreshHandlerTest.class));
+       }
+
+       public void setUp() throws Exception {
+               super.setUp();
+       }
+
+       public void tearDown() throws Exception {
+               super.tearDown();
+       }
+
+       public void testDefaultRefreshHandler() throws Exception {
+               if (this.getTestingEngine() instanceof 
HtmlUnitTestingEngineImpl) {
+                       setBaseUrl(HOST_PATH + "/RefreshHandlerTest");
+                       
getTestContext().setResourceBundleName("RefreshHandlerTest");
+                       try {
+                               beginAt("/testPage.html");
+                               fail("expected exception b/c page refreshes, 
but received no exception");
+                       } catch (RuntimeException re) {
+                               String msg = re.getMessage();
+                               assertTrue(
+                                               "msg was not as expected.\n[" + 
msg + "]",
+                                               msg.endsWith("aborted by 
HtmlUnit: Attempted to refresh a page using an ImmediateRefreshHandler which 
could have caused an OutOfMemoryError Please use WaitingRefreshHandler or 
ThreadedRefreshHandler instead."));
+                       } catch (Exception e) {
+                               throw new Exception("received unexpected 
Exception.", e);
+                       }
+               } else {
+                       System.out
+                                       .println("[WARN] skipping test 
[testDefaultRefreshHandler] b/c it only applies to HtmlUnitTestEngineImpl");
+               }
+       }
+
+       public void testAlternateRefreshHandler() throws Exception {
+               if (this.getTestingEngine() instanceof 
HtmlUnitTestingEngineImpl) {
+                       HtmlUnitTestingEngineImpl engine = 
(HtmlUnitTestingEngineImpl) this
+                                       .getTestingEngine();                    
+                       setBaseUrl(HOST_PATH + "/RefreshHandlerTest");
+                       
getTestContext().setResourceBundleName("RefreshHandlerTest");
+
+                       engine.setRefreshHandler(new ThreadedRefreshHandler());
+                       beginAt("/testPage.html");
+
+                       assertTrue("refresh handler was not of expected type.["
+                                       + 
engine.getWebClient().getRefreshHandler().getClass()
+                                                       .getName() + "]", 
engine.getWebClient()
+                                       .getRefreshHandler() instanceof 
ThreadedRefreshHandler);
+
+               } else {
+                       System.out
+                                       .println("[WARN] skipping test 
[testAlternateRefreshHandler] b/c it only applies to HtmlUnitTestEngineImpl");
+               }
+       }
+
+       public void testChangeRefreshHandler() throws Exception {
+               if (this.getTestingEngine() instanceof 
HtmlUnitTestingEngineImpl) {
+                       HtmlUnitTestingEngineImpl engine = 
(HtmlUnitTestingEngineImpl) this
+                                       .getTestingEngine();
+                       setBaseUrl(HOST_PATH + "/RefreshHandlerTest");
+                       
getTestContext().setResourceBundleName("RefreshHandlerTest");
+
+                       engine.setRefreshHandler(new ThreadedRefreshHandler());
+                       beginAt("/testPage.html");
+
+                       assertTrue("refresh handler was not of expected type.["
+                                       + 
engine.getWebClient().getRefreshHandler().getClass()
+                                                       .getName() + "]", 
engine.getWebClient()
+                                       .getRefreshHandler() instanceof 
ThreadedRefreshHandler);
+
+                       engine.setRefreshHandler(new WaitingRefreshHandler());
+                       assertTrue("refresh handler was not of expected type.["
+                                       + 
engine.getWebClient().getRefreshHandler().getClass()
+                                                       .getName() + "]", 
engine.getWebClient()
+                                       .getRefreshHandler() instanceof 
WaitingRefreshHandler);
+
+               } else {
+                       System.out
+                                       .println("[WARN] skipping test 
[testChangeRefreshHandler] b/c it only applies to HtmlUnitTestEngineImpl");
+               }
+       }
+
+}

Modified: trunk/src/changes/changes.xml
===================================================================
--- trunk/src/changes/changes.xml       2010-07-04 16:01:51 UTC (rev 843)
+++ trunk/src/changes/changes.xml       2010-09-14 12:08:58 UTC (rev 844)
@@ -28,6 +28,11 @@
         </author>
     </properties>
     <body>
+        <release version="2.5" date="UNKNOW" description="">
+            <action type="add" dev="henryju" issue="1864365" due-to="Jason 
McSwain">
+                Added the ability to change RefreshHandler in 
HtmlUnitTestingEngine.
+            </action>
+        </release>
         <release version="2.4" date="July 4, 2010" description="Licensing 
change">
             <action type="update" dev="henryju">
                 License was changed to LGPL v3

Modified: trunk/src/site/fml/faq.fml
===================================================================
--- trunk/src/site/fml/faq.fml  2010-07-04 16:01:51 UTC (rev 843)
+++ trunk/src/site/fml/faq.fml  2010-09-14 12:08:58 UTC (rev 844)
@@ -73,4 +73,20 @@
     </faq>
   </part>
   
+    <part id="HtmlUnit">
+    <title>HtmlUnit testing engine</title>
+   
+    <faq id="htmlunit-change-refreshhandler">
+      <question>
+        How can I change HtmlUnit refresh handler (default is 
ImmediateRefreshHandler)?
+      </question>
+      <answer>
+        <p>Warning: this is not something easily portable to other testing 
engine.</p>
+       <source>if (getTestingEngine() instanceof HtmlUnitTestingEngineImpl) {
+       HtmlUnitTestingEngineImpl engine = (HtmlUnitTestingEngineImpl) 
getTestingEngine();                      
+       engine.setRefreshHandler(new ThreadedRefreshHandler());
+}</source>
+      </answer>
+    </faq>
+  </part>
 </faqs>


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

------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
JWebUnit-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jwebunit-development

Reply via email to