001: /* Licensed to the Apache Software Foundation (ASF) under one or more
002: * contributor license agreements. See the NOTICE file distributed with
003: * this work for additional information regarding copyright ownership.
004: * The ASF licenses this file to You under the Apache License, Version 2.0
005: * (the "License"); you may not use this file except in compliance with
006: * the License. You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.harmony.tests.java.util.regex;
018:
019: import java.util.regex.Matcher;
020: import java.util.regex.Pattern;
021: import java.util.regex.PatternSyntaxException;
022:
023: import junit.framework.TestCase;
024:
025: /**
026: * Tests Pattern compilation modes and modes triggered in pattern strings
027: */
028: @SuppressWarnings("nls")
029: public class ModeTest extends TestCase {
030: public void testCase() throws PatternSyntaxException {
031: Pattern p;
032: Matcher m;
033:
034: p = Pattern.compile("([a-z]+)[0-9]+");
035: m = p.matcher("cAT123#dog345");
036: assertTrue(m.find());
037: assertEquals("dog", m.group(1));
038: assertFalse(m.find());
039:
040: p = Pattern.compile("([a-z]+)[0-9]+", Pattern.CASE_INSENSITIVE);
041: m = p.matcher("cAt123#doG345");
042: assertTrue(m.find());
043: assertEquals("cAt", m.group(1));
044: assertTrue(m.find());
045: assertEquals("doG", m.group(1));
046: assertFalse(m.find());
047:
048: p = Pattern.compile("(?i)([a-z]+)[0-9]+");
049: m = p.matcher("cAt123#doG345");
050: assertTrue(m.find());
051: assertEquals("cAt", m.group(1));
052: assertTrue(m.find());
053: assertEquals("doG", m.group(1));
054: assertFalse(m.find());
055: }
056:
057: public void testMultiline() throws PatternSyntaxException {
058: Pattern p;
059: Matcher m;
060:
061: p = Pattern.compile("^foo");
062: m = p.matcher("foobar");
063: assertTrue(m.find());
064: assertTrue(m.start() == 0 && m.end() == 3);
065: assertFalse(m.find());
066:
067: m = p.matcher("barfoo");
068: assertFalse(m.find());
069:
070: p = Pattern.compile("foo$");
071: m = p.matcher("foobar");
072: assertFalse(m.find());
073:
074: m = p.matcher("barfoo");
075: assertTrue(m.find());
076: assertTrue(m.start() == 3 && m.end() == 6);
077: assertFalse(m.find());
078:
079: p = Pattern.compile("^foo([0-9]*)", Pattern.MULTILINE);
080: m = p.matcher("foo1bar\nfoo2foo3\nbarfoo4");
081: assertTrue(m.find());
082: assertEquals("1", m.group(1));
083: assertTrue(m.find());
084: assertEquals("2", m.group(1));
085: assertFalse(m.find());
086:
087: p = Pattern.compile("foo([0-9]*)$", Pattern.MULTILINE);
088: m = p.matcher("foo1bar\nfoo2foo3\nbarfoo4");
089: assertTrue(m.find());
090: assertEquals("3", m.group(1));
091: assertTrue(m.find());
092: assertEquals("4", m.group(1));
093: assertFalse(m.find());
094:
095: p = Pattern.compile("(?m)^foo([0-9]*)");
096: m = p.matcher("foo1bar\nfoo2foo3\nbarfoo4");
097: assertTrue(m.find());
098: assertEquals("1", m.group(1));
099: 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: }
|