| 1 |
/* |
| 2 |
* Created on Nov 23, 2009 |
| 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 @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 |
|
| 23 |
import java.util.List; |
| 24 |
|
| 25 |
import org.junit.Before; |
| 26 |
import org.junit.Test; |
| 27 |
|
| 28 |
import org.fest.reflect.Jedi; |
| 29 |
import org.fest.reflect.reference.TypeRef; |
| 30 |
import org.fest.test.CodeToTest; |
| 31 |
|
| 32 |
/** |
| 33 |
* Tests for the fluent interface for accessing static methods. |
| 34 |
* |
| 35 |
* @author Yvonne Wang |
| 36 |
* @author Alex Ruiz |
| 37 |
*/ |
| 38 |
public class Method_staticMethod_Test { |
| 39 |
|
| 40 |
@Before |
| 41 |
public void setUp() { |
| 42 |
Jedi.clearCommonPowers(); |
| 43 |
} |
| 44 |
|
| 45 |
@Test |
| 46 |
public void should_throw_error_if_static_method_name_is_null() { |
| 47 |
expectNullPointerException("The name of the static method to access should not be null").on(new CodeToTest() { |
| 48 |
public void run() { |
| 49 |
StaticMethodName.startStaticMethodAccess(null); |
| 50 |
} |
| 51 |
}); |
| 52 |
} |
| 53 |
|
| 54 |
@Test |
| 55 |
public void should_throw_error_if_static_method_name_is_empty() { |
| 56 |
expectIllegalArgumentException("The name of the static method to access should not be empty").on(new CodeToTest() { |
| 57 |
public void run() { |
| 58 |
StaticMethodName.startStaticMethodAccess(""); |
| 59 |
} |
| 60 |
}); |
| 61 |
} |
| 62 |
|
| 63 |
@Test |
| 64 |
public void should_throw_error_if_static_method_return_type_is_null() { |
| 65 |
String msg = "The return type of the static method to access should not be null"; |
| 66 |
expectNullPointerException(msg).on(new CodeToTest() { |
| 67 |
public void run() { |
| 68 |
StaticMethodName.startStaticMethodAccess("commonPowerCount").withReturnType((Class<?>) null); |
| 69 |
} |
| 70 |
}); |
| 71 |
} |
| 72 |
|
| 73 |
@Test |
| 74 |
public void should_throw_error_if_static_method_return_TypeRef_is_null() { |
| 75 |
String msg = "The return type reference of the static method to access should not be null"; |
| 76 |
expectNullPointerException(msg).on(new CodeToTest() { |
| 77 |
public void run() { |
| 78 |
StaticMethodName.startStaticMethodAccess("commonPowerCount").withReturnType((TypeRef<?>) null); |
| 79 |
} |
| 80 |
}); |
| 81 |
} |
| 82 |
|
| 83 |
@Test |
| 84 |
public void should_throw_error_if_static_method_parameter_array_is_null() { |
| 85 |
String msg = "The array of parameter types for the static method to access should not be null"; |
| 86 |
expectNullPointerException(msg).on(new CodeToTest() { |
| 87 |
public void run() { |
| 88 |
Class<?>[] parameterTypes = null; |
| 89 |
StaticMethodName.startStaticMethodAccess("commonPowerCount").withParameterTypes(parameterTypes); |
| 90 |
} |
| 91 |
}); |
| 92 |
} |
| 93 |
|
| 94 |
@Test |
| 95 |
public void should_throw_error_if_static_method_target_is_null() { |
| 96 |
expectNullPointerException("Target should not be null").on(new CodeToTest() { |
| 97 |
public void run() { |
| 98 |
StaticMethodName.startStaticMethodAccess("commonPowerCount").in(null); |
| 99 |
} |
| 100 |
}); |
| 101 |
} |
| 102 |
|
| 103 |
@Test |
| 104 |
public void should_call_static_method_with_no_args_and_return_value() { |
| 105 |
Jedi.addCommonPower("Jump"); |
| 106 |
int count = StaticMethodName.startStaticMethodAccess("commonPowerCount").withReturnType(int.class).in(Jedi.class).invoke(); |
| 107 |
assertEquals(Jedi.commonPowerCount(), count); |
| 108 |
} |
| 109 |
|
| 110 |
@Test |
| 111 |
public void should_call_static_method_with_args_and_return_value() { |
| 112 |
Jedi.addCommonPower("Jump"); |
| 113 |
String power = StaticMethodName.startStaticMethodAccess("commonPowerAt").withReturnType(String.class) |
| 114 |
.withParameterTypes(int.class).in(Jedi.class).invoke(0); |
| 115 |
assertEquals("Jump", power); |
| 116 |
} |
| 117 |
|
| 118 |
@Test |
| 119 |
public void should_call_static_method_with_no_args_and_return_TypeRef() { |
| 120 |
Jedi.addCommonPower("jump"); |
| 121 |
String method = "commonPowers"; |
| 122 |
List<String> powers = StaticMethodName.startStaticMethodAccess(method).withReturnType(new TypeRef<List<String>>() {}) |
| 123 |
.in(Jedi.class).invoke(); |
| 124 |
assertEquals(1, powers.size()); |
| 125 |
assertEquals("jump", powers.get(0)); |
| 126 |
} |
| 127 |
|
| 128 |
@Test |
| 129 |
public void should_call_static_method_with_args_and_return_TypeRef() { |
| 130 |
Jedi.addCommonPower("jump"); |
| 131 |
String method = "commonPowersThatStartWith"; |
| 132 |
List<String> powers = StaticMethodName.startStaticMethodAccess(method).withReturnType(new TypeRef<List<String>>() {}) |
| 133 |
.withParameterTypes(String.class).in(Jedi.class).invoke("ju"); |
| 134 |
assertEquals(1, powers.size()); |
| 135 |
assertEquals("jump", powers.get(0)); |
| 136 |
} |
| 137 |
|
| 138 |
@Test |
| 139 |
public void should_call_static_method_with_args_and_no_return_value() { |
| 140 |
StaticMethodName.startStaticMethodAccess("addCommonPower").withParameterTypes(String.class).in(Jedi.class).invoke("Jump"); |
| 141 |
assertEquals("Jump", Jedi.commonPowerAt(0)); |
| 142 |
} |
| 143 |
|
| 144 |
@Test |
| 145 |
public void should_call_static_method_with_no_args_and_no_return_value() { |
| 146 |
Jedi.addCommonPower("Jump"); |
| 147 |
assertEquals(1, Jedi.commonPowerCount()); |
| 148 |
assertEquals("Jump", Jedi.commonPowerAt(0)); |
| 149 |
StaticMethodName.startStaticMethodAccess("clearCommonPowers").in(Jedi.class).invoke(); |
| 150 |
assertEquals(0, Jedi.commonPowerCount()); |
| 151 |
} |
| 152 |
|
| 153 |
@Test |
| 154 |
public void should_throw_error_if_static_method_name_is_invalid() { |
| 155 |
String message = "Unable to find method 'powerSize' in org.fest.reflect.Jedi with parameter type(s) []"; |
| 156 |
expectReflectionError(message).on(new CodeToTest() { |
| 157 |
public void run() { |
| 158 |
String invalidName = "powerSize"; |
| 159 |
StaticMethodName.startStaticMethodAccess(invalidName).in(Jedi.class); |
| 160 |
} |
| 161 |
}); |
| 162 |
} |
| 163 |
|
| 164 |
@Test |
| 165 |
public void should_throw_error_if_args_for_static_method_are_invalid() { |
| 166 |
expectIllegalArgumentException("argument type mismatch").on(new CodeToTest() { |
| 167 |
public void run() { |
| 168 |
int invalidArg = 8; |
| 169 |
StaticMethodName.startStaticMethodAccess("addCommonPower").withParameterTypes(String.class).in(Jedi.class) |
| 170 |
.invoke(invalidArg); |
| 171 |
} |
| 172 |
}); |
| 173 |
} |
| 174 |
} |