View Javadoc

1   /*
2    * $Id: ExceptionUtilsTestCase.java 22387 2011-07-12 03:53:36Z dirk.olmes $
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 org.mule.tck.junit4.AbstractMuleTestCase;
14  
15  import java.io.IOException;
16  
17  import org.junit.Test;
18  
19  import static org.junit.Assert.assertFalse;
20  import static org.junit.Assert.assertNull;
21  import static org.junit.Assert.assertSame;
22  import static org.junit.Assert.assertTrue;
23  import static org.mule.util.ExceptionUtils.containsType;
24  import static org.mule.util.ExceptionUtils.getDeepestOccurenceOfType;
25  
26  public class ExceptionUtilsTestCase extends AbstractMuleTestCase
27  {
28      @Test
29      public void testContainsType()
30      {
31          assertTrue(containsType(new IllegalArgumentException(), IllegalArgumentException.class));
32  
33          assertTrue(containsType(new Exception(new IllegalArgumentException()), IllegalArgumentException.class));
34  
35          assertTrue(containsType(new Exception(new IllegalArgumentException(new NullPointerException())), NullPointerException.class));
36  
37          assertTrue(containsType(new Exception(new IllegalArgumentException(new NullPointerException())), RuntimeException.class));
38  
39          assertTrue(containsType(new Exception(new IllegalArgumentException(new NullPointerException())), Exception.class));
40  
41          assertFalse(containsType(new Exception(new IllegalArgumentException(new NullPointerException())), IOException.class));
42      }
43  
44      @Test
45      public void testLastIndexOfType_deepestIsTheOneWeWant() throws Exception
46      {
47          IllegalArgumentException expected = new IllegalArgumentException("something");
48          assertExpectationsForDeepestOccurence(expected);
49      }
50  
51      @Test
52      public void testLastIndexOfType_theOneWeWantIsNotTheDeepest() throws Exception
53      {
54          IllegalArgumentException expected = new IllegalArgumentException("something", new NullPointerException("somenull"));
55          assertExpectationsForDeepestOccurence(expected);
56  
57      }
58  
59      private void assertExpectationsForDeepestOccurence(IllegalArgumentException expected)
60      {
61          assertSame(expected, getDeepestOccurenceOfType(expected, IllegalArgumentException.class));
62  
63          assertSame(expected, getDeepestOccurenceOfType(new Exception(expected), IllegalArgumentException.class));
64  
65          assertSame(expected,
66              getDeepestOccurenceOfType(new IllegalArgumentException(new Exception(expected)), IllegalArgumentException.class));
67  
68          assertNull(getDeepestOccurenceOfType(new IllegalArgumentException(new Exception(expected)), IOException.class));
69      }
70  
71      @Test
72      public void testLastIndexOfType_nullParameters() throws Exception
73      {
74          assertNull(getDeepestOccurenceOfType(null, null));
75  
76          assertNull(getDeepestOccurenceOfType(new Exception(), null));
77  
78          assertNull(getDeepestOccurenceOfType(null, Exception.class));
79      }
80  }