View Javadoc

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