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