Coverage Report - org.webslinger.httpunit.WebslingerTestCase
 
Classes in this File Line Coverage Branch Coverage Complexity
WebslingerTestCase
75%
54/72
100%
8/8
0
 
 1  
 package org.webslinger.httpunit;
 2  
 
 3  
 import java.io.File;
 4  
 import java.util.Hashtable;
 5  
 import java.util.regex.Matcher;
 6  
 import java.util.regex.Pattern;
 7  
 
 8  
 import com.meterware.servletunit.ServletRunner;
 9  
 import com.meterware.servletunit.ServletUnitClient;
 10  
 import com.meterware.httpunit.ClientProperties;
 11  
 
 12  
 import junit.framework.TestCase;
 13  
 
 14  
 import org.webslinger.containers.TestContainer;
 15  
 import org.webslinger.servlet.WebslingerServlet;
 16  
 import org.webslinger.servlet.WebslingerServletContextFactoryHardcoded;
 17  
 
 18  
 import com.meterware.httpunit.HttpNotFoundException;
 19  
 import com.meterware.httpunit.WebResponse;
 20  
 
 21  
 public abstract class WebslingerTestCase extends TestContainer.TestContainerTestCase {
 22  
     protected ServletRunner runner;
 23  
     protected ServletUnitClient suc;
 24  
     protected ClientProperties cp;
 25  
 
 26  
     protected WebslingerTestCase(String name) {
 27  31
         super(name);
 28  31
     }
 29  
 
 30  
     protected Hashtable<String, String> getBaseServletParams() throws Exception {
 31  26
         Hashtable<String, String> params = new Hashtable<String, String>();
 32  26
         params.put(WebslingerServlet.class.getName() + ".WebslingerServletContextFactory", WebslingerServletContextFactoryHardcoded.class.getName());
 33  26
         return params;
 34  
     }
 35  
 
 36  
     protected Hashtable<String, String> getServletParams(File testLocation, File moduleRoot) throws Exception {
 37  26
         Hashtable<String, String> params = getBaseServletParams();
 38  26
         params.put("mount-path", testLocation.toString());
 39  26
         params.put("module-base", moduleRoot.toString());
 40  26
         return params;
 41  
     }
 42  
 
 43  
     protected void setUp() throws Exception {
 44  26
         super.setUp();
 45  26
         runner = new ServletRunner();
 46  26
         Hashtable<String, String> params = getServletParams(getTestLocation(), getModuleRoot());
 47  26
         runner.registerServlet("/*", WebslingerServlet.class.getName(), params);
 48  26
         suc = runner.newClient();
 49  26
         cp = suc.getClientProperties();
 50  26
         cp.setAcceptGzip(false);
 51  26
         cp.setAutoRedirect(false);
 52  26
         cp.setAutoRefresh(false);
 53  26
         cp.setIframeSupported(false);
 54  26
         cp.setUserAgent("webslinger-test-runner/0.0");
 55  26
     }
 56  
 
 57  
     protected void tearDown() throws Exception {
 58  26
         runner.shutDown();
 59  26
         super.tearDown();
 60  26
     }
 61  
 
 62  
     public void runSimpleTest(String path, boolean exists, String contentType) {
 63  
         try {
 64  154
             suc.getResponse("http://localhost" + path);
 65  123
             if (!exists) fail("HttpNotFoundException was *not* thrown for " + path);
 66  31
         } catch (HttpNotFoundException e) {
 67  
             // FIXME httpunit doesn't allow for fetching the text from an error page
 68  31
             if (!exists) return;
 69  0
             fail("Couldn't find " + path);
 70  0
         } catch (RuntimeException e) {
 71  0
             throw e;
 72  0
         } catch (Exception e) {
 73  0
             e.printStackTrace();
 74  0
             fail("Caught exception fetching " + path);
 75  123
         }
 76  123
         WebResponse res = suc.getCurrentPage();
 77  
         try {
 78  123
             if (contentType != null) assertEquals(path + " content type", contentType, res.getContentType());
 79  0
         } catch (RuntimeException e) {
 80  0
             throw e;
 81  0
         } catch (Exception e) {
 82  0
             fail("Caught exception fetching " + path);
 83  123
         }
 84  123
     }
 85  
 
 86  
     public void runSimpleTest(String path, boolean exists, String text, String contentType) {
 87  136
         runSimpleTest(path, exists, contentType);
 88  136
         if (!exists) return;
 89  113
         WebResponse res = suc.getCurrentPage();
 90  
         try {
 91  
             //System.err.println("{" + res.getText() + "}" + res.getContentType());
 92  113
             if (text != null) assertEquals(path + " content", text, res.getText());
 93  0
         } catch (RuntimeException e) {
 94  0
             throw e;
 95  0
         } catch (Exception e) {
 96  0
             fail("Caught exception fetching " + path);
 97  113
         }
 98  113
     }
 99  
 
 100  
     public String[] runSimpleTest(String path, boolean exists, Pattern pattern, String contentType) {
 101  10
         runSimpleTest(path, exists, contentType);
 102  10
         if (!exists) return null;
 103  10
         WebResponse res = suc.getCurrentPage();
 104  10
         String text = null;
 105  
         try {
 106  
             //System.err.println("{" + res.getText() + "}" + res.getContentType());
 107  10
             text = res.getText();
 108  0
         } catch (RuntimeException e) {
 109  0
             throw e;
 110  0
         } catch (Exception e) {
 111  0
             fail("Caught exception fetching " + path);
 112  10
         }
 113  10
         Matcher matcher = pattern.matcher(text);
 114  10
         if (!matcher.matches()) fail(path + " content(" + pattern.toString() + "), got(" + text + ")");
 115  10
         String[] parts = new String[matcher.groupCount()];
 116  170
         for (int i = 0; i < parts.length; i++) {
 117  160
             parts[i] = matcher.group(i + 1);
 118  
         }
 119  10
         return parts;
 120  
     }
 121  
 
 122  
     public void runSimpleTest(String path, String text, String contentType) {
 123  53
         runSimpleTest(path, true, text, contentType);
 124  53
     }
 125  
 
 126  
     public String[] runSimpleTest(String path, Pattern pattern, String contentType) {
 127  10
         return runSimpleTest(path, true, pattern, contentType);
 128  
     }
 129  
 }