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