View Javadoc

1   /*
2    * $Id: UdpRoundTripTestCase.java 23104 2011-10-05 14:14:25Z pablo.kraan $
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.AbstractServiceAndFlowTestCase;
14  import org.mule.tck.junit4.rule.DynamicPort;
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  import java.util.Arrays;
25  import java.util.Collection;
26  
27  import org.junit.Rule;
28  import org.junit.Test;
29  import org.junit.runners.Parameterized.Parameters;
30  
31  import static org.junit.Assert.assertEquals;
32  
33  public class UdpRoundTripTestCase extends AbstractServiceAndFlowTestCase
34  {
35  
36      @Rule
37      public DynamicPort outPort = new DynamicPort("outPort");
38  
39      @Rule
40      public DynamicPort inPort = new DynamicPort("inPort");
41  
42      public UdpRoundTripTestCase(ConfigVariant variant, String configResources)
43      {
44          super(variant, configResources);
45      }
46  
47      @Parameters
48      public static Collection<Object[]> parameters()
49      {
50          return Arrays.asList(new Object[][]{
51              {ConfigVariant.SERVICE, "udp-roundtrip-test-config-service.xml"},
52              {ConfigVariant.FLOW, "udp-roundtrip-test-config-flow.xml"}
53          });
54      }
55  
56      @Test
57      public void testSendAndReceiveUDP() throws IOException
58      {
59          // the socket we talk to
60          DatagramSocket socket = new DatagramSocket(inPort.getNumber(), InetAddress.getLocalHost());
61  
62          // prepare outgoing packet
63          ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
64          DataOutputStream dataOut = new DataOutputStream(bytesOut);
65          dataOut.writeFloat(1.0f);
66          dataOut.writeFloat(2.0f);
67          byte[] bytesToSend = bytesOut.toByteArray();
68  
69          DatagramPacket outboundPacket = new DatagramPacket(bytesToSend, bytesToSend.length,
70              InetAddress.getLocalHost(), outPort.getNumber());
71          socket.send(outboundPacket);
72  
73          // receive whatever came back
74          byte[] receiveBuffer = new byte[bytesToSend.length];
75          DatagramPacket inboundPacket = new DatagramPacket(receiveBuffer, receiveBuffer.length);
76          socket.receive(inboundPacket);
77  
78          // compare byte buffers as strings so we get to see the diff
79          assertEquals(Arrays.toString(outboundPacket.getData()), Arrays.toString(inboundPacket.getData()));
80  
81          // make sure the contents are really the same
82          ByteArrayInputStream bytesIn = new ByteArrayInputStream(inboundPacket.getData());
83          DataInputStream dataIn = new DataInputStream(bytesIn);
84          // the delta is only here to make JUnit happy
85          assertEquals(1.0f, dataIn.readFloat(), 0.1f);
86          assertEquals(2.0f, dataIn.readFloat(), 0.1f);
87      }
88  }