Matcher2Test.java revision f33eae7e84eb6d3b0f4e86b59605bb3de73009f3
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.*;
26
27/**
28 * Tests Matcher methods
29 *
30 */
31@TestTargetClass(java.util.regex.Matcher.class)
32public class Matcher2Test extends TestCase {
33
34    @TestTargetNew(
35        level = TestLevel.PARTIAL,
36        notes = "Verifies the basic functionality of toString() method.",
37        method = "toString",
38        args = {}
39    )
40    public void test_toString() {
41        Pattern p = Pattern.compile("foo");
42        Matcher m = p.matcher("bar");
43        assertNotNull(m.toString());
44    }
45
46    @TestTargets({
47        @TestTargetNew(
48            level = TestLevel.PARTIAL_COMPLETE,
49            notes = "Verifies start, end, group, usePattern methods with wrong conditions, IllegalStateException should be thrown",
50            method = "start",
51            args = {}
52        ),
53        @TestTargetNew(
54            level = TestLevel.PARTIAL_COMPLETE,
55            notes = "Verifies start, end, group, usePattern methods with wrong conditions, IllegalStateException should be thrown",
56            method = "end",
57            args = {}
58        ),
59        @TestTargetNew(
60            level = TestLevel.PARTIAL_COMPLETE,
61            notes = "Verifies start, end, group, usePattern methods with wrong conditions, IllegalStateException should be thrown",
62            method = "group",
63            args = {}
64        ),
65        @TestTargetNew(
66            level = TestLevel.PARTIAL_COMPLETE,
67            notes = "Verifies start, end, group, usePattern methods with wrong conditions, IllegalStateException should be thrown",
68            method = "start",
69            args = {int.class}
70        ),
71        @TestTargetNew(
72            level = TestLevel.PARTIAL_COMPLETE,
73            notes = "Verifies start, end, group, usePattern methods with wrong conditions, IllegalStateException should be thrown",
74            method = "end",
75            args = {int.class}
76        ),
77        @TestTargetNew(
78            level = TestLevel.PARTIAL_COMPLETE,
79            notes = "Verifies start, end, group, usePattern methods with wrong conditions, IllegalStateException should be thrown",
80            method = "group",
81            args = {int.class}
82        ),
83        @TestTargetNew(
84            level = TestLevel.PARTIAL_COMPLETE,
85            notes = "Verifies start, end, group, usePattern methods with wrong conditions, IllegalStateException should be thrown",
86            method = "usePattern",
87            args = {java.util.regex.Pattern.class}
88        )
89    })
90    public void testErrorConditions() throws PatternSyntaxException {
91        // Test match cursors in absence of a match
92        Pattern p = Pattern.compile("foo");
93        Matcher m = p.matcher("bar");
94        assertFalse(m.matches());
95
96        try {
97            m.start();
98            fail("IllegalStateException expected");
99        } catch (IllegalStateException e) {
100        }
101
102        try {
103            m.end();
104            fail("IllegalStateException expected");
105        } catch (IllegalStateException e) {
106        }
107
108        try {
109            m.group();
110            fail("IllegalStateException expected");
111        } catch (IllegalStateException e) {
112        }
113
114        try {
115            m.start(1);
116            fail("IllegalStateException expected");
117        } catch (IllegalStateException e) {
118        }
119
120        try {
121            m.end(1);
122            fail("IllegalStateException expected");
123        } catch (IllegalStateException e) {
124        }
125
126        try {
127            m.group(1);
128            fail("IllegalStateException expected");
129        } catch (IllegalStateException e) {
130        }
131
132               // regression test for HARMONY-2418
133        try {
134            m.usePattern(null);
135            fail("IllegalArgumentException expected");
136        } catch (IllegalArgumentException e) {
137                 // PASSED
138        }
139    }
140
141    @TestTargets({
142        @TestTargetNew(
143            level = TestLevel.PARTIAL_COMPLETE,
144            notes = "Verifies end, start, group methods with wrong conditions, IndexOutOfBoundsException, IllegalStateException should be thrown",
145            method = "start",
146            args = {}
147        ),
148        @TestTargetNew(
149            level = TestLevel.PARTIAL_COMPLETE,
150            notes = "Verifies end, start, group methods with wrong conditions, IndexOutOfBoundsException, IllegalStateException should be thrown",
151            method = "end",
152            args = {}
153        ),
154        @TestTargetNew(
155            level = TestLevel.PARTIAL_COMPLETE,
156            notes = "Verifies end, start, group methods with wrong conditions, IndexOutOfBoundsException, IllegalStateException should be thrown",
157            method = "start",
158            args = {int.class}
159        ),
160        @TestTargetNew(
161            level = TestLevel.PARTIAL_COMPLETE,
162            notes = "Verifies end, start, group methods with wrong conditions, IndexOutOfBoundsException, IllegalStateException should be thrown",
163            method = "end",
164            args = {int.class}
165        ),
166        @TestTargetNew(
167            level = TestLevel.PARTIAL_COMPLETE,
168            notes = "Verifies end, start, group methods with wrong conditions, IndexOutOfBoundsException, IllegalStateException should be thrown",
169            method = "group",
170            args = {int.class}
171        )
172    })
173    public void testErrorConditions2() throws PatternSyntaxException {
174        // Test match cursors in absence of a match
175        Pattern p = Pattern.compile("(foo[0-9])(bar[a-z])");
176        Matcher m = p.matcher("foo1barzfoo2baryfoozbar5");
177
178        assertTrue(m.find());
179        assertEquals(0, m.start());
180        assertEquals(8, m.end());
181        assertEquals(0, m.start(1));
182        assertEquals(4, m.end(1));
183        assertEquals(4, m.start(2));
184        assertEquals(8, m.end(2));
185
186        try {
187            m.start(3);
188            fail("IndexOutOfBoundsException expected");
189        } catch (IndexOutOfBoundsException e) {
190        }
191
192        try {
193            m.end(3);
194            fail("IndexOutOfBoundsException expected");
195        } catch (IndexOutOfBoundsException e) {
196        }
197
198        try {
199            m.group(3);
200            fail("IndexOutOfBoundsException expected");
201        } catch (IndexOutOfBoundsException e) {
202        }
203
204        try {
205            m.start(-1);
206            fail("IndexOutOfBoundsException expected");
207        } catch (IndexOutOfBoundsException e) {
208        }
209
210        try {
211            m.end(-1);
212            fail("IndexOutOfBoundsException expected");
213        } catch (IndexOutOfBoundsException e) {
214        }
215
216        try {
217            m.group(-1);
218            fail("IndexOutOfBoundsException expected");
219        } catch (IndexOutOfBoundsException e) {
220        }
221
222        assertTrue(m.find());
223        assertEquals(8, m.start());
224        assertEquals(16, m.end());
225        assertEquals(8, m.start(1));
226        assertEquals(12, m.end(1));
227        assertEquals(12, m.start(2));
228        assertEquals(16, m.end(2));
229
230        try {
231            m.start(3);
232            fail("IndexOutOfBoundsException expected");
233        } catch (IndexOutOfBoundsException e) {
234        }
235
236        try {
237            m.end(3);
238            fail("IndexOutOfBoundsException expected");
239        } catch (IndexOutOfBoundsException e) {
240        }
241
242        try {
243            m.group(3);
244            fail("IndexOutOfBoundsException expected");
245        } catch (IndexOutOfBoundsException e) {
246        }
247
248        try {
249            m.start(-1);
250            fail("IndexOutOfBoundsException expected");
251        } catch (IndexOutOfBoundsException e) {
252        }
253
254        try {
255            m.end(-1);
256            fail("IndexOutOfBoundsException expected");
257        } catch (IndexOutOfBoundsException e) {
258        }
259
260        try {
261            m.group(-1);
262            fail("IndexOutOfBoundsException expected");
263        } catch (IndexOutOfBoundsException e) {
264        }
265
266        assertFalse(m.find());
267
268        try {
269            m.start(3);
270            fail("IllegalStateException expected");
271        } catch (IllegalStateException e) {
272        }
273
274        try {
275            m.end(3);
276            fail("IllegalStateException expected");
277        } catch (IllegalStateException e) {
278        }
279
280        try {
281            m.group(3);
282            fail("IllegalStateException expected");
283        } catch (IllegalStateException e) {
284        }
285
286        try {
287            m.start(-1);
288            fail("IllegalStateException expected");
289        } catch (IllegalStateException e) {
290        }
291
292        try {
293            m.end(-1);
294            fail("IllegalStateException expected");
295        } catch (IllegalStateException e) {
296        }
297
298        try {
299            m.group(-1);
300            fail("IllegalStateException expected");
301        } catch (IllegalStateException e) {
302        }
303    }
304
305    /*
306     * Regression test for HARMONY-997
307     */
308    @TestTargetNew(
309        level = TestLevel.PARTIAL_COMPLETE,
310        notes = "Verifies that IndexOutOfBoundsException exception is thrown while calling of replaceAll method with incorrect string.",
311        method = "replaceAll",
312        args = {java.lang.String.class}
313    )
314    public void testReplacementBackSlash() {
315        String str = "replace me";
316        String replacedString = "me";
317        String substitutionString = "\\";
318        Pattern pat = Pattern.compile(replacedString);
319        Matcher mat = pat.matcher(str);
320        try {
321            String res = mat.replaceAll(substitutionString);
322            fail("IndexOutOfBoundsException should be thrown - " + res);
323        } catch (Exception e) {
324        }
325    }
326}
327
328