View Javadoc

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