1 /*
   2  * Copyright (c) 2013, 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 import javax.management.MBeanAttributeInfo;
  25 import javax.management.MBeanConstructorInfo;
  26 import javax.management.MBeanInfo;
  27 import javax.management.MBeanNotificationInfo;
  28 import javax.management.MBeanOperationInfo;
  29 import javax.management.MBeanParameterInfo;
  30 import javax.management.modelmbean.DescriptorSupport;
  31 import javax.management.openmbean.SimpleType;
  32 
  33 /*
  34  * @test
  35  * @bug 8023669
  36  * @summary Test that hashCode()throws NullPointerException
  37  * @author Shanliang JIANG
  38  * @modules java.management
  39  * @run clean MBeanInfoHashCodeNPETest
  40  * @run build MBeanInfoHashCodeNPETest
  41  * @run main MBeanInfoHashCodeNPETest
  42  */
  43 public class MBeanInfoHashCodeNPETest {
  44     private static int failed = 0;
  45 
  46     public static void main(String[] args) throws Exception {
  47         System.out.println("---MBeanInfoHashCodeNPETest-main ...");
  48 
  49         // ----
  50         System.out.println("\n---Testing on MBeanAttributeInfo...");
  51         MBeanAttributeInfo mbeanAttributeInfo = new MBeanAttributeInfo(
  52                 null, SimpleType.INTEGER.getClassName(), "description", true, true, false);
  53         test(mbeanAttributeInfo, "class name");
  54 
  55         mbeanAttributeInfo = new MBeanAttributeInfo(
  56                 "name", null, "description", true, true, false);
  57         test(mbeanAttributeInfo, "type");
  58 
  59         mbeanAttributeInfo = new MBeanAttributeInfo(
  60                 "name", SimpleType.INTEGER.getClassName(), null, true, true, false);
  61         test(mbeanAttributeInfo, "description");
  62 
  63         // ----
  64         System.out.println("\n---Testing on MBeanConstructorInfo...");
  65         MBeanConstructorInfo mbeanConstructorInfo = new MBeanConstructorInfo(
  66                 null, "", new MBeanParameterInfo[]{}, new DescriptorSupport());
  67         test(mbeanConstructorInfo, "name");
  68 
  69         mbeanConstructorInfo = new MBeanConstructorInfo(
  70                 "", null, new MBeanParameterInfo[]{}, new DescriptorSupport());
  71         test(mbeanConstructorInfo, "description");
  72 
  73         mbeanConstructorInfo = new MBeanConstructorInfo(
  74                 "", "", null, new DescriptorSupport());
  75         test(mbeanConstructorInfo, "MBeanParameterInfo");
  76 
  77         mbeanConstructorInfo = new MBeanConstructorInfo(
  78                 "", "", new MBeanParameterInfo[]{}, null);
  79         test(mbeanConstructorInfo, "descriptor");
  80 
  81         // ----
  82         System.out.println("\n---Testing on MBeanOperationInfo...");
  83         MBeanOperationInfo mbeanOperationInfo = new MBeanOperationInfo(
  84                 null, "description", new MBeanParameterInfo[]{}, "type", 1, new DescriptorSupport());
  85         test(mbeanOperationInfo, "name");
  86 
  87         mbeanOperationInfo = new MBeanOperationInfo(
  88                 "name", null, new MBeanParameterInfo[]{}, "type", 1, new DescriptorSupport());
  89         test(mbeanOperationInfo, "description");
  90 
  91         mbeanOperationInfo = new MBeanOperationInfo(
  92                 "name", "description", null, "type", 1, new DescriptorSupport());
  93         test(mbeanOperationInfo, "MBeanParameterInfo");
  94 
  95         mbeanOperationInfo = new MBeanOperationInfo(
  96                 "name", "description", new MBeanParameterInfo[]{}, null, 1, new DescriptorSupport());
  97         test(mbeanOperationInfo, "type");
  98 
  99         mbeanOperationInfo = new MBeanOperationInfo(
 100                 "name", "description", new MBeanParameterInfo[]{}, "type", -1, new DescriptorSupport());
 101         test(mbeanOperationInfo, "native impact");
 102 
 103         mbeanOperationInfo = new MBeanOperationInfo(
 104                 "name", "description", new MBeanParameterInfo[]{}, "type", 1, null);
 105         test(mbeanOperationInfo, "Descriptor");
 106 
 107         // ----
 108         System.out.println("\n---Testing on MBeanParameterInfo...");
 109         MBeanParameterInfo mbeanParameterInfo = new MBeanParameterInfo(
 110                 null, "type", "description", new DescriptorSupport());
 111         test(mbeanParameterInfo, "name");
 112 
 113         mbeanParameterInfo = new MBeanParameterInfo(
 114                 "name", null, "description", new DescriptorSupport());
 115         test(mbeanParameterInfo, "description");
 116 
 117         mbeanParameterInfo = new MBeanParameterInfo(
 118                 "name", "type", null, new DescriptorSupport());
 119         test(mbeanParameterInfo, "description");
 120 
 121         mbeanParameterInfo = new MBeanParameterInfo(
 122                 "name", "type", "description", null);
 123         test(mbeanParameterInfo, "Descriptor");
 124 
 125         // ----
 126         System.out.println("\n---Testing on MBeanInfo...");
 127         String className = "toto";
 128         String description = "titi";
 129         MBeanAttributeInfo[] attrInfos = new MBeanAttributeInfo[]{};
 130         MBeanConstructorInfo[] constrInfos = new MBeanConstructorInfo[]{};
 131         MBeanOperationInfo[] operaInfos = new MBeanOperationInfo[]{};
 132         MBeanNotificationInfo[] notifInfos = new MBeanNotificationInfo[]{};
 133 
 134         MBeanInfo minfo = new MBeanInfo(null, description, attrInfos, constrInfos, operaInfos, notifInfos);
 135         test(minfo, "class name");
 136 
 137         minfo = new MBeanInfo(className, description, attrInfos, constrInfos, operaInfos, notifInfos);
 138         test(minfo, "name");
 139 
 140         minfo = new MBeanInfo(className, null, attrInfos, constrInfos, operaInfos, notifInfos);
 141         test(minfo, "description");
 142 
 143         minfo = new MBeanInfo(className, description, null, constrInfos, operaInfos, notifInfos);
 144         test(minfo, "attrInfos");
 145 
 146         minfo = new MBeanInfo(className, description, attrInfos, constrInfos, null, notifInfos);
 147         test(minfo, "operaInfos");
 148 
 149         minfo = new MBeanInfo(className, description, attrInfos, constrInfos, operaInfos, null);
 150         test(minfo, "notifInfos");
 151 
 152         Thread.sleep(100);
 153         if (failed > 0) {
 154             throw new RuntimeException("Test failed: "+failed);
 155         } else {
 156             System.out.println("---Test: PASSED");
 157         }
 158     }
 159 
 160     private static void test(Object obj, String param) {
 161         try {
 162             obj.hashCode();
 163             System.out.println("OK: "+obj.getClass().getSimpleName()+".hashCode worked with a null "+param);
 164         } catch (NullPointerException npe) {
 165             System.out.println("--->KO!!! "+obj.getClass().getSimpleName()+".hashCode got NPE with a null "+param);
 166             failed++;
 167         }
 168 
 169         try {
 170             obj.toString();
 171             System.out.println("OK: "+obj.getClass().getSimpleName()+".toString worked with a null "+param);
 172         } catch (NullPointerException npe) {
 173             System.out.println("--->KO!!! "+obj.getClass().getSimpleName()+".toString got NPE.");
 174             failed++;
 175         }
 176     }
 177 }