Path: blob/master/test/jdk/java/util/StringTokenizer/ResetPos.java
41149 views
/*1* Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 4238266 @summary Reset the currentposition of25* StringTokenizer if delimiters changed in a invocation of nextToken() after26* invoking hasMoreTokens()27*/282930import java.util.StringTokenizer;3132public class ResetPos {3334static void checkValue(String val, String checkVal) {35System.out.println("Comparing \""+ val + "\" <----> \"" + checkVal +36"\"");37if (!val.equals(checkVal))38throw new RuntimeException("Test failed");39}4041public static void main(String[] argv) {42// Simple test43StringTokenizer st1 = new StringTokenizer("ab", "b", true);44checkValue("a", st1.nextToken("b"));45st1.hasMoreTokens();46checkValue("b", st1.nextToken(""));4748// Test with retDelims set to true49StringTokenizer st2 = new StringTokenizer("abcd efg", "abc", true);50st2.hasMoreTokens();51checkValue("a", st2.nextToken("bc"));52st2.hasMoreTokens();53checkValue("b", st2.nextToken());54st2.hasMoreTokens();55checkValue("cd", st2.nextToken(" ef"));56st2.hasMoreTokens();57checkValue(" ", st2.nextToken(" "));58st2.hasMoreTokens();59checkValue("ef", st2.nextToken("g"));60st2.hasMoreTokens();61checkValue("g", st2.nextToken("g"));6263// Test with changing delimiters64StringTokenizer st3 = new StringTokenizer("this is,a interesting,sentence of small, words", ",");65st3.hasMoreTokens();66checkValue("this is", st3.nextToken()); // "this is"67st3.hasMoreTokens();68checkValue(",a", st3.nextToken(" ")); // ",a"69st3.hasMoreTokens();70checkValue(" interesting", st3.nextToken(",")); // " interesting"71st3.hasMoreTokens();72checkValue(",sentence", st3.nextToken(" ")); // ",sentence"73st3.hasMoreTokens();74checkValue(" of small", st3.nextToken(",")); // " of small"75st3.hasMoreTokens();76checkValue(" words", st3.nextToken()); // " words"77}78}798081