0N/A/*
2362N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 6324523
0N/A * @summary Check that setting the wrong type of an attribute in a Standard
0N/A * MBean or MXBean causes InvalidAttributeValueException
0N/A * @author Eamonn McManus
0N/A * @run clean SetWrongTypeAttributeTest
0N/A * @run build SetWrongTypeAttributeTest
0N/A * @run main SetWrongTypeAttributeTest
0N/A */
0N/A
0N/Aimport java.util.*;
0N/Aimport javax.management.*;
0N/A
0N/Apublic class SetWrongTypeAttributeTest {
0N/A // In this 2D array, the first element of each subarray is an attribute
0N/A // to do setAttribute on, and the remaining elements are values that
0N/A // should provoke InvalidAttributeValueException.
0N/A private static final Object[][] TEST_VALUES = {
0N/A {"Foo", null, 5, "false", Collections.<String,String>emptyMap()},
0N/A {"Name", 5, false, Collections.<String,String>emptyMap()},
0N/A {"Properties", 5, false, Collections.singleton("foo")},
0N/A };
0N/A
0N/A public static interface BlahMBean {
0N/A public boolean isFoo();
0N/A public void setFoo(boolean foo);
0N/A
0N/A public String getName();
0N/A public void setName(String name);
0N/A
0N/A public Map<String,String> getProperties();
0N/A public void setProperties(Map<String,String> map);
0N/A }
0N/A
0N/A public static interface BlahMXBean {
0N/A public boolean isFoo();
0N/A public void setFoo(boolean foo);
0N/A
0N/A public String getName();
0N/A public void setName(String name);
0N/A
0N/A public Map<String,String> getProperties();
0N/A public void setProperties(Map<String,String> map);
0N/A }
0N/A
0N/A public static class BlahBase {
0N/A public boolean isFoo() {
0N/A return foo;
0N/A }
0N/A public void setFoo(boolean foo) {
0N/A this.foo = foo;
0N/A }
0N/A
0N/A public String getName() {
0N/A return name;
0N/A }
0N/A public void setName(String name) {
0N/A this.name = name;
0N/A }
0N/A
0N/A public Map<String,String> getProperties() {
0N/A return properties;
0N/A }
0N/A public void setProperties(Map<String,String> map) {
0N/A this.properties = map;
0N/A }
0N/A
0N/A private boolean foo;
0N/A private String name;
0N/A private Map<String,String> properties;
0N/A }
0N/A
0N/A public static class Blah extends BlahBase implements BlahMBean {}
0N/A
0N/A public static class MXBlah extends BlahBase implements BlahMXBean {}
0N/A
0N/A public static class StdBlah extends StandardMBean implements BlahMBean {
0N/A public StdBlah() throws NotCompliantMBeanException {
0N/A super(BlahMBean.class);
0N/A }
0N/A
0N/A public boolean isFoo() {
0N/A return foo;
0N/A }
0N/A public void setFoo(boolean foo) {
0N/A this.foo = foo;
0N/A }
0N/A
0N/A public String getName() {
0N/A return name;
0N/A }
0N/A public void setName(String name) {
0N/A this.name = name;
0N/A }
0N/A
0N/A public Map<String,String> getProperties() {
0N/A return properties;
0N/A }
0N/A public void setProperties(Map<String,String> map) {
0N/A this.properties = map;
0N/A }
0N/A
0N/A private boolean foo;
0N/A private String name;
0N/A private Map<String,String> properties;
0N/A }
0N/A
0N/A public static class StdMXBlah extends StandardMBean implements BlahMXBean {
0N/A public StdMXBlah() throws NotCompliantMBeanException {
0N/A super(BlahMXBean.class, true);
0N/A }
0N/A
0N/A public boolean isFoo() {
0N/A return foo;
0N/A }
0N/A public void setFoo(boolean foo) {
0N/A this.foo = foo;
0N/A }
0N/A
0N/A public String getName() {
0N/A return name;
0N/A }
0N/A public void setName(String name) {
0N/A this.name = name;
0N/A }
0N/A
0N/A public Map<String,String> getProperties() {
0N/A return properties;
0N/A }
0N/A public void setProperties(Map<String,String> map) {
0N/A this.properties = map;
0N/A }
0N/A
0N/A private boolean foo;
0N/A private String name;
0N/A private Map<String,String> properties;
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A test("Standard Blah", new Blah());
0N/A test("StandardMBean implementing Blah", new StdBlah());
0N/A test("StandardMBean wrapping Blah",
0N/A new StandardMBean(new Blah(), BlahMBean.class));
0N/A test("MXBean Blah", new MXBlah());
0N/A test("StandardMBean implementing MXBean Blah", new StdMXBlah());
0N/A test("StandardMBean wrapping MXBean Blah",
0N/A new StandardMBean(new MXBlah(), BlahMXBean.class, true));
0N/A
0N/A if (failure == null)
0N/A System.out.println("TEST PASSED");
0N/A else
0N/A throw new Exception("TEST FAILED: " + failure);
0N/A }
0N/A
0N/A private static void test(String what, Object obj) throws Exception {
0N/A System.out.println(what + "...");
0N/A MBeanServer mbs = MBeanServerFactory.newMBeanServer();
0N/A ObjectName on = new ObjectName("a:b=c");
0N/A mbs.registerMBean(obj, on);
0N/A for (Object[] testValue : TEST_VALUES) {
0N/A String attrName = (String) testValue[0];
0N/A for (int i = 1; i < testValue.length; i++) {
0N/A Object value = testValue[i];
0N/A final String doing =
0N/A "setAttribute(" + attrName + ", " + value + ")";
0N/A try {
0N/A mbs.setAttribute(on, new Attribute("Foo", 5));
0N/A fail(what, doing + " succeeded but should fail!");
0N/A } catch (InvalidAttributeValueException e) {
0N/A final String msg =
0N/A doing + ": OK, got expected " +
0N/A "InvalidAttributeValueException";
0N/A System.out.println(msg);
0N/A } catch (Throwable e) {
0N/A fail(what, doing + " got wrong exception: " + e);
0N/A e.printStackTrace(System.out);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A private static void fail(String what, String msg) {
0N/A failure = what + ": " + msg;
0N/A System.out.println("FAILED: " + failure);
0N/A }
0N/A
0N/A private static String failure;
0N/A}