#6 assertEquals(String,String) fails when last character differ

Latest_release
open
5
2004-04-08
2003-10-10
Peik Aschan
No

When comparing two strings that differ in length, and
only the end of the string differs, a
StringIndexOutOfBoundsException is thrown.

For example,
<code>assertEquals("abc1", "abc");</code>
fails.

This occurs only when the first string is longer,
<code>assertEquals("abc", "abc1");</code>
works fine.

Stack trace:
java.lang.StringIndexOutOfBoundsException: String index
out of range: 3
at java.lang.String.charAt(String.java:511)
at
junitx.framework.ComparisonFailure.createMessage
(ComparisonFailure.java:145)
at junitx.framework.ComparisonFailure.<init>
(ComparisonFailure.java:91)
at junitx.framework.Assert.assertEquals
(Assert.java:120)
at junitx.framework.Assert.assertEquals
(Assert.java:109)

Discussion

  • Peik Aschan
    Peik Aschan
    2004-04-08

    • assigned_to: nobody --> vbossica
     
  • Peik Aschan
    Peik Aschan
    2004-04-08

    Logged In: YES
    user_id=845133

    Fix is to change row 145 in
    junitx.framework.ComparisonFailure (version 1.1, 2003/03/21
    06:13:49) from
    if (beginDiff > 0 && !isStopper(actual.charAt(beginDiff))) {

    to:
    if (beginDiff > 0 && (actual.length() >= beginDiff || !isStopper
    (actual.charAt(beginDiff)))) {

    Vladimir, can you put this change into CVS after reviewing it?
    The attached test case displays the problem and its
    resolution.

     
  • Peik Aschan
    Peik Aschan
    2004-04-08

    Test case for junitx.framework.ComparisonFailure

     
  • codeMunky
    codeMunky
    2004-04-08

    Logged In: YES
    user_id=153534

    Additionally, line 147 needs to be changes to handle the
    case where the difference lies in the beginning of the
    String. i.e. assertEquals("1abc", "abc"), also fails.

    Line 147 needs to be changed from

    } else if ((endDiffAct < actual.length()-1) &&
    !isStopper(actual.charAt(endDiffAct))) {

    to this:

    } else if ((endDiffAct < actual.length()-1) && endDiffAct >=
    0 && !isStopper(actual.charAt(endDiffAct))) {

    Code you please review this change and add it to CVS?