View Javadoc

1   /*
2    * $Id: UdpRoundTripTestCase.java 10489 2008-01-23 17:53:38Z dfeist $
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.functional;
12  
13  import org.mule.tck.FunctionalTestCase;
14  import org.mule.transport.ConfigurableKeyedObjectPool;
15  
16  import java.io.ByteArrayInputStream;
17  import java.io.ByteArrayOutputStream;
18  import java.io.DataInputStream;
19  import java.io.DataOutputStream;
20  import java.io.IOException;
21  import java.net.DatagramPacket;
22  import java.net.DatagramSocket;
23  import java.net.InetAddress;
24  
25  import edu.emory.mathcs.backport.java.util.Arrays;
26  
27  public class UdpDynamicEPTestCase extends FunctionalTestCase
28  {
29  
30      protected String getConfigResources()
31      {
32          return "udp-roundtrip-dynamicep-test-config.xml";
33      }
34  
35      public void testSendAndReceiveUDP() throws IOException
36      {
37          int outPort = 61000;
38          DatagramSocket socket = null;
39          try
40          {
41              socket = new DatagramSocket();
42  
43              // prepare outgoing packet
44                  ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
45                  DataOutputStream dataOut = new DataOutputStream(bytesOut);
46                  dataOut.writeFloat(1.0f);
47                  dataOut.writeFloat(2.0f);
48                  byte[] bytesToSend = bytesOut.toByteArray();
49  
50                  DatagramPacket outboundPacket = new DatagramPacket(bytesToSend, bytesToSend.length,
51                      InetAddress.getLocalHost(), outPort);
52                  socket.send(outboundPacket);
53  
54              // receive whatever came back
55              byte[] receiveBuffer = new byte[bytesToSend.length];
56              DatagramPacket inboundPacket = new DatagramPacket(receiveBuffer, receiveBuffer.length);
57              socket.receive(inboundPacket);
58  
59              // compare byte buffers as strings so we get to see the diff
60              assertEquals(Arrays.toString(outboundPacket.getData()),
61                  Arrays.toString(inboundPacket.getData()));
62  
63              // make sure the contents are really the same
64              ByteArrayInputStream bytesIn = new ByteArrayInputStream(inboundPacket.getData());
65              DataInputStream dataIn = new DataInputStream(bytesIn);
66              // the delta is only here to make JUnit happy
67              assertEquals(1.0f, dataIn.readFloat(), 0.1f);
68              assertEquals(2.0f, dataIn.readFloat(), 0.1f);
69          }
70          finally
71          {
72              try
73              {
74                  socket.close();
75                  socket = null;
76              }
77              catch (Exception e)
78              {
79                  e.printStackTrace();
80              }
81          }
82          
83          CustomUdpConnector udpConnector = (CustomUdpConnector) muleContext.getRegistry().lookupConnector("connector.udp.0");
84          ConfigurableKeyedObjectPool pool = udpConnector.getDispatchers();
85          assertEquals(0, pool.getNumActive());
86      }
87  }