Path: blob/master/test/jdk/java/lang/StringBuffer/BufferForwarding.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 StringBuffer27* @author Jim Gish <[email protected]>28*/2930import java.util.ArrayList;31import java.util.List;3233public class BufferForwarding {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 BufferForwarding() {40System.out.println( "Starting BufferForwarding");41}4243public static void main(String... args) {44new BufferForwarding().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}7980void indexOfString() {81StringBuffer sb = new StringBuffer("xyz");82assertEquals( sb.indexOf("y"), 1 );83assertEquals( sb.indexOf("not found"), -1 );84}858687public void indexOfStringint() {88StringBuffer sb = new StringBuffer("xyyz");89assertEquals( sb.indexOf("y",0), 1 );90assertEquals( sb.indexOf("y",1), 1 );91assertEquals( sb.indexOf("y",2), 2 );92assertEquals( sb.indexOf("not found"), -1 );93}949596public void indexOfStringIntNull() {97StringBuffer sb = new StringBuffer();98// should be NPE if null passed99try {100sb.indexOf(null,1);101throw new RuntimeException("Test failed: should have thrown NPE");102} catch (NullPointerException npe) {103// expected: passed104} catch (Throwable t) {105throw new RuntimeException("Test failed: should have thrown NPE. Instead threw "106+ t);107}108}109110111public void indexOfStringNull() {112StringBuffer sb = new StringBuffer();113114// should be NPE if null passed115try {116sb.indexOf(null);117throw new RuntimeException("Test failed: should have thrown NPE");118} catch (NullPointerException npe) {119// expected: passed120} catch (Throwable t) {121throw new RuntimeException("Test failed: should have thrown NPE. Instead threw "122+ t);123}124}125126127public void insertintboolean() {128boolean b = true;129StringBuffer sb = new StringBuffer("012345");130assertEquals( sb.insert( 2, b).toString(), "01true2345");131}132133134public void insertintchar() {135char c = 'C';136StringBuffer sb = new StringBuffer("012345");137assertEquals( sb.insert( 2, c ).toString(), "01C2345");138}139140141public void insertintCharSequence() {142final String initString = "012345";143// three different flavors of CharSequence144CharSequence aString = A_STRING_VAL;145CharSequence aStringBuilder = new StringBuilder(A_STRING_BUILDER_VAL);146CharSequence aStringBuffer = new StringBuffer(A_STRING_BUFFER_VAL);147148assertEquals( new StringBuffer(initString).insert(2, aString).toString(), "01"+A_STRING_VAL+"2345" );149150assertEquals( new StringBuffer(initString).insert(2, aStringBuilder).toString(), "01"+A_STRING_BUILDER_VAL+"2345" );151152assertEquals( new StringBuffer(initString).insert(2, aStringBuffer).toString(), "01"+A_STRING_BUFFER_VAL+"2345" );153154try {155new StringBuffer(initString).insert(7, aString);156throw new RuntimeException("Test failed: should have thrown IndexOutOfBoundsException");157} catch (IndexOutOfBoundsException soob) {158// expected: passed159} catch (Throwable t) {160throw new RuntimeException("Test failed: should have thrown IndexOutOfBoundsException, but instead threw " + t.getMessage());161162}163}164165public void insertintdouble() {166double d = 99d;167StringBuffer sb = new StringBuffer("012345");168assertEquals( sb.insert( 2, d ).toString(), "0199.02345"); }169170171public void insertintfloat() {172float f = 99.0f;173StringBuffer sb = new StringBuffer("012345");174assertEquals( sb.insert( 2, f ).toString(), "0199.02345"); }175176177public void insertintint() {178int i = 99;179StringBuffer sb = new StringBuffer("012345");180assertEquals( sb.insert( 2, i ).toString(), "01992345");181}182183184public void insertintlong() {185long l = 99;186StringBuffer sb = new StringBuffer("012345");187assertEquals( sb.insert( 2, l ).toString(), "01992345"); }188189public void insertintObject() {190StringBuffer sb = new StringBuffer("012345");191List<String> ls = new ArrayList<String>();192ls.add("A"); ls.add("B");193String lsString = ls.toString();194assertEquals( sb.insert(2, ls).toString(), "01"+lsString+"2345");195196try {197sb.insert(sb.length()+1, ls);198throw new RuntimeException("Test failed: should have thrown StringIndexOutOfBoundsException");199} catch (StringIndexOutOfBoundsException soob) {200// expected: passed201} catch (Throwable t) {202throw new RuntimeException("Test failed: should have thrown StringIndexOutOfBoundsException, but instead threw:"203+ t);204205}206}207208209210public void lastIndexOfString() {211String xyz = "xyz";212String xyz3 = "xyzxyzxyz";213StringBuffer sb = new StringBuffer(xyz3);214int pos = sb.lastIndexOf("xyz");215assertEquals( pos, 2*xyz.length() );216}217218public void lastIndexOfStringint() {219StringBuffer sb = new StringBuffer("xyzxyzxyz");220int pos = sb.lastIndexOf("xyz",5);221assertEquals( pos, 3 );222pos = sb.lastIndexOf("xyz", 6);223assertEquals( pos, 6 );224}225226public void assertEquals( String actual, String expected) {227if (!actual.equals( expected )) {228throw new RuntimeException( "Test failed: actual = '" + actual +229"', expected = '" + expected + "'");230}231}232233public void assertEquals( int actual, int expected) {234if (actual != expected) {235throw new RuntimeException( "Test failed: actual = '" + actual +236"', expected = '" + expected + "'");237}238}239}240241242