View Javadoc

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