2
|
1 |
/*
|
|
2 |
* Copyright 2001-2003 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 4808962
|
|
26 |
* @summary Unit tests for String regex methods
|
|
27 |
*/
|
|
28 |
|
|
29 |
|
|
30 |
public class Regex {
|
|
31 |
|
|
32 |
static void ck(boolean x, boolean ans) throws Exception {
|
|
33 |
if (x != ans)
|
|
34 |
throw new Exception("Test failed");
|
|
35 |
}
|
|
36 |
|
|
37 |
static void ck(String x, String ans) throws Exception {
|
|
38 |
if (!x.equals(ans))
|
|
39 |
throw new Exception("Test failed");
|
|
40 |
}
|
|
41 |
|
|
42 |
static void ck(String[] x, String[] ans) throws Exception {
|
|
43 |
if (x.length != ans.length)
|
|
44 |
throw new Exception("Test failed");
|
|
45 |
for (int i = 0; i < x.length; i++) {
|
|
46 |
if (!x[i].equals(ans[i]))
|
|
47 |
throw new Exception("Test failed");
|
|
48 |
}
|
|
49 |
}
|
|
50 |
|
|
51 |
static void testLiteralReplacement() throws Exception {
|
|
52 |
// Test straightforward replacement
|
|
53 |
String data = "abcdefghi";
|
|
54 |
String result = data.replace("def", "abc");
|
|
55 |
if (!result.equals("abcabcghi"))
|
|
56 |
throw new Exception("Test failed");
|
|
57 |
|
|
58 |
// Test replacement with target that has metacharacters
|
|
59 |
data = "abc(def)?ghi";
|
|
60 |
result = data.replace("(def)?", "abc");
|
|
61 |
if (!result.equals("abcabcghi"))
|
|
62 |
throw new Exception("Test failed");
|
|
63 |
|
|
64 |
// Test replacement with replacement that has metacharacters
|
|
65 |
data = "abcdefghi";
|
|
66 |
result = data.replace("def", "\\ab$c");
|
|
67 |
if (!result.equals("abc\\ab$cghi"))
|
|
68 |
throw new Exception("Test failed");
|
|
69 |
}
|
|
70 |
|
|
71 |
public static void main(String[] args) throws Exception {
|
|
72 |
|
|
73 |
// These don't need to be thorough, they just need to check
|
|
74 |
// that we're properly hooked up to java.util.regex
|
|
75 |
|
|
76 |
String foo = "boo:and:foo";
|
|
77 |
|
|
78 |
ck(foo.matches("b+"), false);
|
|
79 |
ck(foo.matches("o+"), false);
|
|
80 |
ck(foo.matches("b..:and:f.*"), true);
|
|
81 |
|
|
82 |
ck(foo.replaceAll("oo", "uu"), "buu:and:fuu");
|
|
83 |
ck(foo.replaceAll("o+", "<$0>"), "b<oo>:and:f<oo>");
|
|
84 |
|
|
85 |
ck(foo.replaceFirst("oo", "uu"), "buu:and:foo");
|
|
86 |
ck(foo.replaceFirst("o+", "<$0>"), "b<oo>:and:foo");
|
|
87 |
|
|
88 |
ck(foo.split(":"), new String[] { "boo", "and", "foo" });
|
|
89 |
ck(foo.split("o"), new String[] { "b", "", ":and:f" });
|
|
90 |
|
|
91 |
ck(foo.split(":", 2), new String[] { "boo", "and:foo" });
|
|
92 |
ck(foo.split("o", -2), new String[] { "b", "", ":and:f", "", "" });
|
|
93 |
|
|
94 |
testLiteralReplacement();
|
|
95 |
}
|
|
96 |
|
|
97 |
|
|
98 |
}
|