KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > util > misc > ThrowableMapperTest


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 package com.sun.enterprise.management.util.misc;
24
25 import java.io.IOException JavaDoc;
26
27 import java.util.List JavaDoc;
28 import java.util.ArrayList JavaDoc;
29
30 import javax.management.MBeanException JavaDoc;
31 import javax.management.InstanceNotFoundException JavaDoc;
32 import javax.management.AttributeNotFoundException JavaDoc;
33
34 import com.sun.appserv.management.base.Util;
35
36 import com.sun.appserv.management.util.misc.ThrowableMapper;
37 import com.sun.appserv.management.util.misc.ExceptionUtil;
38
39
40 public class ThrowableMapperTest extends junit.framework.TestCase
41 {
42         public
43     ThrowableMapperTest()
44     {
45     }
46
47         private List JavaDoc<Throwable JavaDoc>
48     getStandard()
49     {
50         final List JavaDoc<Throwable JavaDoc> items = new ArrayList JavaDoc<Throwable JavaDoc>();
51         
52         items.add( new Throwable JavaDoc( "Throwable test" ) );
53         items.add( new Exception JavaDoc( "Exception test" ) );
54         items.add( new RuntimeException JavaDoc( "RuntimeException test" ) );
55         items.add( new IOException JavaDoc( "IOException test" ) );
56         items.add( new Error JavaDoc( "Error test" ) );
57         
58         items.add( new MBeanException JavaDoc(
59                 new Exception JavaDoc("within MBeanException"), "MBeanException test") );
60         items.add( new AttributeNotFoundException JavaDoc( "Test") );
61         items.add( new InstanceNotFoundException JavaDoc("InstanceNotFoundException test ") );
62         
63         items.add( new ClassNotFoundException JavaDoc( "foo.bar") );
64         
65         return items;
66     }
67     
68         private List JavaDoc<Throwable JavaDoc>
69     _testStandardWrappedWithStandard( final Throwable JavaDoc cause )
70     {
71         final List JavaDoc<Throwable JavaDoc> items = new ArrayList JavaDoc<Throwable JavaDoc>();
72         
73         final Throwable JavaDoc t = new Throwable JavaDoc( "hello", cause);
74         assert( t == ThrowableMapper.map(t) );
75         items.add( t );
76         
77         final Exception JavaDoc e = new Exception JavaDoc( "hello", cause);
78         assert( e == ThrowableMapper.map(e) );
79         items.add( e );
80         
81         final RuntimeException JavaDoc r = new RuntimeException JavaDoc( "hello", cause);
82         assert( r == ThrowableMapper.map(r) );
83         items.add( r );
84         
85         final IOException JavaDoc i = new IOException JavaDoc( "hello" );
86         i.initCause( cause );
87         assert( i == ThrowableMapper.map(i) );
88         items.add( i );
89         
90         final Error JavaDoc err = new Error JavaDoc( "hello", cause);
91         assert( err == ThrowableMapper.map(err) );
92         items.add( err );
93         
94         return items;
95     }
96     
97         public List JavaDoc<Throwable JavaDoc>
98     _testStandardThrowables( final List JavaDoc<Throwable JavaDoc> items )
99     {
100         final List JavaDoc<Throwable JavaDoc> results = new ArrayList JavaDoc<Throwable JavaDoc>();
101         
102         for( final Throwable JavaDoc item : items )
103         {
104             results.addAll( _testStandardWrappedWithStandard( item ) );
105         }
106         
107         return results;
108     }
109
110         public void
111     testStandard()
112     {
113         final List JavaDoc<Throwable JavaDoc> items = _testStandardThrowables( getStandard() );
114         
115         _testStandardThrowables( items );
116     }
117     
118     
119     
120     
121     
122     private static final class ProprietaryThrowable extends Throwable JavaDoc
123     {
124     public static final long serialVersionUID = 987324; // eliminate compile warnings
125
public ProprietaryThrowable( String JavaDoc msg ) { super(msg); }
126     public ProprietaryThrowable( String JavaDoc msg, Throwable JavaDoc cause ) { super(msg,cause); }
127     }
128     
129     private static final class ProprietaryException extends Exception JavaDoc
130     {
131     public static final long serialVersionUID = 987324; // eliminate compile warnings
132
public ProprietaryException( String JavaDoc msg ) { super(msg); }
133     public ProprietaryException( String JavaDoc msg, Throwable JavaDoc cause ) { super(msg,cause); }
134     }
135     
136     private static final class ProprietaryRuntimeException extends Exception JavaDoc
137     {
138     public static final long serialVersionUID = 987324; // eliminate compile warnings
139
public ProprietaryRuntimeException( String JavaDoc msg ) { super(msg); }
140     public ProprietaryRuntimeException( String JavaDoc msg, Throwable JavaDoc cause ) { super(msg,cause); }
141     }
142     
143     private static final class ProprietaryError extends Error JavaDoc
144     {
145     public static final long serialVersionUID = 987324; // eliminate compile warnings
146
public ProprietaryError( String JavaDoc msg ) { super(msg); }
147     public ProprietaryError( String JavaDoc msg, Throwable JavaDoc cause ) { super(msg,cause); }
148     }
149     
150         private List JavaDoc<Throwable JavaDoc>
151     getProprietary()
152     {
153         final List JavaDoc<Throwable JavaDoc> items = new ArrayList JavaDoc<Throwable JavaDoc>();
154         
155         items.add( new ProprietaryThrowable( "ProprietaryThrowable test" ) );
156         items.add( new ProprietaryException( "ProprietaryException test" ) );
157         items.add( new ProprietaryRuntimeException( "ProprietaryRuntimeException test" ) );
158         items.add( new ProprietaryError( "ProprietaryError test" ) );
159         
160         return items;
161     }
162     
163
164         private void
165     verifyMapping(
166         final Throwable JavaDoc original,
167         final Throwable JavaDoc remapped )
168     {
169         assert( remapped != original );
170         assert( remapped.getClass() != original.getClass() );
171         
172         if ( original instanceof RuntimeException JavaDoc )
173         {
174             assert( remapped instanceof RuntimeException JavaDoc );
175         }
176         if ( original instanceof Error JavaDoc )
177         {
178             assert( remapped instanceof Error JavaDoc );
179         }
180         if ( original instanceof Exception JavaDoc )
181         {
182             assert( remapped instanceof Exception JavaDoc );
183         }
184         
185         assert( original.getMessage().equals( remapped.getMessage() ) );
186         
187         assert( ExceptionUtil.getCauses(original).length ==
188                 ExceptionUtil.getCauses(remapped).length );
189                 
190         if ( original.getCause() != null )
191         {
192             verifyMapping( original.getCause(), remapped.getCause() );
193         }
194         
195         final StackTraceElement JavaDoc[] originalStackTrace = original.getStackTrace();
196         final StackTraceElement JavaDoc[] remappedStackTrace = remapped.getStackTrace();
197         assert( originalStackTrace.length == remappedStackTrace.length );
198         for( int i = 0; i < originalStackTrace.length; ++i )
199         {
200             assert( originalStackTrace[i] == remappedStackTrace[ i ] );
201         }
202         
203     }
204     
205         public void
206     xxtestProprietary()
207     {
208         final Throwable JavaDoc t = new ProprietaryThrowable( "hello" );
209         
210         final Throwable JavaDoc rm = ThrowableMapper.map( t );
211         
212         final StackTraceElement JavaDoc[] originalStackTrace = t.getStackTrace();
213         final StackTraceElement JavaDoc[] remappedStackTrace = rm.getStackTrace();
214         assert( originalStackTrace.length == remappedStackTrace.length );
215         for( int i = 0; i < originalStackTrace.length; ++i )
216         {
217             assert( originalStackTrace[i] == remappedStackTrace[ i ] );
218         }
219     }
220
221         public void
222     testProprietary()
223     {
224         final List JavaDoc<Throwable JavaDoc> items = getProprietary();
225         
226         for( final Throwable JavaDoc item : items )
227         {
228             final Throwable JavaDoc remapped = ThrowableMapper.map( item );
229             
230             verifyMapping( item, remapped );
231         }
232     }
233 }
234
235
236     
237
238
239
240
241
242
243
Popular Tags