KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > mbeans > MBeanExceptionFormatterTest


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * $Id: MBeanExceptionFormatterTest.java,v 1.3 2005/12/25 03:43:13 tcfujii Exp $
26  */

27
28 package com.sun.enterprise.admin.mbeans;
29
30 //junit imports
31
import junit.framework.*;
32 import junit.textui.TestRunner;
33
34 //JMX imports
35
import javax.management.MBeanException JavaDoc;
36
37 public class MBeanExceptionFormatterTest extends TestCase
38 {
39     public void testMBeanException()
40     {
41         Exception JavaDoc targetEx = new Exception JavaDoc();
42         MBeanException JavaDoc mbe = new MBeanException JavaDoc(targetEx);
43         Assert.assertEquals(targetEx, mbe.getTargetException());
44         Assert.assertEquals(null, mbe.getCause());
45         Assert.assertEquals(null, mbe.getMessage());
46         Assert.assertEquals(null, mbe.getTargetException().getMessage());
47
48         targetEx = new Exception JavaDoc("actual message");
49         mbe = new MBeanException JavaDoc(targetEx);
50         mbe.initCause(targetEx);
51         Assert.assertEquals(targetEx, mbe.getCause());
52         Assert.assertEquals(null, mbe.getMessage());
53
54         mbe = toMBeanException(null, null);
55         Assert.assertEquals(null, mbe.getMessage());
56         Assert.assertTrue(null != mbe.getCause());
57         Assert.assertTrue(null != mbe.getTargetException());
58
59         mbe = toMBeanException(null, "a");
60         Assert.assertEquals("a", mbe.getMessage());
61
62         Exception JavaDoc e = new Exception JavaDoc("b", new Exception JavaDoc("c", new Exception JavaDoc("d")));
63         mbe = toMBeanException(e, "a");
64         Assert.assertEquals("a(b(c(d)))", mbe.getMessage());
65         Assert.assertEquals(e, mbe.getCause());
66         Assert.assertEquals(e, mbe.getTargetException());
67
68         mbe = toMBeanException(e, null);
69         Assert.assertEquals("b(c(d))", mbe.getMessage());
70
71         e = new Exception JavaDoc();
72         mbe = toMBeanException(e, "a");
73         Assert.assertEquals("a", mbe.getMessage());
74
75         e = new Exception JavaDoc("b", null);
76         mbe = toMBeanException(e, "a");
77         Assert.assertEquals("a(b)", mbe.getMessage());
78
79         e = new Exception JavaDoc("b", new Exception JavaDoc(null, new Exception JavaDoc("c")));
80         mbe = toMBeanException(e, "a");
81         Assert.assertEquals("a(b(c))", mbe.getMessage());
82     }
83
84     MBeanException JavaDoc toMBeanException(final Exception JavaDoc e, final String JavaDoc msg)
85     {
86         return MBeanExceptionFormatter.toMBeanException(e, msg);
87     }
88
89     public MBeanExceptionFormatterTest(String JavaDoc name) throws Exception JavaDoc
90     {
91         super(name);
92     }
93
94     protected void setUp()
95     {
96     }
97
98     protected void tearDown()
99     {
100     }
101
102     public static junit.framework.Test suite()
103     {
104         TestSuite suite = new TestSuite(MBeanExceptionFormatterTest.class);
105         return suite;
106     }
107
108     public static void main(String JavaDoc args[]) throws Exception JavaDoc
109     {
110         final TestRunner runner= new TestRunner();
111         final TestResult result = runner.doRun(
112                 MBeanExceptionFormatterTest.suite(), false);
113         System.exit(result.errorCount() + result.failureCount());
114     }
115 }
Popular Tags