View Javadoc

1   /*
2    * $Id: VMQueueTestCase.java 22449 2011-07-19 07:40:43Z justin.calleja $
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;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertNotNull;
15  import static org.junit.Assert.assertTrue;
16  
17  import java.util.Arrays;
18  import java.util.Collection;
19  import java.util.HashSet;
20  import java.util.Iterator;
21  import java.util.Set;
22  import java.util.StringTokenizer;
23  
24  import org.junit.Test;
25  import org.junit.runners.Parameterized.Parameters;
26  import org.mule.api.MuleMessage;
27  import org.mule.module.client.MuleClient;
28  import org.mule.tck.AbstractServiceAndFlowTestCase;
29  
30  public class VMQueueTestCase extends AbstractServiceAndFlowTestCase
31  {
32  
33      public static final long WAIT = 3000L;
34  
35      public VMQueueTestCase(ConfigVariant variant, String configResources)
36      {
37          super(variant, configResources);
38      }
39      
40      @Parameters
41      public static Collection<Object[]> parameters()
42      {
43          return Arrays.asList(new Object[][]{
44              {ConfigVariant.SERVICE, "vm/vm-queue-test-service.xml"},
45              {ConfigVariant.FLOW, "vm/vm-queue-test-flow.xml"}
46          });
47      }
48  
49      @Test
50      public void testSingleMessage() throws Exception
51      {
52          MuleClient client = new MuleClient(muleContext);
53          client.dispatch("queue", "Marco", null);
54          MuleMessage response = client.request("queue", WAIT);
55          assertNotNull("Response is null", response);
56          assertEquals("Marco", response.getPayload());
57      }
58  
59      @Test
60      public void testMultipleMessages() throws Exception
61      {
62          MuleClient client = new MuleClient(muleContext);
63          Set polos = new HashSet(Arrays.asList(new String[]{"Marco", "Niccolo", "Maffeo"}));
64          Iterator people = polos.iterator();
65          while (people.hasNext())
66          {
67              client.dispatch("queue", people.next(), null);
68          }
69  
70          for (int i = 0; i < 3; ++i)
71          {
72              MuleMessage response = client.request("queue", WAIT);
73              assertNotNull("Response is null", response);
74              String person = (String) response.getPayload();
75              assertTrue(person, polos.contains(person));
76              polos.remove(person);
77          }
78      }
79  
80      @Test
81      public void testPassThrough() throws Exception
82      {
83          MuleClient client = new MuleClient(muleContext);
84          Set polos = new HashSet(Arrays.asList(new String[]{"Marco", "Niccolo", "Maffeo"}));
85          Iterator people = polos.iterator();
86          while (people.hasNext())
87          {
88              client.dispatch("vm://entry", people.next(), null);
89          }
90  
91          for (int i = 0; i < 3; ++i)
92          {
93              MuleMessage response = client.request("queue", WAIT);
94              assertNotNull("Response is null", response);
95              String person = (String) response.getPayload();
96              String name = new StringTokenizer(person).nextToken();
97              assertTrue(name, polos.contains(name));
98              polos.remove(name);
99          }
100     }
101 
102     @Test
103     public void testNamedEndpoint() throws Exception
104     {
105         MuleClient client = new MuleClient(muleContext);
106         Set polos = new HashSet(Arrays.asList(new String[]{"Marco", "Niccolo", "Maffeo"}));
107         Iterator people = polos.iterator();
108         while (people.hasNext())
109         {
110             client.dispatch("entry", people.next(), null);
111         }
112 
113         for (int i = 0; i < 3; ++i)
114         {
115             MuleMessage response = client.request("queue", WAIT);
116             assertNotNull("Response is null", response);
117             String person = (String) response.getPayload();
118             String name = new StringTokenizer(person).nextToken();
119             assertTrue(name, polos.contains(name));
120             polos.remove(name);
121         }
122     }
123 
124 }