View Javadoc

1   /*
2    * $Id: EventMetaDataPropagationTestCase.java 19250 2010-08-30 16:53:14Z dirk.olmes $
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.test.integration;
12  
13  import org.mule.DefaultMuleEvent;
14  import org.mule.DefaultMuleMessage;
15  import org.mule.api.MuleEvent;
16  import org.mule.api.MuleEventContext;
17  import org.mule.api.MuleException;
18  import org.mule.api.MuleMessage;
19  import org.mule.api.MuleSession;
20  import org.mule.api.lifecycle.Callable;
21  import org.mule.api.service.Service;
22  import org.mule.service.ServiceCompositeMessageSource;
23  import org.mule.session.DefaultMuleSession;
24  import org.mule.tck.FunctionalTestCase;
25  import org.mule.tck.testmodels.fruit.Apple;
26  import org.mule.transformer.AbstractMessageAwareTransformer;
27  
28  import java.io.IOException;
29  import java.io.InputStream;
30  import java.io.OutputStream;
31  import java.util.HashMap;
32  import java.util.Map;
33  
34  import javax.activation.DataHandler;
35  import javax.activation.DataSource;
36  
37  public class EventMetaDataPropagationTestCase extends FunctionalTestCase
38  {
39      @Override
40      protected String getConfigResources()
41      {
42          return "org/mule/test/integration/event-metadata-propagation-config.xml";
43      }
44  
45      public void testEventMetaDataPropagation() throws MuleException
46      {
47          Service service = muleContext.getRegistry().lookupService("component1");
48          MuleSession session = new DefaultMuleSession(service, muleContext);
49          MuleEvent event = new DefaultMuleEvent(new DefaultMuleMessage("Test MuleEvent", muleContext),
50              ((ServiceCompositeMessageSource) service.getMessageSource()).getEndpoints().get(0), session);
51          service.sendEvent(event);
52      }
53  
54      public static class DummyComponent implements Callable
55      {
56          public Object onCall(MuleEventContext context) throws Exception
57          {
58              if ("component1".equals(context.getFlowConstruct().getName()))
59              {
60                  Map<String, Object> props = new HashMap<String, Object>();
61                  props.put("stringParam", "param1");
62                  props.put("objectParam", new Apple());
63                  props.put("doubleParam", 12345.6);
64                  props.put("integerParam", 12345);
65                  props.put("longParam", (long) 123456789);
66                  props.put("booleanParam", Boolean.TRUE);
67                  MuleMessage msg = new DefaultMuleMessage(context.getMessageAsString(), props, muleContext);
68                  msg.addAttachment("test1", new DataHandler(new DataSource()
69                  {
70                      public InputStream getInputStream() throws IOException
71                      {
72                          return null;
73                      }
74  
75                      public OutputStream getOutputStream() throws IOException
76                      {
77                          return null;
78                      }
79  
80                      public String getContentType()
81                      {
82                          return "text/plain";
83                      }
84  
85                      public String getName()
86                      {
87                          return "test1";
88                      }
89                  }));
90                  return msg;
91              }
92              else
93              {
94                  MuleMessage msg = context.getMessage();
95                  assertEquals("param1", msg.getInboundProperty("stringParam"));
96                  final Object o = msg.getInboundProperty("objectParam");
97                  assertTrue(o instanceof Apple);
98                  assertEquals(12345.6, 12345.6, msg.<Double>getInboundProperty("doubleParam", 0d));
99                  assertEquals(12345, msg.<Integer>getInboundProperty("integerParam", 0).intValue());
100                 assertEquals(123456789, msg.<Long>getInboundProperty("longParam", 0L).longValue());
101                 assertEquals(Boolean.TRUE, msg.getInboundProperty("booleanParam", Boolean.FALSE));
102                 assertNotNull(msg.getInboundAttachment("test1"));
103             }
104             return null;
105         }
106     }
107 
108     /**
109      * Extend AbstractMessageAwareTransformer, even though it's deprecated, to ensure that it
110      * keeps working for compatibility with older user-written transformers.
111      */
112     @SuppressWarnings("deprecation")
113     public static class DummyTransformer extends AbstractMessageAwareTransformer
114     {
115         @Override
116         public Object transform(MuleMessage msg, String outputEncoding)
117         {
118             assertEquals("param1", msg.getOutboundProperty("stringParam"));
119             final Object o = msg.getOutboundProperty("objectParam");
120             assertTrue(o instanceof Apple);
121             assertEquals(12345.6, 12345.6, msg.<Double>getOutboundProperty("doubleParam", 0d));
122             assertEquals(12345, msg.<Integer>getOutboundProperty("integerParam", 0).intValue());
123             assertEquals(123456789, msg.<Long>getOutboundProperty("longParam", 0L).longValue());
124             assertEquals(Boolean.TRUE, msg.getOutboundProperty("booleanParam", Boolean.FALSE));
125             return msg;
126         }
127     }
128 }