Skip to content
This repository has been archived by the owner on May 31, 2021. It is now read-only.

Use FQDN if possible. #32

Merged
merged 2 commits into from
May 18, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,24 @@
<version>14.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.5.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.5.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.8.5</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/me/moocar/logbackgelf/util/InternetUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import java.net.UnknownHostException;

public class InternetUtils {
private static final String REGEX_IP_ADDRESS = "\\d+(\\.\\d+){3}";

private InternetUtils() {
}

Expand All @@ -23,6 +25,10 @@ private InternetUtils() {
*/
public static String getLocalHostName() throws SocketException, UnknownHostException {
try {
final String canonicalHostName = InetAddress.getLocalHost().getCanonicalHostName();
if (canonicalHostName.contains(".") && !canonicalHostName.matches(REGEX_IP_ADDRESS)) {
return canonicalHostName;
}
return InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
NetworkInterface networkInterface = NetworkInterface.getNetworkInterfaces().nextElement();
Expand Down
59 changes: 59 additions & 0 deletions src/test/java/me/moocar/logbackgelf/util/InternetUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package me.moocar.logbackgelf.util;

import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(InternetUtils.class)
public class InternetUtilsTest {
private static final String RESULT_HOSTNAME = "hostname";
private static final String RESULT_FQDN = "hostname.with.do-main";
private static final String RESULT_IP = "1.2.3.4";

@Mock
private InetAddress myAddress;

@Before
public void setUp() throws UnknownHostException {
PowerMockito.mockStatic(InetAddress.class);
PowerMockito.when(InetAddress.getLocalHost()).thenReturn(myAddress);
}

@Test
public void testHostFQDN() throws UnknownHostException, SocketException {
PowerMockito.when(myAddress.getHostName()).thenReturn(RESULT_HOSTNAME);
PowerMockito.when(myAddress.getCanonicalHostName()).thenReturn(RESULT_FQDN);
Assert.assertEquals(RESULT_FQDN, InternetUtils.getLocalHostName());
}

@Test
public void testHostIP() throws UnknownHostException, SocketException {
PowerMockito.when(myAddress.getHostName()).thenReturn(RESULT_HOSTNAME);
PowerMockito.when(myAddress.getCanonicalHostName()).thenReturn(RESULT_IP);
Assert.assertEquals(RESULT_HOSTNAME, InternetUtils.getLocalHostName());
}

@Test
public void testFQDNIP() throws UnknownHostException, SocketException {
PowerMockito.when(myAddress.getHostName()).thenReturn(RESULT_FQDN);
PowerMockito.when(myAddress.getCanonicalHostName()).thenReturn(RESULT_IP);
Assert.assertEquals(RESULT_FQDN, InternetUtils.getLocalHostName());
}

@Test
public void testIPFQDN() throws UnknownHostException, SocketException {
PowerMockito.when(myAddress.getHostName()).thenReturn(RESULT_IP);
PowerMockito.when(myAddress.getCanonicalHostName()).thenReturn(RESULT_FQDN);
Assert.assertEquals(RESULT_FQDN, InternetUtils.getLocalHostName());
}
}