1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sourceforge.jwebunit.tests;
20
21 import net.sourceforge.jwebunit.tests.util.JettySetup;
22
23 import java.lang.reflect.InvocationTargetException;
24
25 import org.junit.After;
26 import org.junit.Before;
27
28 import net.sourceforge.jwebunit.junit.JWebUnit;
29 import net.sourceforge.jwebunit.tests.util.reflect.StaticMethodInvoker;
30
31 import static net.sourceforge.jwebunit.junit.JWebUnit.*;
32 import static org.junit.Assert.*;
33
34
35
36
37
38
39
40 public abstract class JWebUnitAPITestCase extends JettySetup {
41
42 protected static final Object[] NOARGS = new Object[0];
43
44 public static final int JETTY_PORT = 8082;
45
46 public static final String JETTY_URL = "/jwebunit";
47
48 public static final String HOST_PATH = "http://localhost:" + JETTY_PORT
49 + JETTY_URL;
50
51 @Before
52 public void setUp() throws Exception {
53 getTestContext().setBaseUrl(HOST_PATH);
54 getTestContext().setAuthorization("admin", "admin");
55 }
56
57 @After
58 public void closeBrowser() {
59 JWebUnit.closeBrowser();
60 }
61
62 public void assertPassFail(String methodName, Object passArg,
63 Object failArgs) throws Throwable {
64 assertPassFail(methodName, new Object[] { passArg },
65 new Object[] { failArgs });
66 }
67
68 public void assertPassFail(String methodName, Object[] passArgs,
69 Object[] failArgs) throws Throwable {
70 assertPass(methodName, passArgs);
71 assertFail(methodName, failArgs);
72 }
73
74 protected void assertPass(String methodName, Object arg) throws Throwable {
75 this.assertPass(methodName, new Object[] { arg });
76 }
77
78 protected void assertPass(String methodName, Object[] args)
79 throws Throwable {
80 StaticMethodInvoker invoker = new StaticMethodInvoker(JWebUnit.class, methodName, args);
81 try {
82 invoker.invoke();
83 } catch (InvocationTargetException e) {
84 throw e.getTargetException();
85 }
86 }
87
88 public void assertFail(String methodName, Object arg) {
89 assertFail(methodName, new Object[] { arg });
90 }
91
92 public void assertFail(String methodName, Object[] args) {
93 assertException(AssertionError.class, methodName, args);
94 }
95
96 public <G extends Throwable> G assertException(Class<G> exceptionClass, String methodName,
97 Object[] args) {
98 StaticMethodInvoker invoker = new StaticMethodInvoker(JWebUnit.class, methodName, args);
99 try {
100 invoker.invoke();
101 fail("Expected test failure did not occur for method: "
102 + methodName);
103 return null;
104 } catch (InvocationTargetException e) {
105 assertTrue("Expected " + exceptionClass.getName() + "but was "
106 + e.getTargetException().getClass().getName(),
107 exceptionClass.isInstance(e.getTargetException()));
108 return (G) e.getTargetException();
109 } catch (Exception e) {
110 e.printStackTrace();
111 throw new RuntimeException(e.getMessage());
112 }
113 }
114
115 }