Add int converter to inet utils
This commit is contained in:
@@ -1,16 +1,17 @@
|
||||
package org.springframework.cloud.util;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.apachecommons.CommonsLog;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Inet4Address;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.UnknownHostException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Enumeration;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.apachecommons.CommonsLog;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@@ -77,5 +78,15 @@ public class InetUtils {
|
||||
public boolean override;
|
||||
private String ipAddress;
|
||||
private String hostname;
|
||||
public int getIpAddressAsInt() {
|
||||
InetAddress inetAddress = null;
|
||||
try {
|
||||
inetAddress = InetAddress.getByName(this.ipAddress);
|
||||
}
|
||||
catch (final UnknownHostException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
return ByteBuffer.wrap(inetAddress.getAddress()).getInt();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.springframework.cloud.util;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
@@ -17,77 +18,77 @@ public class IdUtilsTests {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
env = new MockEnvironment();
|
||||
this.env = new MockEnvironment();
|
||||
}
|
||||
|
||||
@After
|
||||
public void destroy() {
|
||||
env = null;
|
||||
this.env = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void emptyEnvironmentWorks() {
|
||||
String instanceId = IdUtils.getDefaultInstanceId(env);
|
||||
String instanceId = IdUtils.getDefaultInstanceId(this.env);
|
||||
assertNull("instanceId was not null", instanceId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void vcapInstanceIdWorks() {
|
||||
env.setProperty("vcap.application.instance_id", DEFAULT_ID);
|
||||
String instanceId = IdUtils.getDefaultInstanceId(env);
|
||||
this.env.setProperty("vcap.application.instance_id", DEFAULT_ID);
|
||||
String instanceId = IdUtils.getDefaultInstanceId(this.env);
|
||||
assertEquals("instanceId was wrong", DEFAULT_ID, instanceId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hostnameWorks() {
|
||||
env.setProperty("spring.cloud.client.hostname", DEFAULT_ID);
|
||||
String instanceId = IdUtils.getDefaultInstanceId(env);
|
||||
this.env.setProperty("spring.cloud.client.hostname", DEFAULT_ID);
|
||||
String instanceId = IdUtils.getDefaultInstanceId(this.env);
|
||||
assertEquals("instanceId was wrong", DEFAULT_ID, instanceId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void appNameWorks() {
|
||||
env.setProperty("spring.application.name", DEFAULT_ID);
|
||||
String instanceId = IdUtils.getDefaultInstanceId(env);
|
||||
this.env.setProperty("spring.application.name", DEFAULT_ID);
|
||||
String instanceId = IdUtils.getDefaultInstanceId(this.env);
|
||||
assertEquals("instanceId was wrong", DEFAULT_ID, instanceId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hostnameAndAppNameWorks() {
|
||||
env.setProperty("spring.application.name", DEFAULT_ID);
|
||||
env.setProperty("spring.cloud.client.hostname", DEFAULT_ID+"2");
|
||||
String instanceId = IdUtils.getDefaultInstanceId(env);
|
||||
this.env.setProperty("spring.application.name", DEFAULT_ID);
|
||||
this.env.setProperty("spring.cloud.client.hostname", DEFAULT_ID+"2");
|
||||
String instanceId = IdUtils.getDefaultInstanceId(this.env);
|
||||
assertEquals("instanceId was wrong", DEFAULT_ID+"2"+":"+DEFAULT_ID, instanceId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void instanceIdWorks() {
|
||||
env.setProperty("spring.cloud.client.hostname", DEFAULT_ID);
|
||||
String instanceId = IdUtils.getDefaultInstanceId(env);
|
||||
this.env.setProperty("spring.cloud.client.hostname", DEFAULT_ID);
|
||||
String instanceId = IdUtils.getDefaultInstanceId(this.env);
|
||||
assertEquals("instanceId was wrong", DEFAULT_ID, instanceId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void portWorks() {
|
||||
env.setProperty("spring.application.name", DEFAULT_ID);
|
||||
String instanceId = IdUtils.getDefaultInstanceId(env);
|
||||
this.env.setProperty("spring.application.name", DEFAULT_ID);
|
||||
String instanceId = IdUtils.getDefaultInstanceId(this.env);
|
||||
assertEquals("instanceId was wrong", DEFAULT_ID, instanceId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void appNameAndPortWorks() {
|
||||
env.setProperty("spring.application.name", DEFAULT_ID);
|
||||
env.setProperty("server.port", "80");
|
||||
String instanceId = IdUtils.getDefaultInstanceId(env);
|
||||
this.env.setProperty("spring.application.name", DEFAULT_ID);
|
||||
this.env.setProperty("server.port", "80");
|
||||
String instanceId = IdUtils.getDefaultInstanceId(this.env);
|
||||
assertEquals("instanceId was wrong", DEFAULT_ID+":80", instanceId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fullWorks() {
|
||||
env.setProperty("spring.cloud.client.hostname", "myhost");
|
||||
env.setProperty("spring.application.name", DEFAULT_ID);
|
||||
env.setProperty("server.port", "80");
|
||||
String instanceId = IdUtils.getDefaultInstanceId(env);
|
||||
this.env.setProperty("spring.cloud.client.hostname", "myhost");
|
||||
this.env.setProperty("spring.application.name", DEFAULT_ID);
|
||||
this.env.setProperty("server.port", "80");
|
||||
String instanceId = IdUtils.getDefaultInstanceId(this.env);
|
||||
assertEquals("instanceId was wrong", "myhost:"+DEFAULT_ID+":80", instanceId);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright 2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.util;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.net.InetAddress;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.cloud.util.InetUtils.HostInfo;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class InetUtilsTests {
|
||||
|
||||
@Test
|
||||
public void testGetFirstNonLoopbackHostInfo() {
|
||||
assertNotNull(InetUtils.getFirstNonLoopbackHostInfo());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetFirstNonLoopbackAddress() {
|
||||
assertNotNull(InetUtils.getFirstNonLoopbackAddress());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvert() throws Exception {
|
||||
assertNotNull(InetUtils.convert(InetAddress.getByName("localhost")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHostInfo() throws Exception {
|
||||
HostInfo info = InetUtils.getFirstNonLoopbackHostInfo();
|
||||
assertNotNull(info.getIpAddressAsInt());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user