1 17 18 package org.apache.avalon.util.exception; 19 20 import junit.framework.TestCase ; 21 22 28 public class ExceptionHelperTest extends TestCase 29 { 30 34 public ExceptionHelperTest( String name ) 35 { 36 super( name ); 37 } 38 39 final public void testCompositeExceptionReport() 40 throws Exception 41 { 42 Throwable e1 = null; 43 Throwable e2 = null; 44 try 45 { 46 doSomething(); 47 } 48 catch( Throwable e ) 49 { 50 e1 = e; 51 } 52 try 53 { 54 doSomething(); 55 } 56 catch( Throwable e ) 57 { 58 e2 = e; 59 } 60 final String message = 61 ExceptionHelper.packException( 62 "Composite exception report", 63 new Throwable []{ e1, e2 }, false ); 64 assertNotNull( message ); 65 } 67 68 final public void testExceptionWithMessageReport() 69 throws Exception 70 { 71 try 72 { 73 doSomething(); 74 } 75 catch( Throwable e ) 76 { 77 final String message = 78 ExceptionHelper.packException( "An error occured.", e ); 79 assertNotNull( message ); 81 } 82 } 83 84 final public void testExceptionWithStackTrace() 85 throws Exception 86 { 87 try 88 { 89 doSomething(); 90 } 91 catch( Throwable e ) 92 { 93 final String message = 94 ExceptionHelper.packException( e, true ); 95 assertNotNull( message ); 96 } 97 } 98 99 final public void testExceptionWithMessageAndStackTrace() 100 throws Exception 101 { 102 try 103 { 104 doSomething(); 105 } 106 catch( Throwable e ) 107 { 108 final String message = 109 ExceptionHelper.packException( "An error occured.", e, true ); 110 assertNotNull( message ); 111 } 112 } 113 114 private void doSomething() throws StandardException 115 { 116 try 117 { 118 doSomethingElse(); 119 } 120 catch( Throwable e ) 121 { 122 final String error = 123 "Unable to do something due to a error condition."; 124 throw new StandardException( error, e ); 125 } 126 } 127 128 private void doSomethingElse() 129 { 130 try 131 { 132 causeSomeError(); 133 } 134 catch( Throwable e ) 135 { 136 final String error = 137 "Unable to do something else due to a error condition."; 138 throw new StandardRuntimeException( error, e ); 139 } 140 } 141 142 private void causeSomeError() 143 { 144 final String error = 145 "Raising exception because that's what I'm programmed to do."; 146 throw new StandardError( error ); 147 } 148 } 149 | Popular Tags |