blob: 7021db15aaf9649ff55a040cb03f89add7d171b5 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2001 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/* @test
25 * @bug 4416970
26 * @summary test chained exception support
27 * @run main/othervm Chaining
28 */
29import java.rmi.MarshalledObject;
30import java.rmi.RemoteException;
31import java.rmi.activation.ActivationException;
32import java.rmi.server.ServerCloneException;
33
34public class Chaining {
35 static void check(Throwable t, String msg, Throwable cause)
36 throws Exception
37 {
38 String tmsg = t.getMessage();
39 if (msg == null ? tmsg != null : !msg.equals(tmsg)) {
40 throw new RuntimeException("message does not match");
41 }
42 if (t.getClass().getField("detail").get(t) != cause) {
43 throw new RuntimeException("detail field does not match");
44 }
45 if (t.getCause() != cause) {
46 throw new RuntimeException("getCause value does not match");
47 }
48 try {
49 t.initCause(cause);
50 throw new RuntimeException("initCause succeeded");
51 } catch (IllegalStateException e) {
52 }
53 }
54
55 static void test(Throwable t, String msg, Throwable cause)
56 throws Exception
57 {
58 check(t, msg, cause);
59 Throwable[] pair = new Throwable[]{t, cause};
60 pair = (Throwable[]) new MarshalledObject(pair).get();
61 check(pair[0], msg, pair[1]);
62 }
63
64 public static void main(String[] args) throws Exception {
65 Exception t = new RuntimeException();
66 String foo = "foo";
67 String fooMsg = "foo; nested exception is: \n\t" + t;
68 test(new RemoteException(), null, null);
69 test(new RemoteException(foo), foo, null);
70 test(new RemoteException(foo, t), fooMsg, t);
71 test(new ActivationException(), null, null);
72 test(new ActivationException(foo), foo, null);
73 test(new ActivationException(foo, t), fooMsg, t);
74 test(new ServerCloneException(foo), foo, null);
75 test(new ServerCloneException(foo, t), fooMsg, t);
76 }
77}