1adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project/* 2adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Copyright (C) 2008 The Android Open Source Project 3adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * 4adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License"); 5adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * you may not use this file except in compliance with the License. 6adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * You may obtain a copy of the License at 7adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * 8adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * http://www.apache.org/licenses/LICENSE-2.0 9adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * 10adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Unless required by applicable law or agreed to in writing, software 11adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS, 12adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * See the License for the specific language governing permissions and 14adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * limitations under the License. 15adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 16adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 17adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectpackage org.apache.harmony.regex.tests.java.util.regex; 18adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 19195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughesimport java.io.ObjectStreamClass; 20195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughesimport java.io.Serializable; 2168eb7077aa93729e8a4ce289afac6643f24b0b18Elliott Hughesimport java.util.regex.Pattern; 22adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.util.regex.PatternSyntaxException; 23adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 24adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport junit.framework.TestCase; 25195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughesimport org.apache.harmony.testframework.serialization.SerializationTest; 26195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughesimport org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert; 27adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 28adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectpublic class PatternSyntaxExceptionTest extends TestCase { 29195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes public void testPatternSyntaxException() { 30195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes // Normal case 31195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes PatternSyntaxException e = new PatternSyntaxException("Foo", "Bar", 0); 32195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes assertEquals("Foo", e.getDescription()); 33195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes assertEquals("Bar", e.getPattern()); 34195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes assertEquals(0, e.getIndex()); 35195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes 36195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes String s = e.getMessage(); 37195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes assertTrue(s.contains("Foo")); 38195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes assertTrue(s.contains("Bar")); 39195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes assertTrue(s.contains("0")); 40195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes 41195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes // No description specified 42195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes e = new PatternSyntaxException(null, "Bar", 0); 43195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes assertEquals(null, e.getDescription()); 44195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes assertEquals("Bar", e.getPattern()); 45195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes assertEquals(0, e.getIndex()); 46195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes 47195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes s = e.getMessage(); 48195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes assertFalse(s.contains("Foo")); 49195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes assertTrue(s.contains("Bar")); 50195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes assertTrue(s.contains("0")); 51195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes 52195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes // No pattern specified 53195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes e = new PatternSyntaxException("Foo", null, 0); 54195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes assertEquals("Foo", e.getDescription()); 55195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes assertEquals(null, e.getPattern()); 56195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes assertEquals(0, e.getIndex()); 57195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes 58195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes s = e.getMessage(); 59195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes assertTrue(s.contains("Foo")); 60195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes assertFalse(s.contains("Bar")); 61195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes assertTrue(s.contains("0")); 62195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes 63195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes // Neither description nor pattern specified 64195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes e = new PatternSyntaxException(null, null, 0); 65195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes assertEquals(null, e.getDescription()); 66195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes assertEquals(null, e.getPattern()); 67195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes assertEquals(0, e.getIndex()); 68195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes 69195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes s = e.getMessage(); 70195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes assertFalse(s.contains("Foo")); 71195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes assertFalse(s.contains("Bar")); 72195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes assertTrue(s.contains("0")); 73195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes 74195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes // No index specified 75195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes e = new PatternSyntaxException("Foo", "Bar", -1); 76195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes assertEquals(-1, e.getIndex()); 77195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes 78195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes s = e.getMessage(); 79195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes assertFalse(s.contains("^")); 80195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes 81c907a4c7191765bb5180c5941a4ddf31f56c33f6Narayan Kamath // No pattern, but index specified. NOTE: This is an "unusual" case since 82c907a4c7191765bb5180c5941a4ddf31f56c33f6Narayan Kamath // it make no sense to provide an index and not a pattern. 83195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes e = new PatternSyntaxException("Foo", null, 0); 84195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes assertEquals(0, e.getIndex()); 85195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes 86195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes s = e.getMessage(); 87c907a4c7191765bb5180c5941a4ddf31f56c33f6Narayan Kamath assertTrue(s.contains("^")); 88c907a4c7191765bb5180c5941a4ddf31f56c33f6Narayan Kamath assertTrue(s.contains("null")); 89195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes } 90195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes 91195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes public void testCase() { 92195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes String regex = "("; 93195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes try { 94195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes Pattern.compile(regex); 95195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes fail("PatternSyntaxException expected"); 96195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes } catch (PatternSyntaxException e) { 97195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes assertEquals(1, e.getIndex()); 98195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes assertEquals(regex, e.getPattern()); 99adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 100195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes } 101195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes 102195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes public void testCase2() { 103195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes String regex = "[4-"; 104195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes try { 105195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes Pattern.compile(regex); 106195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes fail("PatternSyntaxException expected"); 107195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes } catch (PatternSyntaxException e) { 108195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes assertEquals(3, e.getIndex()); 109195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes assertEquals(regex, e.getPattern()); 110195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes } 111195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes } 112195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes 113195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes // Regression test for HARMONY-3787 114195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes public void test_objectStreamField() { 115195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes ObjectStreamClass objectStreamClass = ObjectStreamClass.lookup(PatternSyntaxException.class); 116195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes assertNotNull(objectStreamClass.getField("desc")); 117195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes } 118195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes 119195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes public void testSerializationCompatibility() throws Exception { 120195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes PatternSyntaxException object = new PatternSyntaxException("TESTDESC", "TESTREGEX", 3); 121195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes SerializationTest.verifyGolden(this, object, PATTERNSYNTAXEXCEPTION_COMPARATOR); 122195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes } 123195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes 124195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes public void testSerializationSelf() throws Exception { 125195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes PatternSyntaxException object = new PatternSyntaxException("TESTDESC", "TESTREGEX", 3); 126195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes SerializationTest.verifySelf(object, PATTERNSYNTAXEXCEPTION_COMPARATOR); 127195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes } 128195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes 129195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes private static final SerializableAssert PATTERNSYNTAXEXCEPTION_COMPARATOR = new SerializableAssert() { 130195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes public void assertDeserialized(Serializable initial, Serializable deserialized) { 131195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes 132195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes // do common checks for all throwable objects 133195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes SerializationTest.THROWABLE_COMPARATOR.assertDeserialized(initial, deserialized); 134195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes 135195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes PatternSyntaxException initPatternSyntaxException = (PatternSyntaxException) initial; 136195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes PatternSyntaxException dserPatternSyntaxException = (PatternSyntaxException) deserialized; 137195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes 138195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes // verify fields 139195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes assertEquals(initPatternSyntaxException.getDescription(), 140195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes dserPatternSyntaxException.getDescription()); 141195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes assertEquals(initPatternSyntaxException.getPattern(), 142195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes dserPatternSyntaxException.getPattern()); 143195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes assertEquals(initPatternSyntaxException.getIndex(), 144195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes dserPatternSyntaxException.getIndex()); 145195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes } 146195c30902fd54c9e822183a19d570e15f0f8d007Elliott Hughes }; 147adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project} 148