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.tests.java.util.regex;
18
19import java.io.ObjectStreamClass;
20import java.io.Serializable;
21import java.util.regex.Pattern;
22import java.util.regex.PatternSyntaxException;
23
24import junit.framework.TestCase;
25
26import org.apache.harmony.testframework.serialization.SerializationTest;
27import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
28
29/**
30 * TODO Type description
31 */
32@SuppressWarnings("nls")
33public class PatternSyntaxExceptionTest extends TestCase {
34    public void testCase() {
35        String regex = "(";
36        try {
37            Pattern.compile(regex);
38            fail("PatternSyntaxException expected");
39        } catch (PatternSyntaxException e) {
40            // TOFIX: Commented out assertEquals tests...
41            // TOFIX: should we match exception strings?
42            // assertEquals("Unclosed group", e.getDescription());
43            assertEquals(1, e.getIndex());
44            // assertEquals("Unclosed group near index 1\n(\n ^",
45            // e.getMessage());
46            assertEquals(regex, e.getPattern());
47        }
48    }
49
50    public void testCase2() {
51        String regex = "[4-";
52        try {
53            Pattern.compile(regex);
54            fail("PatternSyntaxException expected");
55        } catch (PatternSyntaxException e) {
56            // TOFIX: Commented out assertEquals tests...
57            // TOFIX: should we match exception strings?
58            // assertEquals("Illegal character range", e.getDescription());
59            assertEquals(3, e.getIndex());
60            // assertEquals("Illegal character range near index 3\n[4-\n ^",
61            // e.getMessage());
62            assertEquals(regex, e.getPattern());
63        }
64    }
65
66    /**
67     * @tests serialization/deserialization compatibility.
68     */
69    public void testSerializationSelf() throws Exception {
70        PatternSyntaxException object = new PatternSyntaxException("TESTDESC",
71                "TESTREGEX", 3);
72        SerializationTest.verifySelf(object, PATTERNSYNTAXEXCEPTION_COMPARATOR);
73    }
74
75    /**
76     * @tests serialization/deserialization compatibility with RI.
77     */
78    public void testSerializationCompatibility() throws Exception {
79        PatternSyntaxException object = new PatternSyntaxException("TESTDESC",
80                "TESTREGEX", 3);
81        SerializationTest.verifyGolden(this, object,
82                PATTERNSYNTAXEXCEPTION_COMPARATOR);
83    }
84
85    // Regression test for HARMONY-3787
86    public void test_objectStreamField() {
87        ObjectStreamClass objectStreamClass = ObjectStreamClass
88                .lookup(PatternSyntaxException.class);
89        assertNotNull(objectStreamClass.getField("desc"));
90    }
91
92    // comparator for BatchUpdateException field updateCounts
93    private static final SerializableAssert PATTERNSYNTAXEXCEPTION_COMPARATOR = new SerializableAssert() {
94        public void assertDeserialized(Serializable initial,
95                Serializable deserialized) {
96
97            // do common checks for all throwable objects
98            SerializationTest.THROWABLE_COMPARATOR.assertDeserialized(initial,
99                    deserialized);
100
101            PatternSyntaxException initPatternSyntaxException = (PatternSyntaxException) initial;
102            PatternSyntaxException dserPatternSyntaxException = (PatternSyntaxException) deserialized;
103
104            // verify fields
105            assertEquals(initPatternSyntaxException.getDescription(),
106                    dserPatternSyntaxException.getDescription());
107            assertEquals(initPatternSyntaxException.getPattern(),
108                    dserPatternSyntaxException.getPattern());
109            assertEquals(initPatternSyntaxException.getIndex(),
110                    dserPatternSyntaxException.getIndex());
111        }
112    };
113}
114