commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Pascal Schumacher (JIRA)" <j...@apache.org>
Subject [jira] [Updated] (LANG-647) ToStringBuilder output makes it difficult to distinguish between an empty String array and an array of one empty String
Date Wed, 19 Apr 2017 16:01:41 GMT

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

Pascal Schumacher updated LANG-647:
-----------------------------------
    Summary: ToStringBuilder output makes it difficult to distinguish between an empty String
array and an array of one empty String  (was: ToStringBuilder output makes it difficult to
distinguich between an empty String array and an array of one empty String)

> ToStringBuilder output makes it difficult to distinguish 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: Patch Needed
>
>
> 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 was sent by Atlassian JIRA
(v6.3.15#6346)

Mime
View raw message