1 /* 2 * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 6250772 27 * @summary Test that *List objects are checked after asList is called. 28 * @author Eamonn McManus 29 * @run clean ListTypeCheckTest 30 * @run build ListTypeCheckTest 31 * @run main ListTypeCheckTest 32 */ 33 34 import java.lang.reflect.*; 35 import java.util.*; 36 import javax.management.*; 37 import javax.management.relation.*; 38 39 /* For compatibility reasons, the classes AttributeList, RoleList, 40 * and RoleUnresolvedList all extend ArrayList<Object> even though 41 * logically they should extend ArrayList<Attribute> etc. They are 42 * all specified to have a method asList() with return type 43 * List<Attribute> etc, and to refuse to add any object other than 44 * an Attribute etc once this method has been called, but not before. 45 */ 46 public class ListTypeCheckTest { 47 public static void main(String[] args) throws Exception { 48 Class[] classes = { 49 AttributeList.class, RoleList.class, RoleUnresolvedList.class, 50 }; 51 for (Class c : classes) 52 test((Class<? extends ArrayList>) c); 53 } 54 55 private static void test(Class<? extends ArrayList> c) throws Exception { 56 System.out.println("Testing " + c.getName()); 57 ArrayList al = c.newInstance(); 58 test(al); 59 } 60 61 private static void test(ArrayList al) throws Exception { 62 test(al, true); 63 al.clear(); 64 Method m = al.getClass().getMethod("asList"); 65 m.invoke(al); 66 test(al, false); 67 } 68 69 private static void test(ArrayList al, boolean allowsBogus) throws Exception { 70 for (int i = 0; i < 5; i++) { 71 try { 72 switch (i) { 73 case 0: 74 al.add("yo"); 75 break; 76 case 1: 77 al.add(0, "yo"); 78 break; 79 case 2: 80 al.addAll(Arrays.asList("foo", "bar")); 81 break; 82 case 3: 83 al.addAll(0, Arrays.asList("foo", "bar")); 84 break; 85 case 4: 86 al.set(0, "foo"); 87 break; 88 default: 89 throw new Exception("test wrong"); 90 } 91 if (!allowsBogus) 92 throw new Exception("op allowed but should fail"); 93 } catch (IllegalArgumentException e) { 94 if (allowsBogus) 95 throw new Exception("got exception but should not", e); 96 } 97 } 98 } 99 }