001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.util.regexp;
020:
021: import java.io.IOException;
022: import java.util.Vector;
023:
024: import junit.framework.TestCase;
025:
026: /**
027: * Tests for all implementations of the RegexpMatcher interface.
028: *
029: */
030: public abstract class RegexpMatcherTest extends TestCase {
031:
032: public final static String UNIX_LINE = "\n";
033:
034: private RegexpMatcher reg;
035:
036: public abstract RegexpMatcher getImplementation();
037:
038: protected final RegexpMatcher getReg() {
039: return reg;
040: }
041:
042: public RegexpMatcherTest(String name) {
043: super (name);
044: }
045:
046: public void setUp() {
047: reg = getImplementation();
048: }
049:
050: public void testMatches() {
051: reg.setPattern("aaaa");
052: assertTrue("aaaa should match itself", reg.matches("aaaa"));
053: assertTrue("aaaa should match xaaaa", reg.matches("xaaaa"));
054: assertTrue("aaaa shouldn\'t match xaaa", !reg.matches("xaaa"));
055: reg.setPattern("^aaaa");
056: assertTrue("^aaaa shouldn\'t match xaaaa", !reg
057: .matches("xaaaa"));
058: assertTrue("^aaaa should match aaaax", reg.matches("aaaax"));
059: reg.setPattern("aaaa$");
060: assertTrue("aaaa$ shouldn\'t match aaaax", !reg
061: .matches("aaaax"));
062: assertTrue("aaaa$ should match xaaaa", reg.matches("xaaaa"));
063: reg.setPattern("[0-9]+");
064: assertTrue("[0-9]+ should match 123", reg.matches("123"));
065: assertTrue("[0-9]+ should match 1", reg.matches("1"));
066: assertTrue("[0-9]+ shouldn\'t match \'\'", !reg.matches(""));
067: assertTrue("[0-9]+ shouldn\'t match a", !reg.matches("a"));
068: reg.setPattern("[0-9]*");
069: assertTrue("[0-9]* should match 123", reg.matches("123"));
070: assertTrue("[0-9]* should match 1", reg.matches("1"));
071: assertTrue("[0-9]* should match \'\'", reg.matches(""));
072: assertTrue("[0-9]* should match a", reg.matches("a"));
073: reg.setPattern("([0-9]+)=\\1");
074: assertTrue("([0-9]+)=\\1 should match 1=1", reg.matches("1=1"));
075: assertTrue("([0-9]+)=\\1 shouldn\'t match 1=2", !reg
076: .matches("1=2"));
077: }
078:
079: public void testGroups() {
080: reg.setPattern("aaaa");
081: Vector v = reg.getGroups("xaaaa");
082: assertEquals("No parens -> no extra groups", 1, v.size());
083: assertEquals("Trivial match with no parens", "aaaa", (String) v
084: .elementAt(0));
085:
086: reg.setPattern("(aaaa)");
087: v = reg.getGroups("xaaaa");
088: assertEquals("Trivial match with single paren", 2, v.size());
089: assertEquals("Trivial match with single paren, full match",
090: "aaaa", (String) v.elementAt(0));
091: assertEquals("Trivial match with single paren, matched paren",
092: "aaaa", (String) v.elementAt(0));
093:
094: reg.setPattern("(a+)b(b+)");
095: v = reg.getGroups("xaabb");
096: assertEquals(3, v.size());
097: assertEquals("aabb", (String) v.elementAt(0));
098: assertEquals("aa", (String) v.elementAt(1));
099: assertEquals("b", (String) v.elementAt(2));
100: }
101:
102: public void testBugzillaReport14619() {
103: reg.setPattern("^(.*)/src/((.*/)*)([a-zA-Z0-9_\\.]+)\\.java$");
104: Vector v = reg.getGroups("de/tom/src/Google.java");
105: assertEquals(5, v.size());
106: assertEquals("de/tom", v.elementAt(1));
107: assertEquals("", v.elementAt(2));
108: assertEquals("", v.elementAt(3));
109: assertEquals("Google", v.elementAt(4));
110: }
111:
112: public void testCaseInsensitiveMatch() {
113: reg.setPattern("aaaa");
114: assertTrue("aaaa doesn't match AAaa", !reg.matches("AAaa"));
115: assertTrue("aaaa matches AAaa ignoring case", reg.matches(
116: "AAaa", RegexpMatcher.MATCH_CASE_INSENSITIVE));
117: }
118:
119: // make sure there are no issues concerning line separator interpretation
120: // a line separator for regex (perl) is always a unix line (ie \n)
121:
122: public void testParagraphCharacter() throws IOException {
123: reg.setPattern("end of text$");
124: assertTrue("paragraph character", !reg
125: .matches("end of text\u2029"));
126: }
127:
128: public void testLineSeparatorCharacter() throws IOException {
129: reg.setPattern("end of text$");
130: assertTrue("line-separator character", !reg
131: .matches("end of text\u2028"));
132: }
133:
134: public void testNextLineCharacter() throws IOException {
135: reg.setPattern("end of text$");
136: assertTrue("next-line character", !reg
137: .matches("end of text\u0085"));
138: }
139:
140: public void testStandaloneCR() throws IOException {
141: reg.setPattern("end of text$");
142: assertTrue("standalone CR", !reg.matches("end of text\r"));
143: }
144:
145: public void testWindowsLineSeparator() throws IOException {
146: reg.setPattern("end of text$");
147: assertTrue("Windows line separator", !reg
148: .matches("end of text\r\n"));
149: }
150:
151: public void testWindowsLineSeparator2() throws IOException {
152: reg.setPattern("end of text\r$");
153: assertTrue("Windows line separator", reg
154: .matches("end of text\r\n"));
155: }
156:
157: public void testUnixLineSeparator() throws IOException {
158: reg.setPattern("end of text$");
159: assertTrue("Unix line separator", reg.matches("end of text\n"));
160: }
161:
162: public void testMultiVersusSingleLine() throws IOException {
163: StringBuffer buf = new StringBuffer();
164: buf.append("Line1").append(UNIX_LINE);
165: buf.append("starttest Line2").append(UNIX_LINE);
166: buf.append("Line3 endtest").append(UNIX_LINE);
167: buf.append("Line4").append(UNIX_LINE);
168: String text = buf.toString();
169:
170: doStartTest1(text);
171: doStartTest2(text);
172: doEndTest1(text);
173: doEndTest2(text);
174: }
175:
176: protected void doStartTest1(String text) {
177: reg.setPattern("^starttest");
178: assertTrue("^starttest in default mode", !reg.matches(text));
179: assertTrue("^starttest in single line mode", !reg.matches(text,
180: RegexpMatcher.MATCH_SINGLELINE));
181: assertTrue("^starttest in multi line mode", reg.matches(text,
182: RegexpMatcher.MATCH_MULTILINE));
183: }
184:
185: protected void doStartTest2(String text) {
186: reg.setPattern("^Line1");
187: assertTrue("^Line1 in default mode", reg.matches(text));
188: assertTrue("^Line1 in single line mode", reg.matches(text,
189: RegexpMatcher.MATCH_SINGLELINE));
190: assertTrue("^Line1 in multi line mode", reg.matches(text,
191: RegexpMatcher.MATCH_MULTILINE));
192: }
193:
194: protected void doEndTest1(String text) {
195: reg.setPattern("endtest$");
196: assertTrue("endtest$ in default mode", !reg.matches(text));
197: assertTrue("endtest$ in single line mode", !reg.matches(text,
198: RegexpMatcher.MATCH_SINGLELINE));
199: assertTrue("endtest$ in multi line mode", reg.matches(text,
200: RegexpMatcher.MATCH_MULTILINE));
201: }
202:
203: protected void doEndTest2(String text) {
204: reg.setPattern("Line4$");
205: assertTrue("Line4$ in default mode", reg.matches(text));
206: assertTrue("Line4$ in single line mode", reg.matches(text,
207: RegexpMatcher.MATCH_SINGLELINE));
208: assertTrue("Line4$ in multi line mode", reg.matches(text,
209: RegexpMatcher.MATCH_MULTILINE));
210: }
211:
212: }
|