1   /*
2    * $Id: AbstractMessageAdapterTestCase.java 10787 2008-02-12 18:51:50Z dfeist $
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.transport;
12  
13  import org.mule.DefaultMuleMessage;
14  import org.mule.RequestContext;
15  import org.mule.api.MessagingException;
16  import org.mule.api.MuleMessage;
17  import org.mule.api.transport.MessageAdapter;
18  import org.mule.tck.AbstractMuleTestCase;
19  
20  public abstract class AbstractMessageAdapterTestCase extends AbstractMuleTestCase
21  {
22      protected void doSetUp() throws Exception
23      {
24          RequestContext.setEvent(getTestEvent("hello"));
25      }
26  
27      protected void doTearDown() throws Exception
28      {
29          RequestContext.clear();
30      }
31  
32      protected void doTestMessageEqualsPayload(Object message, Object payload) throws Exception
33      {
34          assertEquals(message, payload);
35      }
36  
37      public void testMessageRetrieval() throws Exception
38      {
39          Object message = getValidMessage();
40          MessageAdapter adapter = createAdapter(message);
41          MuleMessage muleMessage = new DefaultMuleMessage(adapter);
42  
43          doTestMessageEqualsPayload(message, adapter.getPayload());
44  
45          byte[] bytes = muleMessage.getPayloadAsBytes();
46          assertNotNull(bytes);
47  
48          String stringMessage = muleMessage.getPayloadAsString();
49          assertNotNull(stringMessage);
50  
51          assertNotNull(adapter.getPayload());
52      }
53  
54      public void testMessageProps() throws Exception
55      {
56          MessageAdapter adapter = createAdapter(getValidMessage());
57  
58          adapter.setProperty("TestString", "Test1");
59          adapter.setProperty("TestLong", new Long(20000000));
60          adapter.setProperty("TestInt", new Integer(200000));
61          assertNotNull(adapter.getPropertyNames());
62  
63          Object prop = adapter.getProperty("TestString");
64          assertNotNull(prop);
65          assertEquals("Test1", prop);
66  
67          prop = adapter.getProperty("TestLong");
68          assertNotNull(prop);
69          assertEquals(new Long(20000000), prop);
70  
71          prop = adapter.getProperty("TestInt");
72          assertNotNull(prop);
73          assertEquals(new Integer(200000), prop);
74      }
75  
76      public Object getInvalidMessage()
77      {
78          return new InvalidMessage();
79      }
80  
81      public abstract Object getValidMessage() throws Exception;
82  
83      public abstract MessageAdapter createAdapter(Object payload) throws MessagingException;
84  
85      final class InvalidMessage
86      {
87          public String toString()
88          {
89              return "invalid message";
90          }
91      }
92  
93  }