Util { public static InetAddress getLocalHost() { try { return doGetLocalHost(); } catch (Exception e) { throw new SamzaException("Error while getting localhost", e); } }  static String envVarEscape(String str); static String getSamzaVersion(); static String getTaskClassVersion(Config config); static InetAddress getLocalHost();  }
@Test public void testGetLocalHostNotLoopbackAddress() throws UnknownHostException { mockStatic(InetAddress.class); InetAddress inetAddressLocalHost = mock(InetAddress.class); when(inetAddressLocalHost.isLoopbackAddress()).thenReturn(false); when(InetAddress.getLocalHost()).thenReturn(inetAddressLocalHost); assertEquals(inetAddressLocalHost, Util.getLocalHost()); }
@Test public void testGetLocalHostLoopbackAddressNoExternalAddressFound() throws Exception { mockStatic(InetAddress.class, NetworkInterface.class); InetAddress inetAddressLocalHost = mock(InetAddress.class); when(inetAddressLocalHost.isLoopbackAddress()).thenReturn(true); when(InetAddress.getLocalHost()).thenReturn(inetAddressLocalHost); InetAddress linkLocalAddress = mock(InetAddress.class); when(linkLocalAddress.isLinkLocalAddress()).thenReturn(true); InetAddress loopbackAddress = mock(InetAddress.class); when(loopbackAddress.isLinkLocalAddress()).thenReturn(false); when(loopbackAddress.isLoopbackAddress()).thenReturn(true); NetworkInterface networkInterface0 = mock(NetworkInterface.class); when(networkInterface0.getInetAddresses()).thenReturn( Collections.enumeration(Arrays.asList(linkLocalAddress, loopbackAddress))); NetworkInterface networkInterface1 = mock(NetworkInterface.class); when(networkInterface1.getInetAddresses()).thenReturn( Collections.enumeration(Collections.singletonList(loopbackAddress))); when(NetworkInterface.getNetworkInterfaces()).thenReturn( Collections.enumeration(Arrays.asList(networkInterface0, networkInterface1))); assertEquals(inetAddressLocalHost, Util.getLocalHost()); }
@Test public void testGetLocalHostExternalInet4Address() throws Exception { mockStatic(InetAddress.class, NetworkInterface.class); InetAddress inetAddressLocalHost = mock(InetAddress.class); when(inetAddressLocalHost.isLoopbackAddress()).thenReturn(true); when(InetAddress.getLocalHost()).thenReturn(inetAddressLocalHost); InetAddress linkLocalAddress = mock(InetAddress.class); when(linkLocalAddress.isLinkLocalAddress()).thenReturn(true); Inet4Address externalInet4Address = mock(Inet4Address.class); when(externalInet4Address.isLinkLocalAddress()).thenReturn(false); when(externalInet4Address.isLoopbackAddress()).thenReturn(false); byte[] externalInet4AddressBytes = new byte[]{0, 1, 2, 3}; when(externalInet4Address.getAddress()).thenReturn(externalInet4AddressBytes); InetAddress otherExternalAddress = mock(InetAddress.class); when(otherExternalAddress.isLinkLocalAddress()).thenReturn(false); when(otherExternalAddress.isLoopbackAddress()).thenReturn(false); NetworkInterface networkInterfaceLinkLocal = mock(NetworkInterface.class); when(networkInterfaceLinkLocal.getInetAddresses()).thenReturn( Collections.enumeration(Collections.singletonList(linkLocalAddress))); NetworkInterface networkInterfaceExternal = mock(NetworkInterface.class); when(networkInterfaceExternal.getInetAddresses()).thenReturn( Collections.enumeration(Arrays.asList(otherExternalAddress, externalInet4Address))); when(NetworkInterface.getNetworkInterfaces()).thenReturn( Collections.enumeration(Arrays.asList(networkInterfaceLinkLocal, networkInterfaceExternal))); InetAddress finalInetAddress = mock(InetAddress.class); when(InetAddress.getByAddress(aryEq(externalInet4AddressBytes))).thenReturn(finalInetAddress); assertEquals(finalInetAddress, Util.getLocalHost()); }
@Test public void testGetLocalHostExternalAddressNotInet4Address() throws Exception { mockStatic(InetAddress.class, NetworkInterface.class); InetAddress inetAddressLocalHost = mock(InetAddress.class); when(inetAddressLocalHost.isLoopbackAddress()).thenReturn(true); when(InetAddress.getLocalHost()).thenReturn(inetAddressLocalHost); byte[] externalAddressBytes = new byte[]{0, 1, 2, 3, 4, 5}; InetAddress externalAddress = mock(InetAddress.class); when(externalAddress.isLinkLocalAddress()).thenReturn(false); when(externalAddress.isLoopbackAddress()).thenReturn(false); when(externalAddress.getAddress()).thenReturn(externalAddressBytes); NetworkInterface networkInterface = mock(NetworkInterface.class); when(networkInterface.getInetAddresses()).thenReturn( Collections.enumeration(Collections.singletonList(externalAddress))); when(NetworkInterface.getNetworkInterfaces()).thenReturn( Collections.enumeration(Collections.singletonList(networkInterface))); InetAddress finalInetAddress = mock(InetAddress.class); when(InetAddress.getByAddress(aryEq(externalAddressBytes))).thenReturn(finalInetAddress); assertEquals(finalInetAddress, Util.getLocalHost()); }