1 package net.sourceforge.jwebunit; 2 3 import java.lang.reflect.InvocationTargetException ; 4 5 import junit.framework.AssertionFailedError; 6 import net.sourceforge.jwebunit.util.reflect.MethodInvoker; 7 8 import com.meterware.pseudoserver.PseudoServlet; 9 10 16 public class JWebUnitTest extends WebTestCase { 17 18 protected static final Object [] NOARGS = new Object [0]; 19 protected String hostPath; 20 protected static TestServer server = new TestServer(); 21 22 public JWebUnitTest() { 23 } 24 25 public void setUp() throws Exception { 26 super.setUp(); 27 server.setUp(); 28 hostPath = "http://localhost:" + server.getConnectedPort(); 29 getTestContext().setBaseUrl(hostPath); 30 } 31 32 public void tearDown() throws Exception { 33 server.tearDown(); 34 } 35 36 protected void defineResource(String resourceName, String value) { 37 server.defineResource(resourceName, value); 38 } 39 40 protected void defineWebPage(String pageName, String body) { 41 defineResource(pageName + ".html", "<html><head><title>" + pageName + "</title></head>\n" + 42 "<body>" + body + "</body></html>"); 43 } 44 45 protected void defineResource(String resourceName, PseudoServlet servlet) { 46 server.defineResource(resourceName, servlet); 47 } 48 49 public void assertPassFail(String methodName, Object passArg, Object failArgs) throws Throwable { 50 assertPassFail(methodName, new Object []{passArg}, new Object []{failArgs}); 51 } 52 53 public void assertPassFail(String methodName, Object [] passArgs, Object [] failArgs) throws Throwable { 54 assertPass(methodName, passArgs); 55 assertFail(methodName, failArgs); 56 } 57 58 protected void assertPass(String methodName, Object arg) throws Throwable { 59 this.assertPass(methodName, new Object [] {arg}); 60 } 61 62 63 protected void assertPass(String methodName, Object [] args) throws Throwable { 64 MethodInvoker invoker = new MethodInvoker(this, methodName, args); 65 try { 66 invoker.invoke(); 67 } catch (InvocationTargetException e) { 68 throw e.getTargetException(); 69 } 70 } 71 72 public void assertFail(String methodName, Object arg) { 73 assertFail(methodName, new Object []{arg}); 74 } 75 76 public void assertFail(String methodName, Object [] args) { 77 assertException(AssertionFailedError.class, methodName, args); 78 } 79 80 public void assertException(Class exceptionClass, String methodName, Object [] args) { 81 MethodInvoker invoker = new MethodInvoker(this, methodName, args); 82 try { 83 invoker.invoke(); 84 fail("Expected test failure did not occur for method: " + methodName); 85 } catch (InvocationTargetException e) { 86 assertTrue("Expected " + exceptionClass.getName() + "but was " + e.getTargetException().getClass().getName(), 87 exceptionClass.isInstance(e.getTargetException())); 88 } catch (Exception e) { 89 e.printStackTrace(); 90 throw new RuntimeException (e.getMessage()); 91 } 92 } 93 94 protected void gotoResource(String url) { 95 96 } 97 } 98 | Popular Tags |