1818package com .cloud .cluster ;
1919
2020import java .net .InetAddress ;
21+ import java .net .UnknownHostException ;
2122import java .util .List ;
2223import java .util .Optional ;
2324
@@ -69,9 +70,7 @@ public void testPingPostParameters() {
6970 public void getBindAddressIfAvailable_returnsInetAddress_whenBindAddressIsValid () {
7071 try (MockedStatic <ServerPropertiesUtil > ignored = Mockito .mockStatic (ServerPropertiesUtil .class )) {
7172 Mockito .when (ServerPropertiesUtil .getProperty ("bind.interface" )).thenReturn ("127.0.0.1" );
72-
7373 InetAddress result = clusterServiceServlet .getBindAddressIfAvailable ();
74-
7574 Assert .assertNotNull (result );
7675 Assert .assertEquals ("127.0.0.1" , result .getHostAddress ());
7776 } catch (RuntimeException e ) {
@@ -80,23 +79,53 @@ public void getBindAddressIfAvailable_returnsInetAddress_whenBindAddressIsValid(
8079 }
8180
8281 @ Test
83- public void getBindAddressIfAvailable_returnsNull_whenBindAddressIsBlank () {
82+ public void getBindAddressIfAvailable_returnsInetAddress_whenBindAddressIsHostname () {
83+ String hostname ;
84+ try {
85+ hostname = InetAddress .getLocalHost ().getHostName ();
86+ } catch (UnknownHostException e ) {
87+ hostname = "localhost" ;
88+ }
8489 try (MockedStatic <ServerPropertiesUtil > ignored = Mockito .mockStatic (ServerPropertiesUtil .class )) {
85- Mockito .when (ServerPropertiesUtil .getProperty ("bind.interface" )).thenReturn ("" );
86-
90+ Mockito .when (ServerPropertiesUtil .getProperty ("bind.interface" )).thenReturn (hostname );
8791 InetAddress result = clusterServiceServlet .getBindAddressIfAvailable ();
92+ Assert .assertNotNull (result );
93+ InetAddress address = InetAddress .getByName (hostname );
94+ Assert .assertEquals (address , result );
95+ } catch (UnknownHostException | RuntimeException e ) {
96+ Assert .fail ("Unexpected RuntimeException: " + e .getMessage ());
97+ }
98+ }
8899
100+ private void runBindAddressIfAvailableForNullResult (String bindAddress ) {
101+ try (MockedStatic <ServerPropertiesUtil > ignored = Mockito .mockStatic (ServerPropertiesUtil .class )) {
102+ Mockito .when (ServerPropertiesUtil .getProperty ("bind.interface" )).thenReturn (bindAddress );
103+ InetAddress result = clusterServiceServlet .getBindAddressIfAvailable ();
89104 Assert .assertNull (result );
90105 } catch (RuntimeException e ) {
91106 Assert .fail ("Unexpected RuntimeException: " + e .getMessage ());
92107 }
93108 }
94109
110+ @ Test
111+ public void getBindAddressIfAvailable_returnsInetAddress_whenWildcard () {
112+ runBindAddressIfAvailableForNullResult ("0.0.0.0" );
113+ }
114+
115+ @ Test
116+ public void getBindAddressIfAvailable_returnsInetAddress_whenIp6Wildcard () {
117+ runBindAddressIfAvailableForNullResult ("::" );
118+ }
119+
120+ @ Test
121+ public void getBindAddressIfAvailable_returnsNull_whenBindAddressIsBlank () {
122+ runBindAddressIfAvailableForNullResult ("" );
123+ }
124+
95125 @ Test (expected = RuntimeException .class )
96126 public void getBindAddressIfAvailable_throwsRuntimeException_whenBindAddressIsInvalid () throws RuntimeException {
97127 try (MockedStatic <ServerPropertiesUtil > ignored = Mockito .mockStatic (ServerPropertiesUtil .class )) {
98128 Mockito .when (ServerPropertiesUtil .getProperty ("bind.interface" )).thenReturn ("invalid-address" );
99-
100129 clusterServiceServlet .getBindAddressIfAvailable ();
101130 }
102131 }
0 commit comments