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 
17 package org.apache.harmony.tests.java.util.regex;
18 
19 import java.util.regex.Matcher;
20 import java.util.regex.Pattern;
21 import java.util.regex.PatternSyntaxException;
22 
23 import junit.framework.TestCase;
24 
25 /**
26  * Tests Pattern compilation modes and modes triggered in pattern strings
27  */
28 @SuppressWarnings("nls")
29 public class ModeTest extends TestCase {
testCase()30     public void testCase() throws PatternSyntaxException {
31         Pattern p;
32         Matcher m;
33 
34         p = Pattern.compile("([a-z]+)[0-9]+");
35         m = p.matcher("cAT123#dog345");
36         assertTrue(m.find());
37         assertEquals("dog", m.group(1));
38         assertFalse(m.find());
39 
40         p = Pattern.compile("([a-z]+)[0-9]+", Pattern.CASE_INSENSITIVE);
41         m = p.matcher("cAt123#doG345");
42         assertTrue(m.find());
43         assertEquals("cAt", m.group(1));
44         assertTrue(m.find());
45         assertEquals("doG", m.group(1));
46         assertFalse(m.find());
47 
48         p = Pattern.compile("(?i)([a-z]+)[0-9]+");
49         m = p.matcher("cAt123#doG345");
50         assertTrue(m.find());
51         assertEquals("cAt", m.group(1));
52         assertTrue(m.find());
53         assertEquals("doG", m.group(1));
54         assertFalse(m.find());
55     }
56 
testMultiline()57     public void testMultiline() throws PatternSyntaxException {
58         Pattern p;
59         Matcher m;
60 
61         p = Pattern.compile("^foo");
62         m = p.matcher("foobar");
63         assertTrue(m.find());
64         assertTrue(m.start() == 0 && m.end() == 3);
65         assertFalse(m.find());
66 
67         m = p.matcher("barfoo");
68         assertFalse(m.find());
69 
70         p = Pattern.compile("foo$");
71         m = p.matcher("foobar");
72         assertFalse(m.find());
73 
74         m = p.matcher("barfoo");
75         assertTrue(m.find());
76         assertTrue(m.start() == 3 && m.end() == 6);
77         assertFalse(m.find());
78 
79         p = Pattern.compile("^foo([0-9]*)", Pattern.MULTILINE);
80         m = p.matcher("foo1bar\nfoo2foo3\nbarfoo4");
81         assertTrue(m.find());
82         assertEquals("1", m.group(1));
83         assertTrue(m.find());
84         assertEquals("2", m.group(1));
85         assertFalse(m.find());
86 
87         p = Pattern.compile("foo([0-9]*)$", Pattern.MULTILINE);
88         m = p.matcher("foo1bar\nfoo2foo3\nbarfoo4");
89         assertTrue(m.find());
90         assertEquals("3", m.group(1));
91         assertTrue(m.find());
92         assertEquals("4", m.group(1));
93         assertFalse(m.find());
94 
95         p = Pattern.compile("(?m)^foo([0-9]*)");
96         m = p.matcher("foo1bar\nfoo2foo3\nbarfoo4");
97         assertTrue(m.find());
98         assertEquals("1", m.group(1));
99         assertTrue(m.find());
100         assertEquals("2", m.group(1));
101         assertFalse(m.find());
102 
103         p = Pattern.compile("(?m)foo([0-9]*)$");
104         m = p.matcher("foo1bar\nfoo2foo3\nbarfoo4");
105         assertTrue(m.find());
106         assertEquals("3", m.group(1));
107         assertTrue(m.find());
108         assertEquals("4", m.group(1));
109         assertFalse(m.find());
110     }
111 }
112