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