View Javadoc

1   /*
2    * $Id: AbstractMessageAdapterTestCase.java 7963 2007-08-21 08:53:15Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.tck.providers;
12  
13  import org.mule.impl.RequestContext;
14  import org.mule.tck.AbstractMuleTestCase;
15  import org.mule.umo.MessagingException;
16  import org.mule.umo.provider.MessageTypeNotSupportedException;
17  import org.mule.umo.provider.UMOMessageAdapter;
18  
19  public abstract class AbstractMessageAdapterTestCase extends AbstractMuleTestCase
20  {
21      protected void doSetUp() throws Exception
22      {
23          RequestContext.setEvent(getTestEvent("hello"));
24      }
25  
26      protected void doTearDown() throws Exception
27      {
28          RequestContext.clear();
29      }
30  
31      protected void doTestMessageEqualsPayload(Object message, Object payload) throws Exception
32      {
33          assertEquals(message, payload);
34      }
35  
36      public void testMessageRetrieval() throws Exception
37      {
38          Object message = getValidMessage();
39          UMOMessageAdapter adapter = createAdapter(message);
40  
41          doTestMessageEqualsPayload(message, adapter.getPayload());
42  
43          byte[] bytes = adapter.getPayloadAsBytes();
44          assertNotNull(bytes);
45  
46          String stringMessage = adapter.getPayloadAsString();
47          assertNotNull(stringMessage);
48  
49          assertNotNull(adapter.getPayload());
50  
51          try
52          {
53              adapter = createAdapter(getInvalidMessage());
54              fail("Message adapter should throw MessageTypeNotSupportedException if an invalid message is set");
55          }
56          catch (MessageTypeNotSupportedException e)
57          {
58              // expected
59          }
60      }
61  
62      public void testMessageProps() throws Exception
63      {
64          UMOMessageAdapter adapter = createAdapter(getValidMessage());
65  
66          adapter.setProperty("TestString", "Test1");
67          adapter.setProperty("TestLong", new Long(20000000));
68          adapter.setProperty("TestInt", new Integer(200000));
69          assertNotNull(adapter.getPropertyNames());
70  
71          Object prop = adapter.getProperty("TestString");
72          assertNotNull(prop);
73          assertEquals("Test1", prop);
74  
75          prop = adapter.getProperty("TestLong");
76          assertNotNull(prop);
77          assertEquals(new Long(20000000), prop);
78  
79          prop = adapter.getProperty("TestInt");
80          assertNotNull(prop);
81          assertEquals(new Integer(200000), prop);
82      }
83  
84      public Object getInvalidMessage()
85      {
86          return new InvalidMessage();
87      }
88  
89      public abstract Object getValidMessage() throws Exception;
90  
91      public abstract UMOMessageAdapter createAdapter(Object payload) throws MessagingException;
92  
93      final class InvalidMessage
94      {
95          public String toString()
96          {
97              return "invalid message";
98          }
99      }
100 
101 }