Rename Turbine port listener class and add tests
This commit is contained in:
@@ -7,7 +7,7 @@ import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEven
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
|
||||
public class TurbinePortSpringApplicationRunListener implements
|
||||
public class TurbinePortApplicationListener implements
|
||||
ApplicationListener<ApplicationEnvironmentPreparedEvent> {
|
||||
|
||||
@Override
|
||||
@@ -21,11 +21,20 @@ public class TurbinePortSpringApplicationRunListener implements
|
||||
if (serverPort == null && managementPort == null) {
|
||||
return;
|
||||
}
|
||||
if (serverPort != -1) {
|
||||
if (serverPort != Integer.valueOf(-1)) {
|
||||
Map<String, Object> ports = new HashMap<String, Object>();
|
||||
ports.put("server.port", -1);
|
||||
if (serverPort != null && turbinePort==null) {
|
||||
ports.put("turbine.amqp.port", serverPort);
|
||||
if (turbinePort == null) {
|
||||
// The actual server.port used by the application forced to be -1 (no user
|
||||
// endpoints) because no value was provided for turbine
|
||||
ports.put("server.port", -1);
|
||||
if (serverPort != null) {
|
||||
// Turbine port defaults to server port value supplied by user
|
||||
ports.put("turbine.amqp.port", serverPort);
|
||||
}
|
||||
}
|
||||
else if (managementPort != null && serverPort == null) {
|
||||
// User wants 2 ports, but hasn't specified server.port explicitly
|
||||
ports.put("server.port", managementPort);
|
||||
}
|
||||
event.getEnvironment().getPropertySources()
|
||||
.addFirst(new MapPropertySource("ports", ports));
|
||||
@@ -2,4 +2,4 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
org.springframework.cloud.netflix.turbine.amqp.TurbineAmqpAutoConfiguration
|
||||
|
||||
org.springframework.context.ApplicationListener=\
|
||||
org.springframework.cloud.netflix.turbine.amqp.TurbinePortSpringApplicationRunListener
|
||||
org.springframework.cloud.netflix.turbine.amqp.TurbinePortApplicationListener
|
||||
@@ -0,0 +1,59 @@
|
||||
package org.springframework.cloud.netflix.turbine.amqp;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
|
||||
import org.springframework.boot.test.EnvironmentTestUtils;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.StandardEnvironment;
|
||||
|
||||
public class TurbinePortApplicationListenerTests {
|
||||
|
||||
private TurbinePortApplicationListener listener = new TurbinePortApplicationListener();
|
||||
|
||||
private ConfigurableEnvironment environment = new StandardEnvironment();
|
||||
|
||||
private ApplicationEnvironmentPreparedEvent event = new ApplicationEnvironmentPreparedEvent(new SpringApplication(), null, environment);
|
||||
|
||||
@Test
|
||||
public void noop() {
|
||||
listener.onApplicationEvent(event);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serverPortOnly() {
|
||||
EnvironmentTestUtils.addEnvironment(environment, "server.port=9999");
|
||||
listener.onApplicationEvent(event);
|
||||
assertEquals("-1", environment.resolvePlaceholders("${server.port}"));
|
||||
assertEquals("9999", environment.resolvePlaceholders("${turbine.amqp.port}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void turbinePortOnly() {
|
||||
EnvironmentTestUtils.addEnvironment(environment, "turbine.amqp.port=9999");
|
||||
listener.onApplicationEvent(event);
|
||||
assertEquals("9999", environment.resolvePlaceholders("${turbine.amqp.port}"));
|
||||
assertEquals("0", environment.resolvePlaceholders("${server.port:0}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void turbineAndManagementPorts() {
|
||||
EnvironmentTestUtils.addEnvironment(environment, "turbine.amqp.port=9999", "management.port=9000");
|
||||
listener.onApplicationEvent(event);
|
||||
assertEquals("9999", environment.resolvePlaceholders("${turbine.amqp.port}"));
|
||||
assertEquals("9000", environment.resolvePlaceholders("${server.port:0}"));
|
||||
assertEquals("9000", environment.resolvePlaceholders("${management.port:0}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void turbineAndServerPorts() {
|
||||
EnvironmentTestUtils.addEnvironment(environment, "turbine.amqp.port=9999", "server.port=9000");
|
||||
listener.onApplicationEvent(event);
|
||||
assertEquals("9999", environment.resolvePlaceholders("${turbine.amqp.port}"));
|
||||
assertEquals("9000", environment.resolvePlaceholders("${server.port:0}"));
|
||||
assertEquals("0", environment.resolvePlaceholders("${management.port:0}"));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user