1
2
3
4
5
6
7
8
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 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertNotNull;
23 import static org.junit.Assert.assertTrue;
24
25 public class UdpMuleMessageFactoryTestCase extends AbstractMuleMessageFactoryTestCase
26 {
27 private static final int PORT = 4242;
28
29 @Override
30 protected MuleMessageFactory doCreateMuleMessageFactory()
31 {
32 return new UdpMuleMessageFactory(muleContext);
33 }
34
35 @Override
36 protected Object getValidTransportMessage() throws Exception
37 {
38 InetAddress address = InetAddress.getLocalHost();
39 return new DatagramPacket(TEST_MESSAGE.getBytes(), TEST_MESSAGE.length(), address, PORT);
40 }
41
42 @Override
43 protected Object getUnsupportedTransportMessage()
44 {
45 return "this is an invalid payload for UdpMuleMessageFactory";
46 }
47
48 @Override
49 public void testValidPayload() throws Exception
50 {
51 MuleMessageFactory factory = createMuleMessageFactory();
52
53 MuleMessage message = factory.create(getValidTransportMessage(), encoding);
54 assertNotNull(message);
55 assertPayload(message);
56 assertEquals(PORT, message.getOutboundProperty(UdpConnector.PORT_PROPERTY));
57 assertNotNull(message.getOutboundProperty(UdpConnector.ADDRESS_PROPERTY));
58 }
59
60 private void assertPayload(MuleMessage message)
61 {
62 byte[] expected = TEST_MESSAGE.getBytes();
63 byte[] result = (byte[]) message.getPayload();
64 assertTrue(Arrays.equals(expected, result));
65 }
66 }