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: package org.apache.commons.lang.text;
019:
020: import junit.framework.Test;
021: import junit.framework.TestCase;
022: import junit.framework.TestSuite;
023: import junit.textui.TestRunner;
024:
025: /**
026: * Unit tests for {@link org.apache.commons.lang.text.StrMatcher}.
027: *
028: * @version $Id: StrMatcherTest.java 437554 2006-08-28 06:21:41Z bayard $
029: */
030: public class StrMatcherTest extends TestCase {
031:
032: private static final char[] BUFFER1 = "0,1\t2 3\n\r\f\u0000'\""
033: .toCharArray();
034:
035: private static final char[] BUFFER2 = "abcdef".toCharArray();
036:
037: /**
038: * Main method.
039: *
040: * @param args command line arguments, ignored
041: */
042: public static void main(String[] args) {
043: TestRunner.run(suite());
044: }
045:
046: /**
047: * Return a new test suite containing this test case.
048: *
049: * @return a new test suite containing this test case
050: */
051: public static Test suite() {
052: TestSuite suite = new TestSuite(StrMatcherTest.class);
053: suite.setName("StrMatcher Tests");
054: return suite;
055: }
056:
057: /**
058: * Create a new test case with the specified name.
059: *
060: * @param name the name
061: */
062: public StrMatcherTest(String name) {
063: super (name);
064: }
065:
066: //-----------------------------------------------------------------------
067: public void testCommaMatcher() {
068: StrMatcher matcher = StrMatcher.commaMatcher();
069: assertSame(matcher, StrMatcher.commaMatcher());
070: assertEquals(0, matcher.isMatch(BUFFER1, 0, 0, BUFFER1.length));
071: assertEquals(1, matcher.isMatch(BUFFER1, 1, 0, BUFFER1.length));
072: assertEquals(0, matcher.isMatch(BUFFER1, 2, 0, BUFFER1.length));
073: }
074:
075: //-----------------------------------------------------------------------
076: public void testTabMatcher() {
077: StrMatcher matcher = StrMatcher.tabMatcher();
078: assertSame(matcher, StrMatcher.tabMatcher());
079: assertEquals(0, matcher.isMatch(BUFFER1, 2, 0, BUFFER1.length));
080: assertEquals(1, matcher.isMatch(BUFFER1, 3, 0, BUFFER1.length));
081: assertEquals(0, matcher.isMatch(BUFFER1, 4, 0, BUFFER1.length));
082: }
083:
084: //-----------------------------------------------------------------------
085: public void testSpaceMatcher() {
086: StrMatcher matcher = StrMatcher.spaceMatcher();
087: assertSame(matcher, StrMatcher.spaceMatcher());
088: assertEquals(0, matcher.isMatch(BUFFER1, 4, 0, BUFFER1.length));
089: assertEquals(1, matcher.isMatch(BUFFER1, 5, 0, BUFFER1.length));
090: assertEquals(0, matcher.isMatch(BUFFER1, 6, 0, BUFFER1.length));
091: }
092:
093: //-----------------------------------------------------------------------
094: public void testSplitMatcher() {
095: StrMatcher matcher = StrMatcher.splitMatcher();
096: assertSame(matcher, StrMatcher.splitMatcher());
097: assertEquals(0, matcher.isMatch(BUFFER1, 2, 0, BUFFER1.length));
098: assertEquals(1, matcher.isMatch(BUFFER1, 3, 0, BUFFER1.length));
099: assertEquals(0, matcher.isMatch(BUFFER1, 4, 0, BUFFER1.length));
100: assertEquals(1, matcher.isMatch(BUFFER1, 5, 0, BUFFER1.length));
101: assertEquals(0, matcher.isMatch(BUFFER1, 6, 0, BUFFER1.length));
102: assertEquals(1, matcher.isMatch(BUFFER1, 7, 0, BUFFER1.length));
103: assertEquals(1, matcher.isMatch(BUFFER1, 8, 0, BUFFER1.length));
104: assertEquals(1, matcher.isMatch(BUFFER1, 9, 0, BUFFER1.length));
105: assertEquals(0, matcher.isMatch(BUFFER1, 10, 0, BUFFER1.length));
106: }
107:
108: //-----------------------------------------------------------------------
109: public void testTrimMatcher() {
110: StrMatcher matcher = StrMatcher.trimMatcher();
111: assertSame(matcher, StrMatcher.trimMatcher());
112: assertEquals(0, matcher.isMatch(BUFFER1, 2, 0, BUFFER1.length));
113: assertEquals(1, matcher.isMatch(BUFFER1, 3, 0, BUFFER1.length));
114: assertEquals(0, matcher.isMatch(BUFFER1, 4, 0, BUFFER1.length));
115: assertEquals(1, matcher.isMatch(BUFFER1, 5, 0, BUFFER1.length));
116: assertEquals(0, matcher.isMatch(BUFFER1, 6, 0, BUFFER1.length));
117: assertEquals(1, matcher.isMatch(BUFFER1, 7, 0, BUFFER1.length));
118: assertEquals(1, matcher.isMatch(BUFFER1, 8, 0, BUFFER1.length));
119: assertEquals(1, matcher.isMatch(BUFFER1, 9, 0, BUFFER1.length));
120: assertEquals(1, matcher.isMatch(BUFFER1, 10, 0, BUFFER1.length));
121: }
122:
123: //-----------------------------------------------------------------------
124: public void testSingleQuoteMatcher() {
125: StrMatcher matcher = StrMatcher.singleQuoteMatcher();
126: assertSame(matcher, StrMatcher.singleQuoteMatcher());
127: assertEquals(0, matcher.isMatch(BUFFER1, 10, 0, BUFFER1.length));
128: assertEquals(1, matcher.isMatch(BUFFER1, 11, 0, BUFFER1.length));
129: assertEquals(0, matcher.isMatch(BUFFER1, 12, 0, BUFFER1.length));
130: }
131:
132: //-----------------------------------------------------------------------
133: public void testDoubleQuoteMatcher() {
134: StrMatcher matcher = StrMatcher.doubleQuoteMatcher();
135: assertSame(matcher, StrMatcher.doubleQuoteMatcher());
136: assertEquals(0, matcher.isMatch(BUFFER1, 11, 0, BUFFER1.length));
137: assertEquals(1, matcher.isMatch(BUFFER1, 12, 0, BUFFER1.length));
138: }
139:
140: //-----------------------------------------------------------------------
141: public void testQuoteMatcher() {
142: StrMatcher matcher = StrMatcher.quoteMatcher();
143: assertSame(matcher, StrMatcher.quoteMatcher());
144: assertEquals(0, matcher.isMatch(BUFFER1, 10, 0, BUFFER1.length));
145: assertEquals(1, matcher.isMatch(BUFFER1, 11, 0, BUFFER1.length));
146: assertEquals(1, matcher.isMatch(BUFFER1, 12, 0, BUFFER1.length));
147: }
148:
149: //-----------------------------------------------------------------------
150: public void testNoneMatcher() {
151: StrMatcher matcher = StrMatcher.noneMatcher();
152: assertSame(matcher, StrMatcher.noneMatcher());
153: assertEquals(0, matcher.isMatch(BUFFER1, 0, 0, BUFFER1.length));
154: assertEquals(0, matcher.isMatch(BUFFER1, 1, 0, BUFFER1.length));
155: assertEquals(0, matcher.isMatch(BUFFER1, 2, 0, BUFFER1.length));
156: assertEquals(0, matcher.isMatch(BUFFER1, 3, 0, BUFFER1.length));
157: assertEquals(0, matcher.isMatch(BUFFER1, 4, 0, BUFFER1.length));
158: assertEquals(0, matcher.isMatch(BUFFER1, 5, 0, BUFFER1.length));
159: assertEquals(0, matcher.isMatch(BUFFER1, 6, 0, BUFFER1.length));
160: assertEquals(0, matcher.isMatch(BUFFER1, 7, 0, BUFFER1.length));
161: assertEquals(0, matcher.isMatch(BUFFER1, 8, 0, BUFFER1.length));
162: assertEquals(0, matcher.isMatch(BUFFER1, 9, 0, BUFFER1.length));
163: assertEquals(0, matcher.isMatch(BUFFER1, 10, 0, BUFFER1.length));
164: assertEquals(0, matcher.isMatch(BUFFER1, 11, 0, BUFFER1.length));
165: assertEquals(0, matcher.isMatch(BUFFER1, 12, 0, BUFFER1.length));
166: }
167:
168: //-----------------------------------------------------------------------
169: public void testCharMatcher_char() {
170: StrMatcher matcher = StrMatcher.charMatcher('c');
171: assertEquals(0, matcher.isMatch(BUFFER2, 0, 0, BUFFER2.length));
172: assertEquals(0, matcher.isMatch(BUFFER2, 1, 0, BUFFER2.length));
173: assertEquals(1, matcher.isMatch(BUFFER2, 2, 0, BUFFER2.length));
174: assertEquals(0, matcher.isMatch(BUFFER2, 3, 0, BUFFER2.length));
175: assertEquals(0, matcher.isMatch(BUFFER2, 4, 0, BUFFER2.length));
176: assertEquals(0, matcher.isMatch(BUFFER2, 5, 0, BUFFER2.length));
177: }
178:
179: //-----------------------------------------------------------------------
180: public void testCharSetMatcher_String() {
181: StrMatcher matcher = StrMatcher.charSetMatcher("ace");
182: assertEquals(1, matcher.isMatch(BUFFER2, 0, 0, BUFFER2.length));
183: assertEquals(0, matcher.isMatch(BUFFER2, 1, 0, BUFFER2.length));
184: assertEquals(1, matcher.isMatch(BUFFER2, 2, 0, BUFFER2.length));
185: assertEquals(0, matcher.isMatch(BUFFER2, 3, 0, BUFFER2.length));
186: assertEquals(1, matcher.isMatch(BUFFER2, 4, 0, BUFFER2.length));
187: assertEquals(0, matcher.isMatch(BUFFER2, 5, 0, BUFFER2.length));
188: assertSame(StrMatcher.noneMatcher(), StrMatcher
189: .charSetMatcher(""));
190: assertSame(StrMatcher.noneMatcher(), StrMatcher
191: .charSetMatcher((String) null));
192: assertTrue(StrMatcher.charSetMatcher("a") instanceof StrMatcher.CharMatcher);
193: }
194:
195: //-----------------------------------------------------------------------
196: public void testCharSetMatcher_charArray() {
197: StrMatcher matcher = StrMatcher.charSetMatcher("ace"
198: .toCharArray());
199: assertEquals(1, matcher.isMatch(BUFFER2, 0, 0, BUFFER2.length));
200: assertEquals(0, matcher.isMatch(BUFFER2, 1, 0, BUFFER2.length));
201: assertEquals(1, matcher.isMatch(BUFFER2, 2, 0, BUFFER2.length));
202: assertEquals(0, matcher.isMatch(BUFFER2, 3, 0, BUFFER2.length));
203: assertEquals(1, matcher.isMatch(BUFFER2, 4, 0, BUFFER2.length));
204: assertEquals(0, matcher.isMatch(BUFFER2, 5, 0, BUFFER2.length));
205: assertSame(StrMatcher.noneMatcher(), StrMatcher
206: .charSetMatcher(new char[0]));
207: assertSame(StrMatcher.noneMatcher(), StrMatcher
208: .charSetMatcher((char[]) null));
209: assertTrue(StrMatcher.charSetMatcher("a".toCharArray()) instanceof StrMatcher.CharMatcher);
210: }
211:
212: //-----------------------------------------------------------------------
213: public void testStringMatcher_String() {
214: StrMatcher matcher = StrMatcher.stringMatcher("bc");
215: assertEquals(0, matcher.isMatch(BUFFER2, 0, 0, BUFFER2.length));
216: assertEquals(2, matcher.isMatch(BUFFER2, 1, 0, BUFFER2.length));
217: assertEquals(0, matcher.isMatch(BUFFER2, 2, 0, BUFFER2.length));
218: assertEquals(0, matcher.isMatch(BUFFER2, 3, 0, BUFFER2.length));
219: assertEquals(0, matcher.isMatch(BUFFER2, 4, 0, BUFFER2.length));
220: assertEquals(0, matcher.isMatch(BUFFER2, 5, 0, BUFFER2.length));
221: assertSame(StrMatcher.noneMatcher(), StrMatcher
222: .stringMatcher(""));
223: assertSame(StrMatcher.noneMatcher(), StrMatcher
224: .stringMatcher((String) null));
225: }
226:
227: //-----------------------------------------------------------------------
228: public void testMatcherIndices() {
229: // remember that the API contract is tight for the isMatch() method
230: // all the onus is on the caller, so invalid inputs are not
231: // the concern of StrMatcher, and are not bugs
232: StrMatcher matcher = StrMatcher.stringMatcher("bc");
233: assertEquals(2, matcher.isMatch(BUFFER2, 1, 1, BUFFER2.length));
234: assertEquals(2, matcher.isMatch(BUFFER2, 1, 0, 3));
235: assertEquals(0, matcher.isMatch(BUFFER2, 1, 0, 2));
236: }
237:
238: }
|