1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * 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 libcore.java.lang;
18
19import java.io.InputStream;
20import java.lang.reflect.Array;
21import java.util.EnumMap;
22import java.util.EnumSet;
23import junit.framework.TestCase;
24
25public final class ClassCastExceptionTest extends TestCase {
26    public void testClassCast() throws Exception {
27        Object o = new Exception();
28        try {
29            String.class.cast(o);
30            fail();
31        } catch (ClassCastException ex) {
32            assertEquals("Cannot cast java.lang.Exception to java.lang.String", ex.getMessage());
33        }
34    }
35
36    public void testClassAsSubclass() throws Exception {
37        try {
38            Exception.class.asSubclass(String.class);
39            fail();
40        } catch (ClassCastException ex) {
41            assertEquals("class java.lang.Exception cannot be cast to java.lang.String",
42                    ex.getMessage());
43        }
44    }
45
46    enum E { A, B, C };
47    enum F { A, B, C };
48
49    public void testEnumMapPut() throws Exception {
50        EnumMap m = new EnumMap(E.class);
51        try {
52            m.put(F.A, "world");
53            fail();
54        } catch (ClassCastException ex) {
55            assertNotNull(ex.getMessage());
56        }
57    }
58
59    public void testMiniEnumSetAdd() throws Exception {
60        EnumSet m = EnumSet.noneOf(E.class);
61        try {
62            m.add(F.A);
63            fail();
64        } catch (ClassCastException ex) {
65            assertNotNull(ex.getMessage());
66        }
67    }
68
69    public void testMiniEnumSetAddAll() throws Exception {
70        EnumSet m = EnumSet.noneOf(E.class);
71        EnumSet n = EnumSet.allOf(F.class);
72        try {
73            m.addAll(n);
74            fail();
75        } catch (ClassCastException ex) {
76            assertNotNull(ex.getMessage());
77        }
78    }
79
80    enum HugeE {
81        A0, B0, C0, D0, E0, F0, G0, H0, I0, J0, K0, L0, M0, N0, O0, P0, Q0, R0, S0, T0, U0, V0, W0, X0, Y0, Z0,
82        A1, B1, C1, D1, E1, F1, G1, H1, I1, J1, K1, L1, M1, N1, O1, P1, Q1, R1, S1, T1, U1, V1, W1, X1, Y1, Z1,
83        A2, B2, C2, D2, E2, F2, G2, H2, I2, J2, K2, L2, M2, N2, O2, P2, Q2, R2, S2, T2, U2, V2, W2, X2, Y2, Z2,
84    };
85    enum HugeF {
86        A0, B0, C0, D0, E0, F0, G0, H0, I0, J0, K0, L0, M0, N0, O0, P0, Q0, R0, S0, T0, U0, V0, W0, X0, Y0, Z0,
87        A1, B1, C1, D1, E1, F1, G1, H1, I1, J1, K1, L1, M1, N1, O1, P1, Q1, R1, S1, T1, U1, V1, W1, X1, Y1, Z1,
88        A2, B2, C2, D2, E2, F2, G2, H2, I2, J2, K2, L2, M2, N2, O2, P2, Q2, R2, S2, T2, U2, V2, W2, X2, Y2, Z2,
89    };
90
91    public void testHugeEnumSetAdd() throws Exception {
92        EnumSet m = EnumSet.noneOf(HugeE.class);
93        try {
94            m.add(HugeF.A0);
95            fail();
96        } catch (ClassCastException ex) {
97            assertNotNull(ex.getMessage());
98        }
99    }
100
101    public void testHugeEnumSetAddAll() throws Exception {
102        EnumSet m = EnumSet.noneOf(HugeE.class);
103        EnumSet n = EnumSet.allOf(HugeF.class);
104        try {
105            m.addAll(n);
106            fail();
107        } catch (ClassCastException ex) {
108            assertNotNull(ex.getMessage());
109        }
110    }
111}
112