View Javadoc
1   package org.apache.onami.persist;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Test for {@link AggregatedException}.
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  }