View Javadoc

1   /*
2    * $Id: UdpMuleMessageFactoryTestCase.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.udp;
12  
13  import org.mule.api.MuleMessage;
14  import org.mule.api.transport.MuleMessageFactory;
15  import org.mule.transport.AbstractMuleMessageFactoryTestCase;
16  
17  import java.net.DatagramPacket;
18  import java.net.InetAddress;
19  import java.util.Arrays;
20  
21  public class UdpMuleMessageFactoryTestCase extends AbstractMuleMessageFactoryTestCase
22  {
23      private static final int PORT = 4242;
24  
25      @Override
26      protected MuleMessageFactory doCreateMuleMessageFactory()
27      {
28          return new UdpMuleMessageFactory(muleContext);
29      }
30  
31      @Override
32      protected Object getValidTransportMessage() throws Exception
33      {
34          InetAddress address = InetAddress.getLocalHost();
35          return new DatagramPacket(TEST_MESSAGE.getBytes(), TEST_MESSAGE.length(), address, PORT);
36      }
37  
38      @Override
39      protected Object getUnsupportedTransportMessage()
40      {
41          return "this is an invalid payload for UdpMuleMessageFactory";
42      }
43  
44      @Override
45      public void testValidPayload() throws Exception
46      {
47          MuleMessageFactory factory = createMuleMessageFactory();
48          
49          MuleMessage message = factory.create(getValidTransportMessage(), encoding);
50          assertNotNull(message);
51          assertPayload(message);
52          assertEquals(PORT, message.getOutboundProperty(UdpConnector.PORT_PROPERTY));
53          assertNotNull(message.getOutboundProperty(UdpConnector.ADDRESS_PROPERTY));
54      }
55      
56      private void assertPayload(MuleMessage message)
57      {
58          byte[] expected = TEST_MESSAGE.getBytes();
59          byte[] result = (byte[]) message.getPayload();
60          assertTrue(Arrays.equals(expected, result));
61      }
62  }