1 17 18 package org.apache.avalon.fortress.util.test; 19 20 import junit.framework.TestCase; 21 import org.apache.avalon.fortress.util.CompositeException; 22 23 29 public class CompositeExceptionTestCase extends TestCase 30 { 31 private Exception [] m_exceptions; 32 33 public CompositeExceptionTestCase( String name ) 34 { 35 super( name ); 36 } 37 38 public void setUp() 39 { 40 m_exceptions = new Exception [2]; 41 m_exceptions[0] = new RuntimeException ( "Test1" ); 42 m_exceptions[1] = new RuntimeException ( "Test2" ); 43 } 44 45 public void testRegularCreation() 46 { 47 CompositeException exc = new CompositeException( m_exceptions ); 48 assertNotNull( exc ); 49 assertNotNull( exc.getMessage() ); 50 assertTrue( null == exc.getCause() ); 51 assertNotNull( exc.getExceptions() ); 52 53 final StringBuffer msg = new StringBuffer (); 54 for ( int i = 0; i < m_exceptions.length; i++ ) 55 { 56 if ( i > 0 ) msg.append( '\n' ); 57 msg.append( m_exceptions[i].getMessage() ); 58 } 59 final String message = msg.toString(); 60 61 assertEquals( message, exc.getMessage() ); 62 63 Exception [] exceptions = exc.getExceptions(); 64 assertEquals( m_exceptions.length, exceptions.length ); 65 66 for ( int i = 0; i < exceptions.length; i++ ) 67 { 68 assertEquals( m_exceptions[i], exceptions[i] ); 69 } 70 } 71 72 public void testNestedCreation() 73 { 74 final String message = "Message"; 75 CompositeException exc = new CompositeException( m_exceptions, message ); 76 assertNotNull( exc ); 77 assertNotNull( exc.getMessage() ); 78 assertTrue( null == exc.getCause() ); 79 assertNotNull( exc.getExceptions() ); 80 81 assertEquals( message, exc.getMessage() ); 82 83 Exception [] exceptions = exc.getExceptions(); 84 assertEquals( m_exceptions.length, exceptions.length ); 85 86 for ( int i = 0; i < exceptions.length; i++ ) 87 { 88 assertEquals( m_exceptions[i], exceptions[i] ); 89 } 90 } 91 92 public void testIllegalArgument() 93 { 94 try 95 { 96 new CompositeException( null ); 97 fail( "Did not throw an IllegalArgumentException" ); 98 } 99 catch ( IllegalArgumentException iae ) 100 { 101 } 103 catch ( Exception e ) 104 { 105 fail( "Threw the wrong exception: " + e.getClass().getName() ); 106 } 107 108 try 109 { 110 new CompositeException( new Exception []{} ); 111 fail( "Did not throw an IllegalArgumentException" ); 112 } 113 catch ( IllegalArgumentException iae ) 114 { 115 } 117 catch ( Exception e ) 118 { 119 fail( "Threw the wrong exception: " + e.getClass().getName() ); 120 } 121 } 122 } 123 | Popular Tags |