Skip to content

Commit 7b4c140

Browse files
committed
Get host name from HOSTNAME environment variable
1 parent 90a20a0 commit 7b4c140

2 files changed

Lines changed: 53 additions & 21 deletions

File tree

src/main/java/com/stackify/api/common/EnvironmentDetails.java

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,9 @@ public class EnvironmentDetails {
3434
*/
3535
public static EnvironmentDetail getEnvironmentDetail(final String application, final String environment) {
3636

37-
// lookup the hostname
37+
// lookup the host name
3838

39-
String hostName = null;
40-
41-
try {
42-
InetAddress addr = InetAddress.getLocalHost();
43-
hostName = addr.getHostName();
44-
} catch (Throwable t) {
45-
// Do nothing
46-
}
39+
String hostName = getHostName();
4740

4841
// lookup the current path
4942

@@ -60,6 +53,39 @@ public static EnvironmentDetail getEnvironmentDetail(final String application, f
6053
return environmentBuilder.build();
6154
}
6255

56+
/**
57+
* @return The host name
58+
*/
59+
private static String getHostName() {
60+
61+
// HOSTNAME environment variable
62+
63+
try {
64+
String hostName = System.getenv("HOSTNAME");
65+
66+
if ((hostName != null) && (0 < hostName.length())) {
67+
return hostName;
68+
}
69+
} catch (Throwable t) {
70+
// Do nothing
71+
}
72+
73+
// InetAddress.getLocalHost().getHostName()
74+
75+
try {
76+
InetAddress addr = InetAddress.getLocalHost();
77+
String hostName = addr.getHostName();
78+
79+
if ((hostName != null) && (0 < hostName.length())) {
80+
return hostName;
81+
}
82+
} catch (Throwable t) {
83+
// Do nothing
84+
}
85+
86+
return null;
87+
}
88+
6389
/**
6490
* Hidden to prevent construction
6591
*/

src/test/java/com/stackify/api/common/EnvironmentDetailsTest.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
* @author Eric Martin
3434
*/
3535
@RunWith(PowerMockRunner.class)
36-
@PrepareForTest({EnvironmentDetails.class, InetAddress.class})
36+
@PrepareForTest({EnvironmentDetails.class, System.class, InetAddress.class})
3737
public class EnvironmentDetailsTest {
3838

3939
/**
@@ -45,12 +45,10 @@ public void testGetEnvironmentDetail() throws UnknownHostException {
4545
String application = "application";
4646
String environment = "environment";
4747
String hostName = "hostName";
48-
49-
InetAddress inetAddress = PowerMockito.mock(InetAddress.class);
50-
PowerMockito.when(inetAddress.getHostName()).thenReturn(hostName);
51-
52-
PowerMockito.mockStatic(InetAddress.class);
53-
PowerMockito.when(InetAddress.getLocalHost()).thenReturn(inetAddress);
48+
49+
PowerMockito.mockStatic(System.class);
50+
PowerMockito.when(System.getenv("HOSTNAME")).thenReturn(hostName);
51+
PowerMockito.when(System.getProperty("user.dir")).thenReturn("/some/dir/");
5452

5553
EnvironmentDetail env = EnvironmentDetails.getEnvironmentDetail(application, environment);
5654

@@ -64,22 +62,30 @@ public void testGetEnvironmentDetail() throws UnknownHostException {
6462
}
6563

6664
/**
67-
* testGetEnvironmentDetailHostnameException
65+
* testGetEnvironmentDetailWithoutHostnameEnvVar
6866
* @throws UnknownHostException
6967
*/
7068
@Test
71-
public void testGetEnvironmentDetailHostnameException() throws UnknownHostException {
69+
public void testGetEnvironmentDetailWithoutHostnameEnvVar() throws UnknownHostException {
7270
String application = "application";
7371
String environment = "environment";
74-
72+
String hostName = "hostName";
73+
74+
PowerMockito.mockStatic(System.class);
75+
PowerMockito.when(System.getenv("HOSTNAME")).thenReturn(null);
76+
PowerMockito.when(System.getProperty("user.dir")).thenReturn("/some/dir/");
77+
78+
InetAddress inetAddress = PowerMockito.mock(InetAddress.class);
79+
PowerMockito.when(inetAddress.getHostName()).thenReturn(hostName);
80+
7581
PowerMockito.mockStatic(InetAddress.class);
76-
PowerMockito.when(InetAddress.getLocalHost()).thenThrow(new RuntimeException());
82+
PowerMockito.when(InetAddress.getLocalHost()).thenReturn(inetAddress);
7783

7884
EnvironmentDetail env = EnvironmentDetails.getEnvironmentDetail(application, environment);
7985

8086
Assert.assertNotNull(env);
8187

82-
Assert.assertNull(env.getDeviceName());
88+
Assert.assertEquals(hostName, env.getDeviceName());
8389
Assert.assertNotNull(env.getAppLocation());
8490
Assert.assertNull(env.getAppName());
8591
Assert.assertEquals(application, env.getConfiguredAppName());

0 commit comments

Comments
 (0)