1 23 package com.sun.enterprise.management.util.misc; 24 25 import java.io.IOException ; 26 27 import java.util.List ; 28 import java.util.ArrayList ; 29 30 import javax.management.MBeanException ; 31 import javax.management.InstanceNotFoundException ; 32 import javax.management.AttributeNotFoundException ; 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 <Throwable > 48 getStandard() 49 { 50 final List <Throwable > items = new ArrayList <Throwable >(); 51 52 items.add( new Throwable ( "Throwable test" ) ); 53 items.add( new Exception ( "Exception test" ) ); 54 items.add( new RuntimeException ( "RuntimeException test" ) ); 55 items.add( new IOException ( "IOException test" ) ); 56 items.add( new Error ( "Error test" ) ); 57 58 items.add( new MBeanException ( 59 new Exception ("within MBeanException"), "MBeanException test") ); 60 items.add( new AttributeNotFoundException ( "Test") ); 61 items.add( new InstanceNotFoundException ("InstanceNotFoundException test ") ); 62 63 items.add( new ClassNotFoundException ( "foo.bar") ); 64 65 return items; 66 } 67 68 private List <Throwable > 69 _testStandardWrappedWithStandard( final Throwable cause ) 70 { 71 final List <Throwable > items = new ArrayList <Throwable >(); 72 73 final Throwable t = new Throwable ( "hello", cause); 74 assert( t == ThrowableMapper.map(t) ); 75 items.add( t ); 76 77 final Exception e = new Exception ( "hello", cause); 78 assert( e == ThrowableMapper.map(e) ); 79 items.add( e ); 80 81 final RuntimeException r = new RuntimeException ( "hello", cause); 82 assert( r == ThrowableMapper.map(r) ); 83 items.add( r ); 84 85 final IOException i = new IOException ( "hello" ); 86 i.initCause( cause ); 87 assert( i == ThrowableMapper.map(i) ); 88 items.add( i ); 89 90 final Error err = new Error ( "hello", cause); 91 assert( err == ThrowableMapper.map(err) ); 92 items.add( err ); 93 94 return items; 95 } 96 97 public List <Throwable > 98 _testStandardThrowables( final List <Throwable > items ) 99 { 100 final List <Throwable > results = new ArrayList <Throwable >(); 101 102 for( final Throwable item : items ) 103 { 104 results.addAll( _testStandardWrappedWithStandard( item ) ); 105 } 106 107 return results; 108 } 109 110 public void 111 testStandard() 112 { 113 final List <Throwable > items = _testStandardThrowables( getStandard() ); 114 115 _testStandardThrowables( items ); 116 } 117 118 119 120 121 122 private static final class ProprietaryThrowable extends Throwable 123 { 124 public static final long serialVersionUID = 987324; public ProprietaryThrowable( String msg ) { super(msg); } 126 public ProprietaryThrowable( String msg, Throwable cause ) { super(msg,cause); } 127 } 128 129 private static final class ProprietaryException extends Exception 130 { 131 public static final long serialVersionUID = 987324; public ProprietaryException( String msg ) { super(msg); } 133 public ProprietaryException( String msg, Throwable cause ) { super(msg,cause); } 134 } 135 136 private static final class ProprietaryRuntimeException extends Exception 137 { 138 public static final long serialVersionUID = 987324; public ProprietaryRuntimeException( String msg ) { super(msg); } 140 public ProprietaryRuntimeException( String msg, Throwable cause ) { super(msg,cause); } 141 } 142 143 private static final class ProprietaryError extends Error 144 { 145 public static final long serialVersionUID = 987324; public ProprietaryError( String msg ) { super(msg); } 147 public ProprietaryError( String msg, Throwable cause ) { super(msg,cause); } 148 } 149 150 private List <Throwable > 151 getProprietary() 152 { 153 final List <Throwable > items = new ArrayList <Throwable >(); 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 original, 167 final Throwable remapped ) 168 { 169 assert( remapped != original ); 170 assert( remapped.getClass() != original.getClass() ); 171 172 if ( original instanceof RuntimeException ) 173 { 174 assert( remapped instanceof RuntimeException ); 175 } 176 if ( original instanceof Error ) 177 { 178 assert( remapped instanceof Error ); 179 } 180 if ( original instanceof Exception ) 181 { 182 assert( remapped instanceof Exception ); 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 [] originalStackTrace = original.getStackTrace(); 196 final StackTraceElement [] 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 t = new ProprietaryThrowable( "hello" ); 209 210 final Throwable rm = ThrowableMapper.map( t ); 211 212 final StackTraceElement [] originalStackTrace = t.getStackTrace(); 213 final StackTraceElement [] 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 <Throwable > items = getProprietary(); 225 226 for( final Throwable item : items ) 227 { 228 final Throwable remapped = ThrowableMapper.map( item ); 229 230 verifyMapping( item, remapped ); 231 } 232 } 233 } 234 235 236 237 238 239 240 241 242 243 | Popular Tags |