KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > jwebunit > JWebUnitTest


1 package net.sourceforge.jwebunit;
2
3 import java.lang.reflect.InvocationTargetException JavaDoc;
4
5 import junit.framework.AssertionFailedError;
6 import net.sourceforge.jwebunit.util.reflect.MethodInvoker;
7
8 import com.meterware.pseudoserver.PseudoServlet;
9
10 /**
11  * Superclass for testing jWebUnit. Uses test utilities from httpunit's test package.
12  *
13  * @author Jim Weaver
14  * @author Wilkes Joiner
15  */

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