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.jdbc;
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.sql.SQLException;
15  import java.util.List;
16  import java.util.Map;
17  
18  import org.junit.Before;
19  import org.junit.Test;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertNotNull;
23  import static org.junit.Assert.assertNull;
24  
25  public class SqlExceptionReaderTestCase extends AbstractMuleTestCase
26  {
27  
28      /**
29       * Print the name of this test to standard output
30       */
31      @Before
32      public void registerExceptionReader()
33      {
34          ExceptionHelper.registerExceptionReader(new SQLExceptionReader());
35      }
36  
37      @Test
38      public void testNestedExceptionRetreval() throws Exception
39      {
40          Exception testException = getException();
41          Throwable t = ExceptionHelper.getRootException(testException);
42          assertNotNull(t);
43          assertEquals("blah", t.getMessage());
44          assertNull(t.getCause());
45  
46          t = ExceptionHelper.getRootMuleException(testException);
47          assertNotNull(t);
48          assertEquals("bar", t.getMessage());
49          assertNotNull(t.getCause());
50  
51          List l = ExceptionHelper.getExceptionsAsList(testException);
52          assertEquals(4, l.size());
53  
54          Map info = ExceptionHelper.getExceptionInfo(testException);
55          assertNotNull(info);
56          assertEquals(3, info.size());
57          assertNotNull(info.get("JavaDoc"));
58          assertEquals("1234", info.get("SQL Code"));
59          assertEquals("bad SQL state", info.get("SQL State"));
60      }
61  
62      private Exception getException()
63      {
64  
65          SQLException e = new SQLException("SQL error", "bad SQL state", 1234);
66          e.setNextException(new SQLException("blah"));
67  
68          return new DefaultMuleException(MessageFactory.createStaticMessage("foo"), new DefaultMuleException(
69              MessageFactory.createStaticMessage("bar"), e));
70      }
71  }