View Javadoc

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