Migrate Hazelcast 3 sanity tests to Hazelcast 4

Closes gh-31881
This commit is contained in:
Stephane Nicoll 2022-07-27 09:09:03 +02:00
parent bfc703a40d
commit 9cb614c626
7 changed files with 67 additions and 41 deletions

View File

@ -40,18 +40,18 @@ import static org.mockito.Mockito.mock;
* @author Stephane Nicoll * @author Stephane Nicoll
*/ */
@ClassPathExclusions("hazelcast*.jar") @ClassPathExclusions("hazelcast*.jar")
@ClassPathOverrides("com.hazelcast:hazelcast:3.12.12") @ClassPathOverrides("com.hazelcast:hazelcast:4.2.5")
class Hazelcast3HazelcastHealthIndicatorTests { class Hazelcast4HazelcastHealthIndicatorTests {
@Test @Test
void hazelcastUp() { void hazelcastUp() {
new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(HazelcastAutoConfiguration.class)) new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(HazelcastAutoConfiguration.class))
.withPropertyValues("spring.hazelcast.config=hazelcast-3.xml").run((context) -> { .withPropertyValues("spring.hazelcast.config=hazelcast-4.xml").run((context) -> {
HazelcastInstance hazelcast = context.getBean(HazelcastInstance.class); HazelcastInstance hazelcast = context.getBean(HazelcastInstance.class);
Health health = new HazelcastHealthIndicator(hazelcast).health(); Health health = new HazelcastHealthIndicator(hazelcast).health();
assertThat(health.getStatus()).isEqualTo(Status.UP); assertThat(health.getStatus()).isEqualTo(Status.UP);
assertThat(health.getDetails()).containsOnlyKeys("name", "uuid").containsEntry("name", assertThat(health.getDetails()).containsOnlyKeys("name", "uuid").containsEntry("name",
"actuator-hazelcast-3"); "actuator-hazelcast-4");
assertThat(health.getDetails().get("uuid")).asString().isNotEmpty(); assertThat(health.getDetails().get("uuid")).asString().isNotEmpty();
}); });
} }

View File

@ -1,12 +1,12 @@
<hazelcast xmlns="http://www.hazelcast.com/schema/config" <hazelcast xmlns="http://www.hazelcast.com/schema/config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.hazelcast.com/schema/config xsi:schemaLocation="http://www.hazelcast.com/schema/config
http://www.hazelcast.com/schema/config/hazelcast-config-3.12.xsd"> http://www.hazelcast.com/schema/config/hazelcast-config-4.2.xsd">
<instance-name>actuator-hazelcast-3</instance-name> <instance-name>actuator-hazelcast-4</instance-name>
<map name="defaultCache"/> <map name="defaultCache"/>
<network> <network>
<join> <join>
<tcp-ip enabled="false"/> <auto-detection enabled="false"/>
<multicast enabled="false"/> <multicast enabled="false"/>
</join> </join>
</network> </network>

View File

