View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.udp.functional;
8   
9   import org.mule.tck.junit4.FunctionalTestCase;
10  import org.mule.transport.ConfigurableKeyedObjectPool;
11  
12  import java.io.ByteArrayInputStream;
13  import java.io.ByteArrayOutputStream;
14  import java.io.DataInputStream;
15  import java.io.DataOutputStream;
16  import java.io.IOException;
17  import java.net.DatagramPacket;
18  import java.net.DatagramSocket;
19  import java.net.InetAddress;
20  
21  import edu.emory.mathcs.backport.java.util.Arrays;
22  import org.junit.Test;
23  
24  import static org.junit.Assert.assertEquals;
25  
26  public class UdpDynamicEPTestCase extends FunctionalTestCase
27  {
28  
29      @Override
30      protected String getConfigResources()
31      {
32          return "udp-roundtrip-dynamicep-test-config.xml";
33      }
34  
35      @Test
36      public void testSendAndReceiveUDP() throws IOException
37      {
38          int outPort = 61000;
39          DatagramSocket socket = null;
40          try
41          {
42              socket = new DatagramSocket();
43  
44              // prepare outgoing packet
45                  ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
46                  DataOutputStream dataOut = new DataOutputStream(bytesOut);
47                  dataOut.writeFloat(1.0f);
48                  dataOut.writeFloat(2.0f);
49                  byte[] bytesToSend = bytesOut.toByteArray();
50  
51                  DatagramPacket outboundPacket = new DatagramPacket(bytesToSend, bytesToSend.length,
52                      InetAddress.getLocalHost(), outPort);
53                  socket.send(outboundPacket);
54  
55              // receive whatever came back
56              byte[] receiveBuffer = new byte[bytesToSend.length];
57              DatagramPacket inboundPacket = new DatagramPacket(receiveBuffer, receiveBuffer.length);
58              socket.receive(inboundPacket);
59  
60              // compare byte buffers as strings so we get to see the diff
61              assertEquals(Arrays.toString(outboundPacket.getData()),
62                  Arrays.toString(inboundPacket.getData()));
63  
64              // make sure the contents are really the same
65              ByteArrayInputStream bytesIn = new ByteArrayInputStream(inboundPacket.getData());
66              DataInputStream dataIn = new DataInputStream(bytesIn);
67              // the delta is only here to make JUnit happy
68              assertEquals(1.0f, dataIn.readFloat(), 0.1f);
69              assertEquals(2.0f, dataIn.readFloat(), 0.1f);
70          }
71          finally
72          {
73              try
74              {
75                  socket.close();
76                  socket = null;
77              }
78              catch (Exception e)
79              {
80                  e.printStackTrace();
81              }
82          }
83          
84          CustomUdpConnector udpConnector = (CustomUdpConnector) muleContext.getRegistry().lookupConnector("connector.udp.0");
85          ConfigurableKeyedObjectPool pool = udpConnector.getDispatchers();
86          assertEquals(0, pool.getNumActive());
87      }
88  }