Applying Scott Bassin's patch from LANG-371, adding unit tests that follow the approach of his previous patch to the documentation

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@592643 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2007-11-07 07:25:30 +00:00
parent 91410a3e10
commit 78b9d5fc88
7 changed files with 85 additions and 0 deletions

View File

@ -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());

View File

@ -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());

View File

@ -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());

View File

@ -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());

View File

@ -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());

View File

@ -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());

View File

@ -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;
}
}