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;
8   
9   import org.mule.api.MuleRuntimeException;
10  import org.mule.tck.junit4.AbstractMuleContextTestCase;
11  
12  import javax.resource.spi.work.Work;
13  import javax.resource.spi.work.WorkEvent;
14  import javax.resource.spi.work.WorkException;
15  
16  import org.junit.Test;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertNotNull;
20  import static org.junit.Assert.assertTrue;
21  
22  /**
23   * The test is not there in AbstractConnector, because we need to call a protected
24   * method, and the latter class is in a different package.
25   */
26  public class GenericConnectorTestCase extends AbstractMuleContextTestCase
27  {
28  
29      /**
30       * Throwable should not cause a ClassCastException (MULE-802).
31       * 
32       * @throws Exception
33       */
34      @Test
35      public void testSpiWorkThrowableHandling() throws Exception
36      {
37          try
38          {
39              AbstractConnector connector = getTestConnector();
40              connector.handleWorkException(getTestWorkEvent(), "workRejected");
41          }
42          catch (MuleRuntimeException mrex)
43          {
44              assertNotNull(mrex);
45              assertTrue(mrex.getCause().getClass() == Throwable.class);
46              assertEquals("testThrowable", mrex.getCause().getMessage());
47          }
48      }
49  
50      private WorkEvent getTestWorkEvent()
51      {
52          WorkEvent event = new WorkEvent(this, // source
53              WorkEvent.WORK_REJECTED, getTestWork(), new WorkException(new Throwable("testThrowable")));
54          return event;
55      }
56  
57      private Work getTestWork()
58      {
59          return new Work()
60          {
61              public void release()
62              {
63                  // noop
64              }
65  
66              public void run()
67              {
68                  // noop
69              }
70          };
71      }
72  }