test/jdk/java/lang/StringBuilder/BuilderForwarding.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 33663 jdk/test/java/lang/StringBuilder/BuilderForwarding.java@2cd62a4bd471
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14332
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21596
diff changeset
     2
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
14332
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
     4
 *
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
21596
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 14332
diff changeset
     7
 * published by the Free Software Foundation.
14332
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
     8
 *
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    13
 * accompanied this code).
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    14
 *
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    18
 *
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    21
 * questions.
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    22
 */
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    23
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    24
/**
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    25
 * @test
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    26
 * @bug 6206780
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    27
 * @summary  Test forwarding of methods to super in StringBuilder
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    28
 * @author Jim Gish <jim.gish@oracle.com>
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    29
 */
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    30
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    31
import java.util.ArrayList;
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    32
import java.util.List;
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    33
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    34
public class BuilderForwarding {
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 23010
diff changeset
    35
    private static final String A_STRING_BUFFER_VAL = "aStringBuffer";
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 23010
diff changeset
    36
    private static final String A_STRING_BUILDER_VAL = "aStringBuilder";
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 23010
diff changeset
    37
    private static final String A_STRING_VAL = "aString";
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 23010
diff changeset
    38
    private static final String NON_EMPTY_VAL = "NonEmpty";
14332
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    39
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    40
    public BuilderForwarding() {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    41
        System.out.println( "Starting BuilderForwarding");
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    42
    }
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    43
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    44
    public static void main(String... args) {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    45
        new BuilderForwarding().executeTestMethods();
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    46
    }
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    47
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    48
    public void executeTestMethods() {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    49
        appendCharSequence();
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    50
        indexOfString();
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    51
        indexOfStringIntNull();
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    52
        indexOfStringNull();
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    53
        indexOfStringint();
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    54
        insertintCharSequence();
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    55
        insertintObject();
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    56
        insertintboolean();
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    57
        insertintchar();
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    58
        insertintdouble();
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    59
        insertintfloat();
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    60
        insertintint();
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    61
        insertintlong();
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    62
        lastIndexOfString();
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    63
        lastIndexOfStringint();
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    64
    }
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    65
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    66
    public void appendCharSequence() {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    67
        // three different flavors of CharSequence
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    68
        CharSequence aString = A_STRING_VAL;
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    69
        CharSequence aStringBuilder = new StringBuilder(A_STRING_BUILDER_VAL);
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    70
        CharSequence aStringBuffer = new StringBuffer(A_STRING_BUFFER_VAL);
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    71
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    72
        assertEquals( /*actual*/ new StringBuilder().append(aString).toString(), /*expected*/ A_STRING_VAL );
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    73
        assertEquals( new StringBuilder().append(aStringBuilder).toString(), A_STRING_BUILDER_VAL );
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    74
        assertEquals( new StringBuilder().append(aStringBuffer).toString(), A_STRING_BUFFER_VAL );
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    75
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    76
        assertEquals( /*actual*/ new StringBuilder(NON_EMPTY_VAL).append(aString).toString(), NON_EMPTY_VAL+A_STRING_VAL );
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    77
        assertEquals( new StringBuilder(NON_EMPTY_VAL).append(aStringBuilder).toString(), NON_EMPTY_VAL+A_STRING_BUILDER_VAL );
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    78
        assertEquals( new StringBuilder(NON_EMPTY_VAL).append(aStringBuffer).toString(), NON_EMPTY_VAL+A_STRING_BUFFER_VAL );
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    79
    }
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    80
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    81
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    82
    public void indexOfString() {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    83
        StringBuilder sb = new StringBuilder();
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    84
        // should be NPE if null passed
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    85
        try {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    86
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    87
            sb.indexOf(null);
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    88
            throw new RuntimeException("Test failed: should have thrown NPE");
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    89
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    90
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    91
        } catch (NullPointerException npe) {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    92
            // expected: passed
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    93
        } catch (Throwable t) {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    94
            throw new RuntimeException("Test failed: should have thrown NPE. Instead threw "
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    95
                    + t);
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    96
        }
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    97
        sb = new StringBuilder("xyz");
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    98
        assertEquals( sb.indexOf("y"), 1 );
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
    99
        assertEquals( sb.indexOf("not found"), -1 );
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   100
    }
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   101
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   102
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   103
    public void indexOfStringint() {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   104
        StringBuilder sb = new StringBuilder();
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   105
        // should be NPE if null passed
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   106
        try {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   107
            sb.indexOf(null,1);
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   108
            throw new RuntimeException("Test failed: should have thrown NPE");
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   109
        } catch (NullPointerException npe) {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   110
            // expected: passed
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   111
        } catch (Throwable t) {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   112
            throw new RuntimeException("Test failed: should have thrown NPE");
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   113
        }
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   114
        sb = new StringBuilder("xyyz");
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   115
        assertEquals( sb.indexOf("y",0), 1 );
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   116
        assertEquals( sb.indexOf("y",1), 1 );
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   117
        assertEquals( sb.indexOf("y",2), 2 );
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   118
        assertEquals( sb.indexOf("not found"), -1 );
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   119
    }
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   120
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   121
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   122
    public void indexOfStringIntNull() {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   123
        StringBuffer sb = new StringBuffer();
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   124
        // should be NPE if null passed
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   125
        try {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   126
            sb.indexOf(null,1);
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   127
            throw new RuntimeException("Test failed: should have thrown NPE");
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   128
        } catch (NullPointerException npe) {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   129
            // expected: passed
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   130
        } catch (Throwable t) {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   131
            throw new RuntimeException("Test failed: should have thrown NPE. Instead threw "
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   132
                    + t);
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   133
        }
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   134
    }
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   135
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   136
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   137
    public void indexOfStringNull() {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   138
        StringBuilder sb = new StringBuilder();
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   139
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   140
        // should be NPE if null passed
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   141
        try {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   142
            sb.indexOf(null);
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   143
            throw new RuntimeException("Test failed: should have thrown NPE");
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   144
        } catch (NullPointerException npe) {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   145
            // expected: passed
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   146
        } catch (Throwable t) {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   147
            throw new RuntimeException("Test failed: should have thrown NPE. Instead threw "
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   148
                    + t);
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   149
        }
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   150
    }
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   151
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   152
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   153
    public void insertintboolean() {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   154
        boolean b = true;
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   155
        StringBuilder sb = new StringBuilder("012345");
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   156
        assertEquals( sb.insert( 2, b).toString(), "01true2345");
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   157
    }
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   158
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   159
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   160
    public void insertintchar() {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   161
        char c = 'C';
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   162
        StringBuilder sb = new StringBuilder("012345");
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   163
        assertEquals( sb.insert( 2, c ).toString(), "01C2345");
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   164
    }
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   165
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   166
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   167
    public void insertintCharSequence() {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   168
        final String initString = "012345";
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   169
        // three different flavors of CharSequence
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   170
        CharSequence aString = A_STRING_VAL;
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   171
        CharSequence aStringBuilder = new StringBuilder(A_STRING_BUILDER_VAL);
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   172
        CharSequence aStringBuffer = new StringBuffer(A_STRING_BUFFER_VAL);
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   173
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   174
        assertEquals( new StringBuilder(initString).insert(2, aString).toString(), "01"+A_STRING_VAL+"2345" );
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   175
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   176
        assertEquals( new StringBuilder(initString).insert(2, aStringBuilder).toString(), "01"+A_STRING_BUILDER_VAL+"2345" );
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   177
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   178
        assertEquals( new StringBuilder(initString).insert(2, aStringBuffer).toString(), "01"+A_STRING_BUFFER_VAL+"2345" );
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   179
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   180
        try {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   181
            new StringBuilder(initString).insert(7, aString);
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   182
            throw new RuntimeException("Test failed: should have thrown IndexOutOfBoundsException");
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   183
        } catch (IndexOutOfBoundsException soob) {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   184
            // expected: passed
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   185
        } catch (Throwable t) {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   186
            throw new RuntimeException("Test failed: should have thrown IndexOutOfBoundsException, but instead threw " + t.getMessage());
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   187
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   188
        }
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   189
    }
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   190
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   191
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   192
    public void insertintdouble() {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   193
        double d = 99d;
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   194
        StringBuilder sb = new StringBuilder("012345");
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   195
        assertEquals( sb.insert( 2, d ).toString(), "0199.02345");    }
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   196
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   197
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   198
    public void insertintfloat() {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   199
        float f = 99.0f;
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   200
        StringBuilder sb = new StringBuilder("012345");
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   201
        assertEquals( sb.insert( 2, f ).toString(), "0199.02345");    }
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   202
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   203
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   204
    public void insertintint() {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   205
        int i = 99;
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   206
        StringBuilder sb = new StringBuilder("012345");
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   207
        assertEquals( sb.insert( 2, i ).toString(), "01992345");
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   208
    }
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   209
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   210
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   211
    public void insertintlong() {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   212
        long l = 99;
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   213
        StringBuilder sb = new StringBuilder("012345");
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   214
        assertEquals( sb.insert( 2, l ).toString(), "01992345");    }
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   215
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   216
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   217
    public void insertintObject() {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   218
        StringBuilder sb = new StringBuilder("012345");
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   219
        List<String> ls = new ArrayList<String>();
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   220
        ls.add("A"); ls.add("B");
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   221
        String lsString = ls.toString();
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   222
        assertEquals( sb.insert(2, ls).toString(), "01"+lsString+"2345");
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   223
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   224
        try {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   225
            sb.insert(sb.length()+1, ls);
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   226
            throw new RuntimeException("Test failed: should have thrown StringIndexOutOfBoundsException");
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   227
        } catch (StringIndexOutOfBoundsException soob) {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   228
            // expected: passed
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   229
        } catch (Throwable t) {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   230
            throw new RuntimeException("Test failed: should have thrown StringIndexOutOfBoundsException, but instead threw:"
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   231
                    + t);
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   232
        }
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   233
    }
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   234
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   235
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   236
    public void lastIndexOfString() {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   237
        String xyz = "xyz";
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   238
        String xyz3 = "xyzxyzxyz";
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   239
        StringBuilder sb = new StringBuilder(xyz3);
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   240
        int pos = sb.lastIndexOf("xyz");
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   241
        assertEquals( pos, 2*xyz.length() );
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   242
    }
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   243
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   244
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   245
    public void lastIndexOfStringint() {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   246
        StringBuilder sb = new StringBuilder("xyzxyzxyz");
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   247
        int pos = sb.lastIndexOf("xyz",5);
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   248
        assertEquals( pos, 3 );
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   249
        pos = sb.lastIndexOf("xyz", 6);
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   250
        assertEquals( pos, 6 );
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   251
    }
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   252
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   253
    public void assertEquals( String actual, String expected) {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   254
        if (!actual.equals( expected )) {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   255
            throw new RuntimeException( "Test failed: actual = '" + actual +
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   256
                    "', expected = '" + expected + "'");
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   257
        }
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   258
    }
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   259
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   260
    public void assertEquals( int actual, int expected) {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   261
        if (actual != expected) {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   262
            throw new RuntimeException( "Test failed: actual = '" + actual +
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   263
                    "', expected = '" + expected + "'");
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   264
        }
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   265
    }
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents:
diff changeset
   266
}