diff --git a/src/test/org/apache/commons/lang/builder/DefaultToStringStyleTest.java b/src/test/org/apache/commons/lang/builder/DefaultToStringStyleTest.java index 5e899af75..2b9ddc9f8 100644 --- a/src/test/org/apache/commons/lang/builder/DefaultToStringStyleTest.java +++ b/src/test/org/apache/commons/lang/builder/DefaultToStringStyleTest.java @@ -19,6 +19,8 @@ package org.apache.commons.lang.builder; import java.util.ArrayList; import java.util.HashMap; +import org.apache.commons.lang.builder.ToStringStyleTest.Person; + import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; @@ -91,6 +93,15 @@ public class DefaultToStringStyleTest extends TestCase { assertEquals(baseStr + "[a={}]", new ToStringBuilder(base).append("a", (Object) new String[0], true).toString()); } + public void testPerson() { + Person p = new Person(); + p.name = "John Doe"; + p.age = 33; + p.smoker = false; + String pBaseStr = p.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(p)); + assertEquals(pBaseStr + "[name=John Doe,age=33,smoker=false]", new ToStringBuilder(p).append("name", p.name).append("age", p.age).append("smoker", p.smoker).toString()); + } + public void testLong() { assertEquals(baseStr + "[3]", new ToStringBuilder(base).append(3L).toString()); assertEquals(baseStr + "[a=3]", new ToStringBuilder(base).append("a", 3L).toString()); diff --git a/src/test/org/apache/commons/lang/builder/MultiLineToStringStyleTest.java b/src/test/org/apache/commons/lang/builder/MultiLineToStringStyleTest.java index 3fc762851..021e7e96c 100644 --- a/src/test/org/apache/commons/lang/builder/MultiLineToStringStyleTest.java +++ b/src/test/org/apache/commons/lang/builder/MultiLineToStringStyleTest.java @@ -25,6 +25,7 @@ import junit.framework.TestSuite; import junit.textui.TestRunner; import org.apache.commons.lang.SystemUtils; +import org.apache.commons.lang.builder.ToStringStyleTest.Person; /** * Unit tests {@link org.apache.commons.lang.builder.MultiLineToStringStyleTest}. @@ -93,6 +94,15 @@ public class MultiLineToStringStyleTest extends TestCase { assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR + " a={}" + SystemUtils.LINE_SEPARATOR + "]", new ToStringBuilder(base).append("a", (Object) new String[0], true).toString()); } + public void testPerson() { + Person p = new Person(); + p.name = "Jane Doe"; + p.age = 25; + p.smoker = true; + String pBaseStr = p.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(p)); + assertEquals(pBaseStr + "[" + SystemUtils.LINE_SEPARATOR + " name=Jane Doe" + SystemUtils.LINE_SEPARATOR + " age=25" + SystemUtils.LINE_SEPARATOR + " smoker=true" + SystemUtils.LINE_SEPARATOR + "]", new ToStringBuilder(p).append("name", p.name).append("age", p.age).append("smoker", p.smoker).toString()); + } + public void testLong() { assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR + " 3" + SystemUtils.LINE_SEPARATOR + "]", new ToStringBuilder(base).append(3L).toString()); assertEquals(baseStr + "[" + SystemUtils.LINE_SEPARATOR + " a=3" + SystemUtils.LINE_SEPARATOR + "]", new ToStringBuilder(base).append("a", 3L).toString()); diff --git a/src/test/org/apache/commons/lang/builder/NoFieldNamesToStringStyleTest.java b/src/test/org/apache/commons/lang/builder/NoFieldNamesToStringStyleTest.java index ba7993559..ee850aa6e 100644 --- a/src/test/org/apache/commons/lang/builder/NoFieldNamesToStringStyleTest.java +++ b/src/test/org/apache/commons/lang/builder/NoFieldNamesToStringStyleTest.java @@ -19,6 +19,8 @@ package org.apache.commons.lang.builder; import java.util.ArrayList; import java.util.HashMap; +import org.apache.commons.lang.builder.ToStringStyleTest.Person; + import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; @@ -91,6 +93,15 @@ public class NoFieldNamesToStringStyleTest extends TestCase { assertEquals(baseStr + "[{}]", new ToStringBuilder(base).append("a", (Object) new String[0], true).toString()); } + public void testPerson() { + Person p = new Person(); + p.name = "Ron Paul"; + p.age = 72; + p.smoker = false; + String pBaseStr = p.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(p)); + assertEquals(pBaseStr + "[Ron Paul,72,false]", new ToStringBuilder(p).append("name", p.name).append("age", p.age).append("smoker", p.smoker).toString()); + } + public void testLong() { assertEquals(baseStr + "[3]", new ToStringBuilder(base).append(3L).toString()); assertEquals(baseStr + "[3]", new ToStringBuilder(base).append("a", 3L).toString()); diff --git a/src/test/org/apache/commons/lang/builder/ShortPrefixToStringStyleTest.java b/src/test/org/apache/commons/lang/builder/ShortPrefixToStringStyleTest.java index a5f2e7805..6889e5fb0 100644 --- a/src/test/org/apache/commons/lang/builder/ShortPrefixToStringStyleTest.java +++ b/src/test/org/apache/commons/lang/builder/ShortPrefixToStringStyleTest.java @@ -19,6 +19,8 @@ package org.apache.commons.lang.builder; import java.util.ArrayList; import java.util.HashMap; +import org.apache.commons.lang.builder.ToStringStyleTest.Person; + import junit.framework.TestCase; /** @@ -74,6 +76,15 @@ public class ShortPrefixToStringStyleTest extends TestCase { assertEquals(baseStr + "[a={}]", new ToStringBuilder(base).append("a", (Object) new String[0], true).toString()); } + public void testPerson() { + Person p = new Person(); + p.name = "John Q. Public"; + p.age = 45; + p.smoker = true; + String pBaseStr = p.getClass().getName(); + assertEquals(pBaseStr + "[name=John Q. Public,age=45,smoker=true]", new ToStringBuilder(p).append("name", p.name).append("age", p.age).append("smoker", p.smoker).toString()); + } + public void testLong() { assertEquals(baseStr + "[3]", new ToStringBuilder(base).append(3L).toString()); assertEquals(baseStr + "[a=3]", new ToStringBuilder(base).append("a", 3L).toString()); diff --git a/src/test/org/apache/commons/lang/builder/SimpleToStringStyleTest.java b/src/test/org/apache/commons/lang/builder/SimpleToStringStyleTest.java index 75c6f908a..b658e5dbf 100644 --- a/src/test/org/apache/commons/lang/builder/SimpleToStringStyleTest.java +++ b/src/test/org/apache/commons/lang/builder/SimpleToStringStyleTest.java @@ -19,6 +19,8 @@ package org.apache.commons.lang.builder; import java.util.ArrayList; import java.util.HashMap; +import org.apache.commons.lang.builder.ToStringStyleTest.Person; + import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; @@ -90,6 +92,14 @@ public class SimpleToStringStyleTest extends TestCase { assertEquals("{}", new ToStringBuilder(base).append("a", (Object) new String[0], true).toString()); } + public void testPerson() { + Person p = new Person(); + p.name = "Jane Q. Public"; + p.age = 47; + p.smoker = false; + assertEquals("Jane Q. Public,47,false", new ToStringBuilder(p).append("name", p.name).append("age", p.age).append("smoker", p.smoker).toString()); + } + public void testLong() { assertEquals("3", new ToStringBuilder(base).append(3L).toString()); assertEquals("3", new ToStringBuilder(base).append("a", 3L).toString()); diff --git a/src/test/org/apache/commons/lang/builder/StandardToStringStyleTest.java b/src/test/org/apache/commons/lang/builder/StandardToStringStyleTest.java index 5bba64850..9ffb7d9cc 100644 --- a/src/test/org/apache/commons/lang/builder/StandardToStringStyleTest.java +++ b/src/test/org/apache/commons/lang/builder/StandardToStringStyleTest.java @@ -19,6 +19,8 @@ package org.apache.commons.lang.builder; import java.util.ArrayList; import java.util.HashMap; +import org.apache.commons.lang.builder.ToStringStyleTest.Person; + import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; @@ -106,6 +108,15 @@ public class StandardToStringStyleTest extends TestCase { assertEquals(baseStr + "[a=[]]", new ToStringBuilder(base).append("a", (Object) new String[0], true).toString()); } + public void testPerson() { + Person p = new Person(); + p.name = "Suzy Queue"; + p.age = 19; + p.smoker = false; + String pBaseStr = "ToStringStyleTest.Person"; + assertEquals(pBaseStr + "[name=Suzy Queue,age=19,smoker=false]", new ToStringBuilder(p).append("name", p.name).append("age", p.age).append("smoker", p.smoker).toString()); + } + public void testLong() { assertEquals(baseStr + "[3]", new ToStringBuilder(base).append(3L).toString()); assertEquals(baseStr + "[a=3]", new ToStringBuilder(base).append("a", 3L).toString()); diff --git a/src/test/org/apache/commons/lang/builder/ToStringStyleTest.java b/src/test/org/apache/commons/lang/builder/ToStringStyleTest.java index 225e3954c..ee72691dc 100644 --- a/src/test/org/apache/commons/lang/builder/ToStringStyleTest.java +++ b/src/test/org/apache/commons/lang/builder/ToStringStyleTest.java @@ -123,4 +123,25 @@ public class ToStringStyleTest extends TestCase { assertEquals("", style.getSummaryObjectEndText()); } + /** + * An object used to test {@link ToStringStyle}. + * + * @author Scott Bassin + */ + static class Person { + /** + * Test String field. + */ + String name; + + /** + * Test integer field. + */ + int age; + + /** + * Test boolean field. + */ + boolean smoker; + } }