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