Path: blob/master/test/jdk/java/lang/String/Regex.java
41149 views
/*1* Copyright (c) 2001, 2003, 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 480896225* @summary Unit tests for String regex methods26*/272829public class Regex {3031static void ck(boolean x, boolean ans) throws Exception {32if (x != ans)33throw new Exception("Test failed");34}3536static void ck(String x, String ans) throws Exception {37if (!x.equals(ans))38throw new Exception("Test failed");39}4041static void ck(String[] x, String[] ans) throws Exception {42if (x.length != ans.length)43throw new Exception("Test failed");44for (int i = 0; i < x.length; i++) {45if (!x[i].equals(ans[i]))46throw new Exception("Test failed");47}48}4950static void testLiteralReplacement() throws Exception {51// Test straightforward replacement52String data = "abcdefghi";53String result = data.replace("def", "abc");54if (!result.equals("abcabcghi"))55throw new Exception("Test failed");5657// Test replacement with target that has metacharacters58data = "abc(def)?ghi";59result = data.replace("(def)?", "abc");60if (!result.equals("abcabcghi"))61throw new Exception("Test failed");6263// Test replacement with replacement that has metacharacters64data = "abcdefghi";65result = data.replace("def", "\\ab$c");66if (!result.equals("abc\\ab$cghi"))67throw new Exception("Test failed");68}6970public static void main(String[] args) throws Exception {7172// These don't need to be thorough, they just need to check73// that we're properly hooked up to java.util.regex7475String foo = "boo:and:foo";7677ck(foo.matches("b+"), false);78ck(foo.matches("o+"), false);79ck(foo.matches("b..:and:f.*"), true);8081ck(foo.replaceAll("oo", "uu"), "buu:and:fuu");82ck(foo.replaceAll("o+", "<$0>"), "b<oo>:and:f<oo>");8384ck(foo.replaceFirst("oo", "uu"), "buu:and:foo");85ck(foo.replaceFirst("o+", "<$0>"), "b<oo>:and:foo");8687ck(foo.split(":"), new String[] { "boo", "and", "foo" });88ck(foo.split("o"), new String[] { "b", "", ":and:f" });8990ck(foo.split(":", 2), new String[] { "boo", "and:foo" });91ck(foo.split("o", -2), new String[] { "b", "", ":and:f", "", "" });9293testLiteralReplacement();94}959697}9899100