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  /*
20   * User: djoiner
21   * Date: Jul 19, 2002
22   * Time: 2:53:19 PM
23   */
24  package net.sourceforge.jwebunit.tests.util.reflect;
25  
26  import static org.junit.Assert.*;
27  
28  import java.lang.reflect.InvocationTargetException;
29  
30  import org.junit.Before;
31  import org.junit.Test;
32  
33  import net.sourceforge.jwebunit.tests.util.reflect.MethodInvoker;
34  
35  
36  public class MethodInvokerTest {
37      private Receiver receiver;
38  
39      @Before
40      public void setUp() throws Exception {
41          receiver = new Receiver();
42      }
43  
44      @Test
45      public void testNoArg() throws Exception {
46          MethodInvoker invoker = new MethodInvoker(receiver, "noArg");
47          invoker.invoke();
48          assertTrue(receiver.noArgCalled);
49      }
50  
51      @Test
52      public void testOneArg() throws Exception {
53          MethodInvoker invoker = new MethodInvoker(receiver, "oneArg", "anArg");
54          invoker.invoke();
55          assertTrue(receiver.oneArgCalled);
56      }
57  
58      @Test
59      public void testMultipleArgs() throws Exception {
60          MethodInvoker invoker = new MethodInvoker(receiver, "multiArg", new Object[]{"arg1", "arg2"});
61          invoker.invoke();
62          assertTrue(receiver.multiArgCalled);
63      }
64  
65      @Test(expected=NoSuchMethodException.class)
66      public void testNoMethod() throws IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
67          MethodInvoker invoker = new MethodInvoker(receiver, "noMethod");
68          invoker.invoke();
69      }
70  
71      @Test
72      public void testMethodThrowsException() throws Exception {
73          MethodInvoker invoker = new MethodInvoker(receiver, "throwRuntime");
74          try {
75              invoker.invoke();
76          } catch (InvocationTargetException e) {
77              assertEquals(e.getTargetException().getClass(), RuntimeException.class);
78          }
79      }
80  
81      @Test
82      public void testPrimitiveArgs() throws Exception {
83          MethodInvoker invoker = new MethodInvoker(receiver, "primitiveArg", new Integer(1));
84          invoker.invoke();
85          assertTrue(receiver.primitiveArgCalled);
86      }
87  
88      @Test
89      public void testAllPrimitives() throws Exception {
90          Object[] args = new Object[]{new Boolean(true), new Byte((byte) 1),
91                                       new Character('c'), new Double(1),
92                                       new Float(1), new Integer(1),
93                                       new Long(1), new Short((short) 1)};
94          MethodInvoker invoker = new MethodInvoker(receiver, "allPrimitives", args);
95          invoker.invoke();
96          assertTrue(receiver.allPrimitivesCalled);
97      }
98  
99      class Receiver {
100         private boolean noArgCalled;
101         private boolean oneArgCalled;
102         private boolean multiArgCalled;
103         private boolean primitiveArgCalled;
104         private boolean allPrimitivesCalled;
105 
106         public void noArg() {
107             noArgCalled = true;
108         }
109 
110         public void oneArg(String arg) {
111             oneArgCalled = true;
112         }
113 
114         public void multiArg(String arg1, String arg2) {
115             multiArgCalled = true;
116         }
117 
118         public void primitiveArg(int i) {
119             primitiveArgCalled = true;
120         }
121 
122         public void allPrimitives(boolean bool, byte b,
123                                   char c, double d,
124                                   float f, int i,
125                                   long l, short s) {
126             allPrimitivesCalled = true;
127         }
128 
129         public void throwRuntime() {
130             throw new RuntimeException();
131         }
132     }
133 }