Polish "Inject SpringManagedContext into Hazelcast configuration"
See gh-28801
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2021 the original author or authors.
|
||||
* Copyright 2012-2022 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.
|
||||
@@ -26,6 +26,7 @@ import com.hazelcast.map.EntryProcessor;
|
||||
import com.hazelcast.map.IMap;
|
||||
import com.hazelcast.spring.context.SpringAware;
|
||||
import com.hazelcast.spring.context.SpringManagedContext;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
@@ -36,9 +37,9 @@ import org.springframework.boot.test.context.assertj.AssertableApplicationContex
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.boot.test.context.runner.ContextConsumer;
|
||||
import org.springframework.boot.testsupport.classpath.ClassPathExclusions;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -172,52 +173,36 @@ class HazelcastAutoConfigurationServerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void defaultConfigFile_injectManagedContext() {
|
||||
void autoConfiguredConfigUsesSpringManagedContext() {
|
||||
this.contextRunner.run((context) -> {
|
||||
HazelcastInstance hz = context.getBean(HazelcastInstance.class);
|
||||
IMap<Integer, Integer> map = hz.getMap("myMap");
|
||||
boolean contextInjected = map.executeOnKey(42, new SpringAwareEntryProcessor<>());
|
||||
assertThat(contextInjected).isEqualTo(true);
|
||||
Config config = context.getBean(HazelcastInstance.class).getConfig();
|
||||
assertThat(config.getManagedContext()).isInstanceOf(SpringManagedContext.class);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void defaultConfigFile_injectManagedContext_SpringHazelcastModuleNotAvailable() {
|
||||
this.contextRunner
|
||||
.withClassLoader(new FilteredClassLoader(SpringManagedContext.class))
|
||||
.run((context) -> {
|
||||
HazelcastInstance hz = context.getBean(HazelcastInstance.class);
|
||||
IMap<Integer, Integer> map = hz.getMap("myMap");
|
||||
boolean contextInjected = map.executeOnKey(42, new SpringAwareEntryProcessor<>());
|
||||
assertThat(contextInjected).isEqualTo(false);
|
||||
});
|
||||
void autoConfiguredConfigCanUseSpringAwareComponent() {
|
||||
this.contextRunner.withPropertyValues("test.hazelcast.key=42").run((context) -> {
|
||||
HazelcastInstance hz = context.getBean(HazelcastInstance.class);
|
||||
IMap<String, String> map = hz.getMap("test");
|
||||
assertThat(map.executeOnKey("test.hazelcast.key", new SpringAwareEntryProcessor<>())).isEqualTo("42");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void explicitConfigFile_injectManagedContext() {
|
||||
this.contextRunner
|
||||
.withSystemProperties(HazelcastServerConfiguration.CONFIG_SYSTEM_PROPERTY
|
||||
+ "=classpath:org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.yaml")
|
||||
.run((context) -> {
|
||||
HazelcastInstance hz = context.getBean(HazelcastInstance.class);
|
||||
IMap<Integer, Integer> map = hz.getMap("myMap");
|
||||
boolean contextInjected = map.executeOnKey(42, new SpringAwareEntryProcessor<>());
|
||||
assertThat(contextInjected).isEqualTo(true);
|
||||
});
|
||||
void autoConfiguredConfigWithoutHazelcastSpringDoesNotUseSpringManagedContext() {
|
||||
this.contextRunner.withClassLoader(new FilteredClassLoader(SpringManagedContext.class)).run((context) -> {
|
||||
Config config = context.getBean(HazelcastInstance.class).getConfig();
|
||||
assertThat(config.getManagedContext()).isNull();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void explicitConfigFile_injectManagedContext_SpringHazelcastModuleNotAvailable() {
|
||||
this.contextRunner
|
||||
.withClassLoader(new FilteredClassLoader(SpringManagedContext.class))
|
||||
.withSystemProperties(HazelcastServerConfiguration.CONFIG_SYSTEM_PROPERTY
|
||||
+ "=classpath:org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.yaml")
|
||||
.run((context) -> {
|
||||
HazelcastInstance hz = context.getBean(HazelcastInstance.class);
|
||||
IMap<Integer, Integer> map = hz.getMap("myMap");
|
||||
boolean contextInjected = map.executeOnKey(42, new SpringAwareEntryProcessor<>());
|
||||
assertThat(contextInjected).isEqualTo(false);
|
||||
});
|
||||
void autoConfiguredContextCanOverrideManagementContextUsingCustomizer() {
|
||||
this.contextRunner.withBean(TestHazelcastConfigCustomizer.class).run((context) -> {
|
||||
Config config = context.getBean(HazelcastInstance.class).getConfig();
|
||||
assertThat(config.getManagedContext()).isNull();
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@@ -230,17 +215,6 @@ class HazelcastAutoConfigurationServerTests {
|
||||
|
||||
}
|
||||
|
||||
@SpringAware
|
||||
static class SpringAwareEntryProcessor<K, V> implements EntryProcessor<K, V, Boolean> {
|
||||
@Autowired
|
||||
private ApplicationContext context;
|
||||
|
||||
@Override
|
||||
public Boolean process(Map.Entry<K, V> entry) {
|
||||
return context != null;
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
static class HazelcastConfigNoName {
|
||||
|
||||
@@ -253,4 +227,27 @@ class HazelcastAutoConfigurationServerTests {
|
||||
|
||||
}
|
||||
|
||||
@SpringAware
|
||||
static class SpringAwareEntryProcessor<V> implements EntryProcessor<String, V, String> {
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@Override
|
||||
public String process(Map.Entry<String, V> entry) {
|
||||
return this.environment.getProperty(entry.getKey());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Order(1)
|
||||
static class TestHazelcastConfigCustomizer implements HazelcastConfigCustomizer {
|
||||
|
||||
@Override
|
||||
public void customize(Config config) {
|
||||
config.setManagedContext(null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user