1 /* 2 * Copyright (c) 2008, 2015, 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 6336968 27 * @summary Test adding non-Attribute values to an AttributeList. 28 * @author Eamonn McManus 29 */ 30 31 import java.util.Collections; 32 import java.util.List; 33 import javax.management.Attribute; 34 import javax.management.AttributeList; 35 36 public class AttributeListTypeSafeTest { 37 38 private static String failure; 39 40 public static void main(String[] args) throws Exception { 41 // Test calling asList after adding non-Attribute by various means 42 for (Op op : Op.values()) { 43 AttributeList alist = new AttributeList(); 44 alist.add(new Attribute("foo", "bar")); 45 doOp(alist, op); 46 String what = "asList() after calling " + op + " with non-Attribute"; 47 try { 48 List<Attribute> lista = alist.asList(); 49 fail(what + ": succeeded but should not have"); 50 } catch (IllegalArgumentException e) { 51 System.out.println("OK: " + what + ": got IllegalArgumentException"); 52 } 53 } 54 55 // Test adding non-Attribute by various means after calling asList 56 for (Op op : Op.values()) { 57 AttributeList alist = new AttributeList(); 58 List<Attribute> lista = alist.asList(); 59 lista.add(new Attribute("foo", "bar")); 60 String what = op + " with non-Attribute after calling asList()"; 61 try { 62 doOp(alist, op); 63 fail(what + ": succeeded but should not have"); 64 } catch (IllegalArgumentException e) { 65 System.out.println("OK: " + what + ": got IllegalArgumentException"); 66 } 67 } 68 69 if (failure == null) 70 System.out.println("TEST PASSED"); 71 else 72 throw new Exception("TEST FAILED: " + failure); 73 } 74 75 private static enum Op { 76 ADD("add(Object)"), ADD_AT("add(int, Object)"), 77 ADD_ALL("add(Collection)"), ADD_ALL_AT("add(int, Collection)"), 78 SET("set(int, Object)"); 79 80 private Op(String what) { 81 this.what = what; 82 } 83 84 @Override 85 public String toString() { 86 return what; 87 } 88 89 private final String what; 90 } 91 92 private static void doOp(AttributeList alist, Op op) { 93 Object x = "oops"; 94 switch (op) { 95 case ADD: alist.add(x); break; 96 case ADD_AT: alist.add(0, x); break; 97 case ADD_ALL: alist.add(Collections.singleton(x)); break; 98 case ADD_ALL_AT: alist.add(0, Collections.singleton(x)); break; 99 case SET: alist.set(0, x); break; 100 default: throw new AssertionError("Case not covered"); 101 } 102 } 103 104 private static void fail(String why) { 105 System.out.println("FAIL: " + why); 106 failure = why; 107 } 108 109 }