commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Henri Yandell (JIRA)" <j...@apache.org>
Subject [jira] Updated: (LANG-647) ToStringBuilder output makes it difficult to distinguich between an empty String array and an array of one empty String
Date Mon, 17 Jan 2011 05:57:43 GMT

     [ https://issues.apache.org/jira/browse/LANG-647?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]

Henri Yandell updated LANG-647:
-------------------------------

    Fix Version/s:     (was: 3.0)
                   3.1

I don't think the requested change should be done; it makes an array with an empty String
equal an array with a String containing two double quotes.

Putting quotes around all Strings would be one solution; but this merely opens up a whole
slew of problems in ToStringBuilder's default implementation; namely that it doesn't show
types. "1", '1' and (int)1 all look the same. As do "true" and (boolean)true.

I think the solution here is to have a StringStyle that displays types. Moving to 3.1 as this
wouldn't be a backwards incompatibility.

> 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
>             Fix For: 3.1
>
>
> 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