1/* Licensed to the Apache Software Foundation (ASF) under one or more 2 * contributor license agreements. See the NOTICE file distributed with 3 * this work for additional information regarding copyright ownership. 4 * The ASF licenses this file to You under the Apache License, Version 2.0 5 * (the "License"); you may not use this file except in compliance with 6 * the License. You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package org.apache.harmony.regex.tests.java.util.regex; 18 19import junit.framework.TestCase; 20import java.util.regex.*; 21 22/** 23 * Tests Matcher methods 24 * 25 */ 26public class Matcher2Test extends TestCase { 27 28 public void test_toString() { 29 Pattern p = Pattern.compile("foo"); 30 Matcher m = p.matcher("bar"); 31 assertNotNull(m.toString()); 32 } 33 34 public void testErrorConditions() throws PatternSyntaxException { 35 // Test match cursors in absence of a match 36 Pattern p = Pattern.compile("foo"); 37 Matcher m = p.matcher("bar"); 38 assertFalse(m.matches()); 39 40 try { 41 m.start(); 42 fail("IllegalStateException expected"); 43 } catch (IllegalStateException e) { 44 } 45 46 try { 47 m.end(); 48 fail("IllegalStateException expected"); 49 } catch (IllegalStateException e) { 50 } 51 52 try { 53 m.group(); 54 fail("IllegalStateException expected"); 55 } catch (IllegalStateException e) { 56 } 57 58 try { 59 m.start(1); 60 fail("IllegalStateException expected"); 61 } catch (IllegalStateException e) { 62 } 63 64 try { 65 m.end(1); 66 fail("IllegalStateException expected"); 67 } catch (IllegalStateException e) { 68 } 69 70 try { 71 m.group(1); 72 fail("IllegalStateException expected"); 73 } catch (IllegalStateException e) { 74 } 75 76 // regression test for HARMONY-2418 77 try { 78 m.usePattern(null); 79 fail("IllegalArgumentException expected"); 80 } catch (IllegalArgumentException e) { 81 // PASSED 82 } 83 } 84 85 public void testErrorConditions2() throws PatternSyntaxException { 86 // Test match cursors in absence of a match 87 Pattern p = Pattern.compile("(foo[0-9])(bar[a-z])"); 88 Matcher m = p.matcher("foo1barzfoo2baryfoozbar5"); 89 90 assertTrue(m.find()); 91 assertEquals(0, m.start()); 92 assertEquals(8, m.end()); 93 assertEquals(0, m.start(1)); 94 assertEquals(4, m.end(1)); 95 assertEquals(4, m.start(2)); 96 assertEquals(8, m.end(2)); 97 98 try { 99 m.start(3); 100 fail("IndexOutOfBoundsException expected"); 101 } catch (IndexOutOfBoundsException e) { 102 } 103 104 try { 105 m.end(3); 106 fail("IndexOutOfBoundsException expected"); 107 } catch (IndexOutOfBoundsException e) { 108 } 109 110 try { 111 m.group(3); 112 fail("IndexOutOfBoundsException expected"); 113 } catch (IndexOutOfBoundsException e) { 114 } 115 116 try { 117 m.start(-1); 118 fail("IndexOutOfBoundsException expected"); 119 } catch (IndexOutOfBoundsException e) { 120 } 121 122 try { 123 m.end(-1); 124 fail("IndexOutOfBoundsException expected"); 125 } catch (IndexOutOfBoundsException e) { 126 } 127 128 try { 129 m.group(-1); 130 fail("IndexOutOfBoundsException expected"); 131 } catch (IndexOutOfBoundsException e) { 132 } 133 134 assertTrue(m.find()); 135 assertEquals(8, m.start()); 136 assertEquals(16, m.end()); 137 assertEquals(8, m.start(1)); 138 assertEquals(12, m.end(1)); 139 assertEquals(12, m.start(2)); 140 assertEquals(16, m.end(2)); 141 142 try { 143 m.start(3); 144 fail("IndexOutOfBoundsException expected"); 145 } catch (IndexOutOfBoundsException e) { 146 } 147 148 try { 149 m.end(3); 150 fail("IndexOutOfBoundsException expected"); 151 } catch (IndexOutOfBoundsException e) { 152 } 153 154 try { 155 m.group(3); 156 fail("IndexOutOfBoundsException expected"); 157 } catch (IndexOutOfBoundsException e) { 158 } 159 160 try { 161 m.start(-1); 162 fail("IndexOutOfBoundsException expected"); 163 } catch (IndexOutOfBoundsException e) { 164 } 165 166 try { 167 m.end(-1); 168 fail("IndexOutOfBoundsException expected"); 169 } catch (IndexOutOfBoundsException e) { 170 } 171 172 try { 173 m.group(-1); 174 fail("IndexOutOfBoundsException expected"); 175 } catch (IndexOutOfBoundsException e) { 176 } 177 178 assertFalse(m.find()); 179 180 try { 181 m.start(3); 182 fail("IllegalStateException expected"); 183 } catch (IllegalStateException e) { 184 } 185 186 try { 187 m.end(3); 188 fail("IllegalStateException expected"); 189 } catch (IllegalStateException e) { 190 } 191 192 try { 193 m.group(3); 194 fail("IllegalStateException expected"); 195 } catch (IllegalStateException e) { 196 } 197 198 try { 199 m.start(-1); 200 fail("IllegalStateException expected"); 201 } catch (IllegalStateException e) { 202 } 203 204 try { 205 m.end(-1); 206 fail("IllegalStateException expected"); 207 } catch (IllegalStateException e) { 208 } 209 210 try { 211 m.group(-1); 212 fail("IllegalStateException expected"); 213 } catch (IllegalStateException e) { 214 } 215 } 216 217 /* 218 * Regression test for HARMONY-997 219 */ 220 public void testReplacementBackSlash() { 221 String str = "replace me"; 222 String replacedString = "me"; 223 String substitutionString = "\\"; 224 Pattern pat = Pattern.compile(replacedString); 225 Matcher mat = pat.matcher(str); 226 try { 227 String res = mat.replaceAll(substitutionString); 228 fail("IndexOutOfBoundsException should be thrown - " + res); 229 } catch (Exception e) { 230 } 231 } 232} 233 234