View Javadoc

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