Uploaded image for project: 'Commons Lang'
  1. Commons Lang
  2. LANG-647

ToStringBuilder output makes it difficult to distinguish between an empty String array and an array of one empty String

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Open
    • Minor
    • Resolution: Unresolved
    • 2.5
    • Patch Needed
    • lang.builder.*
    • None

    Description

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

      Attachments

        Activity

          People

            Unassigned Unassigned
            zartc pascal jacob
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: