Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/ObjectName/NullEmptyKeyValueTest.java
38838 views
/*1* Copyright (c) 2005, 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* @run clean NullEmptyKeyValueTest29* @run build NullEmptyKeyValueTest30* @run main NullEmptyKeyValueTest31*/3233import java.util.*;34import javax.management.*;3536public class NullEmptyKeyValueTest {3738private static int createObjectName(int i,39Class c,40String s,41String d,42String k,43String v,44Hashtable<String,String> t)45throws Exception {4647System.out.println("----------------------------------------------");48switch (i) {49case 1:50System.out.println("ObjectName = " + s);51break;52case 2:53System.out.println("ObjectName.Domain = " + d);54System.out.println("ObjectName.Key = " + k);55System.out.println("ObjectName.Value = " + v);56break;57case 3:58System.out.println("ObjectName.Domain = " + d);59System.out.println("ObjectName.Hashtable = " + t);60break;61default:62throw new Exception("Test incorrect: case: " + i);63}64int error = 0;65ObjectName on = null;66try {67switch (i) {68case 1:69on = new ObjectName(s);70break;71case 2:72on = new ObjectName(d, k, v);73break;74case 3:75on = new ObjectName(d, t);76break;77default:78throw new Exception("Test incorrect: case: " + i);79}80if (c != null) {81error++;82System.out.println("Got Unexpected ObjectName = " +83(on == null ? "null" : on.getCanonicalName()));84} else {85System.out.println("Got Expected ObjectName = " +86(on == null ? "null" : on.getCanonicalName()));87}88} catch (Exception e) {89if (c == null || !c.isInstance(e)) {90error++;91System.out.println("Got Unexpected Exception = " +92e.toString());93} else {94System.out.println("Got Expected Exception = " +95e.toString());96}97}98System.out.println("----------------------------------------------");99return error;100}101102private static int createObjectName1(Class c,103String s)104throws Exception {105return createObjectName(1, c, s, null, null, null, null);106}107108private static int createObjectName2(Class c,109String d,110String k,111String v)112throws Exception {113return createObjectName(2, c, null, d, k, v, null);114}115116private static int createObjectName3(Class c,117String d,118Hashtable<String,String> t)119throws Exception {120return createObjectName(3, c, null, d, null, null, t);121}122123public static void main(String[] args) throws Exception {124125final Class npec = NullPointerException.class;126final Class monec = MalformedObjectNameException.class;127128int error = 0;129130error += createObjectName1(npec, null);131error += createObjectName1(null, "d:k=v");132error += createObjectName1(null, ":k=v");133error += createObjectName1(monec, "d:=v");134error += createObjectName1(null, "d:k=");135error += createObjectName1(null, "d:k1=,k2=v2");136error += createObjectName1(null, "d:k1=v1,k2=");137138error += createObjectName2(npec, null, null, null);139error += createObjectName2(null, "d", "k", "v");140error += createObjectName2(npec, null, "k", "v");141error += createObjectName2(null, "", "k", "v");142error += createObjectName2(npec, "d", null, "v");143error += createObjectName2(monec, "d", "", "v");144error += createObjectName2(npec, "d", "k", null);145error += createObjectName2(null, "d", "k", "");146147Hashtable<String,String> h1 = new Hashtable<String,String>();148h1.put("k", "v");149Hashtable<String,String> h2 = new Hashtable<String,String>();150h2.put("", "v");151Hashtable<String,String> h3 = new Hashtable<String,String>();152h3.put("k", "");153Hashtable<String,String> h4 = new Hashtable<String,String>();154h4.put("k1", "");155h4.put("k2", "v2");156Hashtable<String,String> h5 = new Hashtable<String,String>();157h5.put("k1", "v1");158h5.put("k2", "");159error += createObjectName3(npec, null, null);160error += createObjectName3(null, "d", h1);161error += createObjectName3(npec, null, h1);162error += createObjectName3(null, "", h1);163error += createObjectName3(monec, "d", h2);164error += createObjectName3(null, "d", h3);165error += createObjectName3(null, "d", h4);166error += createObjectName3(null, "d", h5);167168if (error > 0) {169final String msg = "Test FAILED! Got " + error + " error(s)";170System.out.println(msg);171throw new IllegalArgumentException(msg);172} else {173System.out.println("Test PASSED!");174}175}176}177178179