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