View Javadoc

1   /**
2    * Copyright (c) 2002-2015, JWebUnit team.
3    *
4    * This file is part of JWebUnit.
5    *
6    * JWebUnit is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Lesser General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * JWebUnit is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public License
17   * along with JWebUnit.  If not, see <http://www.gnu.org/licenses/>.
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   * This class is intended be used by all "testcase" classes that are used to test the functionality of the jwebunit core
36   * api. This isn't to be extended by end users of the jwebunit api.
37   *
38   * @author Nicholas Neuberger
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; //never called
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 }