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;
8   
9   import org.mule.api.endpoint.InboundEndpoint;
10  import org.mule.api.service.Service;
11  import org.mule.api.transport.Connector;
12  import org.mule.tck.testmodels.fruit.Orange;
13  import org.mule.transport.AbstractConnectorTestCase;
14  
15  import java.net.DatagramPacket;
16  
17  import org.junit.Test;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.fail;
21  
22  public class UdpConnectorTestCase extends AbstractConnectorTestCase
23  {
24  
25      @Override
26      public Connector createConnector() throws Exception
27      {
28          UdpConnector c = new UdpConnector(muleContext);
29          c.setName("UdpConnector");
30          return c;
31      }
32  
33      @Override
34      public String getTestEndpointURI()
35      {
36          return "udp://localhost:61024";
37      }
38  
39      @Override
40      public Object getValidMessage() throws Exception
41      {
42          return new DatagramPacket("Hello".getBytes(), 5);
43      }
44  
45      @Test
46      public void testValidListener() throws Exception
47      {
48          Service service = getTestService("orange", Orange.class);
49          Connector connector = getConnector();
50  
51          InboundEndpoint endpoint2 = muleContext.getEndpointFactory()
52              .getInboundEndpoint("udp://localhost:3456");
53  
54          connector.registerListener(endpoint2, getSensingNullMessageProcessor(), service);
55          try
56          {
57              connector.registerListener(endpoint2, getSensingNullMessageProcessor(), service);
58              fail("cannot register on the same endpointUri");
59          }
60          catch (Exception e)
61          {
62              // expected
63          }
64      }
65  
66      @Test
67      public void testProperties() throws Exception
68      {
69          UdpConnector connector = (UdpConnector)this.getConnector();
70  
71          connector.setReceiveBufferSize(1024);
72          assertEquals(1024, connector.getReceiveBufferSize());
73          connector.setReceiveBufferSize(0);
74          assertEquals(UdpConnector.DEFAULT_BUFFER_SIZE, connector.getReceiveBufferSize());
75  
76          connector.setTimeout(-1);
77          assertEquals(UdpConnector.DEFAULT_SOCKET_TIMEOUT, connector.getTimeout());
78      }
79  
80  }