1 /*
   2  * Copyright (c) 2005, 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 /*
  25  * @test
  26  * @bug 6229396
  27  * @summary Test null/empty key/values in ObjectName constructors.
  28  * @author Luis-Miguel Alventosa
  29  *
  30  * @run clean NullEmptyKeyValueTest
  31  * @run build NullEmptyKeyValueTest
  32  * @run main NullEmptyKeyValueTest
  33  */
  34 
  35 import java.util.*;
  36 import javax.management.*;
  37 
  38 public class NullEmptyKeyValueTest {
  39 
  40     private static int createObjectName(int i,
  41                                         Class c,
  42                                         String s,
  43                                         String d,
  44                                         String k,
  45                                         String v,
  46                                         Hashtable<String,String> t)
  47         throws Exception {
  48 
  49         System.out.println("----------------------------------------------");
  50         switch (i) {
  51         case 1:
  52             System.out.println("ObjectName = " + s);
  53             break;
  54         case 2:
  55             System.out.println("ObjectName.Domain = " + d);
  56             System.out.println("ObjectName.Key = " + k);
  57             System.out.println("ObjectName.Value = " + v);
  58             break;
  59         case 3:
  60             System.out.println("ObjectName.Domain = " + d);
  61             System.out.println("ObjectName.Hashtable = " + t);
  62             break;
  63         default:
  64             throw new Exception("Test incorrect: case: " + i);
  65         }
  66         int error = 0;
  67         ObjectName on = null;
  68         try {
  69             switch (i) {
  70             case 1:
  71                 on = new ObjectName(s);
  72                 break;
  73             case 2:
  74                 on = new ObjectName(d, k, v);
  75                 break;
  76             case 3:
  77                 on = new ObjectName(d, t);
  78                 break;
  79             default:
  80                 throw new Exception("Test incorrect: case: " + i);
  81             }
  82             if (c != null) {
  83                 error++;
  84                 System.out.println("Got Unexpected ObjectName = " +
  85                            (on == null ? "null" : on.getCanonicalName()));
  86             } else {
  87                 System.out.println("Got Expected ObjectName = " +
  88                            (on == null ? "null" : on.getCanonicalName()));
  89             }
  90         } catch (Exception e) {
  91             if (c == null || !c.isInstance(e)) {
  92                 error++;
  93                 System.out.println("Got Unexpected Exception = " +
  94                                    e.toString());
  95             } else {
  96                 System.out.println("Got Expected Exception = " +
  97                                    e.toString());
  98             }
  99         }
 100         System.out.println("----------------------------------------------");
 101         return error;
 102     }
 103 
 104     private static int createObjectName1(Class c,
 105                                          String s)
 106         throws Exception {
 107         return createObjectName(1, c, s, null, null, null, null);
 108     }
 109 
 110     private static int createObjectName2(Class c,
 111                                          String d,
 112                                          String k,
 113                                          String v)
 114         throws Exception {
 115         return createObjectName(2, c, null, d, k, v, null);
 116     }
 117 
 118     private static int createObjectName3(Class c,
 119                                          String d,
 120                                          Hashtable<String,String> t)
 121         throws Exception {
 122         return createObjectName(3, c, null, d, null, null, t);
 123     }
 124 
 125     public static void main(String[] args) throws Exception {
 126 
 127         final Class npec = NullPointerException.class;
 128         final Class monec = MalformedObjectNameException.class;
 129 
 130         int error = 0;
 131 
 132         error += createObjectName1(npec, null);
 133         error += createObjectName1(null, "d:k=v");
 134         error += createObjectName1(null, ":k=v");
 135         error += createObjectName1(monec, "d:=v");
 136         error += createObjectName1(null, "d:k=");
 137         error += createObjectName1(null, "d:k1=,k2=v2");
 138         error += createObjectName1(null, "d:k1=v1,k2=");
 139 
 140         error += createObjectName2(npec, null, null, null);
 141         error += createObjectName2(null, "d", "k", "v");
 142         error += createObjectName2(npec, null, "k", "v");
 143         error += createObjectName2(null, "", "k", "v");
 144         error += createObjectName2(npec, "d", null, "v");
 145         error += createObjectName2(monec, "d", "", "v");
 146         error += createObjectName2(npec, "d", "k", null);
 147         error += createObjectName2(null, "d", "k", "");
 148 
 149         Hashtable<String,String> h1 = new Hashtable<String,String>();
 150         h1.put("k", "v");
 151         Hashtable<String,String> h2 = new Hashtable<String,String>();
 152         h2.put("", "v");
 153         Hashtable<String,String> h3 = new Hashtable<String,String>();
 154         h3.put("k", "");
 155         Hashtable<String,String> h4 = new Hashtable<String,String>();
 156         h4.put("k1", "");
 157         h4.put("k2", "v2");
 158         Hashtable<String,String> h5 = new Hashtable<String,String>();
 159         h5.put("k1", "v1");
 160         h5.put("k2", "");
 161         error += createObjectName3(npec, null, null);
 162         error += createObjectName3(null, "d", h1);
 163         error += createObjectName3(npec, null, h1);
 164         error += createObjectName3(null, "", h1);
 165         error += createObjectName3(monec, "d", h2);
 166         error += createObjectName3(null, "d", h3);
 167         error += createObjectName3(null, "d", h4);
 168         error += createObjectName3(null, "d", h5);
 169 
 170         if (error > 0) {
 171             final String msg = "Test FAILED! Got " + error + " error(s)";
 172             System.out.println(msg);
 173             throw new IllegalArgumentException(msg);
 174         } else {
 175             System.out.println("Test PASSED!");
 176         }
 177     }
 178 }