ReplaceTest.java revision f6c387128427e121477c1b32ad35cdcaa5101ba3
1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements.  See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License.  You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.apache.harmony.regex.tests.java.util.regex;
18
19import dalvik.annotation.TestTargetClass;
20import dalvik.annotation.TestTargets;
21import dalvik.annotation.TestTargetNew;
22import dalvik.annotation.TestLevel;
23
24import junit.framework.TestCase;
25import java.util.regex.Pattern;
26import java.util.regex.Matcher;
27import java.util.regex.PatternSyntaxException;
28
29@TestTargetClass(Matcher.class)
30public class ReplaceTest extends TestCase {
31    @TestTargets({
32        @TestTargetNew(
33            level = TestLevel.PARTIAL_COMPLETE,
34            notes = "Verifies the basic functionality of replaceFirst(java.lang.String) & replaceAll(java.lang.String) methods.",
35            method = "replaceFirst",
36            args = {java.lang.String.class}
37        ),
38        @TestTargetNew(
39            level = TestLevel.PARTIAL_COMPLETE,
40            notes = "Verifies the basic functionality of replaceFirst(java.lang.String) & replaceAll(java.lang.String) methods.",
41            method = "replaceAll",
42            args = {java.lang.String.class}
43        )
44    })
45    public void testSimpleReplace() throws PatternSyntaxException {
46        String target, pattern, repl;
47
48        target = "foobarfobarfoofo1barfort";
49        pattern = "fo[^o]";
50        repl = "xxx";
51
52        Pattern p = Pattern.compile(pattern);
53        Matcher m = p.matcher(target);
54
55        assertEquals("foobarxxxarfoofo1barfort", m.replaceFirst(repl));
56        assertEquals("foobarxxxarfooxxxbarxxxt", m.replaceAll(repl));
57    }
58
59    @TestTargets({
60        @TestTargetNew(
61            level = TestLevel.PARTIAL_COMPLETE,
62            notes = "Verifies the functionality of replaceFirst(java.lang.String) & replaceAll(java.lang.String) methods.",
63            method = "replaceFirst",
64            args = {java.lang.String.class}
65        ),
66        @TestTargetNew(
67            level = TestLevel.PARTIAL_COMPLETE,
68            notes = "Verifies the functionality of replaceFirst(java.lang.String) & replaceAll(java.lang.String) methods.",
69            method = "replaceAll",
70            args = {java.lang.String.class}
71        )
72    })
73    public void testCaptureReplace() {
74        String target, pattern, repl, s;
75        Pattern p = null;
76        Matcher m;
77
78        target = "[31]foo;bar[42];[99]xyz";
79        pattern = "\\[([0-9]+)\\]([a-z]+)";
80        repl = "$2[$1]";
81
82        p = Pattern.compile(pattern);
83        m = p.matcher(target);
84        s = m.replaceFirst(repl);
85        assertEquals("foo[31];bar[42];[99]xyz", s);
86        s = m.replaceAll(repl);
87        assertEquals("foo[31];bar[42];xyz[99]", s);
88
89        target = "[31]foo(42)bar{63}zoo;[12]abc(34)def{56}ghi;{99}xyz[88]xyz(77)xyz;";
90        pattern = "\\[([0-9]+)\\]([a-z]+)\\(([0-9]+)\\)([a-z]+)\\{([0-9]+)\\}([a-z]+)";
91        repl = "[$5]$6($3)$4{$1}$2";
92        p = Pattern.compile(pattern);
93        m = p.matcher(target);
94        s = m.replaceFirst(repl);
95        // System.out.println(s);
96        assertEquals("[63]zoo(42)bar{31}foo;[12]abc(34)def{56}ghi;{99}xyz[88]xyz(77)xyz;", s
97                );
98        s = m.replaceAll(repl);
99        // System.out.println(s);
100        assertEquals("[63]zoo(42)bar{31}foo;[56]ghi(34)def{12}abc;{99}xyz[88]xyz(77)xyz;", s
101                );
102    }
103
104    @TestTargetNew(
105        level = TestLevel.PARTIAL_COMPLETE,
106        notes = "Verifies the functionality of replaceAll(java.lang.String) method with backslash chars.",
107        method = "replaceAll",
108        args = {java.lang.String.class}
109    )
110    public void testEscapeReplace() {
111        String target, pattern, repl, s;
112
113        target = "foo'bar''foo";
114        pattern = "'";
115        repl = "\\'";
116        s = target.replaceAll(pattern, repl);
117        assertEquals("foo'bar''foo", s);
118        repl = "\\\\'";
119        s = target.replaceAll(pattern, repl);
120        assertEquals("foo\\'bar\\'\\'foo", s);
121        repl = "\\$3";
122        s = target.replaceAll(pattern, repl);
123        assertEquals("foo$3bar$3$3foo", s);
124    }
125}
126