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.nio.charset;
18
19import java.io.Serializable;
20import java.nio.charset.IllegalCharsetNameException;
21
22import junit.framework.TestCase;
23
24import org.apache.harmony.testframework.serialization.SerializationTest;
25import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
26
27/**
28 * Test class IllegalCharsetNameException.
29 */
30public class IllegalCharsetNameExceptionTest extends TestCase {
31
32	public void testConstructor() {
33		IllegalCharsetNameException ex = new IllegalCharsetNameException(
34				"impossible");
35		assertTrue(ex instanceof IllegalArgumentException);
36		assertNull(ex.getCause());
37		assertEquals(ex.getCharsetName(), "impossible");
38		assertTrue(ex.getMessage().indexOf("impossible") != -1);
39
40		ex = new IllegalCharsetNameException("ascii");
41		assertNull(ex.getCause());
42		assertEquals(ex.getCharsetName(), "ascii");
43		assertTrue(ex.getMessage().indexOf("ascii") != -1);
44
45		ex = new IllegalCharsetNameException("");
46		assertNull(ex.getCause());
47		assertEquals(ex.getCharsetName(), "");
48		ex.getMessage();
49
50		ex = new IllegalCharsetNameException(null);
51		assertNull(ex.getCause());
52		assertNull(ex.getCharsetName());
53		assertTrue(ex.getMessage().indexOf("null") != -1);
54
55	}
56
57    // comparator for IllegalCharsetNameException objects
58    private static final SerializableAssert COMPARATOR = new SerializableAssert() {
59        public void assertDeserialized(Serializable initial,
60                Serializable deserialized) {
61
62            // FIXME?: getMessage() returns more helpful string but
63            // this leads to incompatible message in serial form
64            //
65            // do common checks for all throwable objects
66            // SerializationTest.THROWABLE_COMPARATOR.assertDeserialized(initial,
67            //        deserialized);
68
69            IllegalCharsetNameException initEx = (IllegalCharsetNameException) initial;
70            IllegalCharsetNameException desrEx = (IllegalCharsetNameException) deserialized;
71
72            assertEquals("CharsetName", initEx.getCharsetName(), desrEx
73                    .getCharsetName());
74        }
75    };
76
77    /**
78     * @tests serialization/deserialization compatibility.
79     */
80    public void testSerializationSelf() throws Exception {
81
82        SerializationTest.verifySelf(new IllegalCharsetNameException(
83                "charsetName"), COMPARATOR);
84    }
85
86    /**
87     * @tests serialization/deserialization compatibility with RI.
88     */
89    public void testSerializationCompatibility() throws Exception {
90
91        SerializationTest.verifyGolden(this, new IllegalCharsetNameException(
92                "charsetName"), COMPARATOR);
93    }
94}
95