FileDocCategorySizeDatePackage
ModeTest.javaAPI DocAndroid 1.5 API5204Wed May 06 22:41:04 BST 2009org.apache.harmony.regex.tests.java.util.regex

ModeTest

public class ModeTest extends TestCase
Tests Pattern compilation modes and modes triggered in pattern strings

Fields Summary
Constructors Summary
Methods Summary
public voidtestCase()

        Pattern p;
        Matcher m;

        p = Pattern.compile("([a-z]+)[0-9]+");
        m = p.matcher("cAT123#dog345");
        assertTrue(m.find());
        assertEquals("dog", m.group(1));
        assertFalse(m.find());

        
        p = Pattern.compile("([a-z]+)[0-9]+", Pattern.CASE_INSENSITIVE);
        m = p.matcher("cAt123#doG345");
        assertTrue(m.find());
        assertEquals("cAt", m.group(1));
        assertTrue(m.find());
        assertEquals("doG", m.group(1));
        assertFalse(m.find());

        
        p = Pattern.compile("(?i)([a-z]+)[0-9]+");
        m = p.matcher("cAt123#doG345");
        assertTrue(m.find());
        assertEquals("cAt", m.group(1));
        assertTrue(m.find());
        assertEquals("doG", m.group(1));
        assertFalse(m.find());
    
public voidtestMultiline()

        Pattern p;
        Matcher m;

        p = Pattern.compile("^foo");
        m = p.matcher("foobar");
        assertTrue(m.find());
        assertTrue(m.start() == 0 && m.end() == 3);
        assertFalse(m.find());
        
        m = p.matcher("barfoo");
        assertFalse(m.find());

        
        p = Pattern.compile("foo$");
        m = p.matcher("foobar");
        assertFalse(m.find());

        m = p.matcher("barfoo");
        assertTrue(m.find());
        assertTrue(m.start() == 3 && m.end() == 6);
        assertFalse(m.find());


        p = Pattern.compile("^foo([0-9]*)", Pattern.MULTILINE);
        m = p.matcher("foo1bar\nfoo2foo3\nbarfoo4");
        assertTrue(m.find());
        assertEquals("1", m.group(1));
        assertTrue(m.find());
        assertEquals("2", m.group(1));
        assertFalse(m.find());


        p = Pattern.compile("foo([0-9]*)$", Pattern.MULTILINE);
        m = p.matcher("foo1bar\nfoo2foo3\nbarfoo4");
        assertTrue(m.find());
        assertEquals("3", m.group(1));
        assertTrue(m.find());
        assertEquals("4", m.group(1));
        assertFalse(m.find());


        p = Pattern.compile("(?m)^foo([0-9]*)");
        m = p.matcher("foo1bar\nfoo2foo3\nbarfoo4");
        assertTrue(m.find());
        assertEquals("1", m.group(1));
        assertTrue(m.find());
        assertEquals("2", m.group(1));
        assertFalse(m.find());

        p = Pattern.compile("(?m)foo([0-9]*)$");
        m = p.matcher("foo1bar\nfoo2foo3\nbarfoo4");
        assertTrue(m.find());
        assertEquals("3", m.group(1));
        assertTrue(m.find());
        assertEquals("4", m.group(1));
        assertFalse(m.find());