Path: blob/master/test/jdk/javax/management/ObjectName/NullEmptyKeyValueTest.java
41149 views
/*1* Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 622939626* @summary Test null/empty key/values in ObjectName constructors.27* @author Luis-Miguel Alventosa28*29* @run clean NullEmptyKeyValueTest30* @run build NullEmptyKeyValueTest31* @run main NullEmptyKeyValueTest32*/3334import java.util.*;35import javax.management.*;3637public class NullEmptyKeyValueTest {3839private static int createObjectName(int i,40Class c,41String s,42String d,43String k,44String v,45Hashtable<String,String> t)46throws Exception {4748System.out.println("----------------------------------------------");49switch (i) {50case 1:51System.out.println("ObjectName = " + s);52break;53case 2:54System.out.println("ObjectName.Domain = " + d);55System.out.println("ObjectName.Key = " + k);56System.out.println("ObjectName.Value = " + v);57break;58case 3:59System.out.println("ObjectName.Domain = " + d);60System.out.println("ObjectName.Hashtable = " + t);61break;62default:63throw new Exception("Test incorrect: case: " + i);64}65int error = 0;66ObjectName on = null;67try {68switch (i) {69case 1:70on = new ObjectName(s);71break;72case 2:73on = new ObjectName(d, k, v);74break;75case 3:76on = new ObjectName(d, t);77break;78default:79throw new Exception("Test incorrect: case: " + i);80}81if (c != null) {82error++;83System.out.println("Got Unexpected ObjectName = " +84(on == null ? "null" : on.getCanonicalName()));85} else {86System.out.println("Got Expected ObjectName = " +87(on == null ? "null" : on.getCanonicalName()));88}89} catch (Exception e) {90if (c == null || !c.isInstance(e)) {91error++;92System.out.println("Got Unexpected Exception = " +93e.toString());94} else {95System.out.println("Got Expected Exception = " +96e.toString());97}98}99System.out.println("----------------------------------------------");100return error;101}102103private static int createObjectName1(Class c,104String s)105throws Exception {106return createObjectName(1, c, s, null, null, null, null);107}108109private static int createObjectName2(Class c,110String d,111String k,112String v)113throws Exception {114return createObjectName(2, c, null, d, k, v, null);115}116117private static int createObjectName3(Class c,118String d,119Hashtable<String,String> t)120throws Exception {121return createObjectName(3, c, null, d, null, null, t);122}123124public static void main(String[] args) throws Exception {125126final Class npec = NullPointerException.class;127final Class monec = MalformedObjectNameException.class;128129int error = 0;130131error += createObjectName1(npec, null);132error += createObjectName1(null, "d:k=v");133error += createObjectName1(null, ":k=v");134error += createObjectName1(monec, "d:=v");135error += createObjectName1(null, "d:k=");136error += createObjectName1(null, "d:k1=,k2=v2");137error += createObjectName1(null, "d:k1=v1,k2=");138139error += createObjectName2(npec, null, null, null);140error += createObjectName2(null, "d", "k", "v");141error += createObjectName2(npec, null, "k", "v");142error += createObjectName2(null, "", "k", "v");143error += createObjectName2(npec, "d", null, "v");144error += createObjectName2(monec, "d", "", "v");145error += createObjectName2(npec, "d", "k", null);146error += createObjectName2(null, "d", "k", "");147148Hashtable<String,String> h1 = new Hashtable<String,String>();149h1.put("k", "v");150Hashtable<String,String> h2 = new Hashtable<String,String>();151h2.put("", "v");152Hashtable<String,String> h3 = new Hashtable<String,String>();153h3.put("k", "");154Hashtable<String,String> h4 = new Hashtable<String,String>();155h4.put("k1", "");156h4.put("k2", "v2");157Hashtable<String,String> h5 = new Hashtable<String,String>();158h5.put("k1", "v1");159h5.put("k2", "");160error += createObjectName3(npec, null, null);161error += createObjectName3(null, "d", h1);162error += createObjectName3(npec, null, h1);163error += createObjectName3(null, "", h1);164error += createObjectName3(monec, "d", h2);165error += createObjectName3(null, "d", h3);166error += createObjectName3(null, "d", h4);167error += createObjectName3(null, "d", h5);168169if (error > 0) {170final String msg = "Test FAILED! Got " + error + " error(s)";171System.out.println(msg);172throw new IllegalArgumentException(msg);173} else {174System.out.println("Test PASSED!");175}176}177}178179180