### Question:
SocketAddressParser { public static <T extends SocketAddress> T deriveSocketAddress( String settingName, String settingValue, String defaultHostname, int defaultPort, BiFunction<String,Integer,T> constructor ) { if ( settingValue == null ) { return constructor.apply( defaultHostname, defaultPort ); } settingValue = settingValue.trim(); T socketAddress; if ( (socketAddress = matchHostnamePort( settingValue, constructor )) != null ) { return socketAddress; } if ( (socketAddress = matchPort( settingValue, defaultHostname, constructor )) != null ) { return socketAddress; } throw new IllegalArgumentException( format( "Setting \"%s\" must be in the format " + "\"hostname:port\" or \":port\". \"%s\" does not conform to these formats", settingName, settingValue ) ); }  static T deriveSocketAddress(
            String settingName, String settingValue, String defaultHostname, int defaultPort,
            BiFunction<String,Integer,T> constructor ); static T socketAddress( String settingValue,
            BiFunction<String,Integer,T> constructor );  }### Answer:
@Test void shouldSupportPortOnly() { SocketAddress socketAddress = SocketAddressParser.deriveSocketAddress( "setting.name", ":123", "my.domain", 456, SocketAddress::new ); assertEquals( "my.domain", socketAddress.getHostname() ); assertEquals( 123, socketAddress.getPort() ); assertEquals( "my.domain:123", socketAddress.toString() ); assertFalse( socketAddress.isWildcard() ); }
@Test void shouldSupportDefaultValue() { SocketAddress socketAddress = SocketAddressParser.deriveSocketAddress( "setting.name", null, "my.domain", 456, SocketAddress::new ); assertEquals( "my.domain", socketAddress.getHostname() ); assertEquals( 456, socketAddress.getPort() ); assertEquals( "my.domain:456", socketAddress.toString() ); assertFalse( socketAddress.isWildcard() ); }
@Test void shouldSupportDefaultWildcard() { SocketAddress socketAddress = SocketAddressParser.deriveSocketAddress( "setting.name", null, "0.0.0.0", 456, SocketAddress::new ); assertEquals( "0.0.0.0", socketAddress.getHostname() ); assertEquals( 456, socketAddress.getPort() ); assertEquals( "0.0.0.0:456", socketAddress.toString() ); assertTrue( socketAddress.isWildcard() ); }
@Test void shouldSupportDefaultIPv6Wildcard() { SocketAddress socketAddress = SocketAddressParser.deriveSocketAddress( "setting.name", null, "::", 456, SocketAddress::new ); assertEquals( "::", socketAddress.getHostname() ); assertEquals( 456, socketAddress.getPort() ); assertEquals( "[::]:456", socketAddress.toString() ); assertTrue( socketAddress.isWildcard() ); }
@Test void shouldSupportDefaultIPv6Value() { SocketAddress socketAddress = SocketAddressParser.deriveSocketAddress( "setting.name", null, "fe80:1:2::4", 456, SocketAddress::new ); assertEquals( "fe80:1:2::4", socketAddress.getHostname() ); assertEquals( 456, socketAddress.getPort() ); assertEquals( "[fe80:1:2::4]:456", socketAddress.toString() ); assertFalse( socketAddress.isWildcard() ); }
@Test void shouldNotUseDefaultsWhenSettingValueSupplied() { SocketAddress socketAddress = SocketAddressParser.deriveSocketAddress( "setting.name", "[fe80:3:4::6]:456", "fe80:1:2::4", 123, SocketAddress::new ); assertEquals( "fe80:3:4::6", socketAddress.getHostname() ); assertEquals( 456, socketAddress.getPort() ); assertEquals( "[fe80:3:4::6]:456", socketAddress.toString() ); assertFalse( socketAddress.isWildcard() ); }