1   /*
2    * $Id: MuleMessageTestCase.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;
12  
13  import org.mule.api.MuleEvent;
14  import org.mule.api.MuleMessage;
15  import org.mule.tck.AbstractMuleTestCase;
16  import org.mule.transformer.simple.ByteArrayToObject;
17  import org.mule.transformer.simple.ObjectToByteArray;
18  
19  import java.io.ByteArrayInputStream;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.OutputStream;
23  
24  import javax.activation.DataHandler;
25  import javax.activation.DataSource;
26  
27  public class MuleMessageTestCase extends AbstractMuleTestCase
28  {
29  
30      public void testAttachmentPersistence() throws Exception
31      {
32          ObjectToByteArray transformer = new ObjectToByteArray();
33          transformer.setAcceptUMOMessage(true);
34  
35          MuleEvent event = RequestContext.setEvent(getTestEvent("Mmm... attachments!"));
36          MuleMessage msg = event.getMessage();
37          msg.addAttachment("test-attachment", new DataHandler(new StringDataSource("attachment")));
38  
39          Object serialized = transformer.transform(msg);
40          assertNotNull(serialized);
41  
42          MuleMessage deserialized = (MuleMessage) new ByteArrayToObject().transform(serialized);
43          assertNotNull(deserialized);
44          assertEquals(deserialized.getUniqueId(), msg.getUniqueId());
45          assertEquals(deserialized.getPayload(), msg.getPayload());
46          assertEquals(deserialized.getAttachmentNames(), msg.getAttachmentNames());
47      }
48  
49      // silly little fake DataSource so that we don't need to use javamail
50      protected static class StringDataSource implements DataSource
51      {
52          protected String content;
53  
54          public StringDataSource(String payload)
55          {
56              super();
57              content = payload;
58          }
59  
60          public InputStream getInputStream() throws IOException
61          {
62              return new ByteArrayInputStream(content.getBytes());
63          }
64  
65          public OutputStream getOutputStream()
66          {
67              throw new UnsupportedOperationException("Read-only javax.activation.DataSource");
68          }
69  
70          public String getContentType()
71          {
72              return "text/plain";
73          }
74  
75          public String getName()
76          {
77              return "StringDataSource";
78          }
79      }
80  
81  }