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