View Javadoc
1   package org.apache.commons.jcs3.utils.discovery;
2   
3   import java.util.ArrayList;
4   
5   import org.apache.commons.jcs3.utils.discovery.UDPDiscoveryMessage.BroadcastType;
6   import org.apache.commons.jcs3.utils.serialization.StandardSerializer;
7   
8   /*
9    * Licensed to the Apache Software Foundation (ASF) under one
10   * or more contributor license agreements.  See the NOTICE file
11   * distributed with this work for additional information
12   * regarding copyright ownership.  The ASF licenses this file
13   * to you under the Apache License, Version 2.0 (the
14   * "License"); you may not use this file except in compliance
15   * with the License.  You may obtain a copy of the License at
16   *
17   *   http://www.apache.org/licenses/LICENSE-2.0
18   *
19   * Unless required by applicable law or agreed to in writing,
20   * software distributed under the License is distributed on an
21   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
22   * KIND, either express or implied.  See the License for the
23   * specific language governing permissions and limitations
24   * under the License.
25   */
26  
27  import junit.framework.TestCase;
28  
29  /**
30   * Tests for the sender.
31   */
32  public class UDPDiscoverySenderUnitTest
33      extends TestCase
34  {
35      /** multicast address to send/receive on */
36      private static final String ADDRESS = "228.4.5.9";
37  
38      /** multicast address to send/receive on */
39      private static final int PORT = 5556;
40  
41      /** imaginary host address for sending */
42      private static final String SENDING_HOST = "imaginary host address";
43  
44      /** imaginary port for sending */
45      private static final int SENDING_PORT = 1;
46  
47      /** receiver instance for tests */
48      private UDPDiscoveryReceiver receiver;
49  
50      /** sender instance for tests */
51      private UDPDiscoverySender sender;
52  
53      /**
54       * Set up the receiver. Maybe better to just code sockets here? Set up the sender for sending
55       * the message.
56       * <p>
57       * @throws Exception on error
58       */
59      @Override
60      protected void setUp()
61          throws Exception
62      {
63          super.setUp();
64  
65          receiver = new UDPDiscoveryReceiver( null, null, ADDRESS, PORT );
66          receiver.setSerializer(new StandardSerializer());
67          final Thread t = new Thread( receiver );
68          t.start();
69  
70          sender = new UDPDiscoverySender(null, ADDRESS, PORT, 1, new StandardSerializer());
71      }
72  
73      /**
74       * Kill off the sender and receiver.
75       * <p>
76       * @throws Exception on error
77       */
78      @Override
79      protected void tearDown()
80          throws Exception
81      {
82          receiver.shutdown();
83          sender.close();
84          super.tearDown();
85      }
86  
87      /**
88       * Test sending a live messages.
89       * <p>
90       * @throws Exception on error
91       */
92      public void testPassiveBroadcast()
93          throws Exception
94      {
95          // SETUP
96          final ArrayList<String> cacheNames = new ArrayList<>();
97  
98          // DO WORK
99          sender.passiveBroadcast( SENDING_HOST, SENDING_PORT, cacheNames, 1L );
100 
101         // VERIFY
102         // grab the sent message
103         final Object obj = receiver.waitForMessage() ;
104 
105         assertTrue( "unexpected crap received", obj instanceof UDPDiscoveryMessage );
106 
107         final UDPDiscoveryMessage msg = (UDPDiscoveryMessage) obj;
108         // disabled test because of JCS-89
109         // assertEquals( "wrong host", SENDING_HOST, msg.getHost() );
110         assertEquals( "wrong port", SENDING_PORT, msg.getPort() );
111         assertEquals( "wrong message type", BroadcastType.PASSIVE, msg.getMessageType() );
112     }
113 
114     /**
115      * Test sending a remove broadcast.
116      * <p>
117      * @throws Exception on error
118      */
119     public void testRemoveBroadcast()
120         throws Exception
121     {
122         // SETUP
123         final ArrayList<String> cacheNames = new ArrayList<>();
124 
125         // DO WORK
126         sender.removeBroadcast( SENDING_HOST, SENDING_PORT, cacheNames, 1L );
127 
128         // VERIFY
129         // grab the sent message
130         final Object obj = receiver.waitForMessage();
131 
132         assertTrue( "unexpected crap received", obj instanceof UDPDiscoveryMessage );
133 
134         final UDPDiscoveryMessage msg = (UDPDiscoveryMessage) obj;
135         // disabled test because of JCS-89
136         // assertEquals( "wrong host", SENDING_HOST, msg.getHost() );
137         assertEquals( "wrong port", SENDING_PORT, msg.getPort() );
138         assertEquals( "wrong message type", BroadcastType.REMOVE, msg.getMessageType() );
139     }
140 
141     /**
142      * Test sending a request broadcast.
143      * <p>
144      * @throws Exception on error
145      */
146     public void testRequestBroadcast()
147         throws Exception
148     {
149         // DO WORK
150         sender.requestBroadcast();
151 
152         // VERIFY
153         // grab the sent message
154         final Object obj = receiver.waitForMessage();
155 
156         assertTrue( "unexpected crap received", obj instanceof UDPDiscoveryMessage );
157 
158         final UDPDiscoveryMessage msg = (UDPDiscoveryMessage) obj;
159         assertEquals( "wrong message type", BroadcastType.REQUEST, msg.getMessageType() );
160     }
161 }