UnmappableCharacterExceptionTest.java revision 89c1feb0a69a7707b271086e749975b3f7acacf7
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.nio_char.tests.java.nio.charset;
18
19import dalvik.annotation.TestTargetClass;
20import dalvik.annotation.TestInfo;
21import dalvik.annotation.TestTarget;
22import dalvik.annotation.TestLevel;
23
24import java.io.Serializable;
25import java.nio.charset.CharacterCodingException;
26import java.nio.charset.UnmappableCharacterException;
27
28import junit.framework.TestCase;
29
30import org.apache.harmony.testframework.serialization.SerializationTest;
31import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
32
33@TestTargetClass(UnmappableCharacterException.class)
34/**
35 * Test class UnmappableCharacterException.
36 */
37public class UnmappableCharacterExceptionTest extends TestCase {
38
39@TestInfo(
40      level = TestLevel.COMPLETE,
41      purpose = "",
42      targets = {
43        @TestTarget(
44          methodName = "UnmappableCharacterException",
45          methodArgs = {int.class}
46        ), @TestTarget(
47          methodName = "getMessage",
48          methodArgs = {}
49        ), @TestTarget(
50          methodName = "getInputLength",
51          methodArgs = {}
52        )
53    })
54    public void testConstructor() {
55        UnmappableCharacterException ex = new UnmappableCharacterException(3);
56        assertTrue(ex instanceof CharacterCodingException);
57        assertNull(ex.getCause());
58        assertEquals(ex.getInputLength(), 3);
59        assertTrue(ex.getMessage().indexOf("3") != -1);
60
61        ex = new UnmappableCharacterException(-3);
62        assertNull(ex.getCause());
63        assertEquals(ex.getInputLength(), -3);
64        assertTrue(ex.getMessage().indexOf("-3") != -1);
65
66        ex = new UnmappableCharacterException(0);
67        assertNull(ex.getCause());
68        assertEquals(ex.getInputLength(), 0);
69        assertTrue(ex.getMessage().indexOf("0") != -1);
70
71    }
72
73    // comparator for UnmappableCharacterException objects
74    private static final SerializableAssert COMPARATOR = new SerializableAssert() {
75        public void assertDeserialized(Serializable initial,
76                Serializable deserialized) {
77
78            // do common checks for all throwable objects
79            SerializationTest.THROWABLE_COMPARATOR.assertDeserialized(initial,
80                    deserialized);
81
82            UnmappableCharacterException initEx = (UnmappableCharacterException) initial;
83            UnmappableCharacterException desrEx = (UnmappableCharacterException) deserialized;
84
85            assertEquals("InputLength", initEx.getInputLength(), desrEx
86                    .getInputLength());
87        }
88    };
89
90    /**
91     * @tests serialization/deserialization compatibility.
92     */
93    @TestInfo(
94            level = TestLevel.COMPLETE,
95            purpose = "Verifies serialization.",
96            targets = {
97              @TestTarget(
98                methodName = "!SerializationSelf",
99                methodArgs = {}
100              )
101          })
102    public void testSerializationSelf() throws Exception {
103
104        SerializationTest.verifySelf(new UnmappableCharacterException(11),
105                COMPARATOR);
106    }
107
108    /**
109     * @tests serialization/deserialization compatibility with RI.
110     */
111    @TestInfo(
112            level = TestLevel.COMPLETE,
113            purpose = "Verifies serialization.",
114            targets = {
115              @TestTarget(
116                methodName = "!SerializationGolden",
117                methodArgs = {}
118              )
119          })
120    public void testSerializationCompatibility() throws Exception {
121
122        SerializationTest.verifyGolden(this, new UnmappableCharacterException(
123                11), COMPARATOR);
124    }
125}
126