1 38 package com.gargoylesoftware.htmlunit; 39 40 import java.io.BufferedInputStream; 41 import java.io.File; 42 import java.io.FileInputStream; 43 import java.io.FileNotFoundException; 44 import java.io.IOException; 45 import java.io.InputStream; 46 import java.net.MalformedURLException; 47 import java.net.URL; 48 import java.util.List; 49 import java.util.ListIterator; 50 51 import junit.framework.AssertionFailedError; 52 53 import org.apache.commons.io.FileUtils; 54 import org.apache.commons.io.IOUtils; 55 import org.apache.commons.lang.StringUtils; 56 import org.apache.commons.logging.Log; 57 import org.apache.commons.logging.LogFactory; 58 59 import com.gargoylesoftware.base.testing.BaseTestCase; 60 import com.gargoylesoftware.htmlunit.html.HtmlPage; 61 62 72 public abstract class WebTestCase extends BaseTestCase { 73 74 public static final URL URL_FIRST; 75 76 77 public static final URL URL_SECOND; 78 79 80 public static final URL URL_THIRD; 81 82 83 public static final URL URL_GARGOYLE; 84 85 89 public static final String PROPERTY_GENERATE_TESTPAGES 90 = "com.gargoylesoftware.htmlunit.WebTestCase.GenerateTestpages"; 91 92 static { 93 try { 94 URL_FIRST = new URL("http://first"); 95 URL_SECOND = new URL("http://second"); 96 URL_THIRD = new URL("http://third"); 97 URL_GARGOYLE = new URL("http://www.gargoylesoftware.com/"); 98 } 99 catch( final MalformedURLException e ) { 100 throw new IllegalStateException("Unable to create url constants"); 102 } 103 } 104 108 public WebTestCase( final String name ) { 109 super( name ); 110 } 111 112 113 119 protected static final HtmlPage loadPage( final String html ) throws Exception { 120 return loadPage(html, null); 121 } 122 123 130 protected static final HtmlPage loadPage( final String html, final List collectedAlerts ) 131 throws Exception { 132 return loadPage(html,collectedAlerts, URL_GARGOYLE); 133 } 134 135 136 140 protected final Log getLog() { 141 return LogFactory.getLog(getClass()); 142 } 143 144 145 153 protected static final HtmlPage loadPage( final String html, final List collectedAlerts, final URL url ) 154 155 throws Exception { 156 157 final WebClient client = new WebClient(); 158 if( collectedAlerts != null ) { 159 client.setAlertHandler( new CollectingAlertHandler(collectedAlerts) ); 160 } 161 162 final MockWebConnection webConnection = new MockWebConnection( client ); 163 webConnection.setDefaultResponse( html ); 164 client.setWebConnection( webConnection ); 165 166 final HtmlPage page = (HtmlPage) client.getPage(url); 167 return page; 168 } 169 170 171 175 public static void assertNull( final Object object ) { 176 if( object != null ) { 177 throw new AssertionFailedError("Expected null but found ["+object+"]"); 178 } 179 } 180 181 188 public static InputStream getFileAsStream( final String fileName ) throws FileNotFoundException { 189 return new BufferedInputStream(new FileInputStream(getFileObject(fileName))); 190 } 191 192 201 public static File getFileObject( final String fileName ) throws FileNotFoundException { 202 final String localizedName = fileName.replace( '/', File.separatorChar ); 203 204 File file = new File(localizedName); 205 if( file.exists() == false ) { 206 file = new File("../../"+localizedName); 207 } 208 209 if( file.exists() == false ) { 210 try { 211 System.out.println("currentDir="+new File(".").getCanonicalPath()); 212 } 213 catch( final IOException e ) { 214 e.printStackTrace(); 215 } 216 throw new FileNotFoundException(localizedName); 217 } 218 return file; 219 } 220 221 228 protected void createTestPageForRealBrowserIfNeeded(final String content, final List expectedAlerts) 229 throws IOException { 230 final Log log = LogFactory.getLog(WebTestCase.class); 231 if (System.getProperty(PROPERTY_GENERATE_TESTPAGES) != null) { 232 234 String newContent = StringUtils.replace(content, 236 "alert(", "htmlunitReserved_catchedAlert("); 237 238 final String instrumentationJS = createInstrumentationScript(expectedAlerts); 239 240 newContent = StringUtils.replaceOnce(newContent, "<head>", "<head>" + instrumentationJS); 242 final String endScript = "\n<script>htmlunitReserved_addSummaryAfterOnload();</script>\n"; 243 if (newContent.indexOf("</body>") != -1) { 244 newContent = StringUtils.replaceOnce(newContent, "</body>", endScript + "</body>"); 245 } 246 else { 247 throw new RuntimeException("Currently only content with a <head> and a </body> is supported"); 248 } 249 250 final File f = File.createTempFile("test", ".html"); 251 FileUtils.writeStringToFile(f, newContent, "ISO-8859-1"); 252 log.info("Test file written: " + f.getAbsolutePath()); 253 } 254 else { 255 log.debug("System property \"" + PROPERTY_GENERATE_TESTPAGES 256 + "\" not set, don't generate test html page for real browser"); 257 } 258 } 259 260 265 private String createInstrumentationScript(final List expectedAlerts) throws IOException { 266 final InputStream is = getClass().getClassLoader().getResourceAsStream( 268 "com/gargoylesoftware/htmlunit/alertVerifier.js"); 269 final String baseJS = IOUtils.toString(is); 270 IOUtils.closeQuietly(is); 271 272 final StringBuffer sb = new StringBuffer(); 273 sb.append("\n<script type='text/javascript'>\n"); 274 sb.append("var htmlunitReserved_tab = ["); 275 for (final ListIterator iter = expectedAlerts.listIterator(); iter.hasNext();) 276 { 277 if (iter.hasPrevious()) { 278 sb.append(", "); 279 } 280 final String message = (String) iter.next(); 281 sb.append("{expected: \"").append(message).append("\"}"); 282 } 283 sb.append("];\n\n"); 284 sb.append(baseJS); 285 sb.append("</script>\n"); 286 return sb.toString(); 287 } 288 294 protected static final MockWebConnection getMockConnection(final HtmlPage page) { 295 return (MockWebConnection) page.getWebClient().getWebConnection(); 296 } 297 } 298 299 | Popular Tags |