@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.hazelcast;
import com.hazelcast.client.impl.clientside.HazelcastClientProxy; import com.hazelcast.client.impl.clientside.HazelcastClientProxy;
import com.hazelcast.config.Config; import com.hazelcast.config.Config;
import com.hazelcast.config.JoinConfig;
import com.hazelcast.core.Hazelcast; import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance; import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.spring.context.SpringManagedContext; import com.hazelcast.spring.context.SpringManagedContext;
@ -30,42 +31,43 @@ import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.test.context.runner.ContextConsumer; import org.springframework.boot.test.context.runner.ContextConsumer;
import org.springframework.boot.testsupport.classpath.ClassPathExclusions; import org.springframework.boot.testsupport.classpath.ClassPathExclusions;
import org.springframework.boot.testsupport.classpath.ClassPathOverrides; import org.springframework.boot.testsupport.classpath.ClassPathOverrides;
import org.springframework.core.io.ClassPathResource;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
/** /**
* Tests for {@link HazelcastAutoConfiguration} with Hazelcast 3. * Tests for {@link HazelcastAutoConfiguration} with Hazelcast 4.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
*/ */
@ClassPathExclusions("hazelcast*.jar") @ClassPathExclusions("hazelcast*.jar")
@ClassPathOverrides({ "com.hazelcast:hazelcast:3.12.12", "com.hazelcast:hazelcast-client:3.12.12", @ClassPathOverrides({ "com.hazelcast:hazelcast:4.2.5", "com.hazelcast:hazelcast-spring:4.2.5" })
"com.hazelcast:hazelcast-spring:3.12.12" }) class Hazelcast4AutoConfigurationTests {
class Hazelcast3AutoConfigurationTests {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(HazelcastAutoConfiguration.class)); .withConfiguration(AutoConfigurations.of(HazelcastAutoConfiguration.class));
@Test @Test
void defaultConfigFile() { void serverConfig() {
// no hazelcast-client.xml and hazelcast.xml is present in root classpath this.contextRunner.withPropertyValues(
// this also asserts that XML has priority over YAML "spring.hazelcast.config=org/springframework/boot/autoconfigure/hazelcast/hazelcast-4-server.xml")
// as both hazelcast.yaml and hazelcast.xml in test classpath. .run((context) -> {
this.contextRunner.run((context) -> { Config config = context.getBean(HazelcastInstance.class).getConfig();
Config config = context.getBean(HazelcastInstance.class).getConfig(); assertThat(config.getInstanceName()).isEqualTo("explicit-server");
assertThat(config.getConfigurationUrl()).isEqualTo(new ClassPathResource("hazelcast.xml").getURL()); });
});
} }
@Test @Test
void explicitConfigFileWithXml() { void explicitConfigFileWithXml() {
HazelcastInstance hazelcastServer = Hazelcast.newHazelcastInstance(); Config config = new Config();
JoinConfig join = config.getNetworkConfig().getJoin();
join.getAutoDetectionConfig().setEnabled(false);
join.getMulticastConfig().setEnabled(false);
HazelcastInstance hazelcastServer = Hazelcast.newHazelcastInstance(config);
try { try {
this.contextRunner this.contextRunner
.withPropertyValues("spring.hazelcast.config=org/springframework/boot/autoconfigure/" .withPropertyValues("spring.hazelcast.config="
+ "hazelcast/hazelcast-client-specific.xml") + "org/springframework/boot/autoconfigure/hazelcast/hazelcast-4-client.xml")
.run(assertSpecificHazelcastClient("explicit-xml")); .run(assertSpecificHazelcastClient("explicit-client"));
} }
finally { finally {
hazelcastServer.shutdown(); hazelcastServer.shutdown();
@ -74,10 +76,12 @@ class Hazelcast3AutoConfigurationTests {
@Test @Test
void autoConfiguredConfigUsesSpringManagedContext() { void autoConfiguredConfigUsesSpringManagedContext() {
this.contextRunner.run((context) -> { this.contextRunner.withPropertyValues(
Config config = context.getBean(HazelcastInstance.class).getConfig(); "spring.hazelcast.config=" + "org/springframework/boot/autoconfigure/hazelcast/hazelcast-4-server.xml")
assertThat(config.getManagedContext()).isInstanceOf(SpringManagedContext.class); .run((context) -> {
}); Config config = context.getBean(HazelcastInstance.class).getConfig();
assertThat(config.getManagedContext()).isInstanceOf(SpringManagedContext.class);
});
} }
private ContextConsumer<AssertableApplicationContext> assertSpecificHazelcastClient(String label) { private ContextConsumer<AssertableApplicationContext> assertSpecificHazelcastClient(String label) {

View File

@ -29,24 +29,25 @@ import org.springframework.session.FlushMode;
import org.springframework.session.SaveMode; import org.springframework.session.SaveMode;
import org.springframework.session.data.mongo.MongoIndexedSessionRepository; import org.springframework.session.data.mongo.MongoIndexedSessionRepository;
import org.springframework.session.data.redis.RedisIndexedSessionRepository; import org.springframework.session.data.redis.RedisIndexedSessionRepository;
import org.springframework.session.hazelcast.HazelcastIndexedSessionRepository; import org.springframework.session.hazelcast.Hazelcast4IndexedSessionRepository;
import org.springframework.session.jdbc.JdbcIndexedSessionRepository; import org.springframework.session.jdbc.JdbcIndexedSessionRepository;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
/** /**
* Hazelcast 3 specific tests for {@link SessionAutoConfiguration}. * Hazelcast 4 specific tests for {@link SessionAutoConfiguration}.
* *
* @author Vedran Pavic * @author Vedran Pavic
* @author Stephane Nicoll
*/ */
@ClassPathExclusions("hazelcast*.jar") @ClassPathExclusions("hazelcast*.jar")
@ClassPathOverrides("com.hazelcast:hazelcast:3.12.12") @ClassPathOverrides("com.hazelcast:hazelcast:4.2.5")
class SessionAutoConfigurationHazelcast3Tests extends AbstractSessionAutoConfigurationTests { class SessionAutoConfigurationHazelcast4Tests extends AbstractSessionAutoConfigurationTests {
private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(HazelcastAutoConfiguration.class, SessionAutoConfiguration.class)) .withConfiguration(AutoConfigurations.of(HazelcastAutoConfiguration.class, SessionAutoConfiguration.class))
.withPropertyValues( .withPropertyValues(
"spring.hazelcast.config=org/springframework/boot/autoconfigure/session/hazelcast-3.xml"); "spring.hazelcast.config=org/springframework/boot/autoconfigure/session/hazelcast-4.xml");
@Test @Test
void defaultConfig() { void defaultConfig() {
@ -62,7 +63,7 @@ class SessionAutoConfigurationHazelcast3Tests extends AbstractSessionAutoConfigu
} }
private void validateDefaultConfig(AssertableWebApplicationContext context) { private void validateDefaultConfig(AssertableWebApplicationContext context) {
validateSessionRepository(context, HazelcastIndexedSessionRepository.class); validateSessionRepository(context, Hazelcast4IndexedSessionRepository.class);
} }
@Test @Test
@ -70,15 +71,15 @@ class SessionAutoConfigurationHazelcast3Tests extends AbstractSessionAutoConfigu
this.contextRunner this.contextRunner
.withPropertyValues("spring.session.store-type=hazelcast", .withPropertyValues("spring.session.store-type=hazelcast",
"spring.session.hazelcast.map-name=foo:bar:biz") "spring.session.hazelcast.map-name=foo:bar:biz")
.run((context) -> validateSessionRepository(context, HazelcastIndexedSessionRepository.class)); .run((context) -> validateSessionRepository(context, Hazelcast4IndexedSessionRepository.class));
} }
@Test @Test
void customFlushMode() { void customFlushMode() {
this.contextRunner.withPropertyValues("spring.session.store-type=hazelcast", this.contextRunner.withPropertyValues("spring.session.store-type=hazelcast",
"spring.session.hazelcast.flush-mode=immediate").run((context) -> { "spring.session.hazelcast.flush-mode=immediate").run((context) -> {
HazelcastIndexedSessionRepository repository = validateSessionRepository(context, Hazelcast4IndexedSessionRepository repository = validateSessionRepository(context,
HazelcastIndexedSessionRepository.class); Hazelcast4IndexedSessionRepository.class);
assertThat(repository).hasFieldOrPropertyWithValue("flushMode", FlushMode.IMMEDIATE); assertThat(repository).hasFieldOrPropertyWithValue("flushMode", FlushMode.IMMEDIATE);
}); });
} }
@ -87,8 +88,8 @@ class SessionAutoConfigurationHazelcast3Tests extends AbstractSessionAutoConfigu
void customSaveMode() { void customSaveMode() {
this.contextRunner.withPropertyValues("spring.session.store-type=hazelcast", this.contextRunner.withPropertyValues("spring.session.store-type=hazelcast",
"spring.session.hazelcast.save-mode=on-get-attribute").run((context) -> { "spring.session.hazelcast.save-mode=on-get-attribute").run((context) -> {
HazelcastIndexedSessionRepository repository = validateSessionRepository(context, Hazelcast4IndexedSessionRepository repository = validateSessionRepository(context,
HazelcastIndexedSessionRepository.class); Hazelcast4IndexedSessionRepository.class);
assertThat(repository).hasFieldOrPropertyWithValue("saveMode", SaveMode.ON_GET_ATTRIBUTE); assertThat(repository).hasFieldOrPropertyWithValue("saveMode", SaveMode.ON_GET_ATTRIBUTE);
}); });
} }

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<hazelcast-client xmlns="http://www.hazelcast.com/schema/client-config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.hazelcast.com/schema/client-config hazelcast-client-config-4.2.xsd">
<client-labels>
<label>explicit-client</label>
</client-labels>
</hazelcast-client>

View File

@ -0,0 +1,12 @@
<hazelcast
xsi:schemaLocation="http://www.hazelcast.com/schema/config hazelcast-config-4.2.xsd"
xmlns="http://www.hazelcast.com/schema/config" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<instance-name>explicit-server</instance-name>
<map name="defaultCache" />
<network>
<join>
<auto-detection enabled="false"/>
<multicast enabled="false" />
</join>
</network>
</hazelcast>

View File

@ -1,4 +1,4 @@
<hazelcast xsi:schemaLocation="http://www.hazelcast.com/schema/config hazelcast-config-3.2.xsd" <hazelcast xsi:schemaLocation="http://www.hazelcast.com/schema/config hazelcast-config-4.2.xsd"
xmlns="http://www.hazelcast.com/schema/config" xmlns="http://www.hazelcast.com/schema/config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
@ -11,7 +11,7 @@
<network> <network>
<join> <join>
<tcp-ip enabled="false"/> <auto-detection enabled="false"/>
<multicast enabled="false"/> <multicast enabled="false"/>
</join> </join>
</network> </network>