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.MBeanFeatureInfo;
  27 import javax.management.MBeanInfo;
  28 import javax.management.MBeanNotificationInfo;
  29 import javax.management.MBeanOperationInfo;
  30 import javax.management.MBeanParameterInfo;
  31 import javax.management.modelmbean.DescriptorSupport;
  32 import javax.management.openmbean.SimpleType;
  33 
  34 /*
  35  * @test
  36  * @bug 8023954
  37  * @summary Test that MBean*Info.equals do not throw NPE
  38  * @author Shanliang JIANG
  39  * @modules java.management
  40  * @run clean MBeanInfoEqualsNPETest
  41  * @run build MBeanInfoEqualsNPETest
  42  * @run main MBeanInfoEqualsNPETest
  43  */
  44 public class MBeanInfoEqualsNPETest {
  45     private static int failed = 0;
  46 
  47     public static void main(String[] args) throws Exception {
  48         System.out.println("---MBeanInfoEqualsNPETest-main ...");
  49 
  50         // ----
  51         System.out.println("\n---Testing on MBeanAttributeInfo...");
  52         MBeanAttributeInfo mbeanAttributeInfo0 = new MBeanAttributeInfo(
  53                 "name", SimpleType.INTEGER.getClassName(), "description", true, true, false);
  54         MBeanAttributeInfo mbeanAttributeInfo = new MBeanAttributeInfo(
  55                 null, SimpleType.INTEGER.getClassName(), "description", true, true, false);
  56         test(mbeanAttributeInfo0, mbeanAttributeInfo, "class name");
  57 
  58         mbeanAttributeInfo = new MBeanAttributeInfo(
  59                 "name", null, "description", true, true, false);
  60         test(mbeanAttributeInfo0, mbeanAttributeInfo, "type");
  61 
  62         mbeanAttributeInfo = new MBeanAttributeInfo(
  63                 "name", SimpleType.INTEGER.getClassName(), null, true, true, false);
  64         test(mbeanAttributeInfo0, mbeanAttributeInfo, "description");
  65 
  66         // ----
  67         System.out.println("\n---Testing on MBeanConstructorInfo...");
  68         MBeanConstructorInfo mbeanConstructorInfo0 = new MBeanConstructorInfo(
  69                 "", "", new MBeanParameterInfo[]{}, new DescriptorSupport());
  70         MBeanConstructorInfo mbeanConstructorInfo = new MBeanConstructorInfo(
  71                 null, "", new MBeanParameterInfo[]{}, new DescriptorSupport());
  72         test(mbeanConstructorInfo0, mbeanConstructorInfo, "name");
  73 
  74         mbeanConstructorInfo = new MBeanConstructorInfo(
  75                 "", null, new MBeanParameterInfo[]{}, new DescriptorSupport());
  76         test(mbeanConstructorInfo0, mbeanConstructorInfo, "description");
  77 
  78         mbeanConstructorInfo = new MBeanConstructorInfo(
  79                 "", "", null, new DescriptorSupport());
  80         test(mbeanConstructorInfo0, mbeanConstructorInfo, "MBeanParameterInfo");
  81 
  82         mbeanConstructorInfo = new MBeanConstructorInfo(
  83                 "", "", new MBeanParameterInfo[]{}, null);
  84         test(mbeanConstructorInfo0, mbeanConstructorInfo, "descriptor");
  85 
  86         // ----
  87         System.out.println("\n---Testing on MBeanOperationInfo...");
  88         MBeanOperationInfo mbeanOperationInfo0 = new MBeanOperationInfo(
  89                 "name", "description", new MBeanParameterInfo[]{}, "type",
  90                 MBeanOperationInfo.UNKNOWN, new DescriptorSupport());
  91 
  92         MBeanOperationInfo mbeanOperationInfo = new MBeanOperationInfo(
  93                 null, "description", new MBeanParameterInfo[]{}, "type",
  94                 MBeanOperationInfo.UNKNOWN, new DescriptorSupport());
  95         test(mbeanOperationInfo0, mbeanOperationInfo, "name");
  96 
  97         mbeanOperationInfo = new MBeanOperationInfo(
  98                 "name", null, new MBeanParameterInfo[]{}, "type",
  99                 MBeanOperationInfo.UNKNOWN, new DescriptorSupport());
 100         test(mbeanOperationInfo0, mbeanOperationInfo, "description");
 101 
 102         mbeanOperationInfo = new MBeanOperationInfo(
 103                 "name", "description", null, "type", 1, new DescriptorSupport());
 104         test(mbeanOperationInfo0, mbeanOperationInfo, "MBeanParameterInfo");
 105 
 106         mbeanOperationInfo = new MBeanOperationInfo(
 107                 "name", "description", new MBeanParameterInfo[]{}, null,
 108                 MBeanOperationInfo.UNKNOWN, new DescriptorSupport());
 109         test(mbeanOperationInfo0, mbeanOperationInfo, "type");
 110 
 111         mbeanOperationInfo = new MBeanOperationInfo(
 112                 "name", "description", new MBeanParameterInfo[]{}, null,
 113                 MBeanOperationInfo.UNKNOWN, null);
 114         test(mbeanOperationInfo0, mbeanOperationInfo, "Descriptor");
 115 
 116         // ----
 117         System.out.println("\n---Testing on MBeanParameterInfo...");
 118         MBeanParameterInfo mbeanParameterInfo0 = new MBeanParameterInfo(
 119                 "name", "type", "description", new DescriptorSupport());
 120         MBeanParameterInfo mbeanParameterInfo = new MBeanParameterInfo(
 121                 null, "type", "description", new DescriptorSupport());
 122         test(mbeanParameterInfo0, mbeanParameterInfo, "name");
 123 
 124         mbeanParameterInfo = new MBeanParameterInfo(
 125                 "name", null, "description", new DescriptorSupport());
 126         test(mbeanParameterInfo0, mbeanParameterInfo, "type");
 127 
 128         mbeanParameterInfo = new MBeanParameterInfo(
 129                 "name", "type", null, new DescriptorSupport());
 130         test(mbeanParameterInfo0, mbeanParameterInfo, "description");
 131 
 132         mbeanParameterInfo = new MBeanParameterInfo(
 133                 "name", "type", "description", null);
 134         test(mbeanParameterInfo0, mbeanParameterInfo, "Descriptor");
 135 
 136         // ----
 137         System.out.println("\n---Testing on MBeanFeatureInfo ...");
 138         MBeanFeatureInfo mbeanFeatureInfo0 = new MBeanFeatureInfo(
 139                 "name", "description", new DescriptorSupport());
 140         MBeanFeatureInfo mbeanFeatureInfo = new MBeanFeatureInfo(
 141                 null, "description", new DescriptorSupport());
 142         test(mbeanFeatureInfo0, mbeanFeatureInfo, "name");
 143 
 144         mbeanFeatureInfo = new MBeanFeatureInfo(
 145                 "name", null, new DescriptorSupport());
 146         test(mbeanParameterInfo0, mbeanParameterInfo, "description");
 147 
 148         mbeanFeatureInfo = new MBeanFeatureInfo(
 149                 "name", "description", null);
 150         test(mbeanParameterInfo0, mbeanParameterInfo, "Descriptor");
 151 
 152         // ----
 153         System.out.println("\n---Testing on MBeanInfo...");
 154         String className = "toto";
 155         String description = "titi";
 156         MBeanAttributeInfo[] attrInfos = new MBeanAttributeInfo[]{};
 157         MBeanConstructorInfo[] constrInfos = new MBeanConstructorInfo[]{};
 158         MBeanOperationInfo[] operaInfos = new MBeanOperationInfo[]{};
 159         MBeanNotificationInfo[] notifInfos = new MBeanNotificationInfo[]{};
 160 
 161         MBeanInfo minfo0 = new MBeanInfo("toto", description, attrInfos, constrInfos, operaInfos, notifInfos);
 162         MBeanInfo minfo = new MBeanInfo(null, description, attrInfos, constrInfos, operaInfos, notifInfos);
 163         test(minfo0, minfo, "class name");
 164 
 165         minfo = new MBeanInfo(className, null, attrInfos, constrInfos, operaInfos, notifInfos);
 166         test(minfo0, minfo, "description");
 167 
 168         minfo = new MBeanInfo(className, description, null, constrInfos, operaInfos, notifInfos);
 169         test(minfo0, minfo, "attrInfos");
 170 
 171         minfo = new MBeanInfo(className, description, attrInfos, null, operaInfos, notifInfos);
 172         test(minfo0, minfo, "constrInfos");
 173 
 174         minfo = new MBeanInfo(className, description, attrInfos, constrInfos, null, notifInfos);
 175         test(minfo0, minfo, "operaInfos");
 176 
 177         minfo = new MBeanInfo(className, description, attrInfos, constrInfos, operaInfos, null);
 178         test(minfo0, minfo, "notifInfos");
 179 
 180         if (failed > 0) {
 181             throw new RuntimeException("Test failed: "+failed);
 182         } else {
 183             System.out.println("---Test: PASSED");
 184         }
 185     }
 186 
 187     private static void test(Object obj1, Object obj2, String param) {
 188         try {
 189             obj1.equals(obj2);
 190             System.out.println("OK-1: "+obj1.getClass().getSimpleName()+".equals worked with a null paramer: "+param);
 191         } catch (NullPointerException npe) {
 192             System.out.println("--->KO-1!!! "+obj1.getClass().getSimpleName()+".equals got NPE with a null paramer: "+param);
 193             npe.printStackTrace();
 194             failed++;
 195         }
 196 
 197         try {
 198             obj2.equals(obj1);
 199             System.out.println("OK-2: "+obj2.getClass().getSimpleName()+".equals worked with a null paramer: "+param);
 200         } catch (NullPointerException npe) {
 201             System.out.println("--->KO-2!!! "+obj2.getClass().getSimpleName()+".equals got NPE with a null paramer: "+param);
 202             npe.printStackTrace();
 203             failed++;
 204         }
 205 
 206         try {
 207             obj1.equals(null);
 208             obj2.equals(null);
 209 
 210             System.out.println("OK-3: "+obj1.getClass().getSimpleName()+".equals worked with a null field.");
 211         } catch (NullPointerException npe) {
 212             System.out.println("--->KO-3!!! "+obj1.getClass().getSimpleName()+".equals got NPE with a null field.");
 213             npe.printStackTrace();
 214             failed++;
 215         }
 216     }
 217 }