J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1999 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* @test |
| 25 | * @bug 4238266 @summary Reset the currentposition of |
| 26 | * StringTokenizer if delimiters changed in a invocation of nextToken() after |
| 27 | * invoking hasMoreTokens() |
| 28 | */ |
| 29 | |
| 30 | |
| 31 | import java.util.StringTokenizer; |
| 32 | |
| 33 | public class ResetPos { |
| 34 | |
| 35 | static void checkValue(String val, String checkVal) { |
| 36 | System.out.println("Comparing \""+ val + "\" <----> \"" + checkVal + |
| 37 | "\""); |
| 38 | if (!val.equals(checkVal)) |
| 39 | throw new RuntimeException("Test failed"); |
| 40 | } |
| 41 | |
| 42 | public static void main(String[] argv) { |
| 43 | // Simple test |
| 44 | StringTokenizer st1 = new StringTokenizer("ab", "b", true); |
| 45 | checkValue("a", st1.nextToken("b")); |
| 46 | st1.hasMoreTokens(); |
| 47 | checkValue("b", st1.nextToken("")); |
| 48 | |
| 49 | // Test with retDelims set to true |
| 50 | StringTokenizer st2 = new StringTokenizer("abcd efg", "abc", true); |
| 51 | st2.hasMoreTokens(); |
| 52 | checkValue("a", st2.nextToken("bc")); |
| 53 | st2.hasMoreTokens(); |
| 54 | checkValue("b", st2.nextToken()); |
| 55 | st2.hasMoreTokens(); |
| 56 | checkValue("cd", st2.nextToken(" ef")); |
| 57 | st2.hasMoreTokens(); |
| 58 | checkValue(" ", st2.nextToken(" ")); |
| 59 | st2.hasMoreTokens(); |
| 60 | checkValue("ef", st2.nextToken("g")); |
| 61 | st2.hasMoreTokens(); |
| 62 | checkValue("g", st2.nextToken("g")); |
| 63 | |
| 64 | // Test with changing delimiters |
| 65 | StringTokenizer st3 = new StringTokenizer("this is,a interesting,sentence of small, words", ","); |
| 66 | st3.hasMoreTokens(); |
| 67 | checkValue("this is", st3.nextToken()); // "this is" |
| 68 | st3.hasMoreTokens(); |
| 69 | checkValue(",a", st3.nextToken(" ")); // ",a" |
| 70 | st3.hasMoreTokens(); |
| 71 | checkValue(" interesting", st3.nextToken(",")); // " interesting" |
| 72 | st3.hasMoreTokens(); |
| 73 | checkValue(",sentence", st3.nextToken(" ")); // ",sentence" |
| 74 | st3.hasMoreTokens(); |
| 75 | checkValue(" of small", st3.nextToken(",")); // " of small" |
| 76 | st3.hasMoreTokens(); |
| 77 | checkValue(" words", st3.nextToken()); // " words" |
| 78 | } |
| 79 | } |