View Javadoc

1   /*
2    * $Id: UntilSuccessfulTestCase.java 23071 2011-10-03 20:49:33Z ddossot $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.test.routing;
12  
13  import java.io.Serializable;
14  import java.util.ArrayList;
15  import java.util.List;
16  
17  import org.apache.commons.lang.RandomStringUtils;
18  import org.mule.api.MuleEvent;
19  import org.mule.api.MuleException;
20  import org.mule.api.MuleMessage;
21  import org.mule.api.processor.MessageProcessor;
22  import org.mule.module.client.MuleClient;
23  import org.mule.tck.FunctionalTestCase;
24  import org.mule.tck.functional.FunctionalTestComponent;
25  import org.mule.util.store.AbstractPartitionedObjectStore;
26  
27  public class UntilSuccessfulTestCase extends FunctionalTestCase
28  {
29      private MuleClient client;
30      private FunctionalTestComponent targetMessageProcessor;
31      private FunctionalTestComponent deadLetterQueueProcessor;
32  
33      @Override
34      protected String getConfigResources()
35      {
36          return "until-successful-test.xml";
37      }
38  
39      @Override
40      protected void doSetUp() throws Exception
41      {
42          super.doSetUp();
43  
44          client = new MuleClient(muleContext);
45  
46          targetMessageProcessor = getFunctionalTestComponent("target-mp");
47          deadLetterQueueProcessor = getFunctionalTestComponent("dlq-processor");
48  
49          final AbstractPartitionedObjectStore<Serializable> objectStore = muleContext.getRegistry()
50              .lookupObject("objectStore");
51          objectStore.disposePartition("DEFAULT_PARTITION");
52      }
53  
54      public void testDefaultConfiguration() throws Exception
55      {
56          final String payload = RandomStringUtils.randomAlphanumeric(20);
57          client.dispatch("vm://input-1", payload, null);
58  
59          final List<Object> receivedPayloads = ponderUntilMessageCountReceivedByTargetMessageProcessor(1);
60          assertEquals(1, receivedPayloads.size());
61          assertEquals(payload, receivedPayloads.get(0));
62      }
63  
64      public void testFullConfiguration() throws Exception
65      {
66          final String payload = RandomStringUtils.randomAlphanumeric(20);
67          final MuleMessage response = client.send("vm://input-2", payload, null);
68          assertEquals("ACK", response.getPayloadAsString());
69  
70          List<Object> receivedPayloads = ponderUntilMessageCountReceivedByTargetMessageProcessor(3);
71          assertEquals(3, receivedPayloads.size());
72          for (int i = 0; i <= 2; i++)
73          {
74              assertEquals(payload, receivedPayloads.get(i));
75          }
76  
77          receivedPayloads = ponderUntilMessageCountReceivedByDlqProcessor(1);
78          assertEquals(1, receivedPayloads.size());
79          assertEquals(payload, receivedPayloads.get(0));
80      }
81  
82      public void testFullConfigurationMP() throws Exception
83      {
84          final String payload = RandomStringUtils.randomAlphanumeric(20);
85          final MuleMessage response = client.send("vm://input-2MP", payload, null);
86          assertEquals("ACK", response.getPayloadAsString());
87  
88          final List<Object> receivedPayloads = ponderUntilMessageCountReceivedByTargetMessageProcessor(3);
89          assertEquals(3, receivedPayloads.size());
90          for (int i = 0; i <= 2; i++)
91          {
92              assertEquals(payload, receivedPayloads.get(i));
93          }
94  
95          ponderUntilMessageCountReceivedByCustomMP(1);
96      }
97  
98      public void testRetryOnEndpoint() throws Exception
99      {
100         final String payload = RandomStringUtils.randomAlphanumeric(20);
101         client.dispatch("vm://input-3", payload, null);
102 
103         final List<Object> receivedPayloads = ponderUntilMessageCountReceivedByTargetMessageProcessor(3);
104         assertEquals(3, receivedPayloads.size());
105         for (int i = 0; i <= 2; i++)
106         {
107             assertEquals(payload, receivedPayloads.get(i));
108         }
109     }
110 
111     private List<Object> ponderUntilMessageCountReceivedByTargetMessageProcessor(final int expectedCount)
112         throws InterruptedException
113     {
114         return ponderUntilMessageCountReceived(expectedCount, targetMessageProcessor);
115     }
116 
117     private List<Object> ponderUntilMessageCountReceivedByDlqProcessor(final int expectedCount)
118         throws InterruptedException
119     {
120         return ponderUntilMessageCountReceived(expectedCount, deadLetterQueueProcessor);
121     }
122 
123     private List<Object> ponderUntilMessageCountReceived(final int expectedCount,
124                                                          final FunctionalTestComponent ftc)
125         throws InterruptedException
126     {
127         final List<Object> results = new ArrayList<Object>();
128 
129         while (ftc.getReceivedMessagesCount() < expectedCount)
130         {
131             Thread.yield();
132             Thread.sleep(100L);
133         }
134 
135         for (int i = 0; i < ftc.getReceivedMessagesCount(); i++)
136         {
137             results.add(ftc.getReceivedMessage(1 + i));
138         }
139         return results;
140     }
141 
142     private void ponderUntilMessageCountReceivedByCustomMP(final int expectedCount)
143         throws InterruptedException
144     {
145         while (CustomMP.getCount() < expectedCount)
146         {
147             Thread.yield();
148             Thread.sleep(100L);
149         }
150     }
151 
152     static class CustomMP implements MessageProcessor
153     {
154         private static int count;
155 
156         public static void clearCount()
157         {
158             count = 0;
159         }
160 
161         public static int getCount()
162         {
163             return count;
164         }
165 
166         @Override
167         public MuleEvent process(final MuleEvent event) throws MuleException
168         {
169             count++;
170             return null;
171         }
172     }
173 }