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 6324523 27 * @summary Check that setting the wrong type of an attribute in a Standard 28 * MBean or MXBean causes InvalidAttributeValueException 29 * @author Eamonn McManus 30 * @run clean SetWrongTypeAttributeTest 31 * @run build SetWrongTypeAttributeTest 32 * @run main SetWrongTypeAttributeTest 33 */ 34 35 import java.util.*; 36 import javax.management.*; 37 38 public class SetWrongTypeAttributeTest { 39 // In this 2D array, the first element of each subarray is an attribute 40 // to do setAttribute on, and the remaining elements are values that 41 // should provoke InvalidAttributeValueException. 42 private static final Object[][] TEST_VALUES = { 43 {"Foo", null, 5, "false", Collections.<String,String>emptyMap()}, 44 {"Name", 5, false, Collections.<String,String>emptyMap()}, 45 {"Properties", 5, false, Collections.singleton("foo")}, 46 }; 47 48 public static interface BlahMBean { 49 public boolean isFoo(); 50 public void setFoo(boolean foo); 51 52 public String getName(); 53 public void setName(String name); 54 55 public Map<String,String> getProperties(); 56 public void setProperties(Map<String,String> map); 57 } 58 59 public static interface BlahMXBean { 60 public boolean isFoo(); 61 public void setFoo(boolean foo); 62 63 public String getName(); 64 public void setName(String name); 65 66 public Map<String,String> getProperties(); 67 public void setProperties(Map<String,String> map); 68 } 69 70 public static class BlahBase { 71 public boolean isFoo() { 72 return foo; 73 } 74 public void setFoo(boolean foo) { 75 this.foo = foo; 76 } 77 78 public String getName() { 79 return name; 80 } 81 public void setName(String name) { 82 this.name = name; 83 } 84 85 public Map<String,String> getProperties() { 86 return properties; 87 } 88 public void setProperties(Map<String,String> map) { 89 this.properties = map; 90 } 91 92 private boolean foo; 93 private String name; 94 private Map<String,String> properties; 95 } 96 97 public static class Blah extends BlahBase implements BlahMBean {} 98 99 public static class MXBlah extends BlahBase implements BlahMXBean {} 100 101 public static class StdBlah extends StandardMBean implements BlahMBean { 102 public StdBlah() throws NotCompliantMBeanException { 103 super(BlahMBean.class); 104 } 105 106 public boolean isFoo() { 107 return foo; 108 } 109 public void setFoo(boolean foo) { 110 this.foo = foo; 111 } 112 113 public String getName() { 114 return name; 115 } 116 public void setName(String name) { 117 this.name = name; 118 } 119 120 public Map<String,String> getProperties() { 121 return properties; 122 } 123 public void setProperties(Map<String,String> map) { 124 this.properties = map; 125 } 126 127 private boolean foo; 128 private String name; 129 private Map<String,String> properties; 130 } 131 132 public static class StdMXBlah extends StandardMBean implements BlahMXBean { 133 public StdMXBlah() throws NotCompliantMBeanException { 134 super(BlahMXBean.class, true); 135 } 136 137 public boolean isFoo() { 138 return foo; 139 } 140 public void setFoo(boolean foo) { 141 this.foo = foo; 142 } 143 144 public String getName() { 145 return name; 146 } 147 public void setName(String name) { 148 this.name = name; 149 } 150 151 public Map<String,String> getProperties() { 152 return properties; 153 } 154 public void setProperties(Map<String,String> map) { 155 this.properties = map; 156 } 157 158 private boolean foo; 159 private String name; 160 private Map<String,String> properties; 161 } 162 163 public static void main(String[] args) throws Exception { 164 test("Standard Blah", new Blah()); 165 test("StandardMBean implementing Blah", new StdBlah()); 166 test("StandardMBean wrapping Blah", 167 new StandardMBean(new Blah(), BlahMBean.class)); 168 test("MXBean Blah", new MXBlah()); 169 test("StandardMBean implementing MXBean Blah", new StdMXBlah()); 170 test("StandardMBean wrapping MXBean Blah", 171 new StandardMBean(new MXBlah(), BlahMXBean.class, true)); 172 173 if (failure == null) 174 System.out.println("TEST PASSED"); 175 else 176 throw new Exception("TEST FAILED: " + failure); 177 } 178 179 private static void test(String what, Object obj) throws Exception { 180 System.out.println(what + "..."); 181 MBeanServer mbs = MBeanServerFactory.newMBeanServer(); 182 ObjectName on = new ObjectName("a:b=c"); 183 mbs.registerMBean(obj, on); 184 for (Object[] testValue : TEST_VALUES) { 185 String attrName = (String) testValue[0]; 186 for (int i = 1; i < testValue.length; i++) { 187 Object value = testValue[i]; 188 final String doing = 189 "setAttribute(" + attrName + ", " + value + ")"; 190 try { 191 mbs.setAttribute(on, new Attribute("Foo", 5)); 192 fail(what, doing + " succeeded but should fail!"); 193 } catch (InvalidAttributeValueException e) { 194 final String msg = 195 doing + ": OK, got expected " + 196 "InvalidAttributeValueException"; 197 System.out.println(msg); 198 } catch (Throwable e) { 199 fail(what, doing + " got wrong exception: " + e); 200 e.printStackTrace(System.out); 201 } 202 } 203 } 204 } 205 206 private static void fail(String what, String msg) { 207 failure = what + ": " + msg; 208 System.out.println("FAILED: " + failure); 209 } 210 211 private static String failure; 212 }