View Javadoc

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