IllegalFormatConversionExceptionTest.java revision f6c387128427e121477c1b32ad35cdcaa5101ba3
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.luni.tests.java.util;
18
19import dalvik.annotation.TestTargets;
20import dalvik.annotation.TestLevel;
21import dalvik.annotation.TestTargetNew;
22import dalvik.annotation.TestTargetClass;
23
24import junit.framework.TestCase;
25
26import java.io.Serializable;
27import java.util.IllegalFormatConversionException;
28
29import org.apache.harmony.testframework.serialization.SerializationTest;
30import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
31
32@TestTargetClass(IllegalFormatConversionException.class)
33public class IllegalFormatConversionExceptionTest extends TestCase {
34
35    /**
36     * @tests java.util.IllegalFormatConversionException#IllegalFormatConversionException(char,
37     *        Class)
38     */
39    @TestTargetNew(
40        level = TestLevel.COMPLETE,
41        notes = "",
42        method = "IllegalFormatConversionException",
43        args = {char.class, java.lang.Class.class}
44    )
45    public void test_illegalFormatConversionException() {
46        try {
47            new IllegalFormatConversionException(' ', null);
48            fail("should throw NullPointerExcetpion.");
49        } catch (NullPointerException e) {
50            // desired
51        }
52
53        assertNotNull(new IllegalFormatConversionException(' ', String.class));
54    }
55
56    /**
57     * @tests java.util.IllegalFormatConversionException#getArgumentClass()
58     */
59    @TestTargetNew(
60        level = TestLevel.COMPLETE,
61        notes = "",
62        method = "getArgumentClass",
63        args = {}
64    )
65    public void test_getArgumentClass() {
66        char c = '*';
67        Class<String> argClass = String.class;
68        IllegalFormatConversionException illegalFormatConversionException = new IllegalFormatConversionException(
69                c, argClass);
70        assertEquals(argClass, illegalFormatConversionException
71                .getArgumentClass());
72
73    }
74
75    /**
76     * @tests java.util.IllegalFormatConversionException#getConversion()
77     */
78    @TestTargetNew(
79        level = TestLevel.COMPLETE,
80        notes = "",
81        method = "getConversion",
82        args = {}
83    )
84    public void test_getConversion() {
85        char c = '*';
86        Class<String> argClass = String.class;
87        IllegalFormatConversionException illegalFormatConversionException = new IllegalFormatConversionException(
88                c, argClass);
89        assertEquals(c, illegalFormatConversionException.getConversion());
90
91    }
92
93    /**
94     * @tests java.util.IllegalFormatConversionException#getMessage()
95     */
96    @TestTargetNew(
97        level = TestLevel.COMPLETE,
98        notes = "",
99        method = "getMessage",
100        args = {}
101    )
102    public void test_getMessage() {
103        char c = '*';
104        Class<String> argClass = String.class;
105        IllegalFormatConversionException illegalFormatConversionException = new IllegalFormatConversionException(
106                c, argClass);
107        assertTrue(null != illegalFormatConversionException.getMessage());
108
109    }
110
111    // comparator for IllegalFormatConversionException objects
112    private static final SerializableAssert exComparator = new SerializableAssert() {
113        public void assertDeserialized(Serializable initial,
114                Serializable deserialized) {
115
116            SerializationTest.THROWABLE_COMPARATOR.assertDeserialized(initial,
117                    deserialized);
118
119            IllegalFormatConversionException initEx = (IllegalFormatConversionException) initial;
120            IllegalFormatConversionException desrEx = (IllegalFormatConversionException) deserialized;
121
122            assertEquals("ArgumentClass", initEx.getArgumentClass(), desrEx
123                    .getArgumentClass());
124            assertEquals("Conversion", initEx.getConversion(), desrEx
125                    .getConversion());
126        }
127    };
128
129    /**
130     * @tests serialization/deserialization.
131     */
132    @TestTargetNew(
133        level = TestLevel.COMPLETE,
134        notes = "",
135        method = "!SerializationSelf",
136        args = {}
137    )
138    public void testSerializationSelf() throws Exception {
139
140        SerializationTest.verifySelf(new IllegalFormatConversionException('*',
141                String.class), exComparator);
142    }
143
144    /**
145     * @tests serialization/deserialization compatibility with RI.
146     */
147    @TestTargetNew(
148        level = TestLevel.COMPLETE,
149        notes = "",
150        method = "!SerializationGolden",
151        args = {}
152    )
153    public void testSerializationCompatibility() throws Exception {
154
155        SerializationTest.verifyGolden(this,
156                new IllegalFormatConversionException('*', String.class),
157                exComparator);
158    }
159}
160