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