1 package org.apache.onami.persist;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.junit.Before;
23 import org.junit.Test;
24
25 import static org.hamcrest.CoreMatchers.is;
26 import static org.hamcrest.CoreMatchers.sameInstance;
27 import static org.junit.Assert.assertThat;
28 import static org.junit.Assert.fail;
29
30
31
32
33 public class AggregatedExceptionTest
34 {
35 private AggregatedException.Builder sut;
36
37 @Before
38 public void setUp()
39 throws Exception
40 {
41 sut = new AggregatedException.Builder();
42 }
43
44 @Test
45 public void shouldNotThrowAnythingWhenEmpty()
46 {
47 sut.throwRuntimeExceptionIfHasCauses( "test msg" );
48 }
49
50 @Test
51 public void shouldThrowAggregatedExceptionWithAllCollectedExceptions()
52 {
53 final Exception e0 = new Exception();
54 final Exception e1 = new Exception();
55
56 try
57 {
58 sut.add( e0 );
59 sut.add( e1 );
60 sut.throwRuntimeExceptionIfHasCauses( "test msg" );
61 }
62
63 catch ( AggregatedException e )
64 {
65 assertThat( e.getNumCauses(), is( 2 ) );
66 assertThat( e.getCauses()[0], sameInstance( (Throwable) e0 ) );
67 assertThat( e.getCauses()[1], sameInstance( (Throwable) e1 ) );
68 return;
69 }
70
71 fail( "must throw AggregatedException" );
72 }
73
74 @Test
75 public void shouldThrowOriginalExceptionWhenOnlyOne()
76 {
77 Exception e0 = new RuntimeException( );
78
79 try
80 {
81 sut.add( e0 );
82 sut.throwRuntimeExceptionIfHasCauses( "test msg" );
83 }
84
85 catch ( RuntimeException e )
86 {
87 assertThat( e, sameInstance( (Throwable) e0 ) );
88 return;
89 }
90
91 fail( "must throw RuntimeException" );
92 }
93
94 }