1feebfd5e2743f5832a9e21b6475098fba1867e6aElliott Hughes/* 2feebfd5e2743f5832a9e21b6475098fba1867e6aElliott Hughes * Copyright (C) 2010 The Android Open Source Project 3feebfd5e2743f5832a9e21b6475098fba1867e6aElliott Hughes * 4feebfd5e2743f5832a9e21b6475098fba1867e6aElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 5feebfd5e2743f5832a9e21b6475098fba1867e6aElliott Hughes * you may not use this file except in compliance with the License. 6feebfd5e2743f5832a9e21b6475098fba1867e6aElliott Hughes * You may obtain a copy of the License at 7feebfd5e2743f5832a9e21b6475098fba1867e6aElliott Hughes * 8feebfd5e2743f5832a9e21b6475098fba1867e6aElliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 9feebfd5e2743f5832a9e21b6475098fba1867e6aElliott Hughes * 10feebfd5e2743f5832a9e21b6475098fba1867e6aElliott Hughes * Unless required by applicable law or agreed to in writing, software 11feebfd5e2743f5832a9e21b6475098fba1867e6aElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 12feebfd5e2743f5832a9e21b6475098fba1867e6aElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13feebfd5e2743f5832a9e21b6475098fba1867e6aElliott Hughes * See the License for the specific language governing permissions and 14feebfd5e2743f5832a9e21b6475098fba1867e6aElliott Hughes * limitations under the License. 15feebfd5e2743f5832a9e21b6475098fba1867e6aElliott Hughes */ 16feebfd5e2743f5832a9e21b6475098fba1867e6aElliott Hughes 17feebfd5e2743f5832a9e21b6475098fba1867e6aElliott Hughespackage libcore.java.lang; 18feebfd5e2743f5832a9e21b6475098fba1867e6aElliott Hughes 1991084450d27ac954dfd0a937373fb675547655a7Dan Bornsteinimport java.io.InputStream; 2091084450d27ac954dfd0a937373fb675547655a7Dan Bornsteinimport java.lang.reflect.Array; 21415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughesimport java.util.EnumMap; 22415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughesimport java.util.EnumSet; 23feebfd5e2743f5832a9e21b6475098fba1867e6aElliott Hughesimport junit.framework.TestCase; 24feebfd5e2743f5832a9e21b6475098fba1867e6aElliott Hughes 25feebfd5e2743f5832a9e21b6475098fba1867e6aElliott Hughespublic final class ClassCastExceptionTest extends TestCase { 26415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes public void testClassCast() throws Exception { 27415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes Object o = new Exception(); 28415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes try { 29415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes String.class.cast(o); 30415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes fail(); 31415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes } catch (ClassCastException ex) { 3287778fb754e6e9bf04e1d8e10fd13f49cc2aa608Przemyslaw Szczepaniak assertEquals("Cannot cast java.lang.Exception to java.lang.String", ex.getMessage()); 33415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes } 34415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes } 35415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes 36415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes public void testClassAsSubclass() throws Exception { 37415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes try { 38415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes Exception.class.asSubclass(String.class); 39415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes fail(); 40415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes } catch (ClassCastException ex) { 4161927cc10bf0c26767c9f28da1205e53f87b2697Narayan Kamath assertEquals("class java.lang.Exception cannot be cast to java.lang.String", 4291084450d27ac954dfd0a937373fb675547655a7Dan Bornstein ex.getMessage()); 4391084450d27ac954dfd0a937373fb675547655a7Dan Bornstein } 4491084450d27ac954dfd0a937373fb675547655a7Dan Bornstein } 4591084450d27ac954dfd0a937373fb675547655a7Dan Bornstein 46415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes enum E { A, B, C }; 47415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes enum F { A, B, C }; 48415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes 49415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes public void testEnumMapPut() throws Exception { 50415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes EnumMap m = new EnumMap(E.class); 51415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes try { 52415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes m.put(F.A, "world"); 53415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes fail(); 54415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes } catch (ClassCastException ex) { 55415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes assertNotNull(ex.getMessage()); 56415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes } 57415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes } 58415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes 59415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes public void testMiniEnumSetAdd() throws Exception { 60415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes EnumSet m = EnumSet.noneOf(E.class); 61415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes try { 62415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes m.add(F.A); 63415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes fail(); 64415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes } catch (ClassCastException ex) { 65415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes assertNotNull(ex.getMessage()); 66415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes } 67415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes } 68415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes 69415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes public void testMiniEnumSetAddAll() throws Exception { 70415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes EnumSet m = EnumSet.noneOf(E.class); 71415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes EnumSet n = EnumSet.allOf(F.class); 72415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes try { 73415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes m.addAll(n); 74415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes fail(); 75415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes } catch (ClassCastException ex) { 76415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes assertNotNull(ex.getMessage()); 77415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes } 78415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes } 79415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes 80415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes enum HugeE { 81415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes 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, 82415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes 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, 83415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes 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, 84415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes }; 85415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes enum HugeF { 86415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes 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, 87415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes 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, 88415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes 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, 89415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes }; 90415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes 91415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes public void testHugeEnumSetAdd() throws Exception { 92415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes EnumSet m = EnumSet.noneOf(HugeE.class); 93415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes try { 94415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes m.add(HugeF.A0); 95415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes fail(); 96415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes } catch (ClassCastException ex) { 97415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes assertNotNull(ex.getMessage()); 98415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes } 99415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes } 100415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes 101415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes public void testHugeEnumSetAddAll() throws Exception { 102415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes EnumSet m = EnumSet.noneOf(HugeE.class); 103415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes EnumSet n = EnumSet.allOf(HugeF.class); 104415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes try { 105415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes m.addAll(n); 106415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes fail(); 107415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes } catch (ClassCastException ex) { 108415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes assertNotNull(ex.getMessage()); 109415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes } 110415c7497ec02890a73eb293f98f69c1f6983389bElliott Hughes } 111feebfd5e2743f5832a9e21b6475098fba1867e6aElliott Hughes} 112