commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "pascal jacob (JIRA)" <j...@apache.org>
Subject [jira] Created: (LANG-647) ToStringBuilder output makes it difficult to distinguich between an empty String array and an array of one empty String
Date Wed, 25 Aug 2010 09:42:23 GMT
ToStringBuilder output makes it difficult to distinguich between an empty String array and
an array of one empty String
-----------------------------------------------------------------------------------------------------------------------

                 Key: LANG-647
                 URL: https://issues.apache.org/jira/browse/LANG-647
             Project: Commons Lang
          Issue Type: Improvement
          Components: lang.builder.*
    Affects Versions: 2.5
            Reporter: pascal jacob
            Priority: Minor


ToStringBuilder output is the same for an empty array (i.e. new String[0]) and for an array
containing only a single empty string (i.e. new String[] { "" } ).  This makes it difficult
in some case to see the true nature of arrays.

For example I once had a JUnit test case that print the following trace failure:
java.lang.AssertionError: 
Expected: <InputViewHelper[a={},b={},c={msg},d={time}>
     got: <InputViewHelper[a={},b={},c={msg},d={time}>

Apparently the two objects look like the same! But they are not: one had an empty array; the
other had an array with only a single empty string. With a customized ToStringStyle the difference
became apparent:
Expected: <InputViewHelper[a={},b={},c={msg},d={time}>
     got: <InputViewHelper[a={""},b={},c={msg},d={time}>


The fix is simple, change the method: protected void appendDetail(StringBuffer buffer, String
fieldName, Object value) to:
			protected void appendDetail(StringBuffer buffer, String fieldName, Object value) {
				if((value instanceof String) && ((String)value).isEmpty()) {
					buffer.append("\"\"");
				}
				else {
					super.appendDetail(buffer, fieldName, value);
				}
			}
 
here is the test case that revealed the problem:
	public void testToStringBuilder() {
		ToStringBuilder builder1 = new ToStringBuilder("Builder1");
		builder1.append("empty array", new String[0]);
		builder1.append("array of one empty string", new String[] { "" });
		builder1.append("array of two empty strings", new String[] { "", "" });
		String builder1Result = builder1.toString();
		System.out.println(builder1Result);
		// -----
		ToStringBuilder builder2 = new ToStringBuilder("Builder2", new ToStringStyle() {
			@Override
			protected void appendDetail(StringBuffer buffer, String fieldName, Object value) {
				if((value instanceof String) && ((String)value).isEmpty()) {
					buffer.append("\"\"");
				}
				else {
					super.appendDetail(buffer, fieldName, value);
				}
			}
		});
		builder2.append("empty array", new String[0]);
		builder2.append("array of one empty string", new String[] { "" });
		builder2.append("array of two empty strings", new String[] { "", "" });
		String builder2Result = builder2.toString();
		System.out.println(builder2Result);
	}


-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


Mime
View raw message