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