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:
Gary D. Gregory 2011-09-07 15:18:06 +00:00
parent 8b9dba5315
commit 9ed740b8bf
1 changed files with 40 additions and 16 deletions

View File

@ -16,23 +16,32 @@
*/ */
package org.apache.commons.lang3.reflect; 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.lang.reflect.Method;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import junit.framework.TestCase;
import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.math.NumberUtils; import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.lang3.mutable.Mutable; import org.apache.commons.lang3.mutable.Mutable;
import org.apache.commons.lang3.mutable.MutableObject; import org.apache.commons.lang3.mutable.MutableObject;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
/** /**
* Unit tests MethodUtils * Unit tests MethodUtils
* @version $Id$ * @version $Id$
*/ */
public class MethodUtilsTest extends TestCase { public class MethodUtilsTest {
private static interface PrivateInterface {} private static interface PrivateInterface {}
@ -96,6 +105,10 @@ public class MethodUtilsTest extends TestCase {
public String foo(Object o) { public String foo(Object o) {
return "foo(Object)"; return "foo(Object)";
} }
public void oneParameter(String s) {
// empty
}
} }
private static class TestMutable implements Mutable<Object> { private static class TestMutable implements Mutable<Object> {
@ -108,24 +121,20 @@ public class MethodUtilsTest extends TestCase {
} }
private TestBean testBean; private TestBean testBean;
private Map<Class<?>, Class<?>[]> classCache; private Map<Class<?>, Class<?>[]> classCache = new HashMap<Class<?>, Class<?>[]>();
public MethodUtilsTest(String name) { @Before
super(name); public void setUp() throws Exception {
classCache = new HashMap<Class<?>, Class<?>[]>();
}
@Override
protected void setUp() throws Exception {
super.setUp();
testBean = new TestBean(); testBean = new TestBean();
classCache.clear(); classCache.clear();
} }
@Test
public void testConstructor() throws Exception { public void testConstructor() throws Exception {
assertNotNull(MethodUtils.class.newInstance()); assertNotNull(MethodUtils.class.newInstance());
} }
@Test
public void testInvokeMethod() throws Exception { public void testInvokeMethod() throws Exception {
assertEquals("foo()", MethodUtils.invokeMethod(testBean, "foo", assertEquals("foo()", MethodUtils.invokeMethod(testBean, "foo",
(Object[]) ArrayUtils.EMPTY_CLASS_ARRAY)); (Object[]) ArrayUtils.EMPTY_CLASS_ARRAY));
@ -149,6 +158,7 @@ public class MethodUtilsTest extends TestCase {
NumberUtils.DOUBLE_ONE)); NumberUtils.DOUBLE_ONE));
} }
@Test
public void testInvokeExactMethod() throws Exception { public void testInvokeExactMethod() throws Exception {
assertEquals("foo()", MethodUtils.invokeExactMethod(testBean, "foo", assertEquals("foo()", MethodUtils.invokeExactMethod(testBean, "foo",
(Object[]) ArrayUtils.EMPTY_CLASS_ARRAY)); (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 { public void testInvokeStaticMethod() throws Exception {
assertEquals("bar()", MethodUtils.invokeStaticMethod(TestBean.class, assertEquals("bar()", MethodUtils.invokeStaticMethod(TestBean.class,
"bar", (Object[]) ArrayUtils.EMPTY_CLASS_ARRAY)); "bar", (Object[]) ArrayUtils.EMPTY_CLASS_ARRAY));
@ -214,6 +230,7 @@ public class MethodUtilsTest extends TestCase {
} }
} }
@Test
public void testInvokeExactStaticMethod() throws Exception { public void testInvokeExactStaticMethod() throws Exception {
assertEquals("bar()", MethodUtils.invokeExactStaticMethod(TestBean.class, assertEquals("bar()", MethodUtils.invokeExactStaticMethod(TestBean.class,
"bar", (Object[]) ArrayUtils.EMPTY_CLASS_ARRAY)); "bar", (Object[]) ArrayUtils.EMPTY_CLASS_ARRAY));
@ -251,8 +268,8 @@ public class MethodUtilsTest extends TestCase {
} }
} }
@Test
public void testGetAccessibleInterfaceMethod() throws Exception { public void testGetAccessibleInterfaceMethod() throws Exception {
Class<?>[][] p = { ArrayUtils.EMPTY_CLASS_ARRAY, null }; Class<?>[][] p = { ArrayUtils.EMPTY_CLASS_ARRAY, null };
for (Class<?>[] element : p) { for (Class<?>[] element : p) {
Method method = TestMutable.class.getMethod("getValue", element); Method method = TestMutable.class.getMethod("getValue", element);
@ -262,6 +279,7 @@ public class MethodUtilsTest extends TestCase {
} }
} }
@Test
public void testGetAccessibleMethodPrivateInterface() throws Exception { public void testGetAccessibleMethodPrivateInterface() throws Exception {
Method expected = TestBeanWithInterfaces.class.getMethod("foo"); Method expected = TestBeanWithInterfaces.class.getMethod("foo");
assertNotNull(expected); assertNotNull(expected);
@ -269,6 +287,7 @@ public class MethodUtilsTest extends TestCase {
assertNull(actual); assertNull(actual);
} }
@Test
public void testGetAccessibleInterfaceMethodFromDescription() public void testGetAccessibleInterfaceMethodFromDescription()
throws Exception { throws Exception {
Class<?>[][] p = { ArrayUtils.EMPTY_CLASS_ARRAY, null }; Class<?>[][] p = { ArrayUtils.EMPTY_CLASS_ARRAY, null };
@ -279,25 +298,29 @@ public class MethodUtilsTest extends TestCase {
} }
} }
@Test
public void testGetAccessiblePublicMethod() throws Exception { public void testGetAccessiblePublicMethod() throws Exception {
assertSame(MutableObject.class, MethodUtils.getAccessibleMethod( assertSame(MutableObject.class, MethodUtils.getAccessibleMethod(
MutableObject.class.getMethod("getValue", MutableObject.class.getMethod("getValue",
ArrayUtils.EMPTY_CLASS_ARRAY)).getDeclaringClass()); ArrayUtils.EMPTY_CLASS_ARRAY)).getDeclaringClass());
} }
@Test
public void testGetAccessiblePublicMethodFromDescription() throws Exception { public void testGetAccessiblePublicMethodFromDescription() throws Exception {
assertSame(MutableObject.class, MethodUtils.getAccessibleMethod( assertSame(MutableObject.class, MethodUtils.getAccessibleMethod(
MutableObject.class, "getValue", ArrayUtils.EMPTY_CLASS_ARRAY) MutableObject.class, "getValue", ArrayUtils.EMPTY_CLASS_ARRAY)
.getDeclaringClass()); .getDeclaringClass());
} }
public void testGetAccessibleMethodInaccessible() throws Exception { @Test
public void testGetAccessibleMethodInaccessible() throws Exception {
Method expected = TestBean.class.getDeclaredMethod("privateStuff"); Method expected = TestBean.class.getDeclaredMethod("privateStuff");
Method actual = MethodUtils.getAccessibleMethod(expected); Method actual = MethodUtils.getAccessibleMethod(expected);
assertNull(actual); assertNull(actual);
} }
public void testGetMatchingAccessibleMethod() throws Exception { @Test
public void testGetMatchingAccessibleMethod() throws Exception {
expectMatchingAccessibleMethodParameterTypes(TestBean.class, "foo", expectMatchingAccessibleMethodParameterTypes(TestBean.class, "foo",
ArrayUtils.EMPTY_CLASS_ARRAY, ArrayUtils.EMPTY_CLASS_ARRAY); ArrayUtils.EMPTY_CLASS_ARRAY, ArrayUtils.EMPTY_CLASS_ARRAY);
expectMatchingAccessibleMethodParameterTypes(TestBean.class, "foo", expectMatchingAccessibleMethodParameterTypes(TestBean.class, "foo",
@ -378,6 +401,7 @@ public class MethodUtilsTest extends TestCase {
public void testTwo(GrandParentObject obj) {} public void testTwo(GrandParentObject obj) {}
public void testTwo(ChildInterface obj) {} public void testTwo(ChildInterface obj) {}
} }
interface ChildInterface {} interface ChildInterface {}
public static class GrandParentObject {} public static class GrandParentObject {}
public static class ParentObject extends GrandParentObject {} public static class ParentObject extends GrandParentObject {}