View Javadoc

1   /*
2    * $Id: MixedAnnotationsTestCase.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  package org.mule.api.annotations.param;
11  
12  import org.mule.DefaultMuleMessage;
13  import org.mule.api.MuleMessage;
14  import org.mule.module.client.MuleClient;
15  import org.mule.tck.FunctionalTestCase;
16  import org.mule.util.StringDataSource;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import javax.activation.DataHandler;
22  
23  public class MixedAnnotationsTestCase extends FunctionalTestCase
24  {
25      private MuleMessage muleMessage;
26  
27      public MixedAnnotationsTestCase()
28      {
29          setDisposeManagerPerSuite(true);
30      }
31  
32      @Override
33      protected String getConfigResources()
34      {
35          return "org/mule/test/annotations/mixed-annotations.xml";
36      }
37  
38      @Override
39      public void doSetUp() throws Exception
40      {
41          super.doSetUp();
42          
43          Map<String, Object> props = new HashMap<String, Object>(3);
44          props.put("foo", "fooValue");
45          props.put("bar", "barValue");
46          props.put("baz", "bazValue");
47  
48          muleMessage = new DefaultMuleMessage("test", props, muleContext);
49  
50          try
51          {
52              muleMessage.addAttachment("foo", new DataHandler(new StringDataSource("fooValue")));
53              muleMessage.addAttachment("bar", new DataHandler(new StringDataSource("barValue")));
54              muleMessage.addAttachment("baz", new DataHandler(new StringDataSource("bazValue")));
55          }
56          catch (Exception e)
57          {
58              e.printStackTrace();
59              fail(e.getMessage());
60          }
61      }
62  
63      public void testProcessAllAnnotated() throws Exception
64      {
65          MuleClient client = new MuleClient(muleContext);
66          MuleMessage message = client.send("vm://allAnnotated", muleMessage);
67          assertNotNull("return message from MuleClient.send() should not be null", message);
68          assertTrue("Message payload should be a Map", message.getPayload() instanceof Map);
69          Map<?, ?> result = (Map<?, ?>) message.getPayload();
70          assertEquals(3, result.size());
71  
72          //Payload
73          assertEquals("test", result.get("payload"));
74  
75          //Headers
76          assertNotNull(result.get("inboundHeaders"));
77          Map<?, ?> headers = (Map<?, ?>)result.get("inboundHeaders");
78          assertEquals(2, headers.size());
79          assertEquals("fooValue", headers.get("foo"));
80          assertEquals("barValue", headers.get("bar"));
81  
82          //Attachments
83          assertNotNull(result.get("inboundAttachments"));
84          Map<?, ?> attachments = (Map<?, ?>)result.get("inboundAttachments");
85          assertEquals(3, attachments.size());
86          assertNotNull(attachments.get("foo"));
87          assertNotNull(attachments.get("bar"));
88          assertNotNull(attachments.get("baz"));
89      }
90  
91      public void testPayloadNotAnnotated() throws Exception
92      {
93          //When using param annotations every param needs t obe annotated
94          MuleClient client = new MuleClient(muleContext);
95          MuleMessage message = client.send("vm://someAnnotated", muleMessage);
96          assertNotNull("return message from MuleClient.send() should not be null", message);
97          assertNotNull(message.getExceptionPayload());
98          assertTrue(message.getExceptionPayload().getRootException() instanceof IllegalArgumentException);
99          assertEquals("wrong number of arguments", message.getExceptionPayload().getRootException().getMessage());
100     }
101 }