Home | History | Annotate | Download | only in lang
      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 
     17 package libcore.java.lang;
     18 
     19 import java.io.InputStream;
     20 import java.lang.reflect.Array;
     21 import java.util.EnumMap;
     22 import java.util.EnumSet;
     23 import junit.framework.TestCase;
     24 
     25 public final class ClassCastExceptionTest extends TestCase {
     26     public void testCast() throws Exception {
     27         Object o = new Exception();
     28         try {
     29             String s = (String) o;
     30             fail();
     31         } catch (ClassCastException ex) {
     32             assertEquals("java.lang.Exception cannot be cast to java.lang.String", ex.getMessage());
     33         }
     34     }
     35 
     36     public void testClassCast() throws Exception {
     37         Object o = new Exception();
     38         try {
     39             String.class.cast(o);
     40             fail();
     41         } catch (ClassCastException ex) {
     42             assertEquals("java.lang.Exception cannot be cast to java.lang.String", ex.getMessage());
     43         }
     44     }
     45 
     46     public void testClassAsSubclass() throws Exception {
     47         try {
     48             Exception.class.asSubclass(String.class);
     49             fail();
     50         } catch (ClassCastException ex) {
     51             assertEquals("java.lang.Exception cannot be cast to java.lang.String",
     52                     ex.getMessage());
     53         }
     54     }
     55 
     56     public void testCastOperator() throws Exception {
     57         try {
     58             Object o = (InputStream) makeInteger();
     59             fail();
     60         } catch (ClassCastException ex) {
     61             assertEquals("java.lang.Integer cannot be cast to java.io.InputStream",
     62                     ex.getMessage());
     63         }
     64     }
     65 
     66     public void testCastOperatorWithArrays() throws Exception {
     67         try {
     68             Object o = (E) makeArray(String.class);
     69             fail();
     70         } catch (ClassCastException ex) {
     71             assertEquals("java.lang.String[] cannot be cast to "
     72                     + "libcore.java.lang.ClassCastExceptionTest$E",
     73                     ex.getMessage());
     74         }
     75 
     76         try {
     77             Object o = (E) makeArray(float.class);
     78             fail();
     79         } catch (ClassCastException ex) {
     80             assertEquals("float[] cannot be cast to libcore.java.lang.ClassCastExceptionTest$E",
     81                     ex.getMessage());
     82         }
     83 
     84         try {
     85             Object o = (E) makeArray(char[].class);
     86             fail();
     87         } catch (ClassCastException ex) {
     88             assertEquals("char[][] cannot be cast to libcore.java.lang.ClassCastExceptionTest$E",
     89                     ex.getMessage());
     90         }
     91 
     92         try {
     93             Object o = (Object[][][]) makeInteger();
     94             fail();
     95         } catch (ClassCastException ex) {
     96             assertEquals("java.lang.Integer cannot be cast to java.lang.Object[][][]",
     97                     ex.getMessage());
     98         }
     99     }
    100 
    101     /**
    102      * Helper for {@link #testCastOperator} and {@link
    103      * #testCastOperatorWithArrays}, above. It's important that the
    104      * return type is {@code Object}, since otherwise the compiler
    105      * will just reject the code.
    106      */
    107     private static Object makeInteger() {
    108         return new Integer(5);
    109     }
    110 
    111     /**
    112      * Helper for {@link #testCastOperatorWithArrays} above. It's important that
    113      * the return type is {@code Object}.
    114      */
    115     private static Object makeArray(Class clazz) {
    116         return Array.newInstance(clazz, 1);
    117     }
    118 
    119     enum E { A, B, C };
    120     enum F { A, B, C };
    121 
    122     public void testEnumMapPut() throws Exception {
    123         EnumMap m = new EnumMap(E.class);
    124         try {
    125             m.put(F.A, "world");
    126             fail();
    127         } catch (ClassCastException ex) {
    128             ex.printStackTrace();
    129             assertNotNull(ex.getMessage());
    130         }
    131     }
    132 
    133     public void testMiniEnumSetAdd() throws Exception {
    134         EnumSet m = EnumSet.noneOf(E.class);
    135         try {
    136             m.add(F.A);
    137             fail();
    138         } catch (ClassCastException ex) {
    139             ex.printStackTrace();
    140             assertNotNull(ex.getMessage());
    141         }
    142     }
    143 
    144     public void testMiniEnumSetAddAll() throws Exception {
    145         EnumSet m = EnumSet.noneOf(E.class);
    146         EnumSet n = EnumSet.allOf(F.class);
    147         try {
    148             m.addAll(n);
    149             fail();
    150         } catch (ClassCastException ex) {
    151             ex.printStackTrace();
    152             assertNotNull(ex.getMessage());
    153         }
    154     }
    155 
    156     enum HugeE {
    157         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,
    158         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,
    159         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,
    160     };
    161     enum HugeF {
    162         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,
    163         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,
    164         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,
    165     };
    166 
    167     public void testHugeEnumSetAdd() throws Exception {
    168         EnumSet m = EnumSet.noneOf(HugeE.class);
    169         try {
    170             m.add(HugeF.A0);
    171             fail();
    172         } catch (ClassCastException ex) {
    173             ex.printStackTrace();
    174             assertNotNull(ex.getMessage());
    175         }
    176     }
    177 
    178     public void testHugeEnumSetAddAll() throws Exception {
    179         EnumSet m = EnumSet.noneOf(HugeE.class);
    180         EnumSet n = EnumSet.allOf(HugeF.class);
    181         try {
    182             m.addAll(n);
    183             fail();
    184         } catch (ClassCastException ex) {
    185             ex.printStackTrace();
    186             assertNotNull(ex.getMessage());
    187         }
    188     }
    189 }
    190