Skip to content

Commit

Permalink
refactor IpUtils (apache#1229)
Browse files Browse the repository at this point in the history
  • Loading branch information
dengliming authored Apr 7, 2021
1 parent 67fa656 commit 400ad07
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
package org.dromara.soul.common.utils;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.net.NetworkInterface;
import java.util.Enumeration;

/**
* The type Ip utils.
Expand All @@ -33,10 +34,27 @@ private IpUtils() {
* @return the host
*/
public static String getHost() {
String hostIp = null;
try {
return InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
return "127.0.0.1";
Enumeration<?> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface network = (NetworkInterface) networkInterfaces.nextElement();
Enumeration<?> addresses = network.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress inetAddress = (InetAddress) addresses.nextElement();
String hostAddress = inetAddress.getHostAddress();
if (hostAddress.contains(".") && !inetAddress.isLoopbackAddress()) {
hostIp = hostAddress;
break;
}
}
}
if (hostIp == null) {
hostIp = InetAddress.getLocalHost().getHostAddress();
}
} catch (Exception ignore) {
hostIp = "127.0.0.1";
}
return hostIp;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import org.mockito.MockedStatic;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Vector;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
Expand All @@ -37,30 +39,35 @@
*/
public final class IpUtilsTest {

private MockedStatic<InetAddress> inetAddressMockedStatic;
private MockedStatic<NetworkInterface> networkInterfaceMockedStatic;

@Before
public void setUp() {
inetAddressMockedStatic = mockStatic(InetAddress.class);
networkInterfaceMockedStatic = mockStatic(NetworkInterface.class);
}

@After
public void close() {
inetAddressMockedStatic.close();
networkInterfaceMockedStatic.close();
}

@Test
public void testGetHost() throws UnknownHostException {
InetAddress inetAddress = mock(InetAddress.class);
inetAddressMockedStatic.when((MockedStatic.Verification) InetAddress.getLocalHost()).thenReturn(inetAddress);
when(inetAddress.getHostAddress()).thenReturn("127.0.0.1");
assertEquals("127.0.0.1", IpUtils.getHost());
public void testGetHost() throws Exception {
Vector<InetAddress> addresses = new Vector<>();
addresses.add(InetAddress.getByAddress("local", new byte[]{(byte) 192, (byte) 168, (byte) 1, (byte) 3}));
NetworkInterface nic = mock(NetworkInterface.class);
when(nic.getInetAddresses()).thenReturn(addresses.elements());
Vector<NetworkInterface> nics = new Vector<>();
nics.add(nic);
networkInterfaceMockedStatic.when((MockedStatic.Verification) NetworkInterface.getNetworkInterfaces()).thenReturn(nics.elements());

assertEquals("192.168.1.3", IpUtils.getHost());
}

@Test
public void testGetHostWithException() throws UnknownHostException {
inetAddressMockedStatic.when((MockedStatic.Verification) InetAddress.getLocalHost())
.thenThrow(UnknownHostException.class);
public void testGetHostWithException() throws Exception {
networkInterfaceMockedStatic.when((MockedStatic.Verification) NetworkInterface.getNetworkInterfaces())
.thenThrow(SocketException.class);
assertEquals("127.0.0.1", IpUtils.getHost());
}
}

0 comments on commit 400ad07

Please sign in to comment.