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