View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.expression;
8   
9   import org.mule.DefaultMuleMessage;
10  import org.mule.api.MuleMessage;
11  import org.mule.tck.junit4.AbstractMuleContextTestCase;
12  import org.mule.util.StringDataSource;
13  
14  import java.io.ByteArrayOutputStream;
15  import java.io.IOException;
16  import java.util.HashMap;
17  import java.util.Map;
18  
19  import javax.activation.DataHandler;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertTrue;
23  
24  public abstract class AbstractAttachmentsTestCase extends AbstractMuleContextTestCase
25  {
26      protected MuleMessage message;
27  
28      public AbstractAttachmentsTestCase()
29      {
30          super();
31          setDisposeContextPerClass(true);
32      }
33  
34      @Override
35      protected void doSetUp() throws Exception
36      {
37          Map<String, DataHandler> attachments = createAttachmentsMap();
38          message = new DefaultMuleMessage(TEST_MESSAGE, null, attachments, muleContext);
39      }
40  
41      protected Map<String, DataHandler> createAttachmentsMap()
42      {
43          Map<String, DataHandler> attachments = new HashMap<String, DataHandler>();
44          attachments.put("foo", new DataHandler(new StringDataSource("foovalue")));
45          attachments.put("bar", new DataHandler(new StringDataSource("barvalue")));
46          attachments.put("baz", new DataHandler(new StringDataSource("bazvalue")));
47          return attachments;
48      }
49  
50      protected void assertAttachmentValueEquals(String expected, Object attachment) throws IOException
51      {
52          assertTrue(attachment instanceof DataHandler);
53          DataHandler dataHandler = (DataHandler) attachment;
54          String attachmentString = attachmentToString(dataHandler);
55          assertEquals(expected, attachmentString);
56      }
57  
58      protected String attachmentToString(DataHandler dataHandler) throws IOException
59      {
60          ByteArrayOutputStream baos = new ByteArrayOutputStream();
61          dataHandler.writeTo(baos);
62          return baos.toString();
63      }
64  }