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