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.vm;
8   
9   import org.mule.api.MuleMessage;
10  import org.mule.module.client.MuleClient;
11  import org.mule.tck.junit4.FunctionalTestCase;
12  
13  import java.util.Arrays;
14  import java.util.HashSet;
15  import java.util.Iterator;
16  import java.util.Set;
17  import java.util.StringTokenizer;
18  
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.assertTrue;
24  
25  public class VMQueueTestCase extends FunctionalTestCase
26  {
27  
28      public static final long WAIT = 3000L;
29  
30      @Override
31      protected String getConfigResources()
32      {
33          return "vm/vm-queue-test.xml";
34      }
35  
36      @Test
37      public void testSingleMessage() throws Exception
38      {
39          MuleClient client = new MuleClient(muleContext);
40          client.dispatch("queue", "Marco", null);
41          MuleMessage response = client.request("queue", WAIT);
42          assertNotNull("Response is null", response);
43          assertEquals("Marco", response.getPayload());
44      }
45  
46      @Test
47      public void testMultipleMessages() throws Exception
48      {
49          MuleClient client = new MuleClient(muleContext);
50          Set polos = new HashSet(Arrays.asList(new String[]{"Marco", "Niccolo", "Maffeo"}));
51          Iterator people = polos.iterator();
52          while (people.hasNext())
53          {
54              client.dispatch("queue", people.next(), null);
55          }
56  
57          for (int i = 0; i < 3; ++i)
58          {
59              MuleMessage response = client.request("queue", WAIT);
60              assertNotNull("Response is null", response);
61              String person = (String) response.getPayload();
62              assertTrue(person, polos.contains(person));
63              polos.remove(person);
64          }
65      }
66  
67      @Test
68      public void testPassThrough() throws Exception
69      {
70          MuleClient client = new MuleClient(muleContext);
71          Set polos = new HashSet(Arrays.asList(new String[]{"Marco", "Niccolo", "Maffeo"}));
72          Iterator people = polos.iterator();
73          while (people.hasNext())
74          {
75              client.dispatch("vm://entry", people.next(), null);
76          }
77  
78          for (int i = 0; i < 3; ++i)
79          {
80              MuleMessage response = client.request("queue", WAIT);
81              assertNotNull("Response is null", response);
82              String person = (String) response.getPayload();
83              String name = new StringTokenizer(person).nextToken();
84              assertTrue(name, polos.contains(name));
85              polos.remove(name);
86          }
87      }
88  
89      @Test
90      public void testNamedEndpoint() throws Exception
91      {
92          MuleClient client = new MuleClient(muleContext);
93          Set polos = new HashSet(Arrays.asList(new String[]{"Marco", "Niccolo", "Maffeo"}));
94          Iterator people = polos.iterator();
95          while (people.hasNext())
96          {
97              client.dispatch("entry", people.next(), null);
98          }
99  
100         for (int i = 0; i < 3; ++i)
101         {
102             MuleMessage response = client.request("queue", WAIT);
103             assertNotNull("Response is null", response);
104             String person = (String) response.getPayload();
105             String name = new StringTokenizer(person).nextToken();
106             assertTrue(name, polos.contains(name));
107             polos.remove(name);
108         }
109     }
110 
111 }