1 /* |
|
2 * Copyright 2008 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 6456269 |
|
27 * @summary Test that an MBeanException serialized on JDK 6 deserializes |
|
28 * correctly on JDK 7. |
|
29 * @author Eamonn McManus |
|
30 */ |
|
31 |
|
32 import java.io.ByteArrayInputStream; |
|
33 import java.io.ByteArrayOutputStream; |
|
34 import java.io.ObjectInputStream; |
|
35 import java.io.ObjectOutputStream; |
|
36 import javax.management.MBeanException; |
|
37 |
|
38 // In JDK 6, the Throwable.cause field was always null for an MBeanException, |
|
39 // but it didn't matter because we overrode getCause() to return |
|
40 // MBeanException.exception instead. In JDK 7, we no longer override getCause() |
|
41 // because MBeanException doubles as the serial form of GenericMBeanException. |
|
42 // So we need some care to make sure that objects deserialized from JDK 6 |
|
43 // have the right getCause() behaviour. |
|
44 public class MBeanExceptionInteropTest { |
|
45 private static final byte[] SERIALIZED_MBEAN_EXCEPTION = { |
|
46 -84,-19,0,5,115,114,0,31,106,97,118,97,120,46,109,97, |
|
47 110,97,103,101,109,101,110,116,46,77,66,101,97,110,69,120, |
|
48 99,101,112,116,105,111,110,56,110,-116,-27,110,87,49,-50,2, |
|
49 0,1,76,0,9,101,120,99,101,112,116,105,111,110,116,0, |
|
50 21,76,106,97,118,97,47,108,97,110,103,47,69,120,99,101, |
|
51 112,116,105,111,110,59,120,114,0,28,106,97,118,97,120,46, |
|
52 109,97,110,97,103,101,109,101,110,116,46,74,77,69,120,99, |
|
53 101,112,116,105,111,110,4,-35,76,-20,-109,-99,126,113,2,0, |
|
54 0,120,114,0,19,106,97,118,97,46,108,97,110,103,46,69, |
|
55 120,99,101,112,116,105,111,110,-48,-3,31,62,26,59,28,-60, |
|
56 2,0,0,120,114,0,19,106,97,118,97,46,108,97,110,103, |
|
57 46,84,104,114,111,119,97,98,108,101,-43,-58,53,39,57,119, |
|
58 -72,-53,3,0,3,76,0,5,99,97,117,115,101,116,0,21, |
|
59 76,106,97,118,97,47,108,97,110,103,47,84,104,114,111,119, |
|
60 97,98,108,101,59,76,0,13,100,101,116,97,105,108,77,101, |
|
61 115,115,97,103,101,116,0,18,76,106,97,118,97,47,108,97, |
|
62 110,103,47,83,116,114,105,110,103,59,91,0,10,115,116,97, |
|
63 99,107,84,114,97,99,101,116,0,30,91,76,106,97,118,97, |
|
64 47,108,97,110,103,47,83,116,97,99,107,84,114,97,99,101, |
|
65 69,108,101,109,101,110,116,59,120,112,113,0,126,0,8,116, |
|
66 0,7,79,104,32,100,101,97,114,117,114,0,30,91,76,106, |
|
67 97,118,97,46,108,97,110,103,46,83,116,97,99,107,84,114, |
|
68 97,99,101,69,108,101,109,101,110,116,59,2,70,42,60,60, |
|
69 -3,34,57,2,0,0,120,112,0,0,0,2,115,114,0,27, |
|
70 106,97,118,97,46,108,97,110,103,46,83,116,97,99,107,84, |
|
71 114,97,99,101,69,108,101,109,101,110,116,97,9,-59,-102,38, |
|
72 54,-35,-123,2,0,4,73,0,10,108,105,110,101,78,117,109, |
|
73 98,101,114,76,0,14,100,101,99,108,97,114,105,110,103,67, |
|
74 108,97,115,115,113,0,126,0,6,76,0,8,102,105,108,101, |
|
75 78,97,109,101,113,0,126,0,6,76,0,10,109,101,116,104, |
|
76 111,100,78,97,109,101,113,0,126,0,6,120,112,0,0,0, |
|
77 63,116,0,25,77,66,101,97,110,69,120,99,101,112,116,105, |
|
78 111,110,73,110,116,101,114,111,112,84,101,115,116,116,0,30, |
|
79 77,66,101,97,110,69,120,99,101,112,116,105,111,110,73,110, |
|
80 116,101,114,111,112,84,101,115,116,46,106,97,118,97,116,0, |
|
81 5,119,114,105,116,101,115,113,0,126,0,12,0,0,0,46, |
|
82 113,0,126,0,14,113,0,126,0,15,116,0,4,109,97,105, |
|
83 110,120,115,114,0,34,106,97,118,97,46,108,97,110,103,46, |
|
84 73,108,108,101,103,97,108,65,114,103,117,109,101,110,116,69, |
|
85 120,99,101,112,116,105,111,110,-75,-119,115,-45,125,102,-113,-68, |
|
86 2,0,0,120,114,0,26,106,97,118,97,46,108,97,110,103, |
|
87 46,82,117,110,116,105,109,101,69,120,99,101,112,116,105,111, |
|
88 110,-98,95,6,71,10,52,-125,-27,2,0,0,120,113,0,126, |
|
89 0,3,113,0,126,0,21,116,0,3,66,97,100,117,113,0, |
|
90 126,0,10,0,0,0,2,115,113,0,126,0,12,0,0,0, |
|
91 62,113,0,126,0,14,113,0,126,0,15,113,0,126,0,16, |
|
92 115,113,0,126,0,12,0,0,0,46,113,0,126,0,14,113, |
|
93 0,126,0,15,113,0,126,0,18,120, |
|
94 }; |
|
95 |
|
96 private static volatile String failure; |
|
97 |
|
98 public static void main(String[] args) throws Exception { |
|
99 if (args.length > 0) { |
|
100 if (args[0].equals("write") && args.length == 1) { |
|
101 write(); |
|
102 return; |
|
103 } else { |
|
104 System.err.println( |
|
105 "Usage: java MBeanExceptionInteropTest"); |
|
106 System.err.println( |
|
107 "or: java MBeanExceptionInteropTest write"); |
|
108 System.exit(1); |
|
109 } |
|
110 } |
|
111 |
|
112 // Read the serialized object and check it is correct. |
|
113 ByteArrayInputStream bin = |
|
114 new ByteArrayInputStream(SERIALIZED_MBEAN_EXCEPTION); |
|
115 ObjectInputStream oin = new ObjectInputStream(bin); |
|
116 MBeanException mbeanEx = (MBeanException) oin.readObject(); |
|
117 assertEquals("MBeanException message", "Oh dear", mbeanEx.getMessage()); |
|
118 System.out.println("getCause(): " + mbeanEx.getCause() + "; " + |
|
119 "getTargetException(): " + mbeanEx.getTargetException()); |
|
120 for (Throwable t : |
|
121 new Throwable[] {mbeanEx.getCause(), mbeanEx.getTargetException()}) { |
|
122 if (!(t instanceof IllegalArgumentException)) |
|
123 fail("Nested exception not an IllegalArgumentException: " + t); |
|
124 else |
|
125 assertEquals("Nested exception message", "Bad", t.getMessage()); |
|
126 } |
|
127 |
|
128 if (failure == null) |
|
129 System.out.println("TEST PASSED"); |
|
130 else |
|
131 throw new Exception("TEST FAILED: " + failure); |
|
132 } |
|
133 |
|
134 // Write a file that can be inserted into this source file as the |
|
135 // contents of the SERIALIZED_MBEAN_EXCEPTION array. Run this program |
|
136 // on JDK 6 to generate the array, then test on JDK 7. |
|
137 private static void write() throws Exception { |
|
138 Exception wrapped = new IllegalArgumentException("Bad"); |
|
139 MBeanException mbeanEx = new MBeanException(wrapped, "Oh dear"); |
|
140 ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
|
141 ObjectOutputStream oout = new ObjectOutputStream(bout); |
|
142 oout.writeObject(mbeanEx); |
|
143 oout.close(); |
|
144 byte[] bytes = bout.toByteArray(); |
|
145 for (int i = 0; i < bytes.length; i++) { |
|
146 System.out.printf("%d,", bytes[i]); |
|
147 if (i % 16 == 15) |
|
148 System.out.println(); |
|
149 } |
|
150 if (bytes.length % 16 != 0) |
|
151 System.out.println(); |
|
152 } |
|
153 |
|
154 private static void assertEquals(String what, Object expect, Object actual) { |
|
155 boolean equal = (expect == null) ? (actual == null) : expect.equals(actual); |
|
156 if (equal) |
|
157 System.out.println("OK: " + what + ": " + expect); |
|
158 else |
|
159 fail(what + ": expected " + expect + ", got " + actual); |
|
160 } |
|
161 |
|
162 private static void fail(String why) { |
|
163 System.out.println("FAIL: " + why); |
|
164 failure = why; |
|
165 } |
|
166 } |
|