001: /* Licensed to the Apache Software Foundation (ASF) under one or more
002: * contributor license agreements. See the NOTICE file distributed with
003: * this work for additional information regarding copyright ownership.
004: * The ASF licenses this file to You under the Apache License, Version 2.0
005: * (the "License"); you may not use this file except in compliance with
006: * the License. You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.harmony.tests.java.util.regex;
018:
019: import java.util.regex.Matcher;
020: import java.util.regex.Pattern;
021: import java.util.regex.PatternSyntaxException;
022:
023: import junit.framework.TestCase;
024:
025: /**
026: * Tests Matcher methods
027: */
028: @SuppressWarnings("nls")
029: public class Matcher2Test extends TestCase {
030: public void test_toString() {
031: Pattern p = Pattern.compile("foo");
032: Matcher m = p.matcher("bar");
033: assertNotNull(m.toString());
034: }
035:
036: public void testErrorConditions() throws PatternSyntaxException {
037: // Test match cursors in absence of a match
038: Pattern p = Pattern.compile("foo");
039: Matcher m = p.matcher("bar");
040: assertFalse(m.matches());
041:
042: try {
043: m.start();
044: fail("IllegalStateException expected");
045: } catch (IllegalStateException e) {
046: }
047:
048: try {
049: m.end();
050: fail("IllegalStateException expected");
051: } catch (IllegalStateException e) {
052: }
053:
054: try {
055: m.group();
056: fail("IllegalStateException expected");
057: } catch (IllegalStateException e) {
058: }
059:
060: try {
061: m.start(1);
062: fail("IllegalStateException expected");
063: } catch (IllegalStateException e) {
064: }
065:
066: try {
067: m.end(1);
068: fail("IllegalStateException expected");
069: } catch (IllegalStateException e) {
070: }
071:
072: try {
073: m.group(1);
074: fail("IllegalStateException expected");
075: } catch (IllegalStateException e) {
076: }
077:
078: // regression test for HARMONY-2418
079: try {
080: m.usePattern(null);
081: fail("IllegalArgumentException expected");
082: } catch (IllegalArgumentException e) {
083: // PASSED
084: }
085: }
086:
087: public void testErrorConditions2() throws PatternSyntaxException {
088: // Test match cursors in absence of a match
089: Pattern p = Pattern.compile("(foo[0-9])(bar[a-z])");
090: Matcher m = p.matcher("foo1barzfoo2baryfoozbar5");
091:
092: assertTrue(m.find());
093: assertEquals(0, m.start());
094: assertEquals(8, m.end());
095: assertEquals(0, m.start(1));
096: assertEquals(4, m.end(1));
097: assertEquals(4, m.start(2));
098: assertEquals(8, m.end(2));
099:
100: try {
101: m.start(3);
102: fail("IndexOutOfBoundsException expected");
103: } catch (IndexOutOfBoundsException e) {
104: }
105:
106: try {
107: m.end(3);
108: fail("IndexOutOfBoundsException expected");
109: } catch (IndexOutOfBoundsException e) {
110: }
111:
112: try {
113: m.group(3);
114: fail("IndexOutOfBoundsException expected");
115: } catch (IndexOutOfBoundsException e) {
116: }
117:
118: try {
119: m.start(-1);
120: fail("IndexOutOfBoundsException expected");
121: } catch (IndexOutOfBoundsException e) {
122: }
123:
124: try {
125: m.end(-1);
126: fail("IndexOutOfBoundsException expected");
127: } catch (IndexOutOfBoundsException e) {
128: }
129:
130: try {
131: m.group(-1);
132: fail("IndexOutOfBoundsException expected");
133: } catch (IndexOutOfBoundsException e) {
134: }
135:
136: assertTrue(m.find());
137: assertEquals(8, m.start());
138: assertEquals(16, m.end());
139: assertEquals(8, m.start(1));
140: assertEquals(12, m.end(1));
141: assertEquals(12, m.start(2));
142: assertEquals(16, m.end(2));
143:
144: try {
145: m.start(3);
146: fail("IndexOutOfBoundsException expected");
147: } catch (IndexOutOfBoundsException e) {
148: }
149:
150: try {
151: m.end(3);
152: fail("IndexOutOfBoundsException expected");
153: } catch (IndexOutOfBoundsException e) {
154: }
155:
156: try {
157: m.group(3);
158: fail("IndexOutOfBoundsException expected");
159: } catch (IndexOutOfBoundsException e) {
160: }
161:
162: try {
163: m.start(-1);
164: fail("IndexOutOfBoundsException expected");
165: } catch (IndexOutOfBoundsException e) {
166: }
167:
168: try {
169: m.end(-1);
170: fail("IndexOutOfBoundsException expected");
171: } catch (IndexOutOfBoundsException e) {
172: }
173:
174: try {
175: m.group(-1);
176: fail("IndexOutOfBoundsException expected");
177: } catch (IndexOutOfBoundsException e) {
178: }
179:
180: assertFalse(m.find());
181:
182: try {
183: m.start(3);
184: fail("IllegalStateException expected");
185: } catch (IllegalStateException e) {
186: }
187:
188: try {
189: m.end(3);
190: fail("IllegalStateException expected");
191: } catch (IllegalStateException e) {
192: }
193:
194: try {
195: m.group(3);
196: fail("IllegalStateException expected");
197: } catch (IllegalStateException e) {
198: }
199:
200: try {
201: m.start(-1);
202: fail("IllegalStateException expected");
203: } catch (IllegalStateException e) {
204: }
205:
206: try {
207: m.end(-1);
208: fail("IllegalStateException expected");
209: } catch (IllegalStateException e) {
210: }
211:
212: try {
213: m.group(-1);
214: fail("IllegalStateException expected");
215: } catch (IllegalStateException e) {
216: }
217: }
218:
219: /*
220: * Regression test for HARMONY-997
221: */
222: public void testReplacementBackSlash() {
223: String str = "replace me";
224: String replacedString = "me";
225: String substitutionString = "\\";
226: Pattern pat = Pattern.compile(replacedString);
227: Matcher mat = pat.matcher(str);
228: try {
229: mat.replaceAll(substitutionString);
230: fail("IndexOutOfBoundsException should be thrown");
231: } catch (IndexOutOfBoundsException e) {
232: }
233: }
234: }
|