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.transport.jms;
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.io.IOException;
15  import java.util.List;
16  import java.util.Map;
17  
18  import javax.jms.JMSException;
19  
20  import org.junit.Before;
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 JmsExceptionReaderTestCase extends AbstractMuleTestCase
28  {
29  
30      @Before
31      public void registerExceptionReader()
32      {
33          ExceptionHelper.registerExceptionReader(new JmsExceptionReader());
34      }
35  
36      @Test
37      public void testNestedExceptionRetrieval() throws Exception
38      {
39          Exception testException = getException();
40          Throwable t = ExceptionHelper.getRootException(testException);
41          assertNotNull(t);
42          assertEquals("blah", t.getMessage());
43          assertNull(t.getCause());
44  
45          t = ExceptionHelper.getRootMuleException(testException);
46          assertNotNull(t);
47          assertEquals("bar", t.getMessage());
48          assertNotNull(t.getCause());
49  
50          List l = ExceptionHelper.getExceptionsAsList(testException);
51          assertEquals(4, l.size());
52  
53          Map info = ExceptionHelper.getExceptionInfo(testException);
54          assertNotNull(info);
55          assertEquals(2, info.size());
56          assertNotNull(info.get("JavaDoc"));
57          assertEquals("1234", info.get("JMS Code"));
58      }
59  
60      private Exception getException()
61      {
62  
63          JMSException e = new JMSException("Jms error", "1234");
64          e.setLinkedException(new IOException("blah"));
65  
66          return new DefaultMuleException(MessageFactory.createStaticMessage("foo"), new DefaultMuleException(
67              MessageFactory.createStaticMessage("bar"), e));
68      }
69  
70  }