|
|
|
|
@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
|
|
|
|
|
/* |
|
|
|
|
* Copyright 2002-2012 the original author or authors. |
|
|
|
|
* Copyright 2002-2014 the original author or authors. |
|
|
|
|
* |
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
|
@ -16,24 +16,21 @@
@@ -16,24 +16,21 @@
|
|
|
|
|
|
|
|
|
|
package org.springframework.test.util; |
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.*; |
|
|
|
|
import static org.springframework.test.util.ReflectionTestUtils.*; |
|
|
|
|
|
|
|
|
|
import org.junit.Ignore; |
|
|
|
|
import org.junit.Test; |
|
|
|
|
|
|
|
|
|
import org.springframework.test.AssertThrows; |
|
|
|
|
import org.springframework.test.util.subpackage.Component; |
|
|
|
|
import org.springframework.test.util.subpackage.LegacyEntity; |
|
|
|
|
import org.springframework.test.util.subpackage.Person; |
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.*; |
|
|
|
|
import static org.springframework.test.util.ReflectionTestUtils.*; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Unit tests for {@link ReflectionTestUtils}. |
|
|
|
|
* |
|
|
|
|
* @author Sam Brannen |
|
|
|
|
* @author Juergen Hoeller |
|
|
|
|
*/ |
|
|
|
|
@SuppressWarnings("deprecation") |
|
|
|
|
public class ReflectionTestUtilsTests { |
|
|
|
|
|
|
|
|
|
private static final Float PI = new Float((float) 22 / 7); |
|
|
|
|
@ -43,11 +40,7 @@ public class ReflectionTestUtilsTests {
@@ -43,11 +40,7 @@ public class ReflectionTestUtilsTests {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void setAndGetFields() throws Exception { |
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
|
// Standard
|
|
|
|
|
|
|
|
|
|
public void setFieldForStandardUseCases() throws Exception { |
|
|
|
|
setField(person, "id", new Long(99), long.class); |
|
|
|
|
setField(person, "name", "Tom"); |
|
|
|
|
setField(person, "age", new Integer(42)); |
|
|
|
|
@ -68,10 +61,10 @@ public class ReflectionTestUtilsTests {
@@ -68,10 +61,10 @@ public class ReflectionTestUtilsTests {
|
|
|
|
|
assertEquals("blue", getField(person, "eyeColor")); |
|
|
|
|
assertEquals(Boolean.TRUE, getField(person, "likesPets")); |
|
|
|
|
assertEquals(PI, getField(person, "favoriteNumber")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
|
// Null - non-primitives
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void setFieldWithNullValuesForNonPrimitives() throws Exception { |
|
|
|
|
setField(person, "name", null, String.class); |
|
|
|
|
setField(person, "eyeColor", null, String.class); |
|
|
|
|
setField(person, "favoriteNumber", null, Number.class); |
|
|
|
|
@ -79,36 +72,21 @@ public class ReflectionTestUtilsTests {
@@ -79,36 +72,21 @@ public class ReflectionTestUtilsTests {
|
|
|
|
|
assertNull("name (protected field)", person.getName()); |
|
|
|
|
assertNull("eye color (package private field)", person.getEyeColor()); |
|
|
|
|
assertNull("'favorite number' (package field)", person.getFavoriteNumber()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
|
// Null - primitives
|
|
|
|
|
|
|
|
|
|
new AssertThrows(IllegalArgumentException.class, |
|
|
|
|
"Calling setField() with NULL for a primitive type should throw an IllegalArgumentException.") { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void test() throws Exception { |
|
|
|
|
setField(person, "id", null, long.class); |
|
|
|
|
} |
|
|
|
|
}.runTest(); |
|
|
|
|
|
|
|
|
|
new AssertThrows(IllegalArgumentException.class, |
|
|
|
|
"Calling setField() with NULL for a primitive type should throw an IllegalArgumentException.") { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void test() throws Exception { |
|
|
|
|
setField(person, "age", null, int.class); |
|
|
|
|
} |
|
|
|
|
}.runTest(); |
|
|
|
|
@Test(expected = IllegalArgumentException.class) |
|
|
|
|
public void setFieldWithNullValueForPrimitiveLong() throws Exception { |
|
|
|
|
setField(person, "id", null, long.class); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
new AssertThrows(IllegalArgumentException.class, |
|
|
|
|
"Calling setField() with NULL for a primitive type should throw an IllegalArgumentException.") { |
|
|
|
|
@Test(expected = IllegalArgumentException.class) |
|
|
|
|
public void setFieldWithNullValueForPrimitiveInt() throws Exception { |
|
|
|
|
setField(person, "age", null, int.class); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void test() throws Exception { |
|
|
|
|
setField(person, "likesPets", null, boolean.class); |
|
|
|
|
} |
|
|
|
|
}.runTest(); |
|
|
|
|
@Test(expected = IllegalArgumentException.class) |
|
|
|
|
public void setFieldWithNullValueForPrimitiveBoolean() throws Exception { |
|
|
|
|
setField(person, "likesPets", null, boolean.class); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
@ -123,11 +101,31 @@ public class ReflectionTestUtilsTests {
@@ -123,11 +101,31 @@ public class ReflectionTestUtilsTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void invokeSetterAndMethods() throws Exception { |
|
|
|
|
public void invokeSetterMethodWithExplicitSetterMethodNames() throws Exception { |
|
|
|
|
invokeSetterMethod(person, "setId", new Long(1), long.class); |
|
|
|
|
invokeSetterMethod(person, "setName", "Jerry", String.class); |
|
|
|
|
invokeSetterMethod(person, "setAge", new Integer(33), int.class); |
|
|
|
|
invokeSetterMethod(person, "setEyeColor", "green", String.class); |
|
|
|
|
invokeSetterMethod(person, "setLikesPets", Boolean.FALSE, boolean.class); |
|
|
|
|
invokeSetterMethod(person, "setFavoriteNumber", new Integer(42), Number.class); |
|
|
|
|
|
|
|
|
|
assertEquals("ID (protected method in a superclass)", 1, person.getId()); |
|
|
|
|
assertEquals("name (private method)", "Jerry", person.getName()); |
|
|
|
|
assertEquals("age (protected method)", 33, person.getAge()); |
|
|
|
|
assertEquals("eye color (package private method)", "green", person.getEyeColor()); |
|
|
|
|
assertEquals("'likes pets' flag (protected method for a boolean)", false, person.likesPets()); |
|
|
|
|
assertEquals("'favorite number' (protected method for a Number)", new Integer(42), person.getFavoriteNumber()); |
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
|
// Standard - properties
|
|
|
|
|
assertEquals(new Long(1), invokeGetterMethod(person, "getId")); |
|
|
|
|
assertEquals("Jerry", invokeGetterMethod(person, "getName")); |
|
|
|
|
assertEquals(new Integer(33), invokeGetterMethod(person, "getAge")); |
|
|
|
|
assertEquals("green", invokeGetterMethod(person, "getEyeColor")); |
|
|
|
|
assertEquals(Boolean.FALSE, invokeGetterMethod(person, "likesPets")); |
|
|
|
|
assertEquals(new Integer(42), invokeGetterMethod(person, "getFavoriteNumber")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void invokeSetterMethodWithJavaBeanPropertyNames() throws Exception { |
|
|
|
|
invokeSetterMethod(person, "id", new Long(99), long.class); |
|
|
|
|
invokeSetterMethod(person, "name", "Tom"); |
|
|
|
|
invokeSetterMethod(person, "age", new Integer(42)); |
|
|
|
|
@ -148,34 +146,10 @@ public class ReflectionTestUtilsTests {
@@ -148,34 +146,10 @@ public class ReflectionTestUtilsTests {
|
|
|
|
|
assertEquals("blue", invokeGetterMethod(person, "eyeColor")); |
|
|
|
|
assertEquals(Boolean.TRUE, invokeGetterMethod(person, "likesPets")); |
|
|
|
|
assertEquals(PI, invokeGetterMethod(person, "favoriteNumber")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
|
// Standard - setter methods
|
|
|
|
|
|
|
|
|
|
invokeSetterMethod(person, "setId", new Long(1), long.class); |
|
|
|
|
invokeSetterMethod(person, "setName", "Jerry", String.class); |
|
|
|
|
invokeSetterMethod(person, "setAge", new Integer(33), int.class); |
|
|
|
|
invokeSetterMethod(person, "setEyeColor", "green", String.class); |
|
|
|
|
invokeSetterMethod(person, "setLikesPets", Boolean.FALSE, boolean.class); |
|
|
|
|
invokeSetterMethod(person, "setFavoriteNumber", new Integer(42), Number.class); |
|
|
|
|
|
|
|
|
|
assertEquals("ID (protected method in a superclass)", 1, person.getId()); |
|
|
|
|
assertEquals("name (private method)", "Jerry", person.getName()); |
|
|
|
|
assertEquals("age (protected method)", 33, person.getAge()); |
|
|
|
|
assertEquals("eye color (package private method)", "green", person.getEyeColor()); |
|
|
|
|
assertEquals("'likes pets' flag (protected method for a boolean)", false, person.likesPets()); |
|
|
|
|
assertEquals("'favorite number' (protected method for a Number)", new Integer(42), person.getFavoriteNumber()); |
|
|
|
|
|
|
|
|
|
assertEquals(new Long(1), invokeGetterMethod(person, "getId")); |
|
|
|
|
assertEquals("Jerry", invokeGetterMethod(person, "getName")); |
|
|
|
|
assertEquals(new Integer(33), invokeGetterMethod(person, "getAge")); |
|
|
|
|
assertEquals("green", invokeGetterMethod(person, "getEyeColor")); |
|
|
|
|
assertEquals(Boolean.FALSE, invokeGetterMethod(person, "likesPets")); |
|
|
|
|
assertEquals(new Integer(42), invokeGetterMethod(person, "getFavoriteNumber")); |
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
|
// Null - non-primitives
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void invokeSetterMethodWithNullValuesForNonPrimitives() throws Exception { |
|
|
|
|
invokeSetterMethod(person, "name", null, String.class); |
|
|
|
|
invokeSetterMethod(person, "eyeColor", null, String.class); |
|
|
|
|
invokeSetterMethod(person, "favoriteNumber", null, Number.class); |
|
|
|
|
@ -183,36 +157,21 @@ public class ReflectionTestUtilsTests {
@@ -183,36 +157,21 @@ public class ReflectionTestUtilsTests {
|
|
|
|
|
assertNull("name (private method)", person.getName()); |
|
|
|
|
assertNull("eye color (package private method)", person.getEyeColor()); |
|
|
|
|
assertNull("'favorite number' (protected method for a Number)", person.getFavoriteNumber()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
|
// Null - primitives
|
|
|
|
|
|
|
|
|
|
new AssertThrows(IllegalArgumentException.class, |
|
|
|
|
"Calling invokeSetterMethod() with NULL for a primitive type should throw an IllegalArgumentException.") { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void test() throws Exception { |
|
|
|
|
invokeSetterMethod(person, "id", null, long.class); |
|
|
|
|
} |
|
|
|
|
}.runTest(); |
|
|
|
|
|
|
|
|
|
new AssertThrows(IllegalArgumentException.class, |
|
|
|
|
"Calling invokeSetterMethod() with NULL for a primitive type should throw an IllegalArgumentException.") { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void test() throws Exception { |
|
|
|
|
invokeSetterMethod(person, "age", null, int.class); |
|
|
|
|
} |
|
|
|
|
}.runTest(); |
|
|
|
|
@Test(expected = IllegalArgumentException.class) |
|
|
|
|
public void invokeSetterMethodWithNullValueForPrimitiveLong() throws Exception { |
|
|
|
|
invokeSetterMethod(person, "id", null, long.class); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
new AssertThrows(IllegalArgumentException.class, |
|
|
|
|
"Calling invokeSetterMethod() with NULL for a primitive type should throw an IllegalArgumentException.") { |
|
|
|
|
@Test(expected = IllegalArgumentException.class) |
|
|
|
|
public void invokeSetterMethodWithNullValueForPrimitiveInt() throws Exception { |
|
|
|
|
invokeSetterMethod(person, "age", null, int.class); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void test() throws Exception { |
|
|
|
|
invokeSetterMethod(person, "likesPets", null, boolean.class); |
|
|
|
|
} |
|
|
|
|
}.runTest(); |
|
|
|
|
@Test(expected = IllegalArgumentException.class) |
|
|
|
|
public void invokeSetterMethodWithNullValueForPrimitiveBoolean() throws Exception { |
|
|
|
|
invokeSetterMethod(person, "likesPets", null, boolean.class); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
@ -222,8 +181,8 @@ public class ReflectionTestUtilsTests {
@@ -222,8 +181,8 @@ public class ReflectionTestUtilsTests {
|
|
|
|
|
assertEquals("subtract(5, 2)", 3, difference.intValue()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Ignore("[SPR-8644] findMethod() does not currently support var-args") |
|
|
|
|
@Test |
|
|
|
|
@Ignore("[SPR-8644] findMethod() does not currently support var-args") |
|
|
|
|
public void invokeMethodWithPrimitiveVarArgs() { |
|
|
|
|
// IntelliJ IDEA 11 won't accept int assignment here
|
|
|
|
|
Integer sum = invokeMethod(component, "add", 1, 2, 3, 4); |
|
|
|
|
@ -238,7 +197,7 @@ public class ReflectionTestUtilsTests {
@@ -238,7 +197,7 @@ public class ReflectionTestUtilsTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void invokeMethodsSimulatingLifecycleEvents() { |
|
|
|
|
public void invokeMethodSimulatingLifecycleEvents() { |
|
|
|
|
assertNull("number", component.getNumber()); |
|
|
|
|
assertNull("text", component.getText()); |
|
|
|
|
|
|
|
|
|
|