1 15 package org.apache.tapestry.junit.utils; 16 17 import org.apache.hivemind.ApplicationRuntimeException; 18 import org.apache.tapestry.junit.TapestryTestCase; 19 import org.apache.tapestry.util.RegexpMatch; 20 import org.apache.tapestry.util.RegexpMatcher; 21 22 28 public class TestRegexpMatcher extends TapestryTestCase 29 { 30 31 public void testMatch() 32 { 33 RegexpMatcher m = new RegexpMatcher(); 34 35 assertTrue(m.matches("[a-z]+", "c")); 36 assertTrue(m.matches("foo|foot", "foo")); 37 } 38 39 public void testNonmatch() 40 { 41 RegexpMatcher m = new RegexpMatcher(); 42 43 assertTrue(!m.matches("[0-9]+", "q")); 44 assertTrue(!m.matches("foo|foot", "foot")); 45 } 46 47 public void testBadPattern() 48 { 49 RegexpMatcher m = new RegexpMatcher(); 50 51 try 52 { 53 m.matches("[[[", "x"); 54 55 unreachable(); 56 } 57 catch (ApplicationRuntimeException ex) 58 { 59 checkException(ex, "Unmatched [] in expression"); 60 } 61 } 62 63 public void testClear() 64 { 65 RegexpMatcher m = new RegexpMatcher(); 66 67 m.clear(); 68 } 69 70 public void testContains() 71 { 72 RegexpMatcher m = new RegexpMatcher(); 73 74 assertTrue(m.contains("[a-z]+", "c")); 75 assertTrue(m.contains("^(\\d{5}(-\\d{4})?)$", "06514")); 76 assertTrue(m.contains("^(\\d{5}(-\\d{4})?)$", "06514-3149")); 77 assertTrue(m.contains("foo|foot", "12foot12")); 78 } 79 80 public void testNotContains() 81 { 82 RegexpMatcher m = new RegexpMatcher(); 83 84 assertTrue(!m.contains("[0-9]+", "q")); 85 assertTrue(!m.contains("^(\\d{5}(-\\d{4})?)$", "0651")); 86 assertTrue(!m.contains("^(\\d{5}(-\\d{4})?)$", "065147")); 87 assertTrue(!m.contains("^(\\d{5}(-\\d{4})?)$", "06514-314")); 88 assertTrue(!m.contains("^(\\d{5}(-\\d{4})?)$", "06514-31497")); 89 assertTrue(!m.contains("^(foo|foot)$", "12foot12")); 90 } 91 92 public void testGetEscapedPatternStrings() 93 { 94 RegexpMatcher m = new RegexpMatcher(); 95 96 assertEquals(m.getEscapedPatternString("^\\d$"), "\\^\\\\d\\$"); 97 } 98 99 100 101 public void testGetMatches() 102 { 103 RegexpMatcher m = new RegexpMatcher(); 104 105 String [] matches = m.getMatches("\\d+", "57,232 89 147", 0); 106 107 assertListsEqual(new String [] 108 { "57", "232", "89", "147" }, matches); 109 } 110 111 112 113 public void testGetMatchesAsObjects() 114 { 115 RegexpMatcher m = new RegexpMatcher(); 116 117 RegexpMatch[] matches = m.getMatches("\\w+(=(\\w+))?", "fred,barney=rubble;wilma=flintstone"); 118 119 assertEquals(3, matches.length); 120 121 assertEquals("fred", matches[0].getInput()); 122 assertEquals("fred", matches[0].getGroup(0)); 123 124 assertEquals("barney=rubble", matches[1].getInput()); 125 assertEquals("rubble", matches[1].getGroup(2)); 126 127 assertEquals("wilma=flintstone", matches[2].getInput()); 128 assertEquals("flintstone", matches[2].getGroup(2)); 129 } 130 131 132 133 public void testGetMatchesNoMatch() 134 { 135 RegexpMatcher m = new RegexpMatcher(); 136 137 String [] matches = m.getMatches("A(B|C)", "aBCAaBA", 0); 138 139 assertEquals(0, matches.length); 140 } 141 142 143 144 public void testGetMatchesSubgroup() 145 { 146 RegexpMatcher m = new RegexpMatcher(); 147 148 String matches[] = m.getMatches("A(B|C|fred)", "AA AC AB Afred AA AC", 1); 149 150 assertListsEqual(new String [] 151 { "C", "B", "fred", "C" }, matches); 152 } 153 154 } 155 | Popular Tags |