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/**
26 * Tests Matcher methods
27 */
28@SuppressWarnings("nls")
29public class Matcher2Test extends TestCase {
30    public void test_toString() {
31        Pattern p = Pattern.compile("foo");
32        Matcher m = p.matcher("bar");
33        assertNotNull(m.toString());
34    }
35
36    public void testErrorConditions() throws PatternSyntaxException {
37        // Test match cursors in absence of a match
38        Pattern p = Pattern.compile("foo");
39        Matcher m = p.matcher("bar");
40        assertFalse(m.matches());
41
42        try {
43            m.start();
44            fail("IllegalStateException expected");
45        } catch (IllegalStateException e) {
46        }
47
48        try {
49            m.end();
50            fail("IllegalStateException expected");
51        } catch (IllegalStateException e) {
52        }
53
54        try {
55            m.group();
56            fail("IllegalStateException expected");
57        } catch (IllegalStateException e) {
58        }
59
60        try {
61            m.start(1);
62            fail("IllegalStateException expected");
63        } catch (IllegalStateException e) {
64        }
65
66        try {
67            m.end(1);
68            fail("IllegalStateException expected");
69        } catch (IllegalStateException e) {
70        }
71
72        try {
73            m.group(1);
74            fail("IllegalStateException expected");
75        } catch (IllegalStateException e) {
76        }
77
78        // regression test for HARMONY-2418
79        try {
80            m.usePattern(null);
81            fail("IllegalArgumentException expected");
82        } catch (IllegalArgumentException e) {
83            // PASSED
84        }
85    }
86
87    public void testErrorConditions2() throws PatternSyntaxException {
88        // Test match cursors in absence of a match
89        Pattern p = Pattern.compile("(foo[0-9])(bar[a-z])");
90        Matcher m = p.matcher("foo1barzfoo2baryfoozbar5");
91
92        assertTrue(m.find());
93        assertEquals(0, m.start());
94        assertEquals(8, m.end());
95        assertEquals(0, m.start(1));
96        assertEquals(4, m.end(1));
97        assertEquals(4, m.start(2));
98        assertEquals(8, m.end(2));
99
100        try {
101            m.start(3);
102            fail("IndexOutOfBoundsException expected");
103        } catch (IndexOutOfBoundsException e) {
104        }
105
106        try {
107            m.end(3);
108            fail("IndexOutOfBoundsException expected");
109        } catch (IndexOutOfBoundsException e) {
110        }
111
112        try {
113            m.group(3);
114            fail("IndexOutOfBoundsException expected");
115        } catch (IndexOutOfBoundsException e) {
116        }
117
118        try {
119            m.start(-1);
120            fail("IndexOutOfBoundsException expected");
121        } catch (IndexOutOfBoundsException e) {
122        }
123
124        try {
125            m.end(-1);
126            fail("IndexOutOfBoundsException expected");
127        } catch (IndexOutOfBoundsException e) {
128        }
129
130        try {
131            m.group(-1);
132            fail("IndexOutOfBoundsException expected");
133        } catch (IndexOutOfBoundsException e) {
134        }
135
136        assertTrue(m.find());
137        assertEquals(8, m.start());
138        assertEquals(16, m.end());
139        assertEquals(8, m.start(1));
140        assertEquals(12, m.end(1));
141        assertEquals(12, m.start(2));
142        assertEquals(16, m.end(2));
143
144        try {
145            m.start(3);
146            fail("IndexOutOfBoundsException expected");
147        } catch (IndexOutOfBoundsException e) {
148        }
149
150        try {
151            m.end(3);
152            fail("IndexOutOfBoundsException expected");
153        } catch (IndexOutOfBoundsException e) {
154        }
155
156        try {
157            m.group(3);
158            fail("IndexOutOfBoundsException expected");
159        } catch (IndexOutOfBoundsException e) {
160        }
161
162        try {
163            m.start(-1);
164            fail("IndexOutOfBoundsException expected");
165        } catch (IndexOutOfBoundsException e) {
166        }
167
168        try {
169            m.end(-1);
170            fail("IndexOutOfBoundsException expected");
171        } catch (IndexOutOfBoundsException e) {
172        }
173
174        try {
175            m.group(-1);
176            fail("IndexOutOfBoundsException expected");
177        } catch (IndexOutOfBoundsException e) {
178        }
179
180        assertFalse(m.find());
181
182        try {
183            m.start(3);
184            fail("IllegalStateException expected");
185        } catch (IllegalStateException e) {
186        }
187
188        try {
189            m.end(3);
190            fail("IllegalStateException expected");
191        } catch (IllegalStateException e) {
192        }
193
194        try {
195            m.group(3);
196            fail("IllegalStateException expected");
197        } catch (IllegalStateException e) {
198        }
199
200        try {
201            m.start(-1);
202            fail("IllegalStateException expected");
203        } catch (IllegalStateException e) {
204        }
205
206        try {
207            m.end(-1);
208            fail("IllegalStateException expected");
209        } catch (IllegalStateException e) {
210        }
211
212        try {
213            m.group(-1);
214            fail("IllegalStateException expected");
215        } catch (IllegalStateException e) {
216        }
217    }
218
219    /*
220     * Regression test for HARMONY-997
221     */
222    public void testReplacementBackSlash() {
223        String str = "replace me";
224        String replacedString = "me";
225        String substitutionString = "\\";
226        Pattern pat = Pattern.compile(replacedString);
227        Matcher mat = pat.matcher(str);
228        try {
229            mat.replaceAll(substitutionString);
230            fail("IndexOutOfBoundsException should be thrown");
231        } catch (IndexOutOfBoundsException e) {
232        }
233    }
234}
235