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.MBeanNotificationInfo;
  25 import javax.management.modelmbean.DescriptorSupport;
  26 import javax.management.openmbean.OpenMBeanAttributeInfo;
  27 import javax.management.openmbean.OpenMBeanAttributeInfoSupport;
  28 import javax.management.openmbean.OpenMBeanConstructorInfo;
  29 import javax.management.openmbean.OpenMBeanConstructorInfoSupport;
  30 import javax.management.openmbean.OpenMBeanInfo;
  31 import javax.management.openmbean.OpenMBeanInfoSupport;
  32 import javax.management.openmbean.OpenMBeanOperationInfo;
  33 import javax.management.openmbean.OpenMBeanOperationInfoSupport;
  34 import javax.management.openmbean.OpenMBeanParameterInfo;
  35 import javax.management.openmbean.OpenMBeanParameterInfoSupport;
  36 import javax.management.openmbean.SimpleType;
  37 
  38 /*
  39  * @test
  40  * @bug 8023529
  41  * @summary Test that OpenMBean*Info.hashCode do not throw NPE
  42  * @author Shanliang JIANG
  43  * @run clean OpenMBeanInfoHashCodeNPETest
  44  * @run build OpenMBeanInfoHashCodeNPETest
  45  * @run main OpenMBeanInfoHashCodeNPETest
  46  */
  47 public class OpenMBeanInfoHashCodeNPETest {
  48     private static int failed = 0;
  49 
  50     public static void main(String[] args) throws Exception {
  51         System.out.println("---OpenMBeanInfoHashCodeNPETest-main ...");
  52 
  53         // ----
  54         System.out.println("\n---Testing on OpenMBeanInfohashCodeTest...");
  55         OpenMBeanAttributeInfo openMBeanAttributeInfo = new OpenMBeanAttributeInfoSupport(
  56                 "name", "description", SimpleType.INTEGER, true, true, false, null, new Integer[]{1, 2, 3});
  57         test(openMBeanAttributeInfo, "defaultValue");
  58 
  59         openMBeanAttributeInfo = new OpenMBeanAttributeInfoSupport(
  60                 "name", "description", SimpleType.INTEGER, true, true, false, 1, null);
  61         test(openMBeanAttributeInfo, "legalValues");
  62 
  63         // ----
  64         System.out.println("\n---Testing on OpenMBeanConstructorInfoSupport...");
  65         OpenMBeanConstructorInfo openMBeanConstructorInfo;
  66 
  67         openMBeanConstructorInfo = new OpenMBeanConstructorInfoSupport(
  68                 "name", "description", null, new DescriptorSupport());
  69         test(openMBeanConstructorInfo, "sigs");
  70 
  71         openMBeanConstructorInfo = new OpenMBeanConstructorInfoSupport(
  72                 "name", "description", new OpenMBeanParameterInfo[]{}, null);
  73         test(openMBeanConstructorInfo, "Descriptor");
  74 
  75         // ----
  76         System.out.println("\n---Testing on OpenMBeanOperationInfoSupport...");
  77         OpenMBeanOperationInfo openMBeanOperationInfo;
  78 
  79         openMBeanOperationInfo = new OpenMBeanOperationInfoSupport(
  80                 "name", "description", null,  SimpleType.INTEGER, 1, new DescriptorSupport());
  81         test(openMBeanOperationInfo, "sigs");
  82 
  83         openMBeanOperationInfo = new OpenMBeanOperationInfoSupport(
  84                 "name", "description", new OpenMBeanParameterInfo[]{},  SimpleType.INTEGER, 1, null);
  85         test(openMBeanOperationInfo, "Descriptor");
  86 
  87         // ----
  88         System.out.println("\n---Testing on OpenMBeanParameterInfoSupport 1...");
  89         OpenMBeanParameterInfo openMBeanParameterInfo;
  90 
  91         openMBeanParameterInfo = new OpenMBeanParameterInfoSupport(
  92                 "name", "description", SimpleType.INTEGER, null, -1, 1);
  93         test(openMBeanParameterInfo, "default value");
  94 
  95         openMBeanParameterInfo = new OpenMBeanParameterInfoSupport(
  96                 "name", "description", SimpleType.INTEGER, 0, null, 1);
  97         test(openMBeanParameterInfo, "min value");
  98 
  99         openMBeanParameterInfo = new OpenMBeanParameterInfoSupport(
 100                 "name", "description", SimpleType.INTEGER, 0, -1, null);
 101         test(openMBeanParameterInfo, "max value");
 102 
 103         // ----
 104         System.out.println("\n---Testing on OpenMBeanParameterInfoSupport 2...");
 105         openMBeanParameterInfo = new OpenMBeanParameterInfoSupport(
 106                 "name", "description", SimpleType.INTEGER, 1, new Integer[]{-1, 1, 2});
 107 
 108         openMBeanParameterInfo = new OpenMBeanParameterInfoSupport(
 109                 "name", "description", SimpleType.INTEGER, null, new Integer[]{-1, 1, 2});
 110         test(openMBeanParameterInfo, "default value");
 111 
 112         openMBeanParameterInfo = new OpenMBeanParameterInfoSupport(
 113                 "name", "description", SimpleType.INTEGER, 1, null);
 114         test(openMBeanParameterInfo, "legal values");
 115 
 116         // ----
 117         System.out.println("\n---Testing on OpenMBeanInfoSupport...");
 118         String className = "toto";
 119         String description = "titi";
 120         OpenMBeanAttributeInfo[] attrInfos = new OpenMBeanAttributeInfo[]{};
 121         OpenMBeanConstructorInfo[] constrInfos = new OpenMBeanConstructorInfo[]{};
 122         OpenMBeanOperationInfo[] operaInfos = new OpenMBeanOperationInfo[]{};
 123         MBeanNotificationInfo[] notifInfos = new MBeanNotificationInfo[]{};
 124 
 125         OpenMBeanInfo ominfo = new OpenMBeanInfoSupport(null, description, attrInfos, constrInfos, operaInfos, notifInfos);
 126         test(ominfo, "class name");
 127 
 128         ominfo = new OpenMBeanInfoSupport(className, null, attrInfos, constrInfos, operaInfos, notifInfos);
 129         test(ominfo, "description");
 130 
 131         ominfo = new OpenMBeanInfoSupport(className, description, null, constrInfos, operaInfos, notifInfos);
 132         test(ominfo, "attrInfos");
 133 
 134         ominfo = new OpenMBeanInfoSupport(className, description, attrInfos, null, operaInfos, notifInfos);
 135         test(ominfo, "constructor infos");
 136 
 137         ominfo = new OpenMBeanInfoSupport(className, description, attrInfos, constrInfos, null, notifInfos);
 138         test(ominfo, "operation infos");
 139 
 140         ominfo = new OpenMBeanInfoSupport(className, description, attrInfos, constrInfos, operaInfos, null);
 141         test(ominfo, "notif infos");
 142 
 143         if (failed > 0) {
 144             throw new RuntimeException("Test failed: "+failed);
 145         } else {
 146             System.out.println("---Test: PASSED");
 147         }
 148     }
 149 
 150     private static void test(Object obj, String param) {
 151         try {
 152             obj.hashCode();
 153             System.out.println("OK-1: "+obj.getClass().getSimpleName()+
 154                     ".hashCode worked with a null paramer: "+param);
 155         } catch (NullPointerException npe) {
 156             System.out.println("--->KO-1!!! "+obj.getClass().getSimpleName()+
 157                     ".hashCode got NPE with null paramer: "+param);
 158             npe.printStackTrace();
 159             failed++;
 160         }
 161 
 162         try {
 163             obj.toString();
 164             System.out.println("OK-1: "+obj.getClass().getSimpleName()+
 165                     ".toString worked with a null paramer: "+param);
 166         } catch (NullPointerException npe) {
 167             System.out.println("--->KO-1!!! "+obj.getClass().getSimpleName()+
 168                     ".toString got NPE with null paramer: "+param);
 169             npe.printStackTrace();
 170             failed++;
 171         }
 172     }
 173 }