View Javadoc

1   /*
2    * $Id: VmExceptionStrategyOneWayTestCase.java 23154 2011-10-12 06:00:08Z 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.vm.functional.transactions;
12  
13  import org.hamcrest.core.Is;
14  import org.hamcrest.core.IsInstanceOf;
15  import org.hamcrest.core.IsNull;
16  import org.junit.Before;
17  import org.junit.Test;
18  import org.mule.api.MuleEventContext;
19  import org.mule.api.MuleMessage;
20  import org.mule.api.lifecycle.Callable;
21  import org.mule.api.transformer.TransformerException;
22  import org.mule.config.i18n.CoreMessages;
23  import org.mule.message.DefaultExceptionPayload;
24  import org.mule.message.ExceptionMessage;
25  import org.mule.module.client.MuleClient;
26  import org.mule.tck.junit4.FunctionalTestCase;
27  import org.mule.transformer.AbstractTransformer;
28  import org.mule.transport.NullPayload;
29  import org.mule.util.concurrent.Latch;
30  
31  import java.util.concurrent.TimeUnit;
32  
33  import static org.junit.Assert.assertThat;
34  import static org.junit.Assert.fail;
35  
36  public class VmExceptionStrategyOneWayTestCase extends FunctionalTestCase
37  {
38  
39      public static final int TIMEOUT = 3000;
40      public static final int TINY_TIMEOUT = 300;
41      public static final String ORIGINAL_MESSAGE = "some message";
42      private static Latch outboundComponentLatch;
43      private static Latch deadLetterQueueLatch;
44      private static boolean outboundComponentReached;
45  
46      @Override
47      protected String getConfigResources()
48      {
49          return "vm/vm-exception-strategy-config-one-way.xml";
50      }
51  
52      @Before
53      public void setUp() throws Exception
54      {
55          deadLetterQueueLatch = new Latch();
56          outboundComponentLatch = new Latch();
57          outboundComponentReached = false;
58      }
59  
60      @Test
61      public void testDeadLetterQueueWithInboundEndpointException() throws Exception
62      {
63          MuleClient muleClient = new MuleClient(muleContext);
64          MuleMessage response = muleClient.send("vm://in1", ORIGINAL_MESSAGE, null);
65          if (!deadLetterQueueLatch.await(TIMEOUT, TimeUnit.MILLISECONDS)) {
66              fail("dead letter queue must be reached");
67          }
68          assertThat(outboundComponentReached, Is.is(false));
69          assertThat(response, IsNull.<Object>notNullValue());
70          assertThat(response.getPayload(),IsInstanceOf.instanceOf(NullPayload.class));
71          assertThat(response.getExceptionPayload(), IsNull.<Object>notNullValue());
72          assertThat(response.getExceptionPayload(), IsInstanceOf.instanceOf(DefaultExceptionPayload.class));
73      }
74  
75      @Test
76      public void testDeadLetterQueueWithInboundEndpointResponseException() throws Exception
77      {
78          MuleClient muleClient = new MuleClient(muleContext);
79          MuleMessage response = muleClient.send("vm://in2", ORIGINAL_MESSAGE, null);
80          //TODO PLG - ES - fix this, DLQ call fails since tx was resolved already
81          /*if (!deadLetterQueueLatch.await(TIMEOUT, MILLISECONDS)) {
82              fail("dead letter queue must be reached");
83          }*/
84          assertThat(response, IsNull.<Object>notNullValue());
85          assertThat(response.getPayload(),IsInstanceOf.instanceOf(NullPayload.class));
86          assertThat(response.getExceptionPayload(), IsNull.<Object>notNullValue());
87          assertThat(response.getExceptionPayload(), IsInstanceOf.instanceOf(DefaultExceptionPayload.class));
88          if (!outboundComponentLatch.await(TIMEOUT, TimeUnit.MILLISECONDS))
89          {
90              fail("outbound component not reached");
91          }
92      }
93  
94      @Test
95      public void testDeadLetterQueueWithComponentException() throws Exception
96      {
97          MuleClient muleClient = new MuleClient(muleContext);
98          MuleMessage response = muleClient.send("vm://in3", ORIGINAL_MESSAGE, null);
99          if (!deadLetterQueueLatch.await(TIMEOUT, TimeUnit.MILLISECONDS)) {
100             fail("dead letter queue must be reached");
101         }
102         assertThat(outboundComponentReached, Is.is(false));
103         assertThat(response, IsNull.<Object>notNullValue());
104         assertThat(response.getPayload(),IsInstanceOf.instanceOf(NullPayload.class));
105         assertThat(response.getExceptionPayload(), IsNull.<Object>notNullValue());
106         assertThat(response.getExceptionPayload(), IsInstanceOf.instanceOf(DefaultExceptionPayload.class));
107     }
108 
109     @Test
110     public void testDeadLetterQueueWithOutboundEndpointException() throws Exception
111     {
112         MuleClient muleClient = new MuleClient(muleContext);
113         MuleMessage response = muleClient.send("vm://in4", ORIGINAL_MESSAGE, null);
114         if (!deadLetterQueueLatch.await(TIMEOUT, TimeUnit.MILLISECONDS)) {
115             fail("dead letter queue must be reached");
116         }
117         assertThat(outboundComponentReached, Is.is(false));
118         assertThat(response, IsNull.<Object>notNullValue());
119         assertThat(response.getPayload(),IsInstanceOf.instanceOf(NullPayload.class));
120         assertThat(response.getExceptionPayload(), IsNull.<Object>notNullValue());
121         assertThat(response.getExceptionPayload(), IsInstanceOf.instanceOf(DefaultExceptionPayload.class));
122     }
123 
124     public static class FailingTransformer extends AbstractTransformer
125     {
126 
127         @Override
128         protected Object doTransform(Object src, String enc) throws TransformerException
129         {
130             throw new TransformerException(CoreMessages.failedToBuildMessage(), this);
131         }
132     }
133 
134     public static class DeadLetterQueueComponent implements Callable
135     {
136 
137         public Object onCall(MuleEventContext eventContext) throws Exception
138         {
139             deadLetterQueueLatch.release();
140             MuleMessage message = eventContext.getMessage();
141             assertThat(message, IsNull.<Object>notNullValue());
142             assertThat(message.getExceptionPayload(), IsNull.<Object>nullValue());
143             assertThat(message.getPayload(), IsInstanceOf.instanceOf(ExceptionMessage.class));
144             return eventContext.getMessage();
145         }
146     }
147 
148     public static class OutboundComponent implements Callable
149     {
150 
151         public Object onCall(MuleEventContext eventContext) throws Exception
152         {
153             outboundComponentLatch.release();
154             outboundComponentReached = true;
155             return eventContext.getMessage();
156         }
157     }
158 }