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.transport;
8   
9   import org.mule.api.MuleMessage;
10  import org.mule.api.transport.MessageTypeNotSupportedException;
11  import org.mule.api.transport.MuleMessageFactory;
12  import org.mule.tck.junit4.AbstractMuleContextTestCase;
13  
14  import org.junit.Test;
15  
16  import static org.junit.Assert.assertEquals;
17  import static org.junit.Assert.assertNotNull;
18  import static org.junit.Assert.fail;
19  
20  public abstract class AbstractMuleMessageFactoryTestCase extends AbstractMuleContextTestCase
21  {
22      protected String encoding;
23      
24      /**
25       * Subclasses can set this flag to false, disabling the test for unsupported transport
26       * message types.
27       */
28      protected boolean runUnsuppoprtedTransportMessageTest = true;
29  
30      public AbstractMuleMessageFactoryTestCase()
31      {
32          super();
33          setStartContext(false);
34      }
35      
36      @Override
37      protected void doSetUp() throws Exception
38      {
39          super.doSetUp();
40          encoding = muleContext.getConfiguration().getDefaultEncoding();
41      }
42      
43      @Test
44      public void testNullPayload() throws Exception
45      {
46          MuleMessageFactory factory = createMuleMessageFactory();
47          
48          MuleMessage message = factory.create(null, encoding);
49          assertNotNull(message);
50          assertEquals(NullPayload.getInstance(), message.getPayload());
51      }
52  
53      @Test
54      public void testValidPayload() throws Exception
55      {
56          MuleMessageFactory factory = createMuleMessageFactory();
57      
58          Object payload = getValidTransportMessage();
59          MuleMessage message = factory.create(payload, encoding);
60          assertNotNull(message);
61          assertEquals(payload, message.getPayload());
62      }
63      
64      @Test
65      public void testUnsupportedPayloadType() throws Exception
66      {
67          if (runUnsuppoprtedTransportMessageTest == false)
68          {
69              return;
70          }
71          
72          MuleMessageFactory factory = createMuleMessageFactory();
73          
74          Object invalidPayload = getUnsupportedTransportMessage();
75          try
76          {
77              factory.create(invalidPayload, encoding);
78              fail("Creating a MuleMessageFactory from an invalid transport message must fail"); 
79          }
80          catch (MessageTypeNotSupportedException mtnse)
81          {
82              // this one was expected
83          }
84      }
85  
86      protected MuleMessageFactory createMuleMessageFactory()
87      {
88          MuleMessageFactory factory = doCreateMuleMessageFactory();
89          assertNotNull(factory);
90          return factory;
91      }
92  
93      protected Object getUnsupportedTransportMessage()
94      {
95          throw new AssertionError("Subclasses must properly implement this method");
96      }
97  
98      protected abstract MuleMessageFactory doCreateMuleMessageFactory();
99      
100     protected abstract Object getValidTransportMessage() throws Exception;
101 }