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 package test.java.lang.StringBuffer;
32 
33 import java.util.ArrayList;
34 import java.util.List;
35 import org.testng.annotations.Test;
36 
37 public class BufferForwarding {
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 
BufferForwarding()43     public BufferForwarding() {
44         System.out.println( "Starting BufferForwarding");
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 BufferForwarding().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 
indexOfString()87     void indexOfString() {
88         StringBuffer sb = new StringBuffer("xyz");
89         assertEquals( sb.indexOf("y"), 1 );
90         assertEquals( sb.indexOf("not found"), -1 );
91     }
92 
93 
indexOfStringint()94     public void indexOfStringint() {
95         StringBuffer sb = new StringBuffer("xyyz");
96         assertEquals( sb.indexOf("y",0), 1 );
97         assertEquals( sb.indexOf("y",1), 1 );
98         assertEquals( sb.indexOf("y",2), 2 );
99         assertEquals( sb.indexOf("not found"), -1 );
100     }
101 
102 
indexOfStringIntNull()103     public void indexOfStringIntNull() {
104         StringBuffer sb = new StringBuffer();
105         // should be NPE if null passed
106         try {
107             sb.indexOf(null,1);
108             throw new RuntimeException("Test failed: should have thrown NPE");
109         } catch (NullPointerException npe) {
110             // expected: passed
111         } catch (Throwable t) {
112             throw new RuntimeException("Test failed: should have thrown NPE. Instead threw "
113                     + t);
114         }
115     }
116 
117 
indexOfStringNull()118     public void indexOfStringNull() {
119         StringBuffer sb = new StringBuffer();
120 
121         // should be NPE if null passed
122         try {
123             sb.indexOf(null);
124             throw new RuntimeException("Test failed: should have thrown NPE");
125         } catch (NullPointerException npe) {
126             // expected: passed
127         } catch (Throwable t) {
128             throw new RuntimeException("Test failed: should have thrown NPE. Instead threw "
129                     + t);
130         }
131     }
132 
133 
insertintboolean()134     public void insertintboolean() {
135         boolean b = true;
136         StringBuffer sb = new StringBuffer("012345");
137         assertEquals( sb.insert( 2, b).toString(), "01true2345");
138     }
139 
140 
insertintchar()141     public void insertintchar() {
142         char c = 'C';
143         StringBuffer sb = new StringBuffer("012345");
144         assertEquals( sb.insert( 2, c ).toString(), "01C2345");
145     }
146 
147 
insertintCharSequence()148     public void insertintCharSequence() {
149         final String initString = "012345";
150         // three different flavors of CharSequence
151         CharSequence aString = A_STRING_VAL;
152         CharSequence aStringBuilder = new StringBuilder(A_STRING_BUILDER_VAL);
153         CharSequence aStringBuffer = new StringBuffer(A_STRING_BUFFER_VAL);
154 
155         assertEquals( new StringBuffer(initString).insert(2, aString).toString(), "01"+A_STRING_VAL+"2345" );
156 
157         assertEquals( new StringBuffer(initString).insert(2, aStringBuilder).toString(), "01"+A_STRING_BUILDER_VAL+"2345" );
158 
159         assertEquals( new StringBuffer(initString).insert(2, aStringBuffer).toString(), "01"+A_STRING_BUFFER_VAL+"2345" );
160 
161         try {
162             new StringBuffer(initString).insert(7, aString);
163             throw new RuntimeException("Test failed: should have thrown IndexOutOfBoundsException");
164         } catch (IndexOutOfBoundsException soob) {
165             // expected: passed
166         } catch (Throwable t) {
167             throw new RuntimeException("Test failed: should have thrown IndexOutOfBoundsException, but instead threw " + t.getMessage());
168 
169         }
170     }
171 
insertintdouble()172     public void insertintdouble() {
173         double d = 99d;
174         StringBuffer sb = new StringBuffer("012345");
175         assertEquals( sb.insert( 2, d ).toString(), "0199.02345");    }
176 
177 
insertintfloat()178     public void insertintfloat() {
179         float f = 99.0f;
180         StringBuffer sb = new StringBuffer("012345");
181         assertEquals( sb.insert( 2, f ).toString(), "0199.02345");    }
182 
183 
insertintint()184     public void insertintint() {
185         int i = 99;
186         StringBuffer sb = new StringBuffer("012345");
187         assertEquals( sb.insert( 2, i ).toString(), "01992345");
188     }
189 
190 
insertintlong()191     public void insertintlong() {
192         long l = 99;
193         StringBuffer sb = new StringBuffer("012345");
194         assertEquals( sb.insert( 2, l ).toString(), "01992345");    }
195 
insertintObject()196     public void insertintObject() {
197         StringBuffer sb = new StringBuffer("012345");
198         List<String> ls = new ArrayList<String>();
199         ls.add("A"); ls.add("B");
200         String lsString = ls.toString();
201         assertEquals( sb.insert(2, ls).toString(), "01"+lsString+"2345");
202 
203         try {
204             sb.insert(sb.length()+1, ls);
205             throw new RuntimeException("Test failed: should have thrown StringIndexOutOfBoundsException");
206         } catch (StringIndexOutOfBoundsException soob) {
207             // expected: passed
208         } catch (Throwable t) {
209             throw new RuntimeException("Test failed: should have thrown StringIndexOutOfBoundsException, but instead threw:"
210                     + t);
211 
212         }
213     }
214 
215 
216 
lastIndexOfString()217     public void lastIndexOfString() {
218         String xyz = "xyz";
219         String xyz3 = "xyzxyzxyz";
220         StringBuffer sb = new StringBuffer(xyz3);
221         int pos = sb.lastIndexOf("xyz");
222         assertEquals( pos, 2*xyz.length() );
223     }
224 
lastIndexOfStringint()225     public void lastIndexOfStringint() {
226         StringBuffer sb = new StringBuffer("xyzxyzxyz");
227         int pos = sb.lastIndexOf("xyz",5);
228         assertEquals( pos, 3 );
229         pos = sb.lastIndexOf("xyz", 6);
230         assertEquals( pos, 6 );
231     }
232 
assertEquals( String actual, String expected)233     public void assertEquals( String actual, String expected) {
234         if (!actual.equals( expected )) {
235             throw new RuntimeException( "Test failed: actual = '" + actual +
236                     "', expected = '" + expected + "'");
237         }
238     }
239 
assertEquals( int actual, int expected)240     public void assertEquals( int actual, int expected) {
241         if (actual != expected) {
242             throw new RuntimeException( "Test failed: actual = '" + actual +
243                     "', expected = '" + expected + "'");
244         }
245     }
246 }
247