From db41fb16c07cb76d076e46ec1ad62ec45aa124f5 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 2 Sep 2015 21:39:59 -0700 Subject: [PATCH] Polish Hazelcast auto-configuration Extract a HazelcastInstanceFactory class and cleanup some formatting. See gh-2942 --- .../cache/EhCacheCacheConfiguration.java | 2 +- .../cache/HazelcastCacheConfiguration.java | 49 +++++------ .../condition/ResourceCondition.java | 4 +- .../hazelcast/HazelcastAutoConfiguration.java | 49 ++--------- .../HazelcastConfigResourceCondition.java | 10 +-- .../hazelcast/HazelcastInstanceFactory.java | 85 +++++++++++++++++++ .../hazelcast/HazelcastProperties.java | 10 +-- .../cache/CacheAutoConfigurationTests.java | 31 +++---- .../condition/ResourceConditionTests.java | 4 +- .../HazelcastAutoConfigurationTests.java | 59 +++++++------ 10 files changed, 176 insertions(+), 127 deletions(-) create mode 100644 spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastInstanceFactory.java diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/EhCacheCacheConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/EhCacheCacheConfiguration.java index ee3642c0849..0fcdb9f5d94 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/EhCacheCacheConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/EhCacheCacheConfiguration.java @@ -20,9 +20,9 @@ import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; 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.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ResourceCondition; import org.springframework.cache.ehcache.EhCacheCacheManager; import org.springframework.cache.ehcache.EhCacheManagerUtils; import org.springframework.context.annotation.Bean; diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/HazelcastCacheConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/HazelcastCacheConfiguration.java index 137de632138..712e4d9d418 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/HazelcastCacheConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/HazelcastCacheConfiguration.java @@ -19,10 +19,6 @@ package org.springframework.boot.autoconfigure.cache; import java.io.Closeable; 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.boot.autoconfigure.AutoConfigureAfter; 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.hazelcast.HazelcastAutoConfiguration; import org.springframework.boot.autoconfigure.hazelcast.HazelcastConfigResourceCondition; +import org.springframework.boot.autoconfigure.hazelcast.HazelcastInstanceFactory; import org.springframework.cache.CacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; 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 - * has been configured by the general {@link HazelcastAutoConfiguration} or create - * a separate one if the {@code spring.cache.hazelcast.config} property has been set. + * Hazelcast cache configuration. Can either reuse the {@link HazelcastInstance} that has + * been configured by the general {@link HazelcastAutoConfiguration} or create a separate + * one if the {@code spring.cache.hazelcast.config} property has been set. *

