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.tests.java.util.regex;
18
19import java.util.regex.Matcher;
20import java.util.regex.Pattern;
21import java.util.regex.PatternSyntaxException;
22
23import junit.framework.TestCase;
24
25@SuppressWarnings("nls")
26public class ReplaceTest extends TestCase {
27
28    public void testSimpleReplace() throws PatternSyntaxException {
29        String target, pattern, repl;
30
31        target = "foobarfobarfoofo1";
32        pattern = "fo[^o]";
33        repl = "xxx";
34
35        Pattern p = Pattern.compile(pattern);
36        Matcher m = p.matcher(target);
37
38        assertEquals("foobarxxxarfoofo1", m.replaceFirst(repl));
39        assertEquals("foobarxxxarfooxxx", m.replaceAll(repl));
40    }
41
42    public void testCaptureReplace() {
43        String target, pattern, repl, s;
44        Pattern p = null;
45        Matcher m;
46
47        target = "[31]foo;bar[42];[99]xyz";
48        pattern = "\\[([0-9]+)\\]([a-z]+)";
49        repl = "$2[$1]";
50
51        p = Pattern.compile(pattern);
52        m = p.matcher(target);
53        s = m.replaceFirst(repl);
54        assertEquals("foo[31];bar[42];[99]xyz", s);
55        s = m.replaceAll(repl);
56        assertEquals("foo[31];bar[42];xyz[99]", s);
57
58        target = "[31]foo(42)bar{63}zoo;[12]abc(34)def{56}ghi;{99}xyz[88]xyz(77)xyz;";
59        pattern = "\\[([0-9]+)\\]([a-z]+)\\(([0-9]+)\\)([a-z]+)\\{([0-9]+)\\}([a-z]+)";
60        repl = "[$5]$6($3)$4{$1}$2";
61        p = Pattern.compile(pattern);
62        m = p.matcher(target);
63        s = m.replaceFirst(repl);
64        // System.out.println(s);
65        assertEquals(
66                "[63]zoo(42)bar{31}foo;[12]abc(34)def{56}ghi;{99}xyz[88]xyz(77)xyz;",
67                s);
68        s = m.replaceAll(repl);
69        // System.out.println(s);
70        assertEquals(
71                "[63]zoo(42)bar{31}foo;[56]ghi(34)def{12}abc;{99}xyz[88]xyz(77)xyz;",
72                s);
73    }
74
75    public void testEscapeReplace() {
76        String target, pattern, repl, s;
77
78        target = "foo'bar''foo";
79        pattern = "'";
80        repl = "\\'";
81        s = target.replaceAll(pattern, repl);
82        assertEquals("foo'bar''foo", s);
83        repl = "\\\\'";
84        s = target.replaceAll(pattern, repl);
85        assertEquals("foo\\'bar\\'\\'foo", s);
86        repl = "\\$3";
87        s = target.replaceAll(pattern, repl);
88        assertEquals("foo$3bar$3$3foo", s);
89    }
90}
91