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