From 7c7e7865bf456c782dc28d460bcd224f3bc31286 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Tue, 15 Mar 2016 19:13:40 +0100 Subject: [PATCH] Polishing --- .../test/util/AopTestUtils.java | 3 +- .../test/util/ReflectionTestUtils.java | 3 +- .../test/util/ReflectionTestUtilsTests.java | 66 +++++++++---------- 3 files changed, 36 insertions(+), 36 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/util/AopTestUtils.java b/spring-test/src/main/java/org/springframework/test/util/AopTestUtils.java index 4eda8a45ac..7660ee2cef 100644 --- a/spring-test/src/main/java/org/springframework/test/util/AopTestUtils.java +++ b/spring-test/src/main/java/org/springframework/test/util/AopTestUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 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. @@ -32,6 +32,7 @@ import org.springframework.util.Assert; * @since 4.2 * @see org.springframework.aop.support.AopUtils * @see org.springframework.aop.framework.AopProxyUtils + * @see ReflectionTestUtils */ public class AopTestUtils { diff --git a/spring-test/src/main/java/org/springframework/test/util/ReflectionTestUtils.java b/spring-test/src/main/java/org/springframework/test/util/ReflectionTestUtils.java index 65fe04fb84..c41a487ef1 100644 --- a/spring-test/src/main/java/org/springframework/test/util/ReflectionTestUtils.java +++ b/spring-test/src/main/java/org/springframework/test/util/ReflectionTestUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 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. @@ -60,6 +60,7 @@ import org.springframework.util.StringUtils; * @author Juergen Hoeller * @since 2.5 * @see ReflectionUtils + * @see AopTestUtils */ public class ReflectionTestUtils { diff --git a/spring-test/src/test/java/org/springframework/test/util/ReflectionTestUtilsTests.java b/spring-test/src/test/java/org/springframework/test/util/ReflectionTestUtilsTests.java index 572eea0169..0504075fc8 100644 --- a/spring-test/src/test/java/org/springframework/test/util/ReflectionTestUtilsTests.java +++ b/spring-test/src/test/java/org/springframework/test/util/ReflectionTestUtilsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 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. @@ -22,9 +22,7 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; -import org.springframework.aop.framework.ProxyCreatorSupport; import org.springframework.aop.framework.ProxyFactory; -import org.springframework.aop.support.AopUtils; import org.springframework.test.util.subpackage.Component; import org.springframework.test.util.subpackage.LegacyEntity; import org.springframework.test.util.subpackage.Person; @@ -131,6 +129,37 @@ public class ReflectionTestUtilsTests { assertEquals(PI, getField(person, "favoriteNumber")); } + @Test + public void setFieldAndGetFieldOnCglibProxiedInstance() throws Exception { + ProxyFactory pf = new ProxyFactory(this.person); + pf.setProxyTargetClass(true); + Person proxyPerson = (Person) pf.getProxy(); + + // Set reflectively via Proxy + setField(proxyPerson, "id", new Long(99), long.class); + setField(proxyPerson, "name", "Tom"); + setField(proxyPerson, "age", new Integer(42)); + setField(proxyPerson, "eyeColor", "blue", String.class); + setField(proxyPerson, "likesPets", Boolean.TRUE); + setField(proxyPerson, "favoriteNumber", PI, Number.class); + + // Get directly from Target via getter methods + assertEquals("ID (private field in a superclass)", 99, this.person.getId()); + assertEquals("name (protected field)", "Tom", this.person.getName()); + assertEquals("age (private field)", 42, this.person.getAge()); + assertEquals("eye color (package private field)", "blue", this.person.getEyeColor()); + assertEquals("'likes pets' flag (package private boolean field)", true, this.person.likesPets()); + assertEquals("'favorite number' (package field)", PI, this.person.getFavoriteNumber()); + + // Get reflectively via Proxy + assertEquals(new Long(99), getField(proxyPerson, "id")); + assertEquals("Tom", getField(proxyPerson, "name")); + assertEquals(new Integer(42), getField(proxyPerson, "age")); + assertEquals("blue", getField(proxyPerson, "eyeColor")); + assertEquals(Boolean.TRUE, getField(proxyPerson, "likesPets")); + assertEquals(PI, getField(proxyPerson, "favoriteNumber")); + } + @Test public void setFieldWithNullValuesForNonPrimitives() throws Exception { // Fields must be non-null to start with @@ -362,35 +391,4 @@ public class ReflectionTestUtilsTests { invokeMethod(component, "configure", new Integer(42), "enigma", "baz", "quux"); } - @Test - public void setAndGetFieldOnProxiedInstance() throws Exception { - ProxyFactory pf = new ProxyFactory(this.person); - pf.setProxyTargetClass(true); - - Person proxyPerson = (Person) pf.getProxy(); - - setField(proxyPerson, "id", new Long(99), long.class); - setField(proxyPerson, "name", "Tom"); - setField(proxyPerson, "age", new Integer(42)); - setField(proxyPerson, "eyeColor", "blue", String.class); - setField(proxyPerson, "likesPets", Boolean.TRUE); - setField(proxyPerson, "favoriteNumber", PI, Number.class); - - assertEquals("ID (private field in a superclass)", 99, person.getId()); - assertEquals("name (protected field)", "Tom", person.getName()); - assertEquals("age (private field)", 42, person.getAge()); - assertEquals("eye color (package private field)", "blue", person.getEyeColor()); - assertEquals("'likes pets' flag (package private boolean field)", true, person.likesPets()); - assertEquals("'favorite number' (package field)", PI, person.getFavoriteNumber()); - - assertEquals(new Long(99), getField(proxyPerson, "id")); - assertEquals("Tom", getField(proxyPerson, "name")); - assertEquals(new Integer(42), getField(proxyPerson, "age")); - assertEquals("blue", getField(proxyPerson, "eyeColor")); - assertEquals(Boolean.TRUE, getField(proxyPerson, "likesPets")); - assertEquals(PI, getField(proxyPerson, "favoriteNumber")); - - - } - }