View Javadoc

1   /*
2    * $Id: AttachmentsPropagationTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
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.message;
12  
13  import org.mule.api.MuleEventContext;
14  import org.mule.api.MuleMessage;
15  import org.mule.module.client.MuleClient;
16  import org.mule.tck.AbstractMuleTestCase;
17  import org.mule.tck.functional.EventCallback;
18  import org.mule.tck.functional.FunctionalTestComponent;
19  import org.mule.transport.email.transformers.PlainTextDataSource;
20  
21  import javax.activation.DataHandler;
22  
23  public class AttachmentsPropagationTestCase extends AbstractMuleTestCase implements EventCallback
24  {
25      public void testSanity()
26      {
27          fail("Convert this test to an XML-based configuration");
28      }    
29  
30      @Override
31      protected void doSetUp() throws Exception
32      {
33          super.doSetUp();
34  
35          // TODO Convert this to an XML config
36  //        EndpointBuilder endpointBuilder = new EndpointURIEndpointBuilder("vm://Single", muleContext);
37  //        endpointBuilder.setName("SingleEndpoint");
38  //        ImmutableEndpoint vmSingle = muleContext.getRegistry()
39  //            .lookupEndpointFactory()
40  //            .getOutboundEndpoint(endpointBuilder, muleContext);
41  //        
42  //        EndpointBuilder endpointBuilder2 = new EndpointURIEndpointBuilder("vm://Single", muleContext);
43  //        endpointBuilder2.setName("ChainedEndpoint");
44  //        ImmutableEndpoint vmChained = muleContext.getRegistry()
45  //            .lookupEndpointFactory()
46  //            .getOutboundEndpoint(endpointBuilder2, muleContext);
47  //        
48  //        FunctionalTestComponent single = new FunctionalTestComponent();
49  //        single.setEventCallback(this);
50  //        FunctionalTestComponent chained = new FunctionalTestComponent();
51  //        chained.setEventCallback(this);
52  //        builder.registerComponentInstance(single, "SINGLE", vmSingle.getEndpointURI());
53  //        builder.registerComponentInstance(chained, "CHAINED", vmChained.getEndpointURI(), vmSingle
54  //            .getEndpointURI());
55      }
56  
57      public void eventReceived(MuleEventContext context, Object component) throws Exception
58      {
59          MuleMessage message = context.getMessage();
60          // add an attachment, named after the componentname...
61          message.addAttachment(context.getFlowConstruct().getName(), new DataHandler(
62              new PlainTextDataSource("text/plain", "<content>")));
63  
64          // return the list of attachment names
65          FunctionalTestComponent fc = (FunctionalTestComponent) component;
66          fc.setReturnData(message.getAttachmentNames().toString());
67      }
68  
69      public void testSingleComponentKnowsAttachments() throws Exception
70      {
71  
72          MuleClient client = new MuleClient(muleContext);
73          MuleMessage result = client.send("vm://Single", "", null);
74          assertNotNull(result);
75  
76          // expect SINGLE attachment from SINGLE service
77          assertEquals("[SINGLE]", result.getPayloadAsString());
78      }
79  
80      public void testChainedComponentKnowsAttachments() throws Exception
81      {
82          MuleClient client = new MuleClient(muleContext);
83          MuleMessage result = client.send("vm://Chained", "", null);
84          assertNotNull(result);
85  
86          // expect CHAINED attachment from CHAINED service
87          // and SINGLE attachment from SINGLE service
88          assertEquals("[SINGLE, CHAINED]", result.getPayloadAsString());
89      }
90  
91      public void testClientReceivesAttachments() throws Exception
92      {
93          // a MuleClient should be able to receive attachments
94          MuleClient client = new MuleClient(muleContext);
95  
96          MuleMessage result = client.send("vm://Single", "", null);
97          assertNotNull(result);
98  
99          // expect SINGLE attachment from SINGLE service
100         assertEquals("[SINGLE]", result.getPayloadAsString());
101         assertNotNull(result.getAttachment("SINGLE"));
102         assertEquals("<content>", result.getAttachment("SINGLE").getContent().toString());
103 
104         result = client.send("vm://Chained", "", null);
105         assertNotNull(result);
106 
107         // expect SINGLE and CHAINED attachments
108         assertEquals("[SINGLE, CHAINED]", result.getPayloadAsString());
109         assertNotNull(result.getAttachment("SINGLE"));
110         assertEquals("<content>", result.getAttachment("SINGLE").getContent().toString());
111         assertNotNull(result.getAttachment("CHAINED"));
112         assertEquals("<content>", result.getAttachment("CHAINED").getContent().toString());
113     }
114 }