Debian Codegraph @

src/test/java/org/fest/reflect/method/Method_method_Test.java

raw
1 /*
2 * Created on May 18, 2007
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
5 * License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
10 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
11 * governing permissions and limitations under the License.
12 *
13 * Copyright @2007-2009 the original author or authors.
14 */
15 package org.fest.reflect.method;
16
17 import static org.fest.reflect.util.ExpectedFailures.expectIllegalArgumentException;
18 import static org.fest.reflect.util.ExpectedFailures.expectNullPointerException;
19 import static org.fest.reflect.util.ExpectedFailures.expectReflectionError;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertFalse;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26
27 import java.util.List;
28
29 import org.junit.Before;
30 import org.junit.Test;
31
32 import org.fest.reflect.Jedi;
33 import org.fest.reflect.exception.ReflectionError;
34 import org.fest.reflect.reference.TypeRef;
35 import org.fest.test.CodeToTest;
36
37 /**
38 * Tests for the fluent interface for accessing methods.
39 *
40 * @author Yvonne Wang
41 * @author Alex Ruiz
42 */
43 public class Method_method_Test {
44
45 private Jedi jedi;
46
47 @Before
48 public void setUp() {
49 jedi = new Jedi("Luke");
50 }
51
52 @Test
53 public void should_throw_error_if_method_name_is_null() {
54 expectNullPointerException("The name of the method to access should not be null").on(new CodeToTest() {
55 public void run() {
56 MethodName.startMethodAccess(null);
57 }
58 });
59 }
60
61 @Test
62 public void should_throw_error_if_method_name_is_empty() {
63 expectIllegalArgumentException("The name of the method to access should not be empty").on(new CodeToTest() {
64 public void run() {
65 MethodName.startMethodAccess("");
66 }
67 });
68 }
69
70 @Test
71 public void should_throw_error_if_method_return_type_is_null() {
72 String msg = "The return type of the method to access should not be null";
73 expectNullPointerException(msg).on(new CodeToTest() {
74 public void run() {
75 MethodName.startMethodAccess("setName").withReturnType((Class<?>) null);
76 }
77 });
78 }
79
80 @Test
81 public void should_throw_error_if_method_return_TypeRef_is_null() {
82 String msg = "The return type reference of the method to access should not be null";
83 expectNullPointerException(msg).on(new CodeToTest() {
84 public void run() {
85 MethodName.startMethodAccess("setName").withReturnType((TypeRef<?>) null);
86 }
87 });
88 }
89
90 @Test
91 public void should_throw_error_if_method_parameter_array_is_null() {
92 String msg = "The array of parameter types for the method to access should not be null";
93 expectNullPointerException(msg).on(new CodeToTest() {
94 public void run() {
95 Class<?>[] parameterTypes = null;
96 MethodName.startMethodAccess("setName").withParameterTypes(parameterTypes);
97 }
98 });
99 }
100
101 @Test
102 public void should_throw_error_if_method_target_is_null() {
103 expectNullPointerException("Target should not be null").on(new CodeToTest() {
104 public void run() {
105 MethodName.startMethodAccess("setName").in(null);
106 }
107 });
108 }
109
110 @Test
111 public void should_call_method_with_args_and_no_return_value() {
112 MethodName.startMethodAccess("setName").withParameterTypes(String.class).in(jedi).invoke("Leia");
113 assertEquals("Leia", jedi.getName());
114 }
115
116 @Test
117 public void should_call_method_with_no_args_and_return_value() {
118 String personName = MethodName.startMethodAccess("getName").withReturnType(String.class).in(jedi).invoke();
119 assertEquals("Luke", personName);
120 }
121
122 @Test
123 public void should_call_method_with_args_and_return_value() {
124 jedi.addPower("healing");
125 String power = MethodName.startMethodAccess("powerAt").withReturnType(String.class).withParameterTypes(int.class).in(jedi)
126 .invoke(0);
127 assertEquals("healing", power);
128 }
129
130 @Test
131 public void should_call_method_with_no_args_and_return_TypeRef() {
132 jedi.addPower("jump");
133 List<String> powers = MethodName.startMethodAccess("powers").withReturnType(new TypeRef<List<String>>() {}).in(jedi).invoke();
134 assertEquals(1, powers.size());
135 assertEquals("jump", powers.get(0));
136 }
137
138 @Test
139 public void should_call_method_with_args_and_return_TypeRef() {
140 jedi.addPower("healing");
141 jedi.addPower("jump");
142 String method = "powersThatStartWith";
143 List<String> powers = MethodName.startMethodAccess(method).withReturnType(new TypeRef<List<String>>() {})
144 .withParameterTypes(String.class).in(jedi).invoke("ju");
145 assertEquals(1, powers.size());
146 assertEquals("jump", powers.get(0));
147 }
148
149 @Test
150 public void should_call_method_with_no_args_and_no_return_value() {
151 assertFalse(jedi.isMaster());
152 MethodName.startMethodAccess("makeMaster").in(jedi).invoke();
153 assertTrue(jedi.isMaster());
154 }
155
156 @Test
157 public void should_return_real_method() {
158 java.lang.reflect.Method method = MethodName.startMethodAccess("setName").withParameterTypes(String.class).in(jedi).info();
159 assertNotNull(method);
160 assertEquals("setName", method.getName());
161 Class<?>[] parameterTypes = method.getParameterTypes();
162 assertEquals(1, parameterTypes.length);
163 assertEquals(String.class, parameterTypes[0]);
164 }
165
166 @Test
167 public void should_throw_error_if_method_name_is_invalid() {
168 String message = "Unable to find method 'getAge' in org.fest.reflect.Jedi with parameter type(s) []";
169 expectReflectionError(message).on(new CodeToTest() {
170 public void run() {
171 String invalidName = "getAge";
172 MethodName.startMethodAccess(invalidName).withReturnType(Integer.class).in(jedi);
173 }
174 });
175 }
176
177 @Test
178 public void should_throw_error_if_args_for_method_are_invalid() {
179 expectIllegalArgumentException("argument type mismatch").on(new CodeToTest() {
180 public void run() {
181 int invalidArg = 8;
182 MethodName.startMethodAccess("setName").withParameterTypes(String.class).in(jedi).invoke(invalidArg);
183 }
184 });
185 }
186
187 @Test
188 public void should_rethrow_RuntimeException_thrown_by_method() {
189 try {
190 MethodName.startMethodAccess("throwRuntimeException").in(jedi).invoke();
191 fail("Expecting an IllegalStateException");
192 } catch (IllegalStateException e) {
193 assertEquals("Somehow I got in an illegal state", e.getMessage());
194 }
195 }
196
197 @Test
198 public void should_wrap_with_a_ReflectionError_the_checked_Exception_thrown_by_method() {
199 try {
200 MethodName.startMethodAccess("throwCheckedException").in(jedi).invoke();
201 fail("Expecting an ReflectionError");
202 } catch (ReflectionError e) {
203 Throwable cause = e.getCause();
204 assertTrue(cause instanceof Exception);
205 assertEquals("I don't know what's wrong", cause.getMessage());
206 }
207 }
208 }