2 * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.openflowjava.protocol.impl.connection;
10 import java.io.IOException;
11 import java.net.InetAddress;
12 import java.util.concurrent.ExecutionException;
13 import java.util.concurrent.TimeUnit;
14 import java.util.concurrent.TimeoutException;
15 import org.junit.Assert;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.mockito.Mock;
19 import org.mockito.MockitoAnnotations;
20 import org.opendaylight.openflowjava.protocol.impl.core.UdpChannelInitializer;
21 import org.opendaylight.openflowjava.protocol.impl.core.UdpHandler;
23 import com.google.common.util.concurrent.ListenableFuture;
29 public class UdpHandlerTest {
30 @Mock UdpChannelInitializer udpChannelInitializerMock;
31 UdpHandler udpHandler;
36 public void startUp() {
37 MockitoAnnotations.initMocks(this);
41 * Test to create UdpHandler with empty address and zero port
42 * @throws InterruptedException
43 * @throws ExecutionException
47 public void testWithEmptyAddress() throws InterruptedException, ExecutionException, IOException {
48 udpHandler = new UdpHandler(null, 0);
49 udpHandler.setChannelInitializer(udpChannelInitializerMock);
50 Assert.assertTrue("Wrong - start server", startupServer());
52 Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS).booleanValue());
53 } catch (TimeoutException e) {
54 Assert.fail("Wrong - getIsOnlineFuture timed out");
56 Assert.assertFalse("Wrong - port has been set to zero", udpHandler.getPort() == 0);
61 * Test to create UdpHandler with fill address and given port
62 * @throws InterruptedException
63 * @throws ExecutionException
67 public void testWithAddressAndPort() throws InterruptedException, ExecutionException, IOException{
69 udpHandler = new UdpHandler(InetAddress.getLocalHost(), port);
70 udpHandler.setChannelInitializer(udpChannelInitializerMock);
71 Assert.assertTrue("Wrong - start server", startupServer());
73 Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS).booleanValue());
74 } catch (TimeoutException e) {
75 Assert.fail("Wrong - getIsOnlineFuture timed out");
77 Assert.assertEquals("Wrong - bad port number has been set", port, udpHandler.getPort());
81 private Boolean startupServer() throws InterruptedException, IOException, ExecutionException {
82 ListenableFuture<Boolean> online = udpHandler.getIsOnlineFuture();
84 (new Thread(udpHandler)).start();
86 while (online.isDone() != true && retry++ < 20) {
89 return online.isDone() ;
92 private void shutdownServer() throws InterruptedException, ExecutionException {
93 ListenableFuture<Boolean> shutdownRet = udpHandler.shutdown() ;
94 while ( shutdownRet.isDone() != true )
96 Assert.assertTrue("Wrong - shutdown failed", shutdownRet.get());