View Javadoc

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