J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2004-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 6211220 |
| 27 | * @summary Test that jmx.serial.form=1.0 works for ObjectName |
| 28 | * @author Eamonn McManus |
| 29 | * @run clean SerialCompatTest |
| 30 | * @run build SerialCompatTest |
| 31 | * @run main/othervm SerialCompatTest |
| 32 | */ |
| 33 | |
| 34 | import java.io.*; |
| 35 | import java.util.*; |
| 36 | import javax.management.ObjectName; |
| 37 | |
| 38 | public class SerialCompatTest { |
| 39 | public static void main(String[] args) throws Exception { |
| 40 | System.setProperty("jmx.serial.form", "1.0"); |
| 41 | |
| 42 | /* Check that we really are in jmx.serial.form=1.0 mode. |
| 43 | The property is frozen the first time the ObjectName class |
| 44 | is referenced so checking that it is set to the correct |
| 45 | value now is not enough. */ |
| 46 | ObjectStreamClass osc = ObjectStreamClass.lookup(ObjectName.class); |
| 47 | if (osc.getFields().length != 6) { |
| 48 | throw new Exception("Not using old serial form: fields: " + |
| 49 | Arrays.asList(osc.getFields())); |
| 50 | // new serial form has no fields, uses writeObject |
| 51 | } |
| 52 | |
| 53 | ObjectName on = new ObjectName("a:b=c"); |
| 54 | ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 55 | ObjectOutputStream oos = new ObjectOutputStream(bos); |
| 56 | oos.writeObject(on); |
| 57 | oos.close(); |
| 58 | byte[] bytes = bos.toByteArray(); |
| 59 | ByteArrayInputStream bis = new ByteArrayInputStream(bytes); |
| 60 | ObjectInputStream ois = new ObjectInputStream(bis); |
| 61 | ObjectName on1 = (ObjectName) ois.readObject(); |
| 62 | |
| 63 | // if the bug is present, these will get NullPointerException |
| 64 | for (int i = 0; i <= 11; i++) { |
| 65 | try { |
| 66 | switch (i) { |
| 67 | case 0: |
| 68 | check(on1.getDomain().equals("a")); break; |
| 69 | case 1: |
| 70 | check(on1.getCanonicalName().equals("a:b=c")); break; |
| 71 | case 2: |
| 72 | check(on1.getKeyPropertyListString().equals("b=c")); break; |
| 73 | case 3: |
| 74 | check(on1.getCanonicalKeyPropertyListString().equals("b=c")); |
| 75 | break; |
| 76 | case 4: |
| 77 | check(on1.getKeyProperty("b").equals("c")); break; |
| 78 | case 5: |
| 79 | check(on1.getKeyPropertyList() |
| 80 | .equals(Collections.singletonMap("b", "c"))); break; |
| 81 | case 6: |
| 82 | check(!on1.isDomainPattern()); break; |
| 83 | case 7: |
| 84 | check(!on1.isPattern()); break; |
| 85 | case 8: |
| 86 | check(!on1.isPropertyPattern()); break; |
| 87 | case 9: |
| 88 | check(on1.equals(on)); break; |
| 89 | case 10: |
| 90 | check(on.equals(on1)); break; |
| 91 | case 11: |
| 92 | check(on1.apply(on)); break; |
| 93 | default: |
| 94 | throw new Exception("Test incorrect: case: " + i); |
| 95 | } |
| 96 | } catch (Exception e) { |
| 97 | System.out.println("Test failed with exception:"); |
| 98 | e.printStackTrace(System.out); |
| 99 | failed = true; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | if (failed) |
| 104 | throw new Exception("Some tests failed"); |
| 105 | else |
| 106 | System.out.println("All tests passed"); |
| 107 | } |
| 108 | |
| 109 | private static void check(boolean condition) { |
| 110 | if (!condition) { |
| 111 | new Throwable("Test failed").printStackTrace(System.out); |
| 112 | failed = true; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | private static boolean failed; |
| 117 | } |