- * If the {@link HazelcastAutoConfiguration} has been disabled, an attempt to configure - * a default {@link HazelcastInstance} is still made, using the same defaults. + * If the {@link HazelcastAutoConfiguration} has been disabled, an attempt to configure a + * default {@link HazelcastInstance} is still made, using the same defaults. * * @author Stephane Nicoll * @since 1.3.0 * @see HazelcastConfigResourceCondition */ @Configuration -@ConditionalOnClass({HazelcastInstance.class, HazelcastCacheManager.class}) +@ConditionalOnClass({ HazelcastInstance.class, HazelcastCacheManager.class }) @ConditionalOnMissingBean(CacheManager.class) @Conditional(CacheCondition.class) @AutoConfigureAfter(HazelcastAutoConfiguration.class) @@ -63,19 +64,16 @@ class HazelcastCacheConfiguration { private CacheProperties cacheProperties; @Bean - public HazelcastCacheManager cacheManager(HazelcastInstance existingHazelcastInstance) - throws IOException { - Resource location = this.cacheProperties - .resolveConfigLocation(this.cacheProperties.getHazelcast().getConfig()); + public HazelcastCacheManager cacheManager( + HazelcastInstance existingHazelcastInstance) throws IOException { + Resource config = this.cacheProperties.getHazelcast().getConfig(); + Resource location = this.cacheProperties.resolveConfigLocation(config); if (location != null) { - HazelcastInstance cacheHazelcastInstance = - HazelcastAutoConfiguration.createHazelcastInstance(location); + HazelcastInstance cacheHazelcastInstance = new HazelcastInstanceFactory( + location).getHazelcastInstance(); return new CloseableHazelcastCacheManager(cacheHazelcastInstance); } - else { - return new HazelcastCacheManager(existingHazelcastInstance); - } - + return new HazelcastCacheManager(existingHazelcastInstance); } } @@ -89,10 +87,10 @@ class HazelcastCacheConfiguration { @Bean public HazelcastInstance hazelcastInstance() throws IOException { - Resource location = this.cacheProperties - .resolveConfigLocation(this.cacheProperties.getHazelcast().getConfig()); + Resource config = this.cacheProperties.getHazelcast().getConfig(); + Resource location = this.cacheProperties.resolveConfigLocation(config); if (location != null) { - HazelcastAutoConfiguration.createHazelcastInstance(location); + new HazelcastInstanceFactory(location).getHazelcastInstance(); } 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; public CloseableHazelcastCacheManager(HazelcastInstance hazelcastInstance) { @@ -128,6 +128,7 @@ class HazelcastCacheConfiguration { public void close() throws IOException { this.hazelcastInstance.shutdown(); } + } } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ResourceCondition.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ResourceCondition.java index bb4afce96e7..7c68b2ec23d 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ResourceCondition.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ResourceCondition.java @@ -60,8 +60,8 @@ public abstract class ResourceCondition extends SpringBootCondition { AnnotatedTypeMetadata metadata) { RelaxedPropertyResolver resolver = new RelaxedPropertyResolver( context.getEnvironment(), this.prefix); - if (resolver.containsProperty(propertyName)) { - return ConditionOutcome.match("A '" + this.prefix + propertyName +"' " + if (resolver.containsProperty(this.propertyName)) { + return ConditionOutcome.match("A '" + this.prefix + this.propertyName + "' " + "property is specified"); } return getResourceOutcome(context, metadata); diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfiguration.java index e6a1e92ef6d..95f65ad6aae 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfiguration.java @@ -17,12 +17,6 @@ package org.springframework.boot.autoconfigure.hazelcast; 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.boot.autoconfigure.EnableAutoConfiguration; @@ -34,9 +28,10 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; 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.core.Hazelcast; +import com.hazelcast.core.HazelcastInstance; /** * {@link EnableAutoConfiguration Auto-configuration} for Hazelcast. Creates a @@ -53,37 +48,8 @@ import org.springframework.util.StringUtils; @EnableConfigurationProperties(HazelcastProperties.class) 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 - @ConditionalOnMissingBean({HazelcastInstance.class, Config.class}) + @ConditionalOnMissingBean({ HazelcastInstance.class, Config.class }) @Conditional(ConfigAvailableCondition.class) static class HazelcastConfigFileConfiguration { @@ -95,7 +61,7 @@ public class HazelcastAutoConfiguration { public HazelcastInstance hazelcastInstance() throws IOException { Resource config = this.hazelcastProperties.resolveConfigLocation(); if (config != null) { - return createHazelcastInstance(config); + return new HazelcastInstanceFactory(config).getHazelcastInstance(); } return Hazelcast.newHazelcastInstance(); } @@ -109,7 +75,7 @@ public class HazelcastAutoConfiguration { @Bean public HazelcastInstance hazelcastInstance(Config config) { - return createHazelcastInstance(config); + return new HazelcastInstanceFactory(config).getHazelcastInstance(); } } @@ -123,6 +89,7 @@ public class HazelcastAutoConfiguration { public ConfigAvailableCondition() { super("spring.hazelcast", "config"); } + } } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastConfigResourceCondition.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastConfigResourceCondition.java index f0d24996988..373c7c9844f 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastConfigResourceCondition.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastConfigResourceCondition.java @@ -23,9 +23,9 @@ import org.springframework.context.annotation.ConditionContext; import org.springframework.core.type.AnnotatedTypeMetadata; /** - * {@link SpringBootCondition} used to check if the Hazelcast configuration is - * available. This either kicks in if a default configuration has been found or - * if configurable property referring to the resource to use has been set. + * {@link SpringBootCondition} used to check if the Hazelcast configuration is available. + * This either kicks in if a default configuration has been found or if configurable + * property referring to the resource to use has been set. * * @author Stephane Nicoll * @since 1.3.0 @@ -43,8 +43,8 @@ public abstract class HazelcastConfigResourceCondition extends ResourceCondition protected ConditionOutcome getResourceOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { if (System.getProperty(CONFIG_SYSTEM_PROPERTY) != null) { - return ConditionOutcome.match("System property '" - + CONFIG_SYSTEM_PROPERTY + "' is set."); + return ConditionOutcome.match("System property '" + CONFIG_SYSTEM_PROPERTY + + "' is set."); } return super.getResourceOutcome(context, metadata); } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastInstanceFactory.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastInstanceFactory.java new file mode 100644 index 00000000000..07c29c65afb --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastInstanceFactory.java @@ -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); + } + +} diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastProperties.java index ba33e4f458b..61ef267818e 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastProperties.java @@ -49,12 +49,12 @@ public class HazelcastProperties { * location */ public Resource resolveConfigLocation() { - if (this.config != null) { - Assert.isTrue(this.config.exists(), "Hazelcast configuration does not exist '" - + this.config.getDescription() + "'"); - return this.config; + if (this.config == null) { + return null; } - return null; + Assert.isTrue(this.config.exists(), "Hazelcast configuration does not exist '" + + this.config.getDescription() + "'"); + return this.config; } } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java index ff57a16f7ec..b9a5cd5efa7 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java @@ -375,13 +375,12 @@ public class CacheAutoConfigurationTests { Collection> configs = new ArrayList>(); configs.add(DefaultCacheConfiguration.class); configs.add(HazelcastAutoConfiguration.class); - String mainConfig = - "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml"; - doLoad(configs, "spring.cache.type=hazelcast", - "spring.hazelcast.config=" + mainConfig); - + String mainConfig = "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml"; + doLoad(configs, "spring.cache.type=hazelcast", "spring.hazelcast.config=" + + mainConfig); HazelcastCacheManager cacheManager = validateCacheManager(HazelcastCacheManager.class); - HazelcastInstance hazelcastInstance = this.context.getBean(HazelcastInstance.class); + HazelcastInstance hazelcastInstance = this.context + .getBean(HazelcastInstance.class); assertThat( new DirectFieldAccessor(cacheManager) .getPropertyValue("hazelcastInstance"), @@ -398,17 +397,14 @@ public class CacheAutoConfigurationTests { configs.add(HazelcastAutoConfiguration.class); String mainConfig = "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml"; String cacheConfig = "org/springframework/boot/autoconfigure/cache/hazelcast-specific.xml"; - doLoad(configs, "spring.cache.type=hazelcast", - "spring.cache.hazelcast.config=" + cacheConfig, - "spring.hazelcast.config=" + mainConfig); - HazelcastInstance hazelcastInstance = this.context.getBean(HazelcastInstance.class); + doLoad(configs, "spring.cache.type=hazelcast", "spring.cache.hazelcast.config=" + + cacheConfig, "spring.hazelcast.config=" + mainConfig); + HazelcastInstance hazelcastInstance = this.context + .getBean(HazelcastInstance.class); HazelcastCacheManager cacheManager = validateCacheManager(HazelcastCacheManager.class); - HazelcastInstance cacheHazelcastInstance = (HazelcastInstance) - new DirectFieldAccessor(cacheManager).getPropertyValue("hazelcastInstance"); - - assertThat( - cacheHazelcastInstance, - is(not(hazelcastInstance))); // Our custom cache instance + HazelcastInstance cacheHazelcastInstance = (HazelcastInstance) new DirectFieldAccessor( + cacheManager).getPropertyValue("hazelcastInstance"); + assertThat(cacheHazelcastInstance, is(not(hazelcastInstance))); // Our custom assertThat(hazelcastInstance.getConfig().getConfigurationFile(), is(new ClassPathResource(mainConfig).getFile())); assertThat(cacheHazelcastInstance.getConfig().getConfigurationFile(), @@ -552,8 +548,7 @@ public class CacheAutoConfigurationTests { doLoad(configs, environment); } - private void doLoad(Collection> configs, - String... environment) { + private void doLoad(Collection> configs, String... environment) { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); EnvironmentTestUtils.addEnvironment(applicationContext, environment); for (Class config : configs) { diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ResourceConditionTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ResourceConditionTests.java index 22d60c9027c..3e80f695a6f 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ResourceConditionTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ResourceConditionTests.java @@ -18,7 +18,6 @@ package org.springframework.boot.autoconfigure.condition; import org.junit.After; import org.junit.Test; - import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; @@ -92,8 +91,7 @@ public class ResourceConditionTests { } } - private static class DefaultLocationResourceCondition extends - ResourceCondition { + private static class DefaultLocationResourceCondition extends ResourceCondition { public DefaultLocationResourceCondition() { super("test", "spring.foo.test.", "config", "classpath:/logging.properties"); diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationTests.java index 88a04d34fba..e5a29523007 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationTests.java @@ -19,15 +19,10 @@ package org.springframework.boot.autoconfigure.hazelcast; import java.io.IOException; 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.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; - import org.springframework.beans.factory.BeanCreationException; import org.springframework.boot.test.EnvironmentTestUtils; 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.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.IsMapContaining.hasKey; import static org.hamcrest.core.Is.is; @@ -62,8 +62,8 @@ public class HazelcastAutoConfigurationTests { @Test public void defaultConfigFile() throws IOException { load(); // hazelcast.xml present in root classpath - HazelcastInstance hazelcastInstance = this.context.getBean( - HazelcastInstance.class); + HazelcastInstance hazelcastInstance = this.context + .getBean(HazelcastInstance.class); assertThat(hazelcastInstance.getConfig().getConfigurationUrl(), is(new ClassPathResource("hazelcast.xml").getURL())); } @@ -74,9 +74,10 @@ public class HazelcastAutoConfigurationTests { "classpath:org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml"); try { load(); - HazelcastInstance hazelcastInstance = this.context.getBean( - HazelcastInstance.class); - Map queueConfigs = hazelcastInstance.getConfig().getQueueConfigs(); + HazelcastInstance hazelcastInstance = this.context + .getBean(HazelcastInstance.class); + Map queueConfigs = hazelcastInstance.getConfig() + .getQueueConfigs(); assertThat(queueConfigs.values(), hasSize(1)); assertThat(queueConfigs, hasKey("foobar")); } @@ -87,20 +88,21 @@ public class HazelcastAutoConfigurationTests { @Test public void explicitConfigFile() throws IOException { - load("spring.hazelcast.config=org/springframework/boot/autoconfigure/hazelcast/" + - "hazelcast-specific.xml"); - HazelcastInstance hazelcastInstance = this.context.getBean( - HazelcastInstance.class); + load("spring.hazelcast.config=org/springframework/boot/autoconfigure/hazelcast/" + + "hazelcast-specific.xml"); + HazelcastInstance hazelcastInstance = this.context + .getBean(HazelcastInstance.class); assertThat(hazelcastInstance.getConfig().getConfigurationFile(), - is(new ClassPathResource("org/springframework/boot/autoconfigure/hazelcast" + - "/hazelcast-specific.xml").getFile())); + is(new ClassPathResource( + "org/springframework/boot/autoconfigure/hazelcast" + + "/hazelcast-specific.xml").getFile())); } @Test public void explicitConfigUrl() throws IOException { load("spring.hazelcast.config=hazelcast-default.xml"); - HazelcastInstance hazelcastInstance = this.context.getBean( - HazelcastInstance.class); + HazelcastInstance hazelcastInstance = this.context + .getBean(HazelcastInstance.class); assertThat(hazelcastInstance.getConfig().getConfigurationUrl(), is(new ClassPathResource("hazelcast-default.xml").getURL())); } @@ -115,13 +117,15 @@ public class HazelcastAutoConfigurationTests { @Test public void configInstanceWithName() { Config config = new Config("my-test-instance"); - HazelcastInstance existingHazelcastInstance = Hazelcast.newHazelcastInstance(config); + HazelcastInstance existingHazelcastInstance = Hazelcast + .newHazelcastInstance(config); try { load(HazelcastConfigWithName.class, "spring.hazelcast.config=this-is-ignored.xml"); - HazelcastInstance hazelcastInstance = this.context.getBean( - HazelcastInstance.class); - assertThat(hazelcastInstance.getConfig().getInstanceName(), is("my-test-instance")); + HazelcastInstance hazelcastInstance = this.context + .getBean(HazelcastInstance.class); + assertThat(hazelcastInstance.getConfig().getInstanceName(), + is("my-test-instance")); // Should reuse any existing instance by default. assertThat(hazelcastInstance, is(existingHazelcastInstance)); } @@ -132,11 +136,11 @@ public class HazelcastAutoConfigurationTests { @Test public void configInstanceWithoutName() { - load(HazelcastConfigNoName.class, - "spring.hazelcast.config=this-is-ignored.xml"); - HazelcastInstance hazelcastInstance = this.context.getBean( - HazelcastInstance.class); - Map queueConfigs = hazelcastInstance.getConfig().getQueueConfigs(); + load(HazelcastConfigNoName.class, "spring.hazelcast.config=this-is-ignored.xml"); + HazelcastInstance hazelcastInstance = this.context + .getBean(HazelcastInstance.class); + Map queueConfigs = hazelcastInstance.getConfig() + .getQueueConfigs(); assertThat(queueConfigs.values(), hasSize(1)); assertThat(queueConfigs, hasKey("another-queue")); } @@ -156,7 +160,6 @@ public class HazelcastAutoConfigurationTests { this.context = applicationContext; } - @Configuration static class HazelcastConfigWithName {