View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.util;
8   
9   import org.mule.tck.junit4.AbstractMuleTestCase;
10  
11  import java.io.IOException;
12  
13  import org.junit.Test;
14  
15  import static org.junit.Assert.assertFalse;
16  import static org.junit.Assert.assertNull;
17  import static org.junit.Assert.assertSame;
18  import static org.junit.Assert.assertTrue;
19  import static org.mule.util.ExceptionUtils.containsType;
20  import static org.mule.util.ExceptionUtils.getDeepestOccurenceOfType;
21  
22  public class ExceptionUtilsTestCase extends AbstractMuleTestCase
23  {
24      @Test
25      public void testContainsType()
26      {
27          assertTrue(containsType(new IllegalArgumentException(), IllegalArgumentException.class));
28  
29          assertTrue(containsType(new Exception(new IllegalArgumentException()), IllegalArgumentException.class));
30  
31          assertTrue(containsType(new Exception(new IllegalArgumentException(new NullPointerException())), NullPointerException.class));
32  
33          assertTrue(containsType(new Exception(new IllegalArgumentException(new NullPointerException())), RuntimeException.class));
34  
35          assertTrue(containsType(new Exception(new IllegalArgumentException(new NullPointerException())), Exception.class));
36  
37          assertFalse(containsType(new Exception(new IllegalArgumentException(new NullPointerException())), IOException.class));
38      }
39  
40      @Test
41      public void testLastIndexOfType_deepestIsTheOneWeWant() throws Exception
42      {
43          IllegalArgumentException expected = new IllegalArgumentException("something");
44          assertExpectationsForDeepestOccurence(expected);
45      }
46  
47      @Test
48      public void testLastIndexOfType_theOneWeWantIsNotTheDeepest() throws Exception
49      {
50          IllegalArgumentException expected = new IllegalArgumentException("something", new NullPointerException("somenull"));
51          assertExpectationsForDeepestOccurence(expected);
52  
53      }
54  
55      private void assertExpectationsForDeepestOccurence(IllegalArgumentException expected)
56      {
57          assertSame(expected, getDeepestOccurenceOfType(expected, IllegalArgumentException.class));
58  
59          assertSame(expected, getDeepestOccurenceOfType(new Exception(expected), IllegalArgumentException.class));
60  
61          assertSame(expected,
62              getDeepestOccurenceOfType(new IllegalArgumentException(new Exception(expected)), IllegalArgumentException.class));
63  
64          assertNull(getDeepestOccurenceOfType(new IllegalArgumentException(new Exception(expected)), IOException.class));
65      }
66  
67      @Test
68      public void testLastIndexOfType_nullParameters() throws Exception
69      {
70          assertNull(getDeepestOccurenceOfType(null, null));
71  
72          assertNull(getDeepestOccurenceOfType(new Exception(), null));
73  
74          assertNull(getDeepestOccurenceOfType(null, Exception.class));
75      }
76  }