View Javadoc

1   /*
2    * $Id: TransformerContentTypeTestCase.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.transport.vm.functional;
12  
13  import org.mule.api.MuleEventContext;
14  import org.mule.api.MuleMessage;
15  import org.mule.api.lifecycle.Callable;
16  import org.mule.api.transport.PropertyScope;
17  import org.mule.module.client.MuleClient;
18  import org.mule.tck.FunctionalTestCase;
19  
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import javax.activation.MimeType;
24  
25  public class TransformerContentTypeTestCase extends FunctionalTestCase
26  {
27  
28      @Override
29      protected String getConfigResources()
30      {
31          return "org/mule/test/config/content-type-setting-transform-configs.xml";
32      }
33  
34      public void testContentTypes() throws Exception
35      {
36          MuleMessage response;
37          Map<String, Object> messageProperties = new HashMap<String, Object>();
38  
39          MuleClient client = new MuleClient(muleContext);
40  
41          messageProperties.put("content-type", "text/plain");
42          EchoComponent.setExpectedMimeType("text/xml");
43          response = client.send("vm://in1?connector=vm-in1", "OK", messageProperties);
44          assertNotNull(response);
45          assertEquals("OK", response.getPayload());
46  
47          messageProperties.remove("content-type");
48          response = client.send("vm://in1?connector=vm-in1", "OK", messageProperties);
49          assertNotNull(response);
50          assertEquals("OK", response.getPayload());
51  
52          messageProperties.put("content-type", "text/xml");
53          EchoComponent.setExpectedMimeType("text/plain");
54          response = client.send("vm://in2?connector=vm-in2", "OK", messageProperties);
55          assertNotNull(response);
56          assertEquals("OK", response.getPayload());
57  
58          messageProperties.remove("content-type");
59          response = client.send("vm://in2?connector=vm-in2", "OK", messageProperties);
60          assertNotNull(response);
61          assertEquals("OK", response.getPayload());
62      }
63  
64      public static class EchoComponent implements Callable
65      {
66          static String expectedMimeType;
67  
68          public Object onCall(MuleEventContext eventContext) throws Exception
69          {
70              MuleMessage message = eventContext.getMessage();
71              String contentType = message.getProperty("content-type", PropertyScope.INBOUND);
72              MimeType mt = new MimeType(contentType);
73              String mimeType = mt.getPrimaryType() + "/" + mt.getSubType();
74              assertEquals(expectedMimeType, mimeType);
75              return message;
76          }
77  
78          public static void setExpectedMimeType(String expectedContentType)
79          {
80              EchoComponent.expectedMimeType = expectedContentType;
81          }
82      }
83  }