commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
Subject svn commit: r905093 - in /commons/proper/lang/branches/LANG_2_X/src: main/java/org/apache/commons/lang/builder/ReflectionToStringBuilder.java test/java/org/apache/commons/lang/builder/ToStringBuilderTest.java
Date Sun, 31 Jan 2010 18:38:17 GMT
Author: niallp
Date: Sun Jan 31 18:38:16 2010
New Revision: 905093

URL: http://svn.apache.org/viewvc?rev=905093&view=rev
Log:
Partial port of r775045 & r829635 to 2.x branch - add "upToClass" check to ReflectionToStringBuilder

Modified:
    commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/builder/ReflectionToStringBuilder.java
    commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/builder/ToStringBuilderTest.java

Modified: commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/builder/ReflectionToStringBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/builder/ReflectionToStringBuilder.java?rev=905093&r1=905092&r2=905093&view=diff
==============================================================================
--- commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/builder/ReflectionToStringBuilder.java
(original)
+++ commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/builder/ReflectionToStringBuilder.java
Sun Jan 31 18:38:16 2010
@@ -741,6 +741,12 @@
      *            The last super class to stop appending fields for.
      */
     public void setUpToClass(Class clazz) {
+        if (clazz != null) {
+            Object object = getObject();
+            if (object != null && clazz.isInstance(object) == false) {
+                throw new IllegalArgumentException("Specified class is not a superclass of
the object");
+            }
+        }
         this.upToClass = clazz;
     }
 

Modified: commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/builder/ToStringBuilderTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/builder/ToStringBuilderTest.java?rev=905093&r1=905092&r2=905093&view=diff
==============================================================================
--- commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/builder/ToStringBuilderTest.java
(original)
+++ commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/builder/ToStringBuilderTest.java
Sun Jan 31 18:38:16 2010
@@ -338,7 +338,6 @@
         assertEquals(baseStr + "[a=a,transientA=t]", ToStringBuilder.reflectionToString(baseA,
null, true));
         assertEquals(baseStr + "[a=a]", ToStringBuilder.reflectionToString(baseA, null, false,
null));
         assertEquals(baseStr + "[a=a]", ToStringBuilder.reflectionToString(baseA, null, false,
Object.class));
-        assertEquals(baseStr + "[a=a]", ToStringBuilder.reflectionToString(baseA, null, false,
List.class));
         assertEquals(baseStr + "[a=a]", ToStringBuilder.reflectionToString(baseA, null, false,
ReflectionTestFixtureA.class));
         
         ReflectionTestFixtureB baseB = new ReflectionTestFixtureB();
@@ -350,7 +349,6 @@
         assertEquals(baseStr + "[b=b,transientB=t,a=a,transientA=t]", ToStringBuilder.reflectionToString(baseB,
null, true));
         assertEquals(baseStr + "[b=b,a=a]", ToStringBuilder.reflectionToString(baseB, null,
false, null));
         assertEquals(baseStr + "[b=b,a=a]", ToStringBuilder.reflectionToString(baseB, null,
false, Object.class));
-        assertEquals(baseStr + "[b=b,a=a]", ToStringBuilder.reflectionToString(baseB, null,
false, List.class));
         assertEquals(baseStr + "[b=b,a=a]", ToStringBuilder.reflectionToString(baseB, null,
false, ReflectionTestFixtureA.class));
         assertEquals(baseStr + "[b=b]", ToStringBuilder.reflectionToString(baseB, null, false,
ReflectionTestFixtureB.class));
         this.validateEmptyToStringStyleRegistry();
@@ -938,6 +936,29 @@
     }
 
     /**
+     * Tests ReflectionToStringBuilder setUpToClass().
+     */
+    public void test_setUpToClass_valid() {
+        Integer val = new Integer(5);
+        ReflectionToStringBuilder test = new ReflectionToStringBuilder(val);
+        test.setUpToClass(Number.class);
+    }
+    
+    /**
+     * Tests ReflectionToStringBuilder setUpToClass().
+     */
+    public void test_setUpToClass_invalid() {
+        Integer val = new Integer(5);
+        ReflectionToStringBuilder test = new ReflectionToStringBuilder(val);
+        try {
+            test.setUpToClass(String.class);
+            fail();
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+    }
+    
+    /**
      * Tests ReflectionToStringBuilder.toString() for statics.
      */
     class ReflectionStaticFieldsFixture {



Mime
View raw message