This commit is contained in:
Phillip Webb 2014-06-17 12:14:15 -07:00
parent b439d3522e
commit 25eb6fb81a
6 changed files with 54 additions and 55 deletions

View File

@ -105,7 +105,8 @@ public class HornetQAutoConfiguration {
private ConnectionFactory createEmbeddedConnectionFactory() {
try {
TransportConfiguration transportConfiguration = new TransportConfiguration(
InVMConnectorFactory.class.getName(), properties.getEmbedded().generateTransportParameters());
InVMConnectorFactory.class.getName(), this.properties.getEmbedded()
.generateTransportParameters());
ServerLocator serviceLocator = HornetQClient
.createServerLocatorWithoutHA(transportConfiguration);
return new HornetQConnectionFactory(serviceLocator);

View File

@ -62,7 +62,8 @@ class HornetQEmbeddedConfigurationFactory {
}
TransportConfiguration transportConfiguration = new TransportConfiguration(
InVMAcceptorFactory.class.getName(), properties.generateTransportParameters());
InVMAcceptorFactory.class.getName(),
this.properties.generateTransportParameters());
configuration.getAcceptorConfigurations().add(transportConfiguration);
// HORNETQ-1143

View File

@ -22,7 +22,6 @@ import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import org.hornetq.core.remoting.impl.invm.TransportConstants;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
@ -94,7 +93,7 @@ public class HornetQProperties {
private boolean defaultClusterPassword = true;
public int getServerId() {
return serverId;
return this.serverId;
}
public void setServerId(int serverId) {
@ -155,12 +154,11 @@ public class HornetQProperties {
}
/**
* Creates the minimal transport parameters for an embedded transport configuration.
* <p>Specifies the identifier of the server.
*
* Creates the minimal transport parameters for an embedded transport
* configuration.
* @see TransportConstants#SERVER_ID_PROP_NAME
*/
public Map<String,Object> generateTransportParameters() {
public Map<String, Object> generateTransportParameters() {
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put(TransportConstants.SERVER_ID_PROP_NAME, getServerId());
return parameters;

View File

@ -16,8 +16,6 @@
package org.springframework.boot.autoconfigure.jms.hornetq;
import static org.junit.Assert.*;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
@ -56,6 +54,11 @@ import org.springframework.jms.core.SessionCallback;
import org.springframework.jms.support.destination.DestinationResolver;
import org.springframework.jms.support.destination.DynamicDestinationResolver;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
/**
* Tests for {@link HornetQAutoConfiguration}.
*
@ -88,8 +91,7 @@ public class HornetQAutoConfigurationTests {
@Test
public void nativeConnectionFactoryCustomHost() {
load(EmptyConfiguration.class, "spring.hornetq.mode:native",
"spring.hornetq.host:192.168.1.144",
"spring.hornetq.port:9876");
"spring.hornetq.host:192.168.1.144", "spring.hornetq.port:9876");
HornetQConnectionFactory connectionFactory = this.context
.getBean(HornetQConnectionFactory.class);
assertNettyConnectionFactory(connectionFactory, "192.168.1.144", 9876);
@ -146,8 +148,7 @@ public class HornetQAutoConfigurationTests {
@Test
public void embeddedConnectionFactorEvenIfEmbeddedServiceDisabled() {
// No mode is specified
load(EmptyConfiguration.class,
"spring.hornetq.mode:embedded",
load(EmptyConfiguration.class, "spring.hornetq.mode:embedded",
"spring.hornetq.embedded.enabled:false");
assertEquals(0, this.context.getBeansOfType(EmbeddedJMS.class).size());
@ -157,11 +158,9 @@ public class HornetQAutoConfigurationTests {
assertInVmConnectionFactory(connectionFactory);
}
@Test
public void embeddedServerWithDestinations() {
load(EmptyConfiguration.class,
"spring.hornetq.embedded.queues=Queue1,Queue2",
load(EmptyConfiguration.class, "spring.hornetq.embedded.queues=Queue1,Queue2",
"spring.hornetq.embedded.topics=Topic1");
DestinationChecker checker = new DestinationChecker(this.context);
@ -184,9 +183,10 @@ public class HornetQAutoConfigurationTests {
@Test
public void embeddedServiceWithCustomJmsConfiguration() {
load(CustomJmsConfiguration.class,
"spring.hornetq.embedded.queues=Queue1,Queue2"); // Ignored with custom
// config
load(CustomJmsConfiguration.class, "spring.hornetq.embedded.queues=Queue1,Queue2"); // Ignored
// with
// custom
// config
DestinationChecker checker = new DestinationChecker(this.context);
checker.checkQueue("custom", true); // See CustomJmsConfiguration
@ -207,8 +207,7 @@ public class HornetQAutoConfigurationTests {
File dataFolder = this.folder.newFolder();
// Start the server and post a message to some queue
load(EmptyConfiguration.class,
"spring.hornetq.embedded.queues=TestQueue",
load(EmptyConfiguration.class, "spring.hornetq.embedded.queues=TestQueue",
"spring.hornetq.embedded.persistent:true",
"spring.hornetq.embedded.dataDirectory:" + dataFolder.getAbsolutePath());
@ -223,8 +222,7 @@ public class HornetQAutoConfigurationTests {
this.context.close(); // Shutdown the broker
// Start the server again and check if our message is still here
load(EmptyConfiguration.class,
"spring.hornetq.embedded.queues=TestQueue",
load(EmptyConfiguration.class, "spring.hornetq.embedded.queues=TestQueue",
"spring.hornetq.embedded.persistent:true",
"spring.hornetq.embedded.dataDirectory:" + dataFolder.getAbsolutePath());
@ -238,17 +236,17 @@ public class HornetQAutoConfigurationTests {
@Test
public void severalEmbeddedBrokers() {
load(EmptyConfiguration.class,
"spring.hornetq.embedded.queues=Queue1");
load(EmptyConfiguration.class, "spring.hornetq.embedded.queues=Queue1");
AnnotationConfigApplicationContext anotherContext = doLoad(EmptyConfiguration.class,
"spring.hornetq.embedded.queues=Queue2");
AnnotationConfigApplicationContext anotherContext = doLoad(
EmptyConfiguration.class, "spring.hornetq.embedded.queues=Queue2");
try {
HornetQProperties properties = this.context.getBean(HornetQProperties.class);
HornetQProperties anotherProperties = anotherContext.getBean(HornetQProperties.class);
assertTrue("ServerId should not match",
properties.getEmbedded().getServerId() < anotherProperties.getEmbedded().getServerId());
HornetQProperties anotherProperties = anotherContext
.getBean(HornetQProperties.class);
assertTrue("ServerId should not match", properties.getEmbedded()
.getServerId() < anotherProperties.getEmbedded().getServerId());
DestinationChecker checker = new DestinationChecker(this.context);
checker.checkQueue("Queue1", true);
@ -259,19 +257,18 @@ public class HornetQAutoConfigurationTests {
anotherChecker.checkQueue("Queue1", false);
}
finally {
anotherContext.close();
anotherContext.close();
}
}
@Test
public void connectToASpecificEmbeddedBroker() {
load(EmptyConfiguration.class,
"spring.hornetq.embedded.serverId=93",
load(EmptyConfiguration.class, "spring.hornetq.embedded.serverId=93",
"spring.hornetq.embedded.queues=Queue1");
AnnotationConfigApplicationContext anotherContext = doLoad(EmptyConfiguration.class,
"spring.hornetq.mode=embedded",
"spring.hornetq.embedded.serverId=93", // Connect to the "main" broker
AnnotationConfigApplicationContext anotherContext = doLoad(
EmptyConfiguration.class, "spring.hornetq.mode=embedded",
"spring.hornetq.embedded.serverId=93", // Connect to the "main" broker
"spring.hornetq.embedded.enabled=false"); // do not start a specific one
try {
@ -316,10 +313,12 @@ public class HornetQAutoConfigurationTests {
this.context = doLoad(config, environment);
}
private AnnotationConfigApplicationContext doLoad(Class<?> config, String... environment) {
private AnnotationConfigApplicationContext doLoad(Class<?> config,
String... environment) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
applicationContext.register(config);
applicationContext.register(HornetQAutoConfiguration.class, JmsAutoConfiguration.class);
applicationContext.register(HornetQAutoConfiguration.class,
JmsAutoConfiguration.class);
EnvironmentTestUtils.addEnvironment(applicationContext, environment);
applicationContext.refresh();
return applicationContext;

View File

@ -153,7 +153,8 @@ public class SpringApplicationContextLoader extends AbstractContextLoader {
.detectDefaultConfigurationClasses(declaringClass);
}
protected Map<String, Object> getEnvironmentProperties(MergedContextConfiguration config) {
protected Map<String, Object> getEnvironmentProperties(
MergedContextConfiguration config) {
Map<String, Object> properties = new LinkedHashMap<String, Object>();
// JMX bean names will clash if the same bean is used in multiple contexts
disableJmx(properties);
@ -180,7 +181,7 @@ public class SpringApplicationContextLoader extends AbstractContextLoader {
// Instead of parsing the keys ourselves, we rely on standard handling
private Map<String, String> extractEnvironmentProperties(String[] values) {
StringBuilder sb = new StringBuilder();
StringBuilder sb = new StringBuilder();
for (String value : values) {
sb.append(value).append(LINE_SEPARATOR);
}
@ -190,11 +191,12 @@ public class SpringApplicationContextLoader extends AbstractContextLoader {
props.load(new StringReader(content));
}
catch (IOException e) {
throw new IllegalStateException("Unexpected could not load properties from '"+content+"'", e);
throw new IllegalStateException("Unexpected could not load properties from '"
+ content + "'", e);
}
Map<String, String> properties = new HashMap<String, String>();
for (String name: props.stringPropertyNames()) {
for (String name : props.stringPropertyNames()) {
properties.put(name, props.getProperty(name));
}
return properties;

View File

@ -16,18 +16,19 @@
package org.springframework.boot.test;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.util.Map;
import org.junit.Test;
import org.springframework.test.context.MergedContextConfiguration;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link SpringApplicationContextLoader}
*
*
* @author Stephane Nicoll
*/
public class SpringApplicationContextLoaderTests {
@ -55,12 +56,10 @@ public class SpringApplicationContextLoaderTests {
assertKey(config, "anotherKey", "another=Value");
}
private Map<String, Object> getEnvironmentProperties(Class<?> testClass) {
MergedContextConfiguration configuration = mock(MergedContextConfiguration.class);
doReturn(testClass).when(configuration).getTestClass();
return loader.getEnvironmentProperties(configuration);
return this.loader.getEnvironmentProperties(configuration);
}
private void assertKey(Map<String, Object> actual, String key, Object value) {
@ -68,16 +67,15 @@ public class SpringApplicationContextLoaderTests {
assertEquals(value, actual.get(key));
}
@IntegrationTest({"key=myValue", "anotherKey:anotherValue"})
@IntegrationTest({ "key=myValue", "anotherKey:anotherValue" })
static class SimpleConfig {
}
@IntegrationTest({"key=my=Value", "anotherKey:another:Value"})
@IntegrationTest({ "key=my=Value", "anotherKey:another:Value" })
static class SameSeparatorInValue {
}
@IntegrationTest({"key=my:Value", "anotherKey:another=Value"})
@IntegrationTest({ "key=my:Value", "anotherKey:another=Value" })
static class AnotherSeparatorInValue {
}