1 /* 2 * Copyright (c) 2004, 2008, 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 6211220 6616825 27 * @summary Test that jmx.serial.form=1.0 works for ObjectName 28 * @author Eamonn McManus, Daniel Fuchs 29 * @run clean SerialCompatTest 30 * @run build SerialCompatTest 31 * @run main/othervm -Djdk.jmx.mbeans.allowNonPublic=true -Djmx.serial.form=1.0 SerialCompatTest 32 */ 33 34 import java.io.*; 35 import java.util.*; 36 import javax.management.ObjectName; 37 38 public class SerialCompatTest { 39 40 public static void check6211220() throws Exception { 41 42 ObjectName on = new ObjectName("a:b=c"); 43 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 44 ObjectOutputStream oos = new ObjectOutputStream(bos); 45 oos.writeObject(on); 46 oos.close(); 47 byte[] bytes = bos.toByteArray(); 48 ByteArrayInputStream bis = new ByteArrayInputStream(bytes); 49 ObjectInputStream ois = new ObjectInputStream(bis); 50 ObjectName on1 = (ObjectName) ois.readObject(); 51 52 // if the bug is present, these will get NullPointerException 53 for (int i = 0; i <= 11; i++) { 54 String msg = "6211220 case(" + i + ")"; 55 try { 56 switch (i) { 57 case 0: 58 check(msg, on1.getDomain().equals("a")); 59 break; 60 case 1: 61 check(msg, on1.getCanonicalName().equals("a:b=c")); 62 break; 63 case 2: 64 check(msg, on1.getKeyPropertyListString() 65 .equals("b=c")); 66 break; 67 case 3: 68 check(msg, on1.getCanonicalKeyPropertyListString() 69 .equals("b=c")); 70 break; 71 case 4: 72 check(msg, on1.getKeyProperty("b").equals("c")); 73 break; 74 case 5: 75 check(msg, on1.getKeyPropertyList() 76 .equals(Collections.singletonMap("b", "c"))); 77 break; 78 case 6: 79 check(msg, !on1.isDomainPattern()); 80 break; 81 case 7: 82 check(msg, !on1.isPattern()); 83 break; 84 case 8: 85 check(msg, !on1.isPropertyPattern()); 86 break; 87 case 9: 88 check(msg, on1.equals(on)); 89 break; 90 case 10: 91 check(msg, on.equals(on1)); 92 break; 93 case 11: 94 check(msg, on1.apply(on)); 95 break; 96 default: 97 throw new Exception(msg + ": Test incorrect"); 98 } 99 } catch (Exception e) { 100 System.out.println(msg + ": Test failed with exception:"); 101 e.printStackTrace(System.out); 102 failed = true; 103 } 104 } 105 106 if (failed) { 107 throw new Exception("Some tests for 6211220 failed"); 108 } else { 109 System.out.println("All tests for 6211220 passed"); 110 } 111 } 112 113 static void checkName(String testname, ObjectName on) 114 throws Exception { 115 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 116 ObjectOutputStream oos = new ObjectOutputStream(bos); 117 oos.writeObject(on); 118 oos.close(); 119 byte[] bytes = bos.toByteArray(); 120 ByteArrayInputStream bis = new ByteArrayInputStream(bytes); 121 ObjectInputStream ois = new ObjectInputStream(bis); 122 ObjectName on1 = (ObjectName) ois.readObject(); 123 // if the bug is present, these will get NullPointerException 124 for (int i = 0; i <= 11; i++) { 125 String msg = testname + " case(" + i + ")"; 126 try { 127 switch (i) { 128 case 0: 129 check(msg, on1.getDomain().equals(on.getDomain())); 130 break; 131 case 1: 132 check(msg, on1.getCanonicalName(). 133 equals(on.getCanonicalName())); 134 break; 135 case 2: 136 check(msg, on1.getKeyPropertyListString(). 137 equals(on.getKeyPropertyListString())); 138 break; 139 case 3: 140 check(msg, on1.getCanonicalKeyPropertyListString(). 141 equals(on.getCanonicalKeyPropertyListString())); 142 break; 143 case 4: 144 for (Object ko : on1.getKeyPropertyList().keySet()) { 145 final String key = (String) ko; 146 check(msg, on1.getKeyProperty(key). 147 equals(on.getKeyProperty(key))); 148 } 149 for (Object ko : on.getKeyPropertyList().keySet()) { 150 final String key = (String) ko; 151 check(msg, on1.getKeyProperty(key). 152 equals(on.getKeyProperty(key))); 153 } 154 case 5: 155 check(msg, on1.getKeyPropertyList() 156 .equals(on.getKeyPropertyList())); 157 break; 158 case 6: 159 check(msg, on1.isDomainPattern()==on.isDomainPattern()); 160 break; 161 case 7: 162 check(msg, on1.isPattern() == on.isPattern()); 163 break; 164 case 8: 165 check(msg, 166 on1.isPropertyPattern()==on.isPropertyPattern()); 167 break; 168 case 9: 169 check(msg, on1.equals(on)); 170 break; 171 case 10: 172 check(msg, on.equals(on1)); 173 break; 174 case 11: 175 if (!on.isPattern()) { 176 check(msg, on1.apply(on)); 177 } 178 break; 179 default: 180 throw new Exception("Test incorrect: case: " + i); 181 } 182 } catch (Exception e) { 183 System.out.println("Test (" + i + ") failed with exception:"); 184 e.printStackTrace(System.out); 185 failed = true; 186 } 187 } 188 189 } 190 private static String[] names6616825 = { 191 "a:b=c", "a:b=c,*", "*:*", ":*", ":b=c", ":b=c,*", 192 "a:*,b=c", ":*", ":*,b=c", "*x?:k=\"x\\*z\"", "*x?:k=\"x\\*z\",*", 193 "*x?:*,k=\"x\\*z\"", "*x?:k=\"x\\*z\",*,b=c" 194 }; 195 196 static void check6616825() throws Exception { 197 System.out.println("Testing 616825"); 198 for (String n : names6616825) { 199 final ObjectName on; 200 try { 201 on = new ObjectName(n); 202 } catch (Exception x) { 203 failed = true; 204 System.out.println("Unexpected failure for 6616825 [" + n + 205 "]: " + x); 206 x.printStackTrace(System.out); 207 continue; 208 } 209 try { 210 checkName("616825 " + n, on); 211 } catch (Exception x) { 212 failed = true; 213 System.out.println("6616825 failed for [" + n + "]: " + x); 214 x.printStackTrace(System.out); 215 } 216 } 217 218 if (failed) { 219 throw new Exception("Some tests for 6616825 failed"); 220 } else { 221 System.out.println("All tests for 6616825 passed"); 222 } 223 } 224 225 public static void main(String[] args) throws Exception { 226 /* Check that we really are in jmx.serial.form=1.0 mode. 227 The property is frozen the first time the ObjectName class 228 is referenced so checking that it is set to the correct 229 value now is not enough. */ 230 ObjectStreamClass osc = ObjectStreamClass.lookup(ObjectName.class); 231 if (osc.getFields().length != 6) { 232 throw new Exception("Not using old serial form: fields: " + 233 Arrays.asList(osc.getFields())); 234 // new serial form has no fields, uses writeObject 235 } 236 237 try { 238 check6211220(); 239 } catch (Exception x) { 240 System.err.println(x.getMessage()); 241 } 242 try { 243 check6616825(); 244 } catch (Exception x) { 245 System.err.println(x.getMessage()); 246 } 247 248 if (failed) { 249 throw new Exception("Some tests failed"); 250 } else { 251 System.out.println("All tests passed"); 252 } 253 } 254 255 private static void check(String msg, boolean condition) { 256 if (!condition) { 257 new Throwable("Test failed " + msg).printStackTrace(System.out); 258 failed = true; 259 } 260 } 261 private static boolean failed; 262 }