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.io.ObjectStreamClass;
020: import java.io.Serializable;
021: import java.util.regex.Pattern;
022: import java.util.regex.PatternSyntaxException;
023:
024: import junit.framework.TestCase;
025:
026: import org.apache.harmony.testframework.serialization.SerializationTest;
027: import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
028:
029: /**
030: * TODO Type description
031: */
032: @SuppressWarnings("nls")
033: public class PatternSyntaxExceptionTest extends TestCase {
034: public void testCase() {
035: String regex = "(";
036: try {
037: Pattern.compile(regex);
038: fail("PatternSyntaxException expected");
039: } catch (PatternSyntaxException e) {
040: // TOFIX: Commented out assertEquals tests...
041: // TOFIX: should we match exception strings?
042: // assertEquals("Unclosed group", e.getDescription());
043: assertEquals(1, e.getIndex());
044: // assertEquals("Unclosed group near index 1\n(\n ^",
045: // e.getMessage());
046: assertEquals(regex, e.getPattern());
047: }
048: }
049:
050: public void testCase2() {
051: String regex = "[4-";
052: try {
053: Pattern.compile(regex);
054: fail("PatternSyntaxException expected");
055: } catch (PatternSyntaxException e) {
056: // TOFIX: Commented out assertEquals tests...
057: // TOFIX: should we match exception strings?
058: // assertEquals("Illegal character range", e.getDescription());
059: assertEquals(3, e.getIndex());
060: // assertEquals("Illegal character range near index 3\n[4-\n ^",
061: // e.getMessage());
062: assertEquals(regex, e.getPattern());
063: }
064: }
065:
066: /**
067: * @tests serialization/deserialization compatibility.
068: */
069: public void testSerializationSelf() throws Exception {
070: PatternSyntaxException object = new PatternSyntaxException(
071: "TESTDESC", "TESTREGEX", 3);
072: SerializationTest.verifySelf(object,
073: PATTERNSYNTAXEXCEPTION_COMPARATOR);
074: }
075:
076: /**
077: * @tests serialization/deserialization compatibility with RI.
078: */
079: public void testSerializationCompatibility() throws Exception {
080: PatternSyntaxException object = new PatternSyntaxException(
081: "TESTDESC", "TESTREGEX", 3);
082: SerializationTest.verifyGolden(this , object,
083: PATTERNSYNTAXEXCEPTION_COMPARATOR);
084: }
085:
086: // Regression test for HARMONY-3787
087: public void test_objectStreamField() {
088: ObjectStreamClass objectStreamClass = ObjectStreamClass
089: .lookup(PatternSyntaxException.class);
090: assertNotNull(objectStreamClass.getField("desc"));
091: }
092:
093: // comparator for BatchUpdateException field updateCounts
094: private static final SerializableAssert PATTERNSYNTAXEXCEPTION_COMPARATOR = new SerializableAssert() {
095: public void assertDeserialized(Serializable initial,
096: Serializable deserialized) {
097:
098: // do common checks for all throwable objects
099: SerializationTest.THROWABLE_COMPARATOR.assertDeserialized(
100: initial, deserialized);
101:
102: PatternSyntaxException initPatternSyntaxException = (PatternSyntaxException) initial;
103: PatternSyntaxException dserPatternSyntaxException = (PatternSyntaxException) deserialized;
104:
105: // verify fields
106: assertEquals(initPatternSyntaxException.getDescription(),
107: dserPatternSyntaxException.getDescription());
108: assertEquals(initPatternSyntaxException.getPattern(),
109: dserPatternSyntaxException.getPattern());
110: assertEquals(initPatternSyntaxException.getIndex(),
111: dserPatternSyntaxException.getIndex());
112: }
113: };
114: }
|