View Javadoc

1   /**
2    * Copyright (c) 2002-2015, JWebUnit team.
3    *
4    * This file is part of JWebUnit.
5    *
6    * JWebUnit is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Lesser General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * JWebUnit is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public License
17   * along with JWebUnit.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package net.sourceforge.jwebunit.htmlunit;
20  
21  import org.junit.After;
22  
23  import com.gargoylesoftware.htmlunit.ThreadedRefreshHandler;
24  import com.gargoylesoftware.htmlunit.WaitingRefreshHandler;
25  import net.sourceforge.jwebunit.tests.JWebUnitAPITestCase;
26  import org.junit.Test;
27  
28  import static net.sourceforge.jwebunit.junit.JWebUnit.beginAt;
29  import static net.sourceforge.jwebunit.junit.JWebUnit.getTestContext;
30  import static net.sourceforge.jwebunit.junit.JWebUnit.getTestingEngine;
31  import static net.sourceforge.jwebunit.junit.JWebUnit.setBaseUrl;
32  import static org.junit.Assert.assertTrue;
33  import static org.junit.Assert.fail;
34  
35  /**
36   * Unit test to validate JWebUnit's HtmlUnit plugin will now allow for custom
37   * RefreshHandler to be passed in prior to initialization, as well as changed on
38   * the fly.
39   * 
40   * @Date 09/03/2010
41   * @author Jason McSwain
42   */
43  public class RefreshHandlerTest extends JWebUnitAPITestCase {
44  
45      @Test
46  	public void testDefaultRefreshHandler() throws Exception {
47  		if (getTestingEngine() instanceof HtmlUnitTestingEngineImpl) {
48  			setBaseUrl(HOST_PATH + "/RefreshHandlerTest");
49  			getTestContext().setResourceBundleName("RefreshHandlerTest");
50  			try {
51  				beginAt("/testPage.html");
52  				fail("expected exception b/c page refreshes, but received no exception");
53  			} catch (RuntimeException re) {
54  				String msg = re.getMessage();
55  				assertTrue(
56  						"msg was not as expected.\n[" + msg + "]",
57  						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."));
58  			} catch (Exception e) {
59  				throw new Exception("received unexpected Exception.", e);
60  			}
61  		} else {
62  			System.out
63  					.println("[WARN] skipping test [testDefaultRefreshHandler] b/c it only applies to HtmlUnitTestEngineImpl");
64  		}
65  	}
66  
67      @Test
68  	public void testAlternateRefreshHandler() throws Exception {
69  		if (getTestingEngine() instanceof HtmlUnitTestingEngineImpl) {
70  			HtmlUnitTestingEngineImpl engine = (HtmlUnitTestingEngineImpl) getTestingEngine();			
71  			setBaseUrl(HOST_PATH + "/RefreshHandlerTest");
72  			getTestContext().setResourceBundleName("RefreshHandlerTest");
73  
74  			engine.setRefreshHandler(new ThreadedRefreshHandler());
75  			beginAt("/testPage.html");
76  
77  			assertTrue("refresh handler was not of expected type.["
78  					+ engine.getWebClient().getRefreshHandler().getClass()
79  							.getName() + "]", engine.getWebClient()
80  					.getRefreshHandler() instanceof ThreadedRefreshHandler);
81  
82  		} else {
83  			System.out
84  					.println("[WARN] skipping test [testAlternateRefreshHandler] b/c it only applies to HtmlUnitTestEngineImpl");
85  		}
86  	}
87  
88      @Test
89  	public void testChangeRefreshHandler() throws Exception {
90  		if (getTestingEngine() instanceof HtmlUnitTestingEngineImpl) {
91  			HtmlUnitTestingEngineImpl engine = (HtmlUnitTestingEngineImpl) getTestingEngine();
92  			setBaseUrl(HOST_PATH + "/RefreshHandlerTest");
93  			getTestContext().setResourceBundleName("RefreshHandlerTest");
94  
95  			engine.setRefreshHandler(new ThreadedRefreshHandler());
96  			beginAt("/testPage.html");
97  
98  			assertTrue("refresh handler was not of expected type.["
99  					+ engine.getWebClient().getRefreshHandler().getClass()
100 							.getName() + "]", engine.getWebClient()
101 					.getRefreshHandler() instanceof ThreadedRefreshHandler);
102 
103 			engine.setRefreshHandler(new WaitingRefreshHandler());
104 			assertTrue("refresh handler was not of expected type.["
105 					+ engine.getWebClient().getRefreshHandler().getClass()
106 							.getName() + "]", engine.getWebClient()
107 					.getRefreshHandler() instanceof WaitingRefreshHandler);
108 
109 		} else {
110 			System.out
111 					.println("[WARN] skipping test [testChangeRefreshHandler] b/c it only applies to HtmlUnitTestEngineImpl");
112 		}
113 	}
114     
115     @After
116     public void cleanup() {
117         if (getTestingEngine() instanceof HtmlUnitTestingEngineImpl) {
118             HtmlUnitTestingEngineImpl engine = (HtmlUnitTestingEngineImpl) getTestingEngine();
119             engine.setRefreshHandler(null);
120         }
121     }
122 
123 }