InvalidClassExceptionTest.java revision f6c387128427e121477c1b32ad35cdcaa5101ba3
1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package tests.api.java.io;
19
20import dalvik.annotation.TestTargets;
21import dalvik.annotation.TestLevel;
22import dalvik.annotation.TestTargetNew;
23import dalvik.annotation.TestTargetClass;
24import java.io.InvalidClassException;
25
26@TestTargetClass(InvalidClassException.class)
27public class InvalidClassExceptionTest extends junit.framework.TestCase {
28
29    /**
30     * @tests java.io.InvalidClassException#InvalidClassException(java.lang.String)
31     */
32    @TestTargetNew(
33        level = TestLevel.COMPLETE,
34        notes = "",
35        method = "InvalidClassException",
36        args = {java.lang.String.class}
37    )
38    public void test_ConstructorLjava_lang_String() {
39        final String message = "A message";
40        try {
41            if (true)
42                throw new java.io.InvalidClassException(message);
43        } catch (InvalidClassException e) {
44            // correct
45            assertTrue("Incorrect message read", e.getMessage().equals(message));
46            return;
47        }
48        fail("Failed to throw exception");
49    }
50
51    /**
52     * @tests java.io.InvalidClassException#InvalidClassException(java.lang.String,
53     *        java.lang.String)
54     */
55    @TestTargets({
56        @TestTargetNew(
57            level = TestLevel.COMPLETE,
58            notes = "",
59            method = "InvalidClassException",
60            args = {java.lang.String.class, java.lang.String.class}
61        ),
62        @TestTargetNew(
63            level = TestLevel.COMPLETE,
64            notes = "",
65            method = "getMessage",
66            args = {}
67        )
68    })
69    public void test_ConstructorLjava_lang_StringLjava_lang_String() {
70        // Test for method java.io.InvalidClassException(java.lang.String,
71        // java.lang.String)
72        final String message = "A message";
73        final String className = "Object";
74        try {
75            if (true)
76                throw new java.io.InvalidClassException(className, message);
77        } catch (InvalidClassException e) {
78            // correct
79            String returnedMessage = e.getMessage();
80            assertTrue("Incorrect message read: " + e.getMessage(),
81                    returnedMessage.indexOf(className) >= 0
82                            && returnedMessage.indexOf(message) >= 0);
83            return;
84        }
85        fail("Failed to throw exception");
86    }
87
88    /**
89     * Sets up the fixture, for example, open a network connection. This method
90     * is called before a test is executed.
91     */
92    protected void setUp() {
93    }
94
95    /**
96     * Tears down the fixture, for example, close a network connection. This
97     * method is called after a test is executed.
98     */
99    protected void tearDown() {
100    }
101}
102