From 3fbf1a2ea618cf50d350a975b2771494bfdb17c9 Mon Sep 17 00:00:00 2001 From: Vedran Pavic Date: Sun, 31 Jan 2016 20:18:29 +0100 Subject: [PATCH] Add auto-configuration support for Hazelcast client See gh-7469 --- spring-boot-autoconfigure/pom.xml | 5 + .../hazelcast/HazelcastAutoConfiguration.java | 57 ++----- .../HazelcastClientConfiguration.java | 83 ++++++++++ .../HazelcastConfigResourceCondition.java | 15 +- .../hazelcast/HazelcastInstanceFactory.java | 70 +++++--- .../HazelcastServerConfiguration.java | 84 ++++++++++ .../cache/CacheAutoConfigurationTests.java | 5 + ...HazelcastAutoConfigurationClientTests.java | 155 ++++++++++++++++++ ...azelcastAutoConfigurationServerTests.java} | 13 +- .../hazelcast/hazelcast-client-specific.xml | 6 + 10 files changed, 412 insertions(+), 81 deletions(-) create mode 100644 spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastClientConfiguration.java create mode 100644 spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastServerConfiguration.java create mode 100644 spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationClientTests.java rename spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/{HazelcastAutoConfigurationTests.java => HazelcastAutoConfigurationServerTests.java} (91%) create mode 100644 spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/hazelcast/hazelcast-client-specific.xml diff --git a/spring-boot-autoconfigure/pom.xml b/spring-boot-autoconfigure/pom.xml index 6bbd95a1de..2126c5c248 100755 --- a/spring-boot-autoconfigure/pom.xml +++ b/spring-boot-autoconfigure/pom.xml @@ -75,6 +75,11 @@ hazelcast true + + com.hazelcast + hazelcast-client + true + com.hazelcast hazelcast-spring 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 1eb7d23b81..ded7168543 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 @@ -16,21 +16,15 @@ package org.springframework.boot.autoconfigure.hazelcast; -import java.io.IOException; - -import com.hazelcast.config.Config; -import com.hazelcast.core.Hazelcast; +import com.hazelcast.client.HazelcastClient; import com.hazelcast.core.HazelcastInstance; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate; import org.springframework.boot.context.properties.EnableConfigurationProperties; -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.context.annotation.Import; /** * {@link EnableAutoConfiguration Auto-configuration} for Hazelcast. Creates a @@ -38,58 +32,27 @@ import org.springframework.core.io.Resource; * configuration file is found in the environment. * * @author Stephane Nicoll - * @author Madhura Bhave + * @author Vedran Pavic * @since 1.3.0 * @see HazelcastConfigResourceCondition */ @Configuration @ConditionalOnClass(HazelcastInstance.class) -@ConditionalOnMissingBean(HazelcastInstance.class) @EnableConfigurationProperties(HazelcastProperties.class) public class HazelcastAutoConfiguration { @Configuration - @ConditionalOnMissingBean(Config.class) - @Conditional(ConfigAvailableCondition.class) - static class HazelcastConfigFileConfiguration { - - private final HazelcastProperties hazelcastProperties; - - HazelcastConfigFileConfiguration(HazelcastProperties hazelcastProperties) { - this.hazelcastProperties = hazelcastProperties; - } - - @Bean - public HazelcastInstance hazelcastInstance() throws IOException { - Resource config = this.hazelcastProperties.resolveConfigLocation(); - if (config != null) { - return new HazelcastInstanceFactory(config).getHazelcastInstance(); - } - return Hazelcast.newHazelcastInstance(); - } + @ConditionalOnMissingBean(HazelcastInstance.class) + @Import(HazelcastServerConfiguration.class) + static class ServerConfiguration { } @Configuration - @ConditionalOnSingleCandidate(Config.class) - static class HazelcastConfigConfiguration { - - @Bean - public HazelcastInstance hazelcastInstance(Config config) { - return new HazelcastInstanceFactory(config).getHazelcastInstance(); - } - - } - - /** - * {@link HazelcastConfigResourceCondition} that checks if the - * {@code spring.hazelcast.config} configuration key is defined. - */ - static class ConfigAvailableCondition extends HazelcastConfigResourceCondition { - - ConfigAvailableCondition() { - super("spring.hazelcast.config"); - } + @ConditionalOnClass(HazelcastClient.class) + @ConditionalOnMissingBean(HazelcastInstance.class) + @Import(HazelcastClientConfiguration.class) + static class ClientConfiguration { } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastClientConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastClientConfiguration.java new file mode 100644 index 0000000000..ba3dc3b203 --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastClientConfiguration.java @@ -0,0 +1,83 @@ +/* + * Copyright 2012-2017 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 com.hazelcast.client.HazelcastClient; +import com.hazelcast.client.config.ClientConfig; +import com.hazelcast.core.HazelcastInstance; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Conditional; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.Resource; + +/** + * Configuration for Hazelcast client. + * + * @author Vedran Pavic + * @since 2.0.0 + */ +class HazelcastClientConfiguration { + + static final String CONFIG_SYSTEM_PROPERTY = "hazelcast.client.config"; + + @Configuration + @ConditionalOnMissingBean(ClientConfig.class) + @Conditional(ConfigAvailableCondition.class) + static class HazelcastClientConfigFileConfiguration { + + @Bean + public HazelcastInstance hazelcastInstance(HazelcastProperties properties) + throws IOException { + Resource config = properties.resolveConfigLocation(); + if (config != null) { + return HazelcastInstanceFactory.createHazelcastClient(config); + } + return HazelcastClient.newHazelcastClient(); + } + + } + + @Configuration + @ConditionalOnSingleCandidate(ClientConfig.class) + static class HazelcastClientConfigConfiguration { + + @Bean + public HazelcastInstance hazelcastInstance(ClientConfig config) { + return HazelcastInstanceFactory.createHazelcastClient(config); + } + + } + + /** + * {@link HazelcastConfigResourceCondition} that checks if the + * {@code spring.hazelcast.config} configuration key is defined. + */ + static class ConfigAvailableCondition extends HazelcastConfigResourceCondition { + + ConfigAvailableCondition() { + super(CONFIG_SYSTEM_PROPERTY, "file:./hazelcast-client.xml", + "classpath:/hazelcast-client.xml"); + } + + } + +} 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 0bec54d50a..7de04e03c6 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 @@ -21,6 +21,7 @@ import org.springframework.boot.autoconfigure.condition.ResourceCondition; import org.springframework.boot.autoconfigure.condition.SpringBootCondition; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.type.AnnotatedTypeMetadata; +import org.springframework.util.Assert; /** * {@link SpringBootCondition} used to check if the Hazelcast configuration is available. @@ -29,22 +30,26 @@ import org.springframework.core.type.AnnotatedTypeMetadata; * * @author Stephane Nicoll * @author Madhura Bhave + * @author Vedran Pavic * @since 1.3.0 */ public abstract class HazelcastConfigResourceCondition extends ResourceCondition { - static final String CONFIG_SYSTEM_PROPERTY = "hazelcast.config"; + private final String configSystemProperty; - protected HazelcastConfigResourceCondition(String property) { - super("Hazelcast", property, "file:./hazelcast.xml", "classpath:/hazelcast.xml"); + protected HazelcastConfigResourceCondition(String configSystemProperty, + String... resourceLocations) { + super("Hazelcast", "spring.hazelcast.config", resourceLocations); + Assert.notNull(configSystemProperty, "ConfigSystemProperty must not be null"); + this.configSystemProperty = configSystemProperty; } @Override protected ConditionOutcome getResourceOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { - if (System.getProperty(CONFIG_SYSTEM_PROPERTY) != null) { + if (System.getProperty(this.configSystemProperty) != null) { return ConditionOutcome.match(startConditionMessage() - .because("System property '" + CONFIG_SYSTEM_PROPERTY + "' is set.")); + .because("System property '" + this.configSystemProperty + "' 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 index fffd240055..1369860f1a 100644 --- 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2015 the original author or authors. + * Copyright 2012-2017 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. @@ -19,6 +19,9 @@ package org.springframework.boot.autoconfigure.hazelcast; import java.io.IOException; import java.net.URL; +import com.hazelcast.client.HazelcastClient; +import com.hazelcast.client.config.ClientConfig; +import com.hazelcast.client.config.XmlClientConfigBuilder; import com.hazelcast.config.Config; import com.hazelcast.config.XmlConfigBuilder; import com.hazelcast.core.Hazelcast; @@ -34,32 +37,33 @@ import org.springframework.util.StringUtils; * * @author Stephane Nicoll * @author Phillip Webb + * @author Vedran Pavic * @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); - } +public abstract class HazelcastInstanceFactory { /** - * Create a {@link HazelcastInstanceFactory} for the specified configuration. + * Get the {@link HazelcastInstance}. * @param config the configuration + * @return the {@link HazelcastInstance} */ - public HazelcastInstanceFactory(Config config) { + public static HazelcastInstance createHazelcastInstance(Config config) { Assert.notNull(config, "Config must not be null"); - this.config = config; + if (StringUtils.hasText(config.getInstanceName())) { + return Hazelcast.getOrCreateHazelcastInstance(config); + } + return Hazelcast.newHazelcastInstance(config); } - private Config getConfig(Resource configLocation) throws IOException { + /** + * Get the {@link HazelcastInstance}. + * @param configLocation the location of the configuration file + * @return the {@link HazelcastInstance} + * @throws IOException if the configuration location could not be read + */ + public static HazelcastInstance createHazelcastInstance(Resource configLocation) + throws IOException { + Assert.notNull(configLocation, "ConfigLocation must not be null"); URL configUrl = configLocation.getURL(); Config config = new XmlConfigBuilder(configUrl).build(); if (ResourceUtils.isFileURL(configUrl)) { @@ -68,18 +72,34 @@ public class HazelcastInstanceFactory { else { config.setConfigurationUrl(configUrl); } - return config; + return createHazelcastInstance(config); } /** - * Get the {@link HazelcastInstance}. - * @return the {@link HazelcastInstance} + * Get the client {@link HazelcastInstance}. + * @param config the client configuration + * @return the client {@link HazelcastInstance} */ - public HazelcastInstance getHazelcastInstance() { - if (StringUtils.hasText(this.config.getInstanceName())) { - return Hazelcast.getOrCreateHazelcastInstance(this.config); + public static HazelcastInstance createHazelcastClient(ClientConfig config) { + Assert.notNull(config, "Config must not be null"); + if (StringUtils.hasText(config.getInstanceName())) { + return HazelcastClient.getHazelcastClientByName(config.getInstanceName()); } - return Hazelcast.newHazelcastInstance(this.config); + return HazelcastClient.newHazelcastClient(config); + } + + /** + * Get the client {@link HazelcastInstance}. + * @param configLocation the location of the client configuration file + * @return the client {@link HazelcastInstance} + * @throws IOException if the configuration location could not be read + */ + public static HazelcastInstance createHazelcastClient(Resource configLocation) + throws IOException { + Assert.notNull(configLocation, "ConfigLocation must not be null"); + URL configUrl = configLocation.getURL(); + ClientConfig config = new XmlClientConfigBuilder(configUrl).build(); + return createHazelcastClient(config); } } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastServerConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastServerConfiguration.java new file mode 100644 index 0000000000..8b84976cbc --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastServerConfiguration.java @@ -0,0 +1,84 @@ +/* + * Copyright 2012-2017 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 com.hazelcast.config.Config; +import com.hazelcast.core.Hazelcast; +import com.hazelcast.core.HazelcastInstance; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Conditional; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.Resource; + +/** + * Configuration for Hazelcast server. + * + * @author Stephane Nicoll + * @author Vedran Pavic + * @since 2.0.0 + */ +class HazelcastServerConfiguration { + + static final String CONFIG_SYSTEM_PROPERTY = "hazelcast.config"; + + @Configuration + @ConditionalOnMissingBean(Config.class) + @Conditional(ConfigAvailableCondition.class) + static class HazelcastServerConfigFileConfiguration { + + @Bean + public HazelcastInstance hazelcastInstance(HazelcastProperties properties) + throws IOException { + Resource config = properties.resolveConfigLocation(); + if (config != null) { + return HazelcastInstanceFactory.createHazelcastInstance(config); + } + return Hazelcast.newHazelcastInstance(); + } + + } + + @Configuration + @ConditionalOnSingleCandidate(Config.class) + static class HazelcastServerConfigConfiguration { + + @Bean + public HazelcastInstance hazelcastInstance(Config config) { + return HazelcastInstanceFactory.createHazelcastInstance(config); + } + + } + + /** + * {@link HazelcastConfigResourceCondition} that checks if the + * {@code spring.hazelcast.config} configuration key is defined. + */ + static class ConfigAvailableCondition extends HazelcastConfigResourceCondition { + + ConfigAvailableCondition() { + super(CONFIG_SYSTEM_PROPERTY, "file:./hazelcast.xml", + "classpath:/hazelcast.xml"); + } + + } + +} 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 65258ec12c..7f40b3e1fe 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 @@ -49,12 +49,15 @@ import org.junit.After; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; import org.springframework.beans.DirectFieldAccessor; import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.boot.autoconfigure.cache.support.MockCachingProvider; import org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration; +import org.springframework.boot.junit.runner.classpath.ClassPathExclusions; +import org.springframework.boot.junit.runner.classpath.ModifiedClassPathRunner; import org.springframework.boot.test.util.EnvironmentTestUtils; import org.springframework.cache.Cache; import org.springframework.cache.CacheManager; @@ -91,6 +94,8 @@ import static org.mockito.Mockito.verify; * @author Stephane Nicoll * @author EddĂș MelĂ©ndez */ +@RunWith(ModifiedClassPathRunner.class) +@ClassPathExclusions("hazelcast-client-*.jar") public class CacheAutoConfigurationTests { @Rule diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationClientTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationClientTests.java new file mode 100644 index 0000000000..145a5452a9 --- /dev/null +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationClientTests.java @@ -0,0 +1,155 @@ +/* + * Copyright 2012-2017 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 com.hazelcast.client.config.ClientConfig; +import com.hazelcast.client.impl.HazelcastClientProxy; +import com.hazelcast.config.Config; +import com.hazelcast.core.Hazelcast; +import com.hazelcast.core.HazelcastInstance; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import org.springframework.beans.factory.BeanCreationException; +import org.springframework.boot.test.util.EnvironmentTestUtils; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for client {@link HazelcastAutoConfiguration}. + * + * @author Vedran Pavic + */ +public class HazelcastAutoConfigurationClientTests { + + @Rule + public final ExpectedException thrown = ExpectedException.none(); + + private AnnotationConfigApplicationContext context; + + @After + public void closeContext() { + if (this.context != null) { + this.context.close(); + } + } + + private static HazelcastInstance hazelcastInstance; + + @BeforeClass + public static void init() { + hazelcastInstance = Hazelcast.newHazelcastInstance(); + } + + @AfterClass + public static void close() { + if (hazelcastInstance != null) { + hazelcastInstance.shutdown(); + } + } + + @Test + public void systemProperty() throws IOException { + System.setProperty(HazelcastClientConfiguration.CONFIG_SYSTEM_PROPERTY, + "classpath:org/springframework/boot/autoconfigure/hazelcast/" + + "hazelcast-client-specific.xml"); + try { + load(); + HazelcastInstance hazelcastInstance = this.context + .getBean(HazelcastInstance.class); + assertThat(hazelcastInstance).isInstanceOf(HazelcastClientProxy.class); + assertThat(hazelcastInstance.getName()).startsWith("hz.client_"); + } + finally { + System.clearProperty(HazelcastClientConfiguration.CONFIG_SYSTEM_PROPERTY); + } + } + + @Test + public void explicitConfigFile() throws IOException { + load("spring.hazelcast.config=org/springframework/boot/autoconfigure/" + + "hazelcast/hazelcast-client-specific.xml"); + HazelcastInstance hazelcastInstance = this.context + .getBean(HazelcastInstance.class); + assertThat(hazelcastInstance).isInstanceOf(HazelcastClientProxy.class); + assertThat(hazelcastInstance.getName()).startsWith("hz.client_"); + } + + @Test + public void explicitConfigUrl() throws IOException { + load("spring.hazelcast.config=hazelcast-client-default.xml"); + HazelcastInstance hazelcastInstance = this.context + .getBean(HazelcastInstance.class); + assertThat(hazelcastInstance).isInstanceOf(HazelcastClientProxy.class); + assertThat(hazelcastInstance.getName()).startsWith("hz.client_"); + } + + @Test + public void unknownConfigFile() { + this.thrown.expect(BeanCreationException.class); + this.thrown.expectMessage("foo/bar/unknown.xml"); + load("spring.hazelcast.config=foo/bar/unknown.xml"); + } + + @Test + public void clientConfigHasPriority() { + load(HazelcastServerAndClientConfig.class, "spring.hazelcast.config=this-is-ignored.xml"); + HazelcastInstance hazelcastInstance = this.context + .getBean(HazelcastInstance.class); + assertThat(hazelcastInstance).isInstanceOf(HazelcastClientProxy.class); + } + + private void load(String... environment) { + load(null, environment); + } + + private void load(Class config, String... environment) { + AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); + EnvironmentTestUtils.addEnvironment(applicationContext, environment); + if (config != null) { + applicationContext.register(config); + } + applicationContext.register(HazelcastAutoConfiguration.class); + applicationContext.refresh(); + this.context = applicationContext; + } + + @Configuration + static class HazelcastServerAndClientConfig { + + @Bean + public Config config() { + return new Config(); + } + + @Bean + public ClientConfig clientConfig() { + return new ClientConfig(); + } + + } + +} 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/HazelcastAutoConfigurationServerTests.java similarity index 91% rename from spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationTests.java rename to spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationServerTests.java index ef20564aed..ba25620d04 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/HazelcastAutoConfigurationServerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 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. @@ -27,8 +27,11 @@ import org.junit.After; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; import org.springframework.beans.factory.BeanCreationException; +import org.springframework.boot.junit.runner.classpath.ClassPathExclusions; +import org.springframework.boot.junit.runner.classpath.ModifiedClassPathRunner; import org.springframework.boot.test.util.EnvironmentTestUtils; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; @@ -42,7 +45,9 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Stephane Nicoll */ -public class HazelcastAutoConfigurationTests { +@RunWith(ModifiedClassPathRunner.class) +@ClassPathExclusions("hazelcast-client-*.jar") +public class HazelcastAutoConfigurationServerTests { @Rule public final ExpectedException thrown = ExpectedException.none(); @@ -67,7 +72,7 @@ public class HazelcastAutoConfigurationTests { @Test public void systemProperty() throws IOException { - System.setProperty(HazelcastConfigResourceCondition.CONFIG_SYSTEM_PROPERTY, + System.setProperty(HazelcastServerConfiguration.CONFIG_SYSTEM_PROPERTY, "classpath:org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml"); try { load(); @@ -78,7 +83,7 @@ public class HazelcastAutoConfigurationTests { assertThat(queueConfigs).hasSize(1).containsKey("foobar"); } finally { - System.clearProperty(HazelcastConfigResourceCondition.CONFIG_SYSTEM_PROPERTY); + System.clearProperty(HazelcastServerConfiguration.CONFIG_SYSTEM_PROPERTY); } } diff --git a/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/hazelcast/hazelcast-client-specific.xml b/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/hazelcast/hazelcast-client-specific.xml new file mode 100644 index 0000000000..e5596bf220 --- /dev/null +++ b/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/hazelcast/hazelcast-client-specific.xml @@ -0,0 +1,6 @@ + + + +