Skip to content

Commit

Permalink
test-utils: socket utils improvements, tests
Browse files Browse the repository at this point in the history
Signed-off-by: Ketoth Xupack <ketoth.xupack@gmail.com>
  • Loading branch information
KetothXupack committed Oct 22, 2013
1 parent 29cddd0 commit 1a4d83b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public static int getAvailablePort() {
try (final ServerSocket ss = new ServerSocket(0)) {
return ss.getLocalPort();
} catch (final IOException e) {
throw new IllegalStateException("no ports available");
throw new IllegalStateException("Unable to get local port: no ports available");
}
}

Expand All @@ -139,19 +139,19 @@ public static InetSocketAddress getAvailableLocalAddress() {
return getAvailableAddress(getLocalHostAddress());
}

public static String getLocalHostAddress() {
public static InetAddress getLocalHost() {
try {
return InetAddress.getLocalHost().getHostAddress();
return InetAddress.getLocalHost();
} catch (UnknownHostException e) {
throw new IllegalStateException("Unable to obtain local host address");
throw new IllegalStateException("Unable to obtain local host name");
}
}

public static String getLocalHostAddress() {
return getLocalHost().getHostAddress();
}

public static String getLocalHostName() {
try {
return InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
throw new IllegalStateException("Unable to obtain local host name");
}
return getLocalHost().getHostName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.UnknownHostException;

import static org.easymock.EasyMock.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
import static org.junit.Assume.assumeNoException;

/**
* @author <a href="mailto:ketoth.xupack@gmail.com">ketoth xupack</a>
Expand Down Expand Up @@ -60,6 +60,22 @@ public void ports() throws IOException {
}
}

@Test
public void localAddressChecks() {
final InetAddress host = SocketUtils.getLocalHost();
assertTrue(SocketUtils.isLocal(host));

}

@Test
public void remoteAddress() {
try {
assertFalse(SocketUtils.isLocal(InetAddress.getByName("8.8.8.8")));
} catch (UnknownHostException e) {
assumeNoException(e);
}
}

@Test
public void mocking() {
{
Expand Down

0 comments on commit 1a4d83b

Please sign in to comment.