Path: blob/master/test/jdk/java/lang/StringBuilder/BuilderForwarding.java
41149 views
/*1* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 620678026* @summary Test forwarding of methods to super in StringBuilder27* @author Jim Gish <[email protected]>28*/2930import java.util.ArrayList;31import java.util.List;3233public class BuilderForwarding {34private static final String A_STRING_BUFFER_VAL = "aStringBuffer";35private static final String A_STRING_BUILDER_VAL = "aStringBuilder";36private static final String A_STRING_VAL = "aString";37private static final String NON_EMPTY_VAL = "NonEmpty";3839public BuilderForwarding() {40System.out.println( "Starting BuilderForwarding");41}4243public static void main(String... args) {44new BuilderForwarding().executeTestMethods();45}4647public void executeTestMethods() {48appendCharSequence();49indexOfString();50indexOfStringIntNull();51indexOfStringNull();52indexOfStringint();53insertintCharSequence();54insertintObject();55insertintboolean();56insertintchar();57insertintdouble();58insertintfloat();59insertintint();60insertintlong();61lastIndexOfString();62lastIndexOfStringint();63}6465public void appendCharSequence() {66// three different flavors of CharSequence67CharSequence aString = A_STRING_VAL;68CharSequence aStringBuilder = new StringBuilder(A_STRING_BUILDER_VAL);69CharSequence aStringBuffer = new StringBuffer(A_STRING_BUFFER_VAL);7071assertEquals( /*actual*/ new StringBuilder().append(aString).toString(), /*expected*/ A_STRING_VAL );72assertEquals( new StringBuilder().append(aStringBuilder).toString(), A_STRING_BUILDER_VAL );73assertEquals( new StringBuilder().append(aStringBuffer).toString(), A_STRING_BUFFER_VAL );7475assertEquals( /*actual*/ new StringBuilder(NON_EMPTY_VAL).append(aString).toString(), NON_EMPTY_VAL+A_STRING_VAL );76assertEquals( new StringBuilder(NON_EMPTY_VAL).append(aStringBuilder).toString(), NON_EMPTY_VAL+A_STRING_BUILDER_VAL );77assertEquals( new StringBuilder(NON_EMPTY_VAL).append(aStringBuffer).toString(), NON_EMPTY_VAL+A_STRING_BUFFER_VAL );78}798081public void indexOfString() {82StringBuilder sb = new StringBuilder();83// should be NPE if null passed84try {8586sb.indexOf(null);87throw new RuntimeException("Test failed: should have thrown NPE");888990} catch (NullPointerException npe) {91// expected: passed92} catch (Throwable t) {93throw new RuntimeException("Test failed: should have thrown NPE. Instead threw "94+ t);95}96sb = new StringBuilder("xyz");97assertEquals( sb.indexOf("y"), 1 );98assertEquals( sb.indexOf("not found"), -1 );99}100101102public void indexOfStringint() {103StringBuilder sb = new StringBuilder();104// should be NPE if null passed105try {106sb.indexOf(null,1);107throw new RuntimeException("Test failed: should have thrown NPE");108} catch (NullPointerException npe) {109// expected: passed110} catch (Throwable t) {111throw new RuntimeException("Test failed: should have thrown NPE");112}113sb = new StringBuilder("xyyz");114assertEquals( sb.indexOf("y",0), 1 );115assertEquals( sb.indexOf("y",1), 1 );116assertEquals( sb.indexOf("y",2), 2 );117assertEquals( sb.indexOf("not found"), -1 );118}119120121public void indexOfStringIntNull() {122StringBuffer sb = new StringBuffer();123// should be NPE if null passed124try {125sb.indexOf(null,1);126throw new RuntimeException("Test failed: should have thrown NPE");127} catch (NullPointerException npe) {128// expected: passed129} catch (Throwable t) {130throw new RuntimeException("Test failed: should have thrown NPE. Instead threw "131+ t);132}133}134135136public void indexOfStringNull() {137StringBuilder sb = new StringBuilder();138139// should be NPE if null passed140try {141sb.indexOf(null);142throw new RuntimeException("Test failed: should have thrown NPE");143} catch (NullPointerException npe) {144// expected: passed145} catch (Throwable t) {146throw new RuntimeException("Test failed: should have thrown NPE. Instead threw "147+ t);148}149}150151152public void insertintboolean() {153boolean b = true;154StringBuilder sb = new StringBuilder("012345");155assertEquals( sb.insert( 2, b).toString(), "01true2345");156}157158159public void insertintchar() {160char c = 'C';161StringBuilder sb = new StringBuilder("012345");162assertEquals( sb.insert( 2, c ).toString(), "01C2345");163}164165166public void insertintCharSequence() {167final String initString = "012345";168// three different flavors of CharSequence169CharSequence aString = A_STRING_VAL;170CharSequence aStringBuilder = new StringBuilder(A_STRING_BUILDER_VAL);171CharSequence aStringBuffer = new StringBuffer(A_STRING_BUFFER_VAL);172173assertEquals( new StringBuilder(initString).insert(2, aString).toString(), "01"+A_STRING_VAL+"2345" );174175assertEquals( new StringBuilder(initString).insert(2, aStringBuilder).toString(), "01"+A_STRING_BUILDER_VAL+"2345" );176177assertEquals( new StringBuilder(initString).insert(2, aStringBuffer).toString(), "01"+A_STRING_BUFFER_VAL+"2345" );178179try {180new StringBuilder(initString).insert(7, aString);181throw new RuntimeException("Test failed: should have thrown IndexOutOfBoundsException");182} catch (IndexOutOfBoundsException soob) {183// expected: passed184} catch (Throwable t) {185throw new RuntimeException("Test failed: should have thrown IndexOutOfBoundsException, but instead threw " + t.getMessage());186187}188}189190191public void insertintdouble() {192double d = 99d;193StringBuilder sb = new StringBuilder("012345");194assertEquals( sb.insert( 2, d ).toString(), "0199.02345"); }195196197public void insertintfloat() {198float f = 99.0f;199StringBuilder sb = new StringBuilder("012345");200assertEquals( sb.insert( 2, f ).toString(), "0199.02345"); }201202203public void insertintint() {204int i = 99;205StringBuilder sb = new StringBuilder("012345");206assertEquals( sb.insert( 2, i ).toString(), "01992345");207}208209210public void insertintlong() {211long l = 99;212StringBuilder sb = new StringBuilder("012345");213assertEquals( sb.insert( 2, l ).toString(), "01992345"); }214215216public void insertintObject() {217StringBuilder sb = new StringBuilder("012345");218List<String> ls = new ArrayList<String>();219ls.add("A"); ls.add("B");220String lsString = ls.toString();221assertEquals( sb.insert(2, ls).toString(), "01"+lsString+"2345");222223try {224sb.insert(sb.length()+1, ls);225throw new RuntimeException("Test failed: should have thrown StringIndexOutOfBoundsException");226} catch (StringIndexOutOfBoundsException soob) {227// expected: passed228} catch (Throwable t) {229throw new RuntimeException("Test failed: should have thrown StringIndexOutOfBoundsException, but instead threw:"230+ t);231}232}233234235public void lastIndexOfString() {236String xyz = "xyz";237String xyz3 = "xyzxyzxyz";238StringBuilder sb = new StringBuilder(xyz3);239int pos = sb.lastIndexOf("xyz");240assertEquals( pos, 2*xyz.length() );241}242243244public void lastIndexOfStringint() {245StringBuilder sb = new StringBuilder("xyzxyzxyz");246int pos = sb.lastIndexOf("xyz",5);247assertEquals( pos, 3 );248pos = sb.lastIndexOf("xyz", 6);249assertEquals( pos, 6 );250}251252public void assertEquals( String actual, String expected) {253if (!actual.equals( expected )) {254throw new RuntimeException( "Test failed: actual = '" + actual +255"', expected = '" + expected + "'");256}257}258259public void assertEquals( int actual, int expected) {260if (actual != expected) {261throw new RuntimeException( "Test failed: actual = '" + actual +262"', expected = '" + expected + "'");263}264}265}266267268