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 StringBuffer
28  * @author Jim Gish <jim.gish@oracle.com>
29  */
30 
31 import java.util.ArrayList;
32 import java.util.List;
33 
34 public class BufferForwarding {
35     private static final String A_STRING_BUFFER_VAL = "aStringBuffer";
36     private static final String A_STRING_BUILDER_VAL = "aStringBuilder";
37     private static final String A_STRING_VAL = "aString";
38     private static final String NON_EMPTY_VAL = "NonEmpty";
39 
BufferForwarding()40     public BufferForwarding() {
41         System.out.println( "Starting BufferForwarding");
42     }
43 
main(String... args)44     public static void main(String... args) {
45         new BufferForwarding().executeTestMethods();
46     }
47 
executeTestMethods()48     public void executeTestMethods() {
49         appendCharSequence();
50         indexOfString();
51         indexOfStringIntNull();
52         indexOfStringNull();
53         indexOfStringint();
54         insertintCharSequence();
55         insertintObject();
56         insertintboolean();
57         insertintchar();
58         insertintdouble();
59         insertintfloat();
60         insertintint();
61         insertintlong();
62         lastIndexOfString();
63         lastIndexOfStringint();
64     }
65 
appendCharSequence()66     public void appendCharSequence() {
67         // three different flavors of CharSequence
68         CharSequence aString = A_STRING_VAL;
69         CharSequence aStringBuilder = new StringBuilder(A_STRING_BUILDER_VAL);
70         CharSequence aStringBuffer = new StringBuffer(A_STRING_BUFFER_VAL);
71 
72         assertEquals( /*actual*/ new StringBuilder().append(aString).toString(), /*expected*/ A_STRING_VAL );
73         assertEquals( new StringBuilder().append(aStringBuilder).toString(), A_STRING_BUILDER_VAL );
74         assertEquals( new StringBuilder().append(aStringBuffer).toString(), A_STRING_BUFFER_VAL );
75 
76         assertEquals( /*actual*/ new StringBuilder(NON_EMPTY_VAL).append(aString).toString(), NON_EMPTY_VAL+A_STRING_VAL );
77         assertEquals( new StringBuilder(NON_EMPTY_VAL).append(aStringBuilder).toString(), NON_EMPTY_VAL+A_STRING_BUILDER_VAL );
78         assertEquals( new StringBuilder(NON_EMPTY_VAL).append(aStringBuffer).toString(), NON_EMPTY_VAL+A_STRING_BUFFER_VAL );
79     }
80 
indexOfString()81     void indexOfString() {
82         StringBuffer sb = new StringBuffer("xyz");
83         assertEquals( sb.indexOf("y"), 1 );
84         assertEquals( sb.indexOf("not found"), -1 );
85     }
86 
87 
indexOfStringint()88     public void indexOfStringint() {
89         StringBuffer sb = new StringBuffer("xyyz");
90         assertEquals( sb.indexOf("y",0), 1 );
91         assertEquals( sb.indexOf("y",1), 1 );
92         assertEquals( sb.indexOf("y",2), 2 );
93         assertEquals( sb.indexOf("not found"), -1 );
94     }
95 
96 
indexOfStringIntNull()97     public void indexOfStringIntNull() {
98         StringBuffer sb = new StringBuffer();
99         // should be NPE if null passed
100         try {
101             sb.indexOf(null,1);
102             throw new RuntimeException("Test failed: should have thrown NPE");
103         } catch (NullPointerException npe) {
104             // expected: passed
105         } catch (Throwable t) {
106             throw new RuntimeException("Test failed: should have thrown NPE. Instead threw "
107                     + t);
108         }
109     }
110 
111 
indexOfStringNull()112     public void indexOfStringNull() {
113         StringBuffer sb = new StringBuffer();
114 
115         // should be NPE if null passed
116         try {
117             sb.indexOf(null);
118             throw new RuntimeException("Test failed: should have thrown NPE");
119         } catch (NullPointerException npe) {
120             // expected: passed
121         } catch (Throwable t) {
122             throw new RuntimeException("Test failed: should have thrown NPE. Instead threw "
123                     + t);
124         }
125     }
126 
127 
insertintboolean()128     public void insertintboolean() {
129         boolean b = true;
130         StringBuffer sb = new StringBuffer("012345");
131         assertEquals( sb.insert( 2, b).toString(), "01true2345");
132     }
133 
134 
insertintchar()135     public void insertintchar() {
136         char c = 'C';
137         StringBuffer sb = new StringBuffer("012345");
138         assertEquals( sb.insert( 2, c ).toString(), "01C2345");
139     }
140 
141 
insertintCharSequence()142     public void insertintCharSequence() {
143         final String initString = "012345";
144         // three different flavors of CharSequence
145         CharSequence aString = A_STRING_VAL;
146         CharSequence aStringBuilder = new StringBuilder(A_STRING_BUILDER_VAL);
147         CharSequence aStringBuffer = new StringBuffer(A_STRING_BUFFER_VAL);
148 
149         assertEquals( new StringBuffer(initString).insert(2, aString).toString(), "01"+A_STRING_VAL+"2345" );
150 
151         assertEquals( new StringBuffer(initString).insert(2, aStringBuilder).toString(), "01"+A_STRING_BUILDER_VAL+"2345" );
152 
153         assertEquals( new StringBuffer(initString).insert(2, aStringBuffer).toString(), "01"+A_STRING_BUFFER_VAL+"2345" );
154 
155         try {
156             new StringBuffer(initString).insert(7, aString);
157             throw new RuntimeException("Test failed: should have thrown IndexOutOfBoundsException");
158         } catch (IndexOutOfBoundsException soob) {
159             // expected: passed
160         } catch (Throwable t) {
161             throw new RuntimeException("Test failed: should have thrown IndexOutOfBoundsException, but instead threw " + t.getMessage());
162 
163         }
164     }
165 
insertintdouble()166     public void insertintdouble() {
167         double d = 99d;
168         StringBuffer sb = new StringBuffer("012345");
169         assertEquals( sb.insert( 2, d ).toString(), "0199.02345");    }
170 
171 
insertintfloat()172     public void insertintfloat() {
173         float f = 99.0f;
174         StringBuffer sb = new StringBuffer("012345");
175         assertEquals( sb.insert( 2, f ).toString(), "0199.02345");    }
176 
177 
insertintint()178     public void insertintint() {
179         int i = 99;
180         StringBuffer sb = new StringBuffer("012345");
181         assertEquals( sb.insert( 2, i ).toString(), "01992345");
182     }
183 
184 
insertintlong()185     public void insertintlong() {
186         long l = 99;
187         StringBuffer sb = new StringBuffer("012345");
188         assertEquals( sb.insert( 2, l ).toString(), "01992345");    }
189 
insertintObject()190     public void insertintObject() {
191         StringBuffer sb = new StringBuffer("012345");
192         List<String> ls = new ArrayList<String>();
193         ls.add("A"); ls.add("B");
194         String lsString = ls.toString();
195         assertEquals( sb.insert(2, ls).toString(), "01"+lsString+"2345");
196 
197         try {
198             sb.insert(sb.length()+1, ls);
199             throw new RuntimeException("Test failed: should have thrown StringIndexOutOfBoundsException");
200         } catch (StringIndexOutOfBoundsException soob) {
201             // expected: passed
202         } catch (Throwable t) {
203             throw new RuntimeException("Test failed: should have thrown StringIndexOutOfBoundsException, but instead threw:"
204                     + t);
205 
206         }
207     }
208 
209 
210 
lastIndexOfString()211     public void lastIndexOfString() {
212         String xyz = "xyz";
213         String xyz3 = "xyzxyzxyz";
214         StringBuffer sb = new StringBuffer(xyz3);
215         int pos = sb.lastIndexOf("xyz");
216         assertEquals( pos, 2*xyz.length() );
217     }
218 
lastIndexOfStringint()219     public void lastIndexOfStringint() {
220         StringBuffer sb = new StringBuffer("xyzxyzxyz");
221         int pos = sb.lastIndexOf("xyz",5);
222         assertEquals( pos, 3 );
223         pos = sb.lastIndexOf("xyz", 6);
224         assertEquals( pos, 6 );
225     }
226 
assertEquals( String actual, String expected)227     public void assertEquals( String actual, String expected) {
228         if (!actual.equals( expected )) {
229             throw new RuntimeException( "Test failed: actual = '" + actual +
230                     "', expected = '" + expected + "'");
231         }
232     }
233 
assertEquals( int actual, int expected)234     public void assertEquals( int actual, int expected) {
235         if (actual != expected) {
236             throw new RuntimeException( "Test failed: actual = '" + actual +
237                     "', expected = '" + expected + "'");
238         }
239     }
240 }
241