Polish Hazelcast auto-configuration

Extract a HazelcastInstanceFactory class and cleanup some formatting.

See gh-2942
This commit is contained in:
Phillip Webb 2015-09-02 21:39:59 -07:00
parent 138d66706a
commit db41fb16c0
10 changed files with 176 additions and 127 deletions

View File

@ -20,9 +20,9 @@ import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager; import net.sf.ehcache.CacheManager;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ResourceCondition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ResourceCondition;
import org.springframework.cache.ehcache.EhCacheCacheManager; import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerUtils; import org.springframework.cache.ehcache.EhCacheManagerUtils;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;

View File

@ -19,10 +19,6 @@ package org.springframework.boot.autoconfigure.cache;
import java.io.Closeable; import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.spring.cache.HazelcastCacheManager;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@ -30,26 +26,31 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate; import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration; import org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration;
import org.springframework.boot.autoconfigure.hazelcast.HazelcastConfigResourceCondition; import org.springframework.boot.autoconfigure.hazelcast.HazelcastConfigResourceCondition;
import org.springframework.boot.autoconfigure.hazelcast.HazelcastInstanceFactory;
import org.springframework.cache.CacheManager; import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.spring.cache.HazelcastCacheManager;
/** /**
* Hazelcast cache configuration. Can either reuse the {@link HazelcastInstance} that * Hazelcast cache configuration. Can either reuse the {@link HazelcastInstance} that has
* has been configured by the general {@link HazelcastAutoConfiguration} or create * been configured by the general {@link HazelcastAutoConfiguration} or create a separate
* a separate one if the {@code spring.cache.hazelcast.config} property has been set. * one if the {@code spring.cache.hazelcast.config} property has been set.
* <p> * <p>
* If the {@link HazelcastAutoConfiguration} has been disabled, an attempt to configure * If the {@link HazelcastAutoConfiguration} has been disabled, an attempt to configure a
* a default {@link HazelcastInstance} is still made, using the same defaults. * default {@link HazelcastInstance} is still made, using the same defaults.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 1.3.0 * @since 1.3.0
* @see HazelcastConfigResourceCondition * @see HazelcastConfigResourceCondition
*/ */
@Configuration @Configuration
@ConditionalOnClass({HazelcastInstance.class, HazelcastCacheManager.class}) @ConditionalOnClass({ HazelcastInstance.class, HazelcastCacheManager.class })
@ConditionalOnMissingBean(CacheManager.class) @ConditionalOnMissingBean(CacheManager.class)
@Conditional(CacheCondition.class) @Conditional(CacheCondition.class)
@AutoConfigureAfter(HazelcastAutoConfiguration.class) @AutoConfigureAfter(HazelcastAutoConfiguration.class)
@ -63,19 +64,16 @@ class HazelcastCacheConfiguration {
private CacheProperties cacheProperties; private CacheProperties cacheProperties;
@Bean @Bean
public HazelcastCacheManager cacheManager(HazelcastInstance existingHazelcastInstance) public HazelcastCacheManager cacheManager(
throws IOException { HazelcastInstance existingHazelcastInstance) throws IOException {
Resource location = this.cacheProperties Resource config = this.cacheProperties.getHazelcast().getConfig();
.resolveConfigLocation(this.cacheProperties.getHazelcast().getConfig()); Resource location = this.cacheProperties.resolveConfigLocation(config);
if (location != null) { if (location != null) {
HazelcastInstance cacheHazelcastInstance = HazelcastInstance cacheHazelcastInstance = new HazelcastInstanceFactory(
HazelcastAutoConfiguration.createHazelcastInstance(location); location).getHazelcastInstance();
return new CloseableHazelcastCacheManager(cacheHazelcastInstance); return new CloseableHazelcastCacheManager(cacheHazelcastInstance);
} }
else { return new HazelcastCacheManager(existingHazelcastInstance);
return new HazelcastCacheManager(existingHazelcastInstance);
}
} }
} }
@ -89,10 +87,10 @@ class HazelcastCacheConfiguration {
@Bean @Bean
public HazelcastInstance hazelcastInstance() throws IOException { public HazelcastInstance hazelcastInstance() throws IOException {
Resource location = this.cacheProperties Resource config = this.cacheProperties.getHazelcast().getConfig();
.resolveConfigLocation(this.cacheProperties.getHazelcast().getConfig()); Resource location = this.cacheProperties.resolveConfigLocation(config);
if (location != null) { if (location != null) {
HazelcastAutoConfiguration.createHazelcastInstance(location); new HazelcastInstanceFactory(location).getHazelcastInstance();
} }
return Hazelcast.newHazelcastInstance(); return Hazelcast.newHazelcastInstance();
} }
@ -116,7 +114,9 @@ class HazelcastCacheConfiguration {
} }
private static class CloseableHazelcastCacheManager extends HazelcastCacheManager implements Closeable { private static class CloseableHazelcastCacheManager extends HazelcastCacheManager
implements Closeable {
private final HazelcastInstance hazelcastInstance; private final HazelcastInstance hazelcastInstance;
public CloseableHazelcastCacheManager(HazelcastInstance hazelcastInstance) { public CloseableHazelcastCacheManager(HazelcastInstance hazelcastInstance) {
@ -128,6 +128,7 @@ class HazelcastCacheConfiguration {
public void close() throws IOException { public void close() throws IOException {
this.hazelcastInstance.shutdown(); this.hazelcastInstance.shutdown();
} }
} }
} }

View File

@ -60,8 +60,8 @@ public abstract class ResourceCondition extends SpringBootCondition {
AnnotatedTypeMetadata metadata) { AnnotatedTypeMetadata metadata) {
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver( RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(
context.getEnvironment(), this.prefix); context.getEnvironment(), this.prefix);
if (resolver.containsProperty(propertyName)) { if (resolver.containsProperty(this.propertyName)) {
return ConditionOutcome.match("A '" + this.prefix + propertyName +"' " return ConditionOutcome.match("A '" + this.prefix + this.propertyName + "' "
+ "property is specified"); + "property is specified");
} }
return getResourceOutcome(context, metadata); return getResourceOutcome(context, metadata);

View File

@ -17,12 +17,6 @@
package org.springframework.boot.autoconfigure.hazelcast; package org.springframework.boot.autoconfigure.hazelcast;
import java.io.IOException; import java.io.IOException;
import java.net.URL;
import com.hazelcast.config.Config;
import com.hazelcast.config.XmlConfigBuilder;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@ -34,9 +28,10 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
import org.springframework.util.ResourceUtils; import com.hazelcast.config.Config;
import org.springframework.util.StringUtils; import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
/** /**
* {@link EnableAutoConfiguration Auto-configuration} for Hazelcast. Creates a * {@link EnableAutoConfiguration Auto-configuration} for Hazelcast. Creates a
@ -53,37 +48,8 @@ import org.springframework.util.StringUtils;
@EnableConfigurationProperties(HazelcastProperties.class) @EnableConfigurationProperties(HazelcastProperties.class)
public class HazelcastAutoConfiguration { public class HazelcastAutoConfiguration {
/**
* Create a {@link HazelcastInstance} based on the specified configuration location.
* @param location the location of the configuration file
* @return a {@link HazelcastInstance} for the specified configuration
* @throws IOException the configuration file could not be read
*/
public static HazelcastInstance createHazelcastInstance(Resource location)
throws IOException {
Assert.notNull(location, "Config must not be null");
URL configUrl = location.getURL();
Config config = new XmlConfigBuilder(configUrl).build();
if (ResourceUtils.isFileURL(configUrl)) {
config.setConfigurationFile(location.getFile());
}
else {
config.setConfigurationUrl(configUrl);
}
return createHazelcastInstance(config);
}
private static HazelcastInstance createHazelcastInstance(Config config) {
if (StringUtils.hasText(config.getInstanceName())) {
return Hazelcast.getOrCreateHazelcastInstance(config);
}
return Hazelcast.newHazelcastInstance(config);
}
@Configuration @Configuration
@ConditionalOnMissingBean({HazelcastInstance.class, Config.class}) @ConditionalOnMissingBean({ HazelcastInstance.class, Config.class })
@Conditional(ConfigAvailableCondition.class) @Conditional(ConfigAvailableCondition.class)
static class HazelcastConfigFileConfiguration { static class HazelcastConfigFileConfiguration {
@ -95,7 +61,7 @@ public class HazelcastAutoConfiguration {
public HazelcastInstance hazelcastInstance() throws IOException { public HazelcastInstance hazelcastInstance() throws IOException {
Resource config = this.hazelcastProperties.resolveConfigLocation(); Resource config = this.hazelcastProperties.resolveConfigLocation();
if (config != null) { if (config != null) {
return createHazelcastInstance(config); return new HazelcastInstanceFactory(config).getHazelcastInstance();
} }
return Hazelcast.newHazelcastInstance(); return Hazelcast.newHazelcastInstance();
} }
@ -109,7 +75,7 @@ public class HazelcastAutoConfiguration {
@Bean @Bean
public HazelcastInstance hazelcastInstance(Config config) { public HazelcastInstance hazelcastInstance(Config config) {
return createHazelcastInstance(config); return new HazelcastInstanceFactory(config).getHazelcastInstance();
} }
} }
@ -123,6 +89,7 @@ public class HazelcastAutoConfiguration {
public ConfigAvailableCondition() { public ConfigAvailableCondition() {
super("spring.hazelcast", "config"); super("spring.hazelcast", "config");
} }
} }
} }

View File

@ -23,9 +23,9 @@ import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata; import org.springframework.core.type.AnnotatedTypeMetadata;
/** /**
* {@link SpringBootCondition} used to check if the Hazelcast configuration is * {@link SpringBootCondition} used to check if the Hazelcast configuration is available.
* available. This either kicks in if a default configuration has been found or * This either kicks in if a default configuration has been found or if configurable
* if configurable property referring to the resource to use has been set. * property referring to the resource to use has been set.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 1.3.0 * @since 1.3.0
@ -43,8 +43,8 @@ public abstract class HazelcastConfigResourceCondition extends ResourceCondition
protected ConditionOutcome getResourceOutcome(ConditionContext context, protected ConditionOutcome getResourceOutcome(ConditionContext context,
AnnotatedTypeMetadata metadata) { AnnotatedTypeMetadata metadata) {
if (System.getProperty(CONFIG_SYSTEM_PROPERTY) != null) { if (System.getProperty(CONFIG_SYSTEM_PROPERTY) != null) {
return ConditionOutcome.match("System property '" return ConditionOutcome.match("System property '" + CONFIG_SYSTEM_PROPERTY
+ CONFIG_SYSTEM_PROPERTY + "' is set."); + "' is set.");
} }
return super.getResourceOutcome(context, metadata); return super.getResourceOutcome(context, metadata);
} }

View File

@ -0,0 +1,85 @@
/*
* Copyright 2012-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.boot.autoconfigure.hazelcast;
import java.io.IOException;
import java.net.URL;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;
import com.hazelcast.config.Config;
import com.hazelcast.config.XmlConfigBuilder;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
/**
* Factory that can be used to create a {@link HazelcastInstance}.
*
* @author Stephane Nicoll
* @author Phillip Webb
* @since 1.3.0
*/
public class HazelcastInstanceFactory {
private Config config;
/**
* Create a {@link HazelcastInstanceFactory} for the specified configuration location.
* @param configLocation the location of the configuration file
* @throws IOException if the configuration location could not be read
*/
public HazelcastInstanceFactory(Resource configLocation) throws IOException {
Assert.notNull(configLocation, "ConfigLocation must not be null");
this.config = getConfig(configLocation);
}
/**
* Create a {@link HazelcastInstanceFactory} for the specified configuration.
* @param config the configuration
*/
public HazelcastInstanceFactory(Config config) {
Assert.notNull(config, "Config must not be null");
this.config = config;
}
private Config getConfig(Resource configLocation) throws IOException {
URL configUrl = configLocation.getURL();
Config config = new XmlConfigBuilder(configUrl).build();
if (ResourceUtils.isFileURL(configUrl)) {
config.setConfigurationFile(configLocation.getFile());
}
else {
config.setConfigurationUrl(configUrl);
}
return config;
}
/**
* Get the {@link HazelcastInstance}.
* @return the {@link HazelcastInstance}
*/
public HazelcastInstance getHazelcastInstance() {
if (StringUtils.hasText(this.config.getInstanceName())) {
return Hazelcast.getOrCreateHazelcastInstance(this.config);
}
return Hazelcast.newHazelcastInstance(this.config);
}
}

View File

@ -49,12 +49,12 @@ public class HazelcastProperties {
* location * location
*/ */
public Resource resolveConfigLocation() { public Resource resolveConfigLocation() {
if (this.config != null) { if (this.config == null) {
Assert.isTrue(this.config.exists(), "Hazelcast configuration does not exist '" return null;
+ this.config.getDescription() + "'");
return this.config;
} }
return null; Assert.isTrue(this.config.exists(), "Hazelcast configuration does not exist '"
+ this.config.getDescription() + "'");
return this.config;
} }
} }

View File

@ -375,13 +375,12 @@ public class CacheAutoConfigurationTests {
Collection<Class<?>> configs = new ArrayList<Class<?>>(); Collection<Class<?>> configs = new ArrayList<Class<?>>();
configs.add(DefaultCacheConfiguration.class); configs.add(DefaultCacheConfiguration.class);
configs.add(HazelcastAutoConfiguration.class); configs.add(HazelcastAutoConfiguration.class);
String mainConfig = String mainConfig = "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml";
"org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml"; doLoad(configs, "spring.cache.type=hazelcast", "spring.hazelcast.config="
doLoad(configs, "spring.cache.type=hazelcast", + mainConfig);
"spring.hazelcast.config=" + mainConfig);
HazelcastCacheManager cacheManager = validateCacheManager(HazelcastCacheManager.class); HazelcastCacheManager cacheManager = validateCacheManager(HazelcastCacheManager.class);
HazelcastInstance hazelcastInstance = this.context.getBean(HazelcastInstance.class); HazelcastInstance hazelcastInstance = this.context
.getBean(HazelcastInstance.class);
assertThat( assertThat(
new DirectFieldAccessor(cacheManager) new DirectFieldAccessor(cacheManager)
.getPropertyValue("hazelcastInstance"), .getPropertyValue("hazelcastInstance"),
@ -398,17 +397,14 @@ public class CacheAutoConfigurationTests {
configs.add(HazelcastAutoConfiguration.class); configs.add(HazelcastAutoConfiguration.class);
String mainConfig = "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml"; String mainConfig = "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml";
String cacheConfig = "org/springframework/boot/autoconfigure/cache/hazelcast-specific.xml"; String cacheConfig = "org/springframework/boot/autoconfigure/cache/hazelcast-specific.xml";
doLoad(configs, "spring.cache.type=hazelcast", doLoad(configs, "spring.cache.type=hazelcast", "spring.cache.hazelcast.config="
"spring.cache.hazelcast.config=" + cacheConfig, + cacheConfig, "spring.hazelcast.config=" + mainConfig);
"spring.hazelcast.config=" + mainConfig); HazelcastInstance hazelcastInstance = this.context
HazelcastInstance hazelcastInstance = this.context.getBean(HazelcastInstance.class); .getBean(HazelcastInstance.class);
HazelcastCacheManager cacheManager = validateCacheManager(HazelcastCacheManager.class); HazelcastCacheManager cacheManager = validateCacheManager(HazelcastCacheManager.class);
HazelcastInstance cacheHazelcastInstance = (HazelcastInstance) HazelcastInstance cacheHazelcastInstance = (HazelcastInstance) new DirectFieldAccessor(
new DirectFieldAccessor(cacheManager).getPropertyValue("hazelcastInstance"); cacheManager).getPropertyValue("hazelcastInstance");
assertThat(cacheHazelcastInstance, is(not(hazelcastInstance))); // Our custom
assertThat(
cacheHazelcastInstance,
is(not(hazelcastInstance))); // Our custom cache instance
assertThat(hazelcastInstance.getConfig().getConfigurationFile(), assertThat(hazelcastInstance.getConfig().getConfigurationFile(),
is(new ClassPathResource(mainConfig).getFile())); is(new ClassPathResource(mainConfig).getFile()));
assertThat(cacheHazelcastInstance.getConfig().getConfigurationFile(), assertThat(cacheHazelcastInstance.getConfig().getConfigurationFile(),
@ -552,8 +548,7 @@ public class CacheAutoConfigurationTests {
doLoad(configs, environment); doLoad(configs, environment);
} }
private void doLoad(Collection<Class<?>> configs, private void doLoad(Collection<Class<?>> configs, String... environment) {
String... environment) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(applicationContext, environment); EnvironmentTestUtils.addEnvironment(applicationContext, environment);
for (Class<?> config : configs) { for (Class<?> config : configs) {

View File

@ -18,7 +18,6 @@ package org.springframework.boot.autoconfigure.condition;
import org.junit.After; import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@ -92,8 +91,7 @@ public class ResourceConditionTests {
} }
} }
private static class DefaultLocationResourceCondition extends private static class DefaultLocationResourceCondition extends ResourceCondition {
ResourceCondition {
public DefaultLocationResourceCondition() { public DefaultLocationResourceCondition() {
super("test", "spring.foo.test.", "config", "classpath:/logging.properties"); super("test", "spring.foo.test.", "config", "classpath:/logging.properties");

View File

@ -19,15 +19,10 @@ package org.springframework.boot.autoconfigure.hazelcast;
import java.io.IOException; import java.io.IOException;
import java.util.Map; import java.util.Map;
import com.hazelcast.config.Config;
import com.hazelcast.config.QueueConfig;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import org.junit.After; import org.junit.After;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@ -35,6 +30,11 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import com.hazelcast.config.Config;
import com.hazelcast.config.QueueConfig;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize; import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.hamcrest.collection.IsMapContaining.hasKey; import static org.hamcrest.collection.IsMapContaining.hasKey;
import static org.hamcrest.core.Is.is; import static org.hamcrest.core.Is.is;
@ -62,8 +62,8 @@ public class HazelcastAutoConfigurationTests {
@Test @Test
public void defaultConfigFile() throws IOException { public void defaultConfigFile() throws IOException {
load(); // hazelcast.xml present in root classpath load(); // hazelcast.xml present in root classpath
HazelcastInstance hazelcastInstance = this.context.getBean( HazelcastInstance hazelcastInstance = this.context
HazelcastInstance.class); .getBean(HazelcastInstance.class);
assertThat(hazelcastInstance.getConfig().getConfigurationUrl(), assertThat(hazelcastInstance.getConfig().getConfigurationUrl(),
is(new ClassPathResource("hazelcast.xml").getURL())); is(new ClassPathResource("hazelcast.xml").getURL()));
} }
@ -74,9 +74,10 @@ public class HazelcastAutoConfigurationTests {
"classpath:org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml"); "classpath:org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml");
try { try {
load(); load();
HazelcastInstance hazelcastInstance = this.context.getBean( HazelcastInstance hazelcastInstance = this.context
HazelcastInstance.class); .getBean(HazelcastInstance.class);
Map<String, QueueConfig> queueConfigs = hazelcastInstance.getConfig().getQueueConfigs(); Map<String, QueueConfig> queueConfigs = hazelcastInstance.getConfig()
.getQueueConfigs();
assertThat(queueConfigs.values(), hasSize(1)); assertThat(queueConfigs.values(), hasSize(1));
assertThat(queueConfigs, hasKey("foobar")); assertThat(queueConfigs, hasKey("foobar"));
} }
@ -87,20 +88,21 @@ public class HazelcastAutoConfigurationTests {
@Test @Test
public void explicitConfigFile() throws IOException { public void explicitConfigFile() throws IOException {
load("spring.hazelcast.config=org/springframework/boot/autoconfigure/hazelcast/" + load("spring.hazelcast.config=org/springframework/boot/autoconfigure/hazelcast/"
"hazelcast-specific.xml"); + "hazelcast-specific.xml");
HazelcastInstance hazelcastInstance = this.context.getBean( HazelcastInstance hazelcastInstance = this.context
HazelcastInstance.class); .getBean(HazelcastInstance.class);
assertThat(hazelcastInstance.getConfig().getConfigurationFile(), assertThat(hazelcastInstance.getConfig().getConfigurationFile(),
is(new ClassPathResource("org/springframework/boot/autoconfigure/hazelcast" + is(new ClassPathResource(
"/hazelcast-specific.xml").getFile())); "org/springframework/boot/autoconfigure/hazelcast"
+ "/hazelcast-specific.xml").getFile()));
} }
@Test @Test
public void explicitConfigUrl() throws IOException { public void explicitConfigUrl() throws IOException {
load("spring.hazelcast.config=hazelcast-default.xml"); load("spring.hazelcast.config=hazelcast-default.xml");
HazelcastInstance hazelcastInstance = this.context.getBean( HazelcastInstance hazelcastInstance = this.context
HazelcastInstance.class); .getBean(HazelcastInstance.class);
assertThat(hazelcastInstance.getConfig().getConfigurationUrl(), assertThat(hazelcastInstance.getConfig().getConfigurationUrl(),
is(new ClassPathResource("hazelcast-default.xml").getURL())); is(new ClassPathResource("hazelcast-default.xml").getURL()));
} }
@ -115,13 +117,15 @@ public class HazelcastAutoConfigurationTests {
@Test @Test
public void configInstanceWithName() { public void configInstanceWithName() {
Config config = new Config("my-test-instance"); Config config = new Config("my-test-instance");
HazelcastInstance existingHazelcastInstance = Hazelcast.newHazelcastInstance(config); HazelcastInstance existingHazelcastInstance = Hazelcast
.newHazelcastInstance(config);
try { try {
load(HazelcastConfigWithName.class, load(HazelcastConfigWithName.class,
"spring.hazelcast.config=this-is-ignored.xml"); "spring.hazelcast.config=this-is-ignored.xml");
HazelcastInstance hazelcastInstance = this.context.getBean( HazelcastInstance hazelcastInstance = this.context
HazelcastInstance.class); .getBean(HazelcastInstance.class);
assertThat(hazelcastInstance.getConfig().getInstanceName(), is("my-test-instance")); assertThat(hazelcastInstance.getConfig().getInstanceName(),
is("my-test-instance"));
// Should reuse any existing instance by default. // Should reuse any existing instance by default.
assertThat(hazelcastInstance, is(existingHazelcastInstance)); assertThat(hazelcastInstance, is(existingHazelcastInstance));
} }
@ -132,11 +136,11 @@ public class HazelcastAutoConfigurationTests {
@Test @Test
public void configInstanceWithoutName() { public void configInstanceWithoutName() {
load(HazelcastConfigNoName.class, load(HazelcastConfigNoName.class, "spring.hazelcast.config=this-is-ignored.xml");
"spring.hazelcast.config=this-is-ignored.xml"); HazelcastInstance hazelcastInstance = this.context
HazelcastInstance hazelcastInstance = this.context.getBean( .getBean(HazelcastInstance.class);
HazelcastInstance.class); Map<String, QueueConfig> queueConfigs = hazelcastInstance.getConfig()
Map<String, QueueConfig> queueConfigs = hazelcastInstance.getConfig().getQueueConfigs(); .getQueueConfigs();
assertThat(queueConfigs.values(), hasSize(1)); assertThat(queueConfigs.values(), hasSize(1));
assertThat(queueConfigs, hasKey("another-queue")); assertThat(queueConfigs, hasKey("another-queue"));
} }
@ -156,7 +160,6 @@ public class HazelcastAutoConfigurationTests {
this.context = applicationContext; this.context = applicationContext;
} }
@Configuration @Configuration
static class HazelcastConfigWithName { static class HazelcastConfigWithName {