1 17 18 package org.apache.tools.ant.util.regexp; 19 20 import java.io.*; 21 import java.util.Vector ; 22 23 import junit.framework.Test; 24 import junit.framework.TestCase; 25 import junit.framework.TestSuite; 26 27 31 public abstract class RegexpMatcherTest extends TestCase { 32 33 public final static String UNIX_LINE = "\n"; 34 35 private RegexpMatcher reg; 36 37 public abstract RegexpMatcher getImplementation(); 38 39 protected final RegexpMatcher getReg() {return reg;} 40 41 public RegexpMatcherTest(String name) { 42 super(name); 43 } 44 45 public void setUp() { 46 reg = getImplementation(); 47 } 48 49 public void testMatches() { 50 reg.setPattern("aaaa"); 51 assertTrue("aaaa should match itself", reg.matches("aaaa")); 52 assertTrue("aaaa should match xaaaa", reg.matches("xaaaa")); 53 assertTrue("aaaa shouldn\'t match xaaa", !reg.matches("xaaa")); 54 reg.setPattern("^aaaa"); 55 assertTrue("^aaaa shouldn\'t match xaaaa", !reg.matches("xaaaa")); 56 assertTrue("^aaaa should match aaaax", reg.matches("aaaax")); 57 reg.setPattern("aaaa$"); 58 assertTrue("aaaa$ shouldn\'t match aaaax", !reg.matches("aaaax")); 59 assertTrue("aaaa$ should match xaaaa", reg.matches("xaaaa")); 60 reg.setPattern("[0-9]+"); 61 assertTrue("[0-9]+ should match 123", reg.matches("123")); 62 assertTrue("[0-9]+ should match 1", reg.matches("1")); 63 assertTrue("[0-9]+ shouldn\'t match \'\'", !reg.matches("")); 64 assertTrue("[0-9]+ shouldn\'t match a", !reg.matches("a")); 65 reg.setPattern("[0-9]*"); 66 assertTrue("[0-9]* should match 123", reg.matches("123")); 67 assertTrue("[0-9]* should match 1", reg.matches("1")); 68 assertTrue("[0-9]* should match \'\'", reg.matches("")); 69 assertTrue("[0-9]* should match a", reg.matches("a")); 70 reg.setPattern("([0-9]+)=\\1"); 71 assertTrue("([0-9]+)=\\1 should match 1=1", reg.matches("1=1")); 72 assertTrue("([0-9]+)=\\1 shouldn\'t match 1=2", !reg.matches("1=2")); 73 } 74 75 public void testGroups() { 76 reg.setPattern("aaaa"); 77 Vector v = reg.getGroups("xaaaa"); 78 assertEquals("No parens -> no extra groups", 1, v.size()); 79 assertEquals("Trivial match with no parens", "aaaa", 80 (String ) v.elementAt(0)); 81 82 reg.setPattern("(aaaa)"); 83 v = reg.getGroups("xaaaa"); 84 assertEquals("Trivial match with single paren", 2, v.size()); 85 assertEquals("Trivial match with single paren, full match", "aaaa", 86 (String ) v.elementAt(0)); 87 assertEquals("Trivial match with single paren, matched paren", "aaaa", 88 (String ) v.elementAt(0)); 89 90 reg.setPattern("(a+)b(b+)"); 91 v = reg.getGroups("xaabb"); 92 assertEquals(3, v.size()); 93 assertEquals("aabb", (String ) v.elementAt(0)); 94 assertEquals("aa", (String ) v.elementAt(1)); 95 assertEquals("b", (String ) v.elementAt(2)); 96 } 97 98 public void testBugzillaReport14619() { 99 reg.setPattern("^(.*)/src/((.*/)*)([a-zA-Z0-9_\\.]+)\\.java$"); 100 Vector v = reg.getGroups("de/tom/src/Google.java"); 101 assertEquals(5, v.size()); 102 assertEquals("de/tom", v.elementAt(1)); 103 assertEquals("", v.elementAt(2)); 104 assertEquals("", v.elementAt(3)); 105 assertEquals("Google", v.elementAt(4)); 106 } 107 108 public void testCaseInsensitiveMatch() { 109 reg.setPattern("aaaa"); 110 assertTrue("aaaa doesn't match AAaa", !reg.matches("AAaa")); 111 assertTrue("aaaa matches AAaa ignoring case", 112 reg.matches("AAaa", RegexpMatcher.MATCH_CASE_INSENSITIVE)); 113 } 114 115 116 119 public void testParagraphCharacter() throws IOException { 120 reg.setPattern("end of text$"); 121 assertTrue("paragraph character", !reg.matches("end of text\u2029")); 122 } 123 124 public void testLineSeparatorCharacter() throws IOException { 125 reg.setPattern("end of text$"); 126 assertTrue("line-separator character", !reg.matches("end of text\u2028")); 127 } 128 129 public void testNextLineCharacter() throws IOException { 130 reg.setPattern("end of text$"); 131 assertTrue("next-line character", !reg.matches("end of text\u0085")); 132 } 133 134 public void testStandaloneCR() throws IOException { 135 reg.setPattern("end of text$"); 136 assertTrue("standalone CR", !reg.matches("end of text\r")); 137 } 138 139 public void testWindowsLineSeparator() throws IOException { 140 reg.setPattern("end of text$"); 141 assertTrue("Windows line separator", !reg.matches("end of text\r\n")); 142 } 143 144 public void testWindowsLineSeparator2() throws IOException { 145 reg.setPattern("end of text\r$"); 146 assertTrue("Windows line separator", reg.matches("end of text\r\n")); 147 } 148 149 public void testUnixLineSeparator() throws IOException { 150 reg.setPattern("end of text$"); 151 assertTrue("Unix line separator", reg.matches("end of text\n")); 152 } 153 154 155 public void testMultiVersusSingleLine() throws IOException { 156 StringBuffer buf = new StringBuffer (); 157 buf.append("Line1").append(UNIX_LINE); 158 buf.append("starttest Line2").append(UNIX_LINE); 159 buf.append("Line3 endtest").append(UNIX_LINE); 160 buf.append("Line4").append(UNIX_LINE); 161 String text = buf.toString(); 162 163 doStartTest1(text); 164 doStartTest2(text); 165 doEndTest1(text); 166 doEndTest2(text); 167 } 168 169 protected void doStartTest1(String text) { 170 reg.setPattern("^starttest"); 171 assertTrue("^starttest in default mode", !reg.matches(text)); 172 assertTrue("^starttest in single line mode", 173 !reg.matches(text, RegexpMatcher.MATCH_SINGLELINE)); 174 assertTrue("^starttest in multi line mode", 175 reg.matches(text, RegexpMatcher.MATCH_MULTILINE)); 176 } 177 178 protected void doStartTest2(String text) { 179 reg.setPattern("^Line1"); 180 assertTrue("^Line1 in default mode", reg.matches(text)); 181 assertTrue("^Line1 in single line mode", 182 reg.matches(text, RegexpMatcher.MATCH_SINGLELINE)); 183 assertTrue("^Line1 in multi line mode", 184 reg.matches(text, RegexpMatcher.MATCH_MULTILINE)); 185 } 186 187 protected void doEndTest1(String text) { 188 reg.setPattern("endtest$"); 189 assertTrue("endtest$ in default mode", !reg.matches(text)); 190 assertTrue("endtest$ in single line mode", 191 !reg.matches(text, RegexpMatcher.MATCH_SINGLELINE)); 192 assertTrue("endtest$ in multi line mode", 193 reg.matches(text, RegexpMatcher.MATCH_MULTILINE)); 194 } 195 196 protected void doEndTest2(String text) { 197 reg.setPattern("Line4$"); 198 assertTrue("Line4$ in default mode", reg.matches(text)); 199 assertTrue("Line4$ in single line mode", 200 reg.matches(text, RegexpMatcher.MATCH_SINGLELINE)); 201 assertTrue("Line4$ in multi line mode", 202 reg.matches(text, RegexpMatcher.MATCH_MULTILINE)); 203 } 204 205 } 206 | Popular Tags |