1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.udp.functional;
12
13 import static org.junit.Assert.assertEquals;
14
15 import java.io.ByteArrayInputStream;
16 import java.io.ByteArrayOutputStream;
17 import java.io.DataInputStream;
18 import java.io.DataOutputStream;
19 import java.io.IOException;
20 import java.net.DatagramPacket;
21 import java.net.DatagramSocket;
22 import java.net.InetAddress;
23 import java.util.Arrays;
24 import java.util.Collection;
25
26 import org.junit.Test;
27 import org.junit.runners.Parameterized.Parameters;
28 import org.mule.tck.AbstractServiceAndFlowTestCase;
29 import org.mule.transport.ConfigurableKeyedObjectPool;
30
31 public class UdpDynamicEPTestCase extends AbstractServiceAndFlowTestCase
32 {
33 public UdpDynamicEPTestCase(ConfigVariant variant, String configResources)
34 {
35 super(variant, configResources);
36 }
37
38 @Parameters
39 public static Collection<Object[]> parameters()
40 {
41 return Arrays.asList(new Object[][]{
42 {ConfigVariant.SERVICE, "udp-roundtrip-dynamicep-test-config-service.xml"},
43 {ConfigVariant.FLOW, "udp-roundtrip-dynamicep-test-config-flow.xml"}
44 });
45 }
46
47 @Test
48 public void testSendAndReceiveUDP() throws IOException
49 {
50 int outPort = 61000;
51 DatagramSocket socket = null;
52 try
53 {
54 socket = new DatagramSocket();
55
56
57 ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
58 DataOutputStream dataOut = new DataOutputStream(bytesOut);
59 dataOut.writeFloat(1.0f);
60 dataOut.writeFloat(2.0f);
61 byte[] bytesToSend = bytesOut.toByteArray();
62
63 DatagramPacket outboundPacket = new DatagramPacket(bytesToSend, bytesToSend.length,
64 InetAddress.getLocalHost(), outPort);
65 socket.send(outboundPacket);
66
67
68 byte[] receiveBuffer = new byte[bytesToSend.length];
69 DatagramPacket inboundPacket = new DatagramPacket(receiveBuffer, receiveBuffer.length);
70 socket.receive(inboundPacket);
71
72
73 assertEquals(Arrays.toString(outboundPacket.getData()),
74 Arrays.toString(inboundPacket.getData()));
75
76
77 ByteArrayInputStream bytesIn = new ByteArrayInputStream(inboundPacket.getData());
78 DataInputStream dataIn = new DataInputStream(bytesIn);
79
80 assertEquals(1.0f, dataIn.readFloat(), 0.1f);
81 assertEquals(2.0f, dataIn.readFloat(), 0.1f);
82 }
83 finally
84 {
85 try
86 {
87 if (socket != null)
88 {
89 socket.close();
90 }
91 socket = null;
92 }
93 catch (Exception e)
94 {
95 e.printStackTrace();
96 }
97 }
98
99 CustomUdpConnector udpConnector = (CustomUdpConnector) muleContext.getRegistry().lookupConnector("connector.udp.0");
100 ConfigurableKeyedObjectPool pool = udpConnector.getDispatchers();
101 assertEquals(0, pool.getNumActive());
102 }
103 }