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.test.config;
8   
9   import org.mule.api.DefaultMuleException;
10  import org.mule.config.ExceptionHelper;
11  import org.mule.config.i18n.MessageFactory;
12  import org.mule.tck.junit4.AbstractMuleTestCase;
13  
14  import java.util.List;
15  import java.util.Map;
16  
17  import org.junit.Test;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertNotNull;
21  import static org.junit.Assert.assertNull;
22  
23  public class ExceptionHelperTestCase extends AbstractMuleTestCase
24  {
25  
26      @Test
27      public void testNestedExceptionRetreval() throws Exception
28      {
29          Exception testException = getException();
30          Throwable t = ExceptionHelper.getRootException(testException);
31          assertNotNull(t);
32          assertEquals("blah", t.getMessage());
33          assertNull(t.getCause());
34  
35          t = ExceptionHelper.getRootMuleException(testException);
36          assertNotNull(t);
37          assertEquals("bar", t.getMessage());
38          assertNotNull(t.getCause());
39  
40          List<?> l = ExceptionHelper.getExceptionsAsList(testException);
41          assertEquals(3, l.size());
42  
43          Map<?, ?> info = ExceptionHelper.getExceptionInfo(testException);
44          assertNotNull(info);
45          assertEquals(1, info.size());
46          assertNotNull(info.get("JavaDoc"));
47      }
48      
49      @Test
50      public void testSummarizeWithDepthBeyondStackTraceLength()
51      {
52          Exception exception = getException();
53          int numberOfStackFrames = exception.getStackTrace().length;
54          int depth = numberOfStackFrames + 1;
55          
56          Throwable summary = ExceptionHelper.summarise(exception, depth);
57          assertNotNull(summary);
58      }
59  
60      private Exception getException()
61      {
62          return new DefaultMuleException(MessageFactory.createStaticMessage("foo"), new DefaultMuleException(
63              MessageFactory.createStaticMessage("bar"), new Exception("blah")));
64      }
65  }