1 /* 2 * Copyright (C) 2008 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 android.core; 18 19 import junit.framework.TestCase; 20 21 import java.lang.reflect.Array; 22 import android.test.suitebuilder.annotation.SmallTest; 23 24 /** 25 * Test java.lang.reflect.Array methods. 26 */ 27 public class ReflectArrayTest extends TestCase { 28 29 @SmallTest testSingleInt()30 public void testSingleInt() throws Exception { 31 Object intArray = Array.newInstance(Integer.TYPE, 2); 32 33 int[] array = (int[]) intArray; 34 array[0] = 5; 35 Array.setInt(intArray, 1, 6); 36 37 assertEquals(5, Array.getInt(intArray, 0)); 38 assertEquals(6, array[1]); 39 40 try { 41 array[2] = 27; 42 fail("store should have failed"); 43 } catch (ArrayIndexOutOfBoundsException abe) { 44 // expected 45 } 46 47 assertEquals(2, array.length); 48 assertEquals(Array.getLength(intArray), array.length); 49 50 try { 51 int[][] wrongArray = (int[][]) intArray; 52 fail("cast should have failed"); 53 } catch (ClassCastException cce) { 54 // expected 55 } 56 57 intArray = Array.newInstance(Integer.TYPE, 0); 58 assertEquals(0, Array.getLength(intArray)); 59 } 60 61 @SmallTest testSingle()62 public void testSingle() throws Exception { 63 Object strArray = Array.newInstance(String.class, 2); 64 65 String[] array = (String[]) strArray; 66 array[0] = "entry zero"; 67 Array.set(strArray, 1, "entry one"); 68 69 //System.out.println("array: " + array); 70 71 assertEquals("entry zero", Array.get(strArray, 0)); 72 assertEquals("entry one", array[1]); 73 74 assertEquals(2, array.length); 75 assertEquals(Array.getLength(strArray), array.length); 76 } 77 78 @SmallTest testMultiInt()79 public void testMultiInt() throws Exception { 80 int[] dimensions = {3, 2, 1}; 81 Object intIntIntArray = Array.newInstance(Integer.TYPE, dimensions); 82 int[][][] array3 = (int[][][]) intIntIntArray; 83 84 array3[0][0][0] = 123; 85 array3[2][1][0] = 456; 86 87 try { 88 array3[2][1][1] = 768; 89 fail("store should have failed"); 90 } catch (ArrayIndexOutOfBoundsException abe) { 91 // expected 92 } 93 94 //System.out.println("array3: " + array3); 95 } 96 97 @SmallTest testMulti()98 public void testMulti() throws Exception { 99 int[] dimensions = {1, 2, 3}; 100 Object strStrStrArray = Array.newInstance(String.class, dimensions); 101 String[][][] array3 = (String[][][]) strStrStrArray; 102 103 array3[0][0][0] = "zero zero zero"; 104 array3[0][1][2] = "zero one two"; 105 106 try { 107 array3[1][0][0] = "bad store"; 108 fail("store should have failed"); 109 } catch (ArrayIndexOutOfBoundsException abe) { 110 // expected 111 } 112 113 try { 114 String[][] array2 = (String[][]) strStrStrArray; 115 fail("expecting bad cast"); 116 } catch (ClassCastException cce) { 117 // expected 118 } 119 //System.out.println("array3: " + array3); 120 121 122 int[] dimensions2 = {1, 2}; 123 strStrStrArray = Array.newInstance(String[].class, dimensions2); 124 array3 = (String[][][]) strStrStrArray; 125 array3[0][1] = new String[3]; 126 array3[0][1][2] = "zero one two"; 127 try { 128 array3[1][0][0] = "bad store"; 129 fail("store should have failed"); 130 } catch (ArrayIndexOutOfBoundsException abe) { 131 // expected 132 } 133 //System.out.println("array3: " + array3); 134 } 135 } 136 137