1   /*
2    * $Id: EventMetaDataPropagationTestCase.java 11343 2008-03-13 10:58:26Z tcarlson $
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.test.integration;
12  
13  import org.mule.DefaultMuleEvent;
14  import org.mule.DefaultMuleMessage;
15  import org.mule.DefaultMuleSession;
16  import org.mule.api.MuleException;
17  import org.mule.api.MuleEvent;
18  import org.mule.api.MuleEventContext;
19  import org.mule.api.MuleMessage;
20  import org.mule.api.MuleSession;
21  import org.mule.api.endpoint.ImmutableEndpoint;
22  import org.mule.api.lifecycle.Callable;
23  import org.mule.api.service.Service;
24  import org.mule.api.transformer.TransformerException;
25  import org.mule.tck.FunctionalTestCase;
26  import org.mule.tck.testmodels.fruit.Apple;
27  import org.mule.transformer.AbstractMessageAwareTransformer;
28  
29  import java.io.IOException;
30  import java.io.InputStream;
31  import java.io.OutputStream;
32  import java.util.HashMap;
33  import java.util.Map;
34  
35  import javax.activation.DataHandler;
36  import javax.activation.DataSource;
37  
38  public class EventMetaDataPropagationTestCase extends FunctionalTestCase
39  {
40  
41      protected String getConfigResources()
42      {
43          return "org/mule/test/integration/event-metadata-propagation-config.xml";
44      }
45  
46      public void testEventMetaDataPropagation() throws MuleException
47      {
48          Service service = muleContext.getRegistry().lookupService("component1");
49          MuleSession session = new DefaultMuleSession(service, muleContext);
50          MuleEvent event =
51                  new DefaultMuleEvent(new DefaultMuleMessage("Test MuleEvent"),
52                          (ImmutableEndpoint)service.getInboundRouter().getEndpoints().get(0), session, true);
53          session.sendEvent(event);
54      }
55  
56      public static class DummyComponent implements Callable
57      {
58  
59          public Object onCall(MuleEventContext context) throws Exception
60          {
61              if ("component1".equals(context.getService().getName()))
62              {
63                  Map props = new HashMap();
64                  props.put("stringParam", "param1");
65                  props.put("objectParam", new Apple());
66                  props.put("doubleParam", new Double(12345.6));
67                  props.put("integerParam", new Integer(12345));
68                  props.put("longParam", new Long(123456789));
69                  props.put("booleanParam", Boolean.TRUE);
70                  MuleMessage msg = new DefaultMuleMessage(context.getMessageAsString(), props);
71                  msg.addAttachment("test1", new DataHandler(new DataSource()
72                  {
73                      public InputStream getInputStream() throws IOException
74                      {
75                          return null;
76                      }
77  
78                      public OutputStream getOutputStream() throws IOException
79                      {
80                          return null;
81                      }
82  
83                      public String getContentType()
84                      {
85                          return "text/plain";
86                      }
87  
88                      public String getName()
89                      {
90                          return "test1";
91                      }
92                  }));
93                  return msg;
94              }
95              else
96              {
97                  MuleMessage msg = context.getMessage();
98                  assertEquals("param1", msg.getProperty("stringParam"));
99                  assertTrue(msg.getProperty("objectParam") instanceof Apple);
100                 assertEquals(12345.6, 12345.6, msg.getDoubleProperty("doubleParam", 0));
101                 assertEquals(12345, msg.getIntProperty("integerParam", 0));
102                 assertEquals(123456789, msg.getLongProperty("longParam", 0));
103                 assertEquals(true, msg.getBooleanProperty("booleanParam", false));
104                 assertNotNull(msg.getAttachment("test1"));
105             }
106             return null;
107         }
108     }
109 
110     public static class DummyTransformer extends AbstractMessageAwareTransformer
111     {
112 
113         public Object transform(MuleMessage msg, String outputEncoding) throws TransformerException
114         {
115             assertEquals("param1", msg.getProperty("stringParam"));
116             assertTrue(msg.getProperty("objectParam") instanceof Apple);
117             assertEquals(12345.6, 12345.6, msg.getDoubleProperty("doubleParam", 0));
118             assertEquals(12345, msg.getIntProperty("integerParam", 0));
119             assertEquals(123456789, msg.getLongProperty("longParam", 0));
120             assertEquals(true, msg.getBooleanProperty("booleanParam", false));
121             return msg;
122         }
123     }
124 
125 }