Convert to JUnit 4 from 3.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1166220 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
8b9dba5315
commit
9ed740b8bf
|
@ -16,23 +16,32 @@
|
|||
*/
|
||||
package org.apache.commons.lang3.reflect;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.apache.commons.lang3.math.NumberUtils;
|
||||
import org.apache.commons.lang3.mutable.Mutable;
|
||||
import org.apache.commons.lang3.mutable.MutableObject;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit tests MethodUtils
|
||||
* @version $Id$
|
||||
*/
|
||||
public class MethodUtilsTest extends TestCase {
|
||||
public class MethodUtilsTest {
|
||||
|
||||
private static interface PrivateInterface {}
|
||||
|
||||
|
@ -96,6 +105,10 @@ public class MethodUtilsTest extends TestCase {
|
|||
public String foo(Object o) {
|
||||
return "foo(Object)";
|
||||
}
|
||||
|
||||
public void oneParameter(String s) {
|
||||
// empty
|
||||
}
|
||||
}
|
||||
|
||||
private static class TestMutable implements Mutable<Object> {
|
||||
|
@ -108,24 +121,20 @@ public class MethodUtilsTest extends TestCase {
|
|||
}
|
||||
|
||||
private TestBean testBean;
|
||||
private Map<Class<?>, Class<?>[]> classCache;
|
||||
private Map<Class<?>, Class<?>[]> classCache = new HashMap<Class<?>, Class<?>[]>();
|
||||
|
||||
public MethodUtilsTest(String name) {
|
||||
super(name);
|
||||
classCache = new HashMap<Class<?>, Class<?>[]>();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
testBean = new TestBean();
|
||||
classCache.clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructor() throws Exception {
|
||||
assertNotNull(MethodUtils.class.newInstance());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvokeMethod() throws Exception {
|
||||
assertEquals("foo()", MethodUtils.invokeMethod(testBean, "foo",
|
||||
(Object[]) ArrayUtils.EMPTY_CLASS_ARRAY));
|
||||
|
@ -149,6 +158,7 @@ public class MethodUtilsTest extends TestCase {
|
|||
NumberUtils.DOUBLE_ONE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvokeExactMethod() throws Exception {
|
||||
assertEquals("foo()", MethodUtils.invokeExactMethod(testBean, "foo",
|
||||
(Object[]) ArrayUtils.EMPTY_CLASS_ARRAY));
|
||||
|
@ -185,6 +195,12 @@ public class MethodUtilsTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvokeExactMethodNoParam() throws Exception {
|
||||
assertEquals("foo()", MethodUtils.invokeExactMethod(testBean, "foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvokeStaticMethod() throws Exception {
|
||||
assertEquals("bar()", MethodUtils.invokeStaticMethod(TestBean.class,
|
||||
"bar", (Object[]) ArrayUtils.EMPTY_CLASS_ARRAY));
|
||||
|
@ -214,6 +230,7 @@ public class MethodUtilsTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvokeExactStaticMethod() throws Exception {
|
||||
assertEquals("bar()", MethodUtils.invokeExactStaticMethod(TestBean.class,
|
||||
"bar", (Object[]) ArrayUtils.EMPTY_CLASS_ARRAY));
|
||||
|
@ -251,8 +268,8 @@ public class MethodUtilsTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAccessibleInterfaceMethod() throws Exception {
|
||||
|
||||
Class<?>[][] p = { ArrayUtils.EMPTY_CLASS_ARRAY, null };
|
||||
for (Class<?>[] element : p) {
|
||||
Method method = TestMutable.class.getMethod("getValue", element);
|
||||
|
@ -262,6 +279,7 @@ public class MethodUtilsTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAccessibleMethodPrivateInterface() throws Exception {
|
||||
Method expected = TestBeanWithInterfaces.class.getMethod("foo");
|
||||
assertNotNull(expected);
|
||||
|
@ -269,6 +287,7 @@ public class MethodUtilsTest extends TestCase {
|
|||
assertNull(actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAccessibleInterfaceMethodFromDescription()
|
||||
throws Exception {
|
||||
Class<?>[][] p = { ArrayUtils.EMPTY_CLASS_ARRAY, null };
|
||||
|
@ -279,25 +298,29 @@ public class MethodUtilsTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAccessiblePublicMethod() throws Exception {
|
||||
assertSame(MutableObject.class, MethodUtils.getAccessibleMethod(
|
||||
MutableObject.class.getMethod("getValue",
|
||||
ArrayUtils.EMPTY_CLASS_ARRAY)).getDeclaringClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAccessiblePublicMethodFromDescription() throws Exception {
|
||||
assertSame(MutableObject.class, MethodUtils.getAccessibleMethod(
|
||||
MutableObject.class, "getValue", ArrayUtils.EMPTY_CLASS_ARRAY)
|
||||
.getDeclaringClass());
|
||||
}
|
||||
|
||||
public void testGetAccessibleMethodInaccessible() throws Exception {
|
||||
@Test
|
||||
public void testGetAccessibleMethodInaccessible() throws Exception {
|
||||
Method expected = TestBean.class.getDeclaredMethod("privateStuff");
|
||||
Method actual = MethodUtils.getAccessibleMethod(expected);
|
||||
assertNull(actual);
|
||||
}
|
||||
|
||||
public void testGetMatchingAccessibleMethod() throws Exception {
|
||||
@Test
|
||||
public void testGetMatchingAccessibleMethod() throws Exception {
|
||||
expectMatchingAccessibleMethodParameterTypes(TestBean.class, "foo",
|
||||
ArrayUtils.EMPTY_CLASS_ARRAY, ArrayUtils.EMPTY_CLASS_ARRAY);
|
||||
expectMatchingAccessibleMethodParameterTypes(TestBean.class, "foo",
|
||||
|
@ -378,7 +401,8 @@ public class MethodUtilsTest extends TestCase {
|
|||
public void testTwo(GrandParentObject obj) {}
|
||||
public void testTwo(ChildInterface obj) {}
|
||||
}
|
||||
interface ChildInterface {}
|
||||
|
||||
interface ChildInterface {}
|
||||
public static class GrandParentObject {}
|
||||
public static class ParentObject extends GrandParentObject {}
|
||||
public static class ChildObject extends ParentObject implements ChildInterface {}
|
||||
|
|
Loading…
Reference in New Issue