Merge "mv log4j.xml from src/main => src/test"
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / core / TcpHandlerTest.java
index 6faad908f01cf60d798a67d6e263376561a0d583..8bbb0bc58558d4b4871de2fa13c97db2fe2bcaff 100644 (file)
@@ -9,17 +9,17 @@
 package org.opendaylight.openflowjava.protocol.impl.core;
 
 import static org.junit.Assert.assertEquals;
-import io.netty.channel.ChannelHandlerContext;
+import static org.junit.Assert.fail;
 
+import com.google.common.util.concurrent.ListenableFuture;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.unix.Errors;
 import java.io.IOException;
 import java.net.BindException;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.net.Socket;
 import java.util.concurrent.ExecutionException;
-
-import io.netty.channel.unix.Errors;
-import org.junit.Assert;
 import org.junit.Test;
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
@@ -27,15 +27,14 @@ import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHan
 import org.opendaylight.openflowjava.protocol.impl.deserialization.DeserializationFactory;
 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializationFactory;
 
-import com.google.common.util.concurrent.ListenableFuture;
-
 /**
+ * Unit tests for TcpHandler.
  *
  * @author jameshall
  */
 public class TcpHandlerTest {
 
-    private InetAddress serverAddress = InetAddress.getLoopbackAddress() ;
+    private final InetAddress serverAddress = InetAddress.getLoopbackAddress() ;
     @Mock ChannelHandlerContext mockChHndlrCtx ;
     @Mock TcpChannelInitializer mockChannelInitializer;
     @Mock SwitchConnectionHandler mockSwitchConnHndler ;
@@ -45,22 +44,18 @@ public class TcpHandlerTest {
     TcpHandler tcpHandler ;
 
     /**
-     * Initialize mocks
+     * Initialize mocks.
      */
     public TcpHandlerTest() {
         MockitoAnnotations.initMocks(this);
     }
 
     /**
-     * Test run with null address set
-     * @throws IOException
-     * @throws InterruptedException
-     * @throws ExecutionException
+     * Test run with null address set.
      */
     @Test
     public void testRunWithNullAddress() throws IOException, InterruptedException, ExecutionException  {
-
-        tcpHandler = new TcpHandler(null, 0);
+        tcpHandler = new TcpHandler(null, 0, () -> { });
         tcpHandler.setChannelInitializer(mockChannelInitializer);
 
         assertEquals("failed to start server", true, startupServer(false)) ;
@@ -69,15 +64,11 @@ public class TcpHandlerTest {
     }
 
     /**
-     * Test run with null address set on Epoll native transport
-     * @throws IOException
-     * @throws InterruptedException
-     * @throws ExecutionException
+     * Test run with null address set on Epoll native transport.
      */
     @Test
     public void testRunWithNullAddressOnEpoll() throws IOException, InterruptedException, ExecutionException  {
-
-        tcpHandler = new TcpHandler(null, 0);
+        tcpHandler = new TcpHandler(null, 0, () -> { });
         tcpHandler.setChannelInitializer(mockChannelInitializer);
 
         //Use Epoll native transport
@@ -87,15 +78,11 @@ public class TcpHandlerTest {
     }
 
     /**
-     * Test run with address set
-     * @throws IOException
-     * @throws InterruptedException
-     * @throws ExecutionException
+     * Test run with address set.
      */
     @Test
     public void testRunWithAddress() throws IOException, InterruptedException, ExecutionException  {
-
-        tcpHandler = new TcpHandler(serverAddress, 0);
+        tcpHandler = new TcpHandler(serverAddress, 0, () -> { });
         tcpHandler.setChannelInitializer(mockChannelInitializer);
 
         assertEquals("failed to start server", true, startupServer(false)) ;
@@ -104,15 +91,11 @@ public class TcpHandlerTest {
     }
 
     /**
-     * Test run with address set on Epoll native transport
-     * @throws IOException
-     * @throws InterruptedException
-     * @throws ExecutionException
+     * Test run with address set on Epoll native transport.
      */
     @Test
     public void testRunWithAddressOnEpoll() throws IOException, InterruptedException, ExecutionException  {
-
-        tcpHandler = new TcpHandler(serverAddress, 0);
+        tcpHandler = new TcpHandler(serverAddress, 0, () -> { });
         tcpHandler.setChannelInitializer(mockChannelInitializer);
 
         //Use Epoll native transport
@@ -122,21 +105,18 @@ public class TcpHandlerTest {
     }
 
     /**
-     * Test run with encryption
-     * @throws InterruptedException
-     * @throws IOException
-     * @throws ExecutionException
+     * Test run with encryption.
      */
     @Test
     public void testRunWithEncryption() throws InterruptedException, IOException, ExecutionException {
         int serverPort = 28001;
-        tcpHandler = new TcpHandler(serverAddress, serverPort);
+        tcpHandler = new TcpHandler(serverAddress, serverPort, () -> { });
         tcpHandler.setChannelInitializer(mockChannelInitializer);
 
-        assertEquals( "failed to start server", true, startupServer(false));
-        assertEquals( "wrong connection count", 0, tcpHandler.getNumberOfConnections());
-        assertEquals( "wrong port", serverPort, tcpHandler.getPort());
-        assertEquals( "wrong address", serverAddress.getHostAddress(), tcpHandler.getAddress());
+        assertEquals("failed to start server", true, startupServer(false));
+        assertEquals("wrong connection count", 0, tcpHandler.getNumberOfConnections());
+        assertEquals("wrong port", serverPort, tcpHandler.getPort());
+        assertEquals("wrong address", serverAddress.getHostAddress(), tcpHandler.getAddress());
 
         assertEquals("failed to connect client", true, clientConnection(tcpHandler.getPort()));
 
@@ -144,22 +124,19 @@ public class TcpHandlerTest {
     }
 
     /**
-     * Test run with encryption on Epoll native transport
-     * @throws InterruptedException
-     * @throws IOException
-     * @throws ExecutionException
+     * Test run with encryption on Epoll native transport.
      */
     @Test
     public void testRunWithEncryptionOnEpoll() throws InterruptedException, IOException, ExecutionException {
         int serverPort = 28001;
-        tcpHandler = new TcpHandler(serverAddress, serverPort);
+        tcpHandler = new TcpHandler(serverAddress, serverPort, () -> { });
         tcpHandler.setChannelInitializer(mockChannelInitializer);
 
         //Use Epoll native transport
-        assertEquals( "failed to start server", true, startupServer(true));
-        assertEquals( "wrong connection count", 0, tcpHandler.getNumberOfConnections());
-        assertEquals( "wrong port", serverPort, tcpHandler.getPort());
-        assertEquals( "wrong address", serverAddress.getHostAddress(), tcpHandler.getAddress());
+        assertEquals("failed to start server", true, startupServer(true));
+        assertEquals("wrong connection count", 0, tcpHandler.getNumberOfConnections());
+        assertEquals("wrong port", serverPort, tcpHandler.getPort());
+        assertEquals("wrong address", serverAddress.getHostAddress(), tcpHandler.getAddress());
 
         assertEquals("failed to connect client", true, clientConnection(tcpHandler.getPort()));
 
@@ -167,77 +144,59 @@ public class TcpHandlerTest {
     }
 
     /**
-     * Test run on already used port
-     * @throws IOException
+     * Test run on already used port.
      */
-    @Test
+    @Test(expected = BindException.class)
     public void testSocketAlreadyInUse() throws IOException {
         int serverPort = 28001;
         Socket firstBinder = new Socket();
-        boolean exceptionThrown = false;
+
         try {
             firstBinder.bind(new InetSocketAddress(serverAddress, serverPort));
-        } catch (Exception e) {
-            Assert.fail("Test precondition failed - not able to bind socket to port " + serverPort);
-        }
-        try {
-            tcpHandler = new TcpHandler(serverAddress, serverPort);
+            tcpHandler = new TcpHandler(serverAddress, serverPort, () -> { });
             tcpHandler.setChannelInitializer(mockChannelInitializer);
             tcpHandler.initiateEventLoopGroups(null, false);
             tcpHandler.run();
-        } catch (Exception e) {
-            if (e instanceof BindException) {
-                exceptionThrown = true;
-            }
+        } finally {
+            firstBinder.close();
         }
-        firstBinder.close();
-        Assert.assertTrue("Expected BindException has not been thrown", exceptionThrown == true);
     }
 
     /**
-     * Test run on already used port
-     * @throws IOException
+     * Test run on already used port.
      */
     @Test
     public void testSocketAlreadyInUseOnEpoll() throws IOException {
         int serverPort = 28001;
         Socket firstBinder = new Socket();
-        boolean exceptionThrown = false;
+
         try {
             firstBinder.bind(new InetSocketAddress(serverAddress, serverPort));
-        } catch (Exception e) {
-            Assert.fail("Test precondition failed - not able to bind socket to port " + serverPort);
-        }
-        try {
-            tcpHandler = new TcpHandler(serverAddress, serverPort);
+
+            tcpHandler = new TcpHandler(serverAddress, serverPort, () -> { });
             tcpHandler.setChannelInitializer(mockChannelInitializer);
             //Use Epoll native transport
             tcpHandler.initiateEventLoopGroups(null, true);
             tcpHandler.run();
-        } catch (Exception e) {
-            if (e instanceof BindException || e instanceof Errors.NativeIoException) {
-                exceptionThrown = true;
-            }
+            fail("Expected BindException or Errors.NativeIoException");
+        } catch (BindException | Errors.NativeIoException e) {
+            // expected
+        } finally {
+            firstBinder.close();
         }
-        firstBinder.close();
-        Assert.assertTrue("Expected BindException has not been thrown", exceptionThrown == true);
     }
 
     /**
-     * Trigger the server shutdown and wait 2 seconds for completion
+     * Trigger the server shutdown and wait 2 seconds for completion.
      */
     private void shutdownServer() throws InterruptedException, ExecutionException {
         ListenableFuture<Boolean> shutdownRet = tcpHandler.shutdown() ;
-        while ( shutdownRet.isDone() != true )
+        while (shutdownRet.isDone() != true) {
             Thread.sleep(100) ;
+        }
         assertEquals("shutdown failed", true, shutdownRet.get());
     }
 
-    /**
-     * @throws InterruptedException
-     * @throws IOException
-     * @throws ExecutionException
-     */
     private Boolean startupServer(boolean isEpollEnabled) throws InterruptedException, IOException, ExecutionException {
         ListenableFuture<Boolean> online = tcpHandler.getIsOnlineFuture();
         /**
@@ -245,19 +204,17 @@ public class TcpHandlerTest {
          * Else use Nio based transport.
          */
         tcpHandler.initiateEventLoopGroups(null, isEpollEnabled);
-            (new Thread(tcpHandler)).start();
-            int retry = 0;
-            while (online.isDone() != true && retry++ < 20) {
-                Thread.sleep(100);
-            }
+        new Thread(tcpHandler).start();
+        int retry = 0;
+        while (online.isDone() != true && retry++ < 20) {
+            Thread.sleep(100);
+        }
         return online.isDone() ;
     }
-    /**
-     * @throws IOException
-     */
+
     private static Boolean clientConnection(int port) throws IOException {
         // Connect, and disconnect
-        Socket socket = new Socket(InetAddress.getLoopbackAddress(), port );
+        Socket socket = new Socket(InetAddress.getLoopbackAddress(), port);
         Boolean result = socket.isConnected();
         socket.close() ;
         return result ;