View Javadoc

1   /*
2    * $Id: MessageRootIdPropagationTestCase.java 23247 2011-10-24 17:16:56Z mike.schilling $
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  package org.mule.issues;
11  
12  import org.mule.DefaultMuleMessage;
13  import org.mule.api.MuleMessage;
14  import org.mule.module.client.MuleClient;
15  import org.mule.tck.junit4.FunctionalTestCase;
16  import org.mule.tck.junit4.rule.DynamicPort;
17  import org.mule.transformer.AbstractMessageTransformer;
18  
19  import java.util.Collection;
20  import java.util.HashMap;
21  import java.util.HashSet;
22  import java.util.Map;
23  import java.util.Set;
24  
25  import org.junit.Rule;
26  import org.junit.Test;
27  import static org.junit.Assert.assertEquals;
28  
29  public class MessageRootIdPropagationTestCase extends FunctionalTestCase
30  {
31      @Rule
32      public DynamicPort port1 = new DynamicPort("port1");
33  
34      @Override
35      protected String getConfigResources()
36      {
37          return "org/mule/issues/message-root-id.xml";
38      }
39  
40      @Test
41      public void testRootIDs() throws Exception
42      {
43          RootIDGatherer.initialize();
44          MuleClient client = new MuleClient(muleContext);
45  
46          DefaultMuleMessage msg = new DefaultMuleMessage("Hello", muleContext);
47          msg.setOutboundProperty("where", "client");
48          RootIDGatherer.process(msg);
49          MuleMessage response = client.send("vm://vmin", msg);
50          Thread.sleep(1000);
51          System.out.println(RootIDGatherer.getIdMap());
52          assertEquals(6, RootIDGatherer.getMessageCount());
53          assertEquals(1, RootIDGatherer.getIds().size());
54      }
55  
56      static class RootIDGatherer extends AbstractMessageTransformer
57      {
58          static int messageCount;
59          static Map<String, String>idMap;
60          static int counter;
61  
62  
63          public static void initialize()
64          {
65              idMap = new HashMap<String, String>();
66              messageCount = 0;
67          }
68  
69          public static synchronized void process(MuleMessage msg)
70          {
71              String id = msg.getMessageRootId();
72              messageCount++;
73              String where = msg.<String>getOutboundProperty("where");
74              if (where == null)
75              {
76                  where = "location_" + counter++;
77              }
78              idMap.put(where, id);
79          }
80  
81          @Override
82          public Object transformMessage(MuleMessage msg, String encoding)
83          {
84              process(msg);
85              return msg.getPayload();
86          }
87  
88          public static Set<String> getIds()
89          {
90              return new HashSet<String>(idMap.values());
91          }
92  
93          public static int getMessageCount()
94          {
95              return messageCount;
96          }
97  
98          public static Map<String, String> getIdMap()
99          {
100             return idMap;
101         }
102     }
103 }