package net.dempsy.transport.tcp;

import static net.dempsy.util.Functional.uncheck;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.Test;

public class TcpUtilsTest {

    @Test
    public void testGetFirstNonLocalhostInetAddress() throws Exception {
        final InetAddress addr = TcpUtils.getFirstNonLocalhostInetAddress();
        assertNotNull(addr);
        assertFalse(addr.isLoopbackAddress());
    }

    @Test
    public void testGetFirstNonLocalhostInetAddressNamed() throws Exception {
        final List<NetworkInterface> ifs = Collections.list(TcpUtils.getInterfaces(null)).stream()
                .filter(nif -> !uncheck(() -> nif.isLoopback()))
                .collect(Collectors.toList());

        final NetworkInterface nif;
        final NetworkInterface notNif;

        if (ifs.size() > 1) {
            nif = ifs.get(1);
            notNif = ifs.get(0);
        } else if (ifs.size() == 1) {
            nif = ifs.get(0);
            notNif = null;
        } else {
            nif = null;
            notNif = null;
        }

        if (nif != null) { // otherwise we can do no testing.
            final String name = nif.getDisplayName();
            final List<InetAddress> expectedAddrs = Collections.list(nif.getInetAddresses());
            if (expectedAddrs.size() > 0) { // otherwise, we still can't really do anything without a lot of work
                final InetAddress expected = expectedAddrs.get(0);
                final InetAddress addr = TcpUtils.getFirstNonLocalhostInetAddress(name);
                assertEquals(expected, addr);

                if (notNif != null) {
                    final List<InetAddress> noneOfThese = Collections.list(notNif.getInetAddresses());
                    assertFalse(noneOfThese.contains(addr));
                }
            } else
                System.out.println("Can't test TcpUtils.getFirstNonLocalhostInetAddress(name)");
        } else
            System.out.println("Can't test TcpUtils.getFirstNonLocalhostInetAddress(name)");
    }
}

@@ -81,143 +81,8 @@
 eck;
-%0Aimport static org.junit.Assert.assertEquals;%0Aimport static org.junit.Assert.assertFalse;%0Aimport static org.junit.Assert.assertNotNull;
 %0A%0Aim
@@ -256,16 +256,152 @@
 .Test;%0A%0A
+import static org.junit.Assert.assertEquals;%0Aimport static org.junit.Assert.assertFalse;%0Aimport static org.junit.Assert.assertNotNull;%0A%0A
 public c
@@ -420,32 +420,30 @@
 sTest %7B%0A%0A   
- 
 @Test%0A
- 
    public vo
@@ -494,34 +494,32 @@
 ows Exception %7B%0A
-  
       final Inet
@@ -579,26 +579,24 @@
 ss();%0A      
-  
 assertNotNul
@@ -604,18 +604,16 @@
 (addr);%0A
-  
       as
@@ -652,21 +652,19 @@
 ());%0A   
- 
 %7D%0A%0A
- 
    @Test
@@ -667,17 +667,16 @@
 Test%0A   
- 
 public v
@@ -737,26 +737,24 @@
 Exception %7B%0A
-  
       final 
@@ -850,20 +850,16 @@
         
-   
- 
 .filter(
@@ -911,20 +911,16 @@
         
-    
 .collect
@@ -945,26 +945,24 @@
 ());%0A%0A      
- 
- 
 final Networ
@@ -977,18 +977,16 @@
 ce nif;%0A
-  
       fi
@@ -1021,21 +1021,18 @@
 %0A%0A      
-  
 if
- 
 (ifs.siz
@@ -1043,35 +1043,32 @@
 %3E 1) %7B%0A         
-   
 nif = ifs.get(1)
@@ -1069,19 +1069,16 @@
 get(1);%0A
-   
         
@@ -1101,26 +1101,24 @@
 t(0);%0A      
-  
 %7D else if (i
@@ -1114,17 +1114,16 @@
  else if
- 
 (ifs.siz
@@ -1130,27 +1130,24 @@
 e() == 1) %7B%0A
-   
          nif
@@ -1162,35 +1162,32 @@
 et(0);%0A         
-   
 notNif = null;%0A 
@@ -1187,26 +1187,24 @@
 null;%0A      
-  
 %7D else %7B%0A   
@@ -1205,27 +1205,24 @@
  %7B%0A         
- 
-  
 nif = null;%0A
@@ -1217,27 +1217,24 @@
 nif = null;%0A
-   
          not
@@ -1251,18 +1251,16 @@
 ;%0A      
- 
- 
 %7D%0A%0A     
@@ -1260,21 +1260,18 @@
 %0A%0A      
-  
 if
- 
 (nif != 
@@ -1318,27 +1318,24 @@
 g.%0A         
-  
- 
 final String
@@ -1364,19 +1364,16 @@
 Name();%0A
-   
         
@@ -1457,25 +1457,166 @@
 s())
-;%0A            if 
+.stream()%0A               .filter(ina -%3E ina.getAddress().length == 4) // only include IPV4 addresses%0A               .collect(Collectors.toList());%0A         if
 (exp
@@ -1714,36 +1714,32 @@
 ork%0A            
-    
 final InetAddres
@@ -1769,28 +1769,24 @@
 drs.get(0);%0A
-    
             
@@ -1866,28 +1866,24 @@
             
-    
 assertEquals
@@ -1917,15 +1917,10 @@
     
-    
 if
- 
 (not
@@ -1945,29 +1945,24 @@
             
-   
-  
 final List%3CI
@@ -2043,29 +2043,24 @@
             
-     
 assertFalse(
@@ -2100,20 +2100,16 @@
         
-    
 %7D%0A      
@@ -2107,38 +2107,31 @@
  %7D%0A         
- 
-  
 %7D else%0A
-    
             
@@ -2213,36 +2213,31 @@
 e)%22);%0A      
-  
 %7D else%0A
-   
          Sys
@@ -2314,16 +2314,15 @@
 ame)%22);%0A
- 
    %7D%0A%7D%0A

Fix a test failure on systems with IPV6 enabled.