From 33d923c936a511080be1773a5befd9de74c3fff8 Mon Sep 17 00:00:00 2001
From: "li.qi" <15868175516@163.com>
Date: Sat, 11 May 2024 22:39:19 +0800
Subject: [PATCH 1/8] test: add Test for ConditionalOnI18nEnabled Annotation
---
microsphere-i18n-spring-boot/pom.xml | 10 +++
.../ConditionalOnI18nEnabledTests.java | 61 +++++++++++++++++++
2 files changed, 71 insertions(+)
create mode 100644 microsphere-i18n-spring-boot/src/test/java/io/microsphere/i18n/spring/boot/condition/ConditionalOnI18nEnabledTests.java
diff --git a/microsphere-i18n-spring-boot/pom.xml b/microsphere-i18n-spring-boot/pom.xml
index c6237aa..504f1b3 100644
--- a/microsphere-i18n-spring-boot/pom.xml
+++ b/microsphere-i18n-spring-boot/pom.xml
@@ -75,6 +75,16 @@
test
+
+ org.junit.jupiter
+ junit-jupiter
+ test
+
+
+ org.assertj
+ assertj-core
+ test
+
org.springframework
spring-test
diff --git a/microsphere-i18n-spring-boot/src/test/java/io/microsphere/i18n/spring/boot/condition/ConditionalOnI18nEnabledTests.java b/microsphere-i18n-spring-boot/src/test/java/io/microsphere/i18n/spring/boot/condition/ConditionalOnI18nEnabledTests.java
new file mode 100644
index 0000000..d29fe59
--- /dev/null
+++ b/microsphere-i18n-spring-boot/src/test/java/io/microsphere/i18n/spring/boot/condition/ConditionalOnI18nEnabledTests.java
@@ -0,0 +1,61 @@
+package io.microsphere.i18n.spring.boot.condition;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.springframework.boot.test.context.FilteredClassLoader;
+import org.springframework.boot.test.context.runner.ApplicationContextRunner;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+
+class ConditionalOnI18nEnabledTests {
+
+ ApplicationContextRunner applicationContextRunner;
+
+ @BeforeEach
+ void setup() {
+ applicationContextRunner = new ApplicationContextRunner();
+ }
+
+ @Test
+ void shouldEnableI18nByDefault() {
+ applicationContextRunner.withUserConfiguration(Config.class)
+ .run(context -> assertTrue(context.containsBean("a")));
+ }
+
+ @Test
+ void shouldEnableI18nWhenPropertyIsTrue() {
+ applicationContextRunner.withUserConfiguration(Config.class)
+ .withPropertyValues("microsphere.i18n.enabled=true")
+ .run(context -> assertTrue(context.containsBean("a")));
+ }
+
+ @Test
+ void shouldDisableI18nWhenPropertyIsFalse() {
+ applicationContextRunner.withUserConfiguration(Config.class)
+ .withPropertyValues("microsphere.i18n.enabled=false")
+ .run(context -> assertFalse(context.containsBean("a")));
+ }
+ @Test
+ void shouldDisableI18nWhenMissingClass() {
+ applicationContextRunner.withUserConfiguration(Config.class)
+ .withPropertyValues("microsphere.i18n.enabled=true")
+ .withClassLoader(new FilteredClassLoader(
+ "io.microsphere.i18n.ServiceMessageSource",
+ "io.microsphere.i18n.spring.annotation.EnableI18n"))
+ .run(context -> assertFalse(context.containsBean("a")));
+ }
+
+ @Configuration
+ static class Config {
+
+ @Bean
+ @ConditionalOnI18nEnabled
+ public String a() {
+ return "a";
+ }
+ }
+}
From f19975d7dc7324498dd76390c4362b9b8cb7139e Mon Sep 17 00:00:00 2001
From: "li.qi" <15868175516@163.com>
Date: Sat, 11 May 2024 23:57:56 +0800
Subject: [PATCH 2/8] test: add Test for I18nAutoConfigurationTests
---
.../I18nAutoConfigurationTests.java | 50 +++++++++++++++++++
1 file changed, 50 insertions(+)
create mode 100644 microsphere-i18n-spring-boot/src/test/java/io/microsphere/i18n/spring/boot/autoconfigure/I18nAutoConfigurationTests.java
diff --git a/microsphere-i18n-spring-boot/src/test/java/io/microsphere/i18n/spring/boot/autoconfigure/I18nAutoConfigurationTests.java b/microsphere-i18n-spring-boot/src/test/java/io/microsphere/i18n/spring/boot/autoconfigure/I18nAutoConfigurationTests.java
new file mode 100644
index 0000000..62fadbb
--- /dev/null
+++ b/microsphere-i18n-spring-boot/src/test/java/io/microsphere/i18n/spring/boot/autoconfigure/I18nAutoConfigurationTests.java
@@ -0,0 +1,50 @@
+package io.microsphere.i18n.spring.boot.autoconfigure;
+
+import io.microsphere.i18n.spring.DelegatingServiceMessageSource;
+import io.microsphere.i18n.spring.beans.factory.ServiceMessageSourceFactoryBean;
+import io.microsphere.i18n.spring.beans.factory.config.I18nBeanPostProcessor;
+import io.microsphere.i18n.spring.beans.factory.support.ServiceMessageSourceBeanLifecyclePostProcessor;
+import io.microsphere.i18n.spring.context.I18nApplicationListener;
+import io.microsphere.i18n.spring.context.MessageSourceAdapter;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.springframework.boot.autoconfigure.AutoConfigurations;
+import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
+import org.springframework.boot.test.context.runner.ApplicationContextRunner;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class I18nAutoConfigurationTests {
+
+ ApplicationContextRunner applicationContextRunner;
+
+ @BeforeEach
+ void setup() {
+ applicationContextRunner = new ApplicationContextRunner();
+ }
+
+ @Test
+ void shouldContainServiceMessageSourceFactoryBean() {
+ applicationContextRunner.withPropertyValues("spring.application.name=I18nAutoConfigurationTests")
+ .withConfiguration(AutoConfigurations.of(I18nAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class))
+ .run(context -> {
+ assertTrue(context.containsBean("applicationServiceMessageSource"));
+ assertEquals("I18nAutoConfigurationTests",
+ context.getBean("applicationServiceMessageSource", ServiceMessageSourceFactoryBean.class).getSource());
+
+ assertFalse(context.getBeansOfType(ServiceMessageSourceFactoryBean.class).isEmpty());
+ assertInstanceOf(DelegatingServiceMessageSource.class, context.getBean("serviceMessageSource"));
+
+ assertTrue(context.containsBean("messageSource"));
+ assertInstanceOf(MessageSourceAdapter.class, context.getBean("messageSource"));
+
+ assertNotNull(context.getBean(I18nApplicationListener.class));
+ assertNotNull(context.getBean(I18nBeanPostProcessor.class));
+ assertNotNull(context.getBean(ServiceMessageSourceBeanLifecyclePostProcessor.class));
+ });
+ }
+}
From 6f35b0c4f0d14aadd6c5d385396bdf4b2aeb3468 Mon Sep 17 00:00:00 2001
From: "li.qi" <15868175516@163.com>
Date: Sun, 12 May 2024 07:33:49 +0800
Subject: [PATCH 3/8] test: add Test for I18nEndpointAutoConfiguration
---
.../I18nEndpointAutoConfigurationTest.java | 45 +++++++++++++++++++
1 file changed, 45 insertions(+)
create mode 100644 microsphere-i18n-spring-boot/src/test/java/io/microsphere/i18n/spring/boot/actuate/autoconfigure/I18nEndpointAutoConfigurationTest.java
diff --git a/microsphere-i18n-spring-boot/src/test/java/io/microsphere/i18n/spring/boot/actuate/autoconfigure/I18nEndpointAutoConfigurationTest.java b/microsphere-i18n-spring-boot/src/test/java/io/microsphere/i18n/spring/boot/actuate/autoconfigure/I18nEndpointAutoConfigurationTest.java
new file mode 100644
index 0000000..55e803a
--- /dev/null
+++ b/microsphere-i18n-spring-boot/src/test/java/io/microsphere/i18n/spring/boot/actuate/autoconfigure/I18nEndpointAutoConfigurationTest.java
@@ -0,0 +1,45 @@
+package io.microsphere.i18n.spring.boot.actuate.autoconfigure;
+
+import io.microsphere.i18n.spring.boot.actuate.I18nEndpoint;
+import io.microsphere.i18n.spring.boot.autoconfigure.I18nAutoConfiguration;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.springframework.boot.autoconfigure.AutoConfigurations;
+import org.springframework.boot.test.context.runner.ApplicationContextRunner;
+
+import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
+
+
+class I18nEndpointAutoConfigurationTest {
+
+ ApplicationContextRunner applicationContextRunner;
+
+ @BeforeEach
+ void setup() {
+ applicationContextRunner = new ApplicationContextRunner()
+ .withConfiguration(AutoConfigurations.of(
+ I18nAutoConfiguration.class,
+ I18nEndpointAutoConfiguration.class
+ ));
+ }
+
+ @Test
+ void shouldHaveEndpointBean() {
+ applicationContextRunner.withPropertyValues("management.endpoints.web.exposure.include=i18n")
+ .run(context -> assertThat(context).hasSingleBean(I18nEndpoint.class));
+ }
+
+
+ @Test
+ void shouldNotHaveEndpointBean() {
+ applicationContextRunner
+ .run(context -> assertThat(context).doesNotHaveBean(I18nEndpoint.class));
+ }
+
+ @Test
+ void shouldNotHaveEndpointBeanWhenEnablePropertyIsFalse() {
+ applicationContextRunner.withPropertyValues("management.endpoint.i18n.enabled=false")
+ .withPropertyValues("management.endpoints.web.exposure.include=*")
+ .run(context -> assertThat(context).doesNotHaveBean(I18nEndpoint.class));
+ }
+}
From fe982bc16dcca270d1309de18703ba849f284679 Mon Sep 17 00:00:00 2001
From: "li.qi" <15868175516@163.com>
Date: Sun, 12 May 2024 07:53:31 +0800
Subject: [PATCH 4/8] test: use assertj
---
.../I18nAutoConfigurationTests.java | 27 +++++++++----------
.../ConditionalOnI18nEnabledTests.java | 12 ++++-----
2 files changed, 18 insertions(+), 21 deletions(-)
diff --git a/microsphere-i18n-spring-boot/src/test/java/io/microsphere/i18n/spring/boot/autoconfigure/I18nAutoConfigurationTests.java b/microsphere-i18n-spring-boot/src/test/java/io/microsphere/i18n/spring/boot/autoconfigure/I18nAutoConfigurationTests.java
index 62fadbb..c3dcfbb 100644
--- a/microsphere-i18n-spring-boot/src/test/java/io/microsphere/i18n/spring/boot/autoconfigure/I18nAutoConfigurationTests.java
+++ b/microsphere-i18n-spring-boot/src/test/java/io/microsphere/i18n/spring/boot/autoconfigure/I18nAutoConfigurationTests.java
@@ -12,11 +12,7 @@
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertFalse;
-import static org.junit.jupiter.api.Assertions.assertInstanceOf;
-import static org.junit.jupiter.api.Assertions.assertNotNull;
-import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
class I18nAutoConfigurationTests {
@@ -32,19 +28,20 @@ void shouldContainServiceMessageSourceFactoryBean() {
applicationContextRunner.withPropertyValues("spring.application.name=I18nAutoConfigurationTests")
.withConfiguration(AutoConfigurations.of(I18nAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class))
.run(context -> {
- assertTrue(context.containsBean("applicationServiceMessageSource"));
- assertEquals("I18nAutoConfigurationTests",
- context.getBean("applicationServiceMessageSource", ServiceMessageSourceFactoryBean.class).getSource());
+ assertThat(context)
+ .hasBean("applicationServiceMessageSource")
+ .getBean("applicationServiceMessageSource")
+ .hasFieldOrPropertyWithValue("source", "I18nAutoConfigurationTests");
- assertFalse(context.getBeansOfType(ServiceMessageSourceFactoryBean.class).isEmpty());
- assertInstanceOf(DelegatingServiceMessageSource.class, context.getBean("serviceMessageSource"));
- assertTrue(context.containsBean("messageSource"));
- assertInstanceOf(MessageSourceAdapter.class, context.getBean("messageSource"));
+ assertThat(context).getBeans(ServiceMessageSourceFactoryBean.class).hasSizeGreaterThanOrEqualTo(1);
- assertNotNull(context.getBean(I18nApplicationListener.class));
- assertNotNull(context.getBean(I18nBeanPostProcessor.class));
- assertNotNull(context.getBean(ServiceMessageSourceBeanLifecyclePostProcessor.class));
+ assertThat(context).getBean("serviceMessageSource").isInstanceOf(DelegatingServiceMessageSource.class);
+ assertThat(context).getBean("messageSource").isInstanceOf(MessageSourceAdapter.class);
+
+ assertThat(context).hasSingleBean(I18nApplicationListener.class)
+ .hasSingleBean(I18nBeanPostProcessor.class)
+ .hasSingleBean(ServiceMessageSourceBeanLifecyclePostProcessor.class);
});
}
}
diff --git a/microsphere-i18n-spring-boot/src/test/java/io/microsphere/i18n/spring/boot/condition/ConditionalOnI18nEnabledTests.java b/microsphere-i18n-spring-boot/src/test/java/io/microsphere/i18n/spring/boot/condition/ConditionalOnI18nEnabledTests.java
index d29fe59..0804578 100644
--- a/microsphere-i18n-spring-boot/src/test/java/io/microsphere/i18n/spring/boot/condition/ConditionalOnI18nEnabledTests.java
+++ b/microsphere-i18n-spring-boot/src/test/java/io/microsphere/i18n/spring/boot/condition/ConditionalOnI18nEnabledTests.java
@@ -7,8 +7,7 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
-import static org.junit.jupiter.api.Assertions.assertFalse;
-import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
class ConditionalOnI18nEnabledTests {
@@ -23,22 +22,23 @@ void setup() {
@Test
void shouldEnableI18nByDefault() {
applicationContextRunner.withUserConfiguration(Config.class)
- .run(context -> assertTrue(context.containsBean("a")));
+ .run(context -> assertThat(context).hasBean("a"));
}
@Test
void shouldEnableI18nWhenPropertyIsTrue() {
applicationContextRunner.withUserConfiguration(Config.class)
.withPropertyValues("microsphere.i18n.enabled=true")
- .run(context -> assertTrue(context.containsBean("a")));
+ .run(context -> assertThat(context).hasBean("a"));
}
@Test
void shouldDisableI18nWhenPropertyIsFalse() {
applicationContextRunner.withUserConfiguration(Config.class)
.withPropertyValues("microsphere.i18n.enabled=false")
- .run(context -> assertFalse(context.containsBean("a")));
+ .run(context -> assertThat(context).doesNotHaveBean("a"));
}
+
@Test
void shouldDisableI18nWhenMissingClass() {
applicationContextRunner.withUserConfiguration(Config.class)
@@ -46,7 +46,7 @@ void shouldDisableI18nWhenMissingClass() {
.withClassLoader(new FilteredClassLoader(
"io.microsphere.i18n.ServiceMessageSource",
"io.microsphere.i18n.spring.annotation.EnableI18n"))
- .run(context -> assertFalse(context.containsBean("a")));
+ .run(context -> assertThat(context).doesNotHaveBean("a"));
}
@Configuration
From c3c03cd3a85fe7749d46ea1b94a8e0a6bd9597f6 Mon Sep 17 00:00:00 2001
From: "li.qi" <15868175516@163.com>
Date: Sun, 12 May 2024 09:57:29 +0800
Subject: [PATCH 5/8] test: add Test for I18nEndpoint
---
microsphere-i18n-spring-boot/pom.xml | 5 ++
.../spring/boot/actuate/I18nEndpointTest.java | 51 +++++++++++++++++++
2 files changed, 56 insertions(+)
create mode 100644 microsphere-i18n-spring-boot/src/test/java/io/microsphere/i18n/spring/boot/actuate/I18nEndpointTest.java
diff --git a/microsphere-i18n-spring-boot/pom.xml b/microsphere-i18n-spring-boot/pom.xml
index 504f1b3..1419335 100644
--- a/microsphere-i18n-spring-boot/pom.xml
+++ b/microsphere-i18n-spring-boot/pom.xml
@@ -85,6 +85,11 @@
assertj-core
test
+
+ com.jayway.jsonpath
+ json-path-assert
+ test
+
org.springframework
spring-test
diff --git a/microsphere-i18n-spring-boot/src/test/java/io/microsphere/i18n/spring/boot/actuate/I18nEndpointTest.java b/microsphere-i18n-spring-boot/src/test/java/io/microsphere/i18n/spring/boot/actuate/I18nEndpointTest.java
new file mode 100644
index 0000000..6128c44
--- /dev/null
+++ b/microsphere-i18n-spring-boot/src/test/java/io/microsphere/i18n/spring/boot/actuate/I18nEndpointTest.java
@@ -0,0 +1,51 @@
+package io.microsphere.i18n.spring.boot.actuate;
+
+import com.jayway.jsonpath.Configuration;
+import com.jayway.jsonpath.DocumentContext;
+import io.microsphere.i18n.spring.annotation.EnableI18n;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.springframework.boot.autoconfigure.AutoConfigurations;
+import org.springframework.boot.test.context.runner.ApplicationContextRunner;
+import org.springframework.context.annotation.Bean;
+
+import java.util.Map;
+
+import static com.jayway.jsonpath.JsonPath.using;
+import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+
+class I18nEndpointTest {
+ ApplicationContextRunner applicationContextRunner;
+
+ @BeforeEach
+ void setup() {
+ applicationContextRunner = new ApplicationContextRunner()
+ .withConfiguration(AutoConfigurations.of(EndpointConfiguration.class));
+ }
+
+ @Test
+ void shouldReturnCommonI18nMessages() {
+ applicationContextRunner.run(context -> {
+ Map> invoke = context.getBean(I18nEndpoint.class).invoke();
+ DocumentContext parse = using(Configuration.defaultConfiguration()).parse(invoke);
+ assertThat(parse, hasJsonPath("$.['common.i18n_messages_en.properties']"));
+ assertThat(parse, hasJsonPath("$.['common.i18n_messages_zh.properties']"));
+ assertThat(parse, hasJsonPath("$.['common.i18n_messages_zh_CN.properties']"));
+ assertThat(parse, hasJsonPath("$.['META-INF/i18n/common/i18n_messages_en.properties']"));
+ assertThat(parse, hasJsonPath("$.['META-INF/i18n/common/i18n_messages_en.properties'].['common.a']", equalTo("a")));
+ assertThat(parse, hasJsonPath("$.['META-INF/i18n/common/i18n_messages_zh.properties']"));
+ assertThat(parse, hasJsonPath("$.['META-INF/i18n/common/i18n_messages_zh_CN.properties']"));
+ assertThat(parse, hasJsonPath("$.['META-INF/i18n/common/i18n_messages_zh_CN.properties'].['common.a']", equalTo("啊")));
+ });
+ }
+
+ @EnableI18n
+ static class EndpointConfiguration {
+ @Bean
+ I18nEndpoint i18nEndpoint() {
+ return new I18nEndpoint();
+ }
+ }
+}
From bfa8a1bf205ca73ded18376bec2f36ecbc0ca538 Mon Sep 17 00:00:00 2001
From: "li.qi" <15868175516@163.com>
Date: Sun, 12 May 2024 10:07:19 +0800
Subject: [PATCH 6/8] refactor: format
---
microsphere-i18n-spring-boot/pom.xml | 3 +++
1 file changed, 3 insertions(+)
diff --git a/microsphere-i18n-spring-boot/pom.xml b/microsphere-i18n-spring-boot/pom.xml
index 1419335..0bec721 100644
--- a/microsphere-i18n-spring-boot/pom.xml
+++ b/microsphere-i18n-spring-boot/pom.xml
@@ -80,16 +80,19 @@
junit-jupiter
test
+
org.assertj
assertj-core
test
+
com.jayway.jsonpath
json-path-assert
test
+
org.springframework
spring-test
From 81604a7fc2fe7b717498ce03b433e6be6b061604 Mon Sep 17 00:00:00 2001
From: "li.qi" <15868175516@163.com>
Date: Sun, 12 May 2024 10:07:32 +0800
Subject: [PATCH 7/8] test: add Test for I18nCloudAutoConfiguration
---
microsphere-i18n-spring-cloud/pom.xml | 18 ++++++++++
.../I18nCloudAutoConfigurationTest.java | 33 +++++++++++++++++++
2 files changed, 51 insertions(+)
create mode 100644 microsphere-i18n-spring-cloud/src/test/java/io/microsphere/i18n/spring/cloud/autoconfigure/I18nCloudAutoConfigurationTest.java
diff --git a/microsphere-i18n-spring-cloud/pom.xml b/microsphere-i18n-spring-cloud/pom.xml
index cc5989d..5f4c343 100644
--- a/microsphere-i18n-spring-cloud/pom.xml
+++ b/microsphere-i18n-spring-cloud/pom.xml
@@ -81,6 +81,24 @@
test
+
+ org.junit.jupiter
+ junit-jupiter
+ test
+
+
+
+ org.assertj
+ assertj-core
+ test
+
+
+
+ com.jayway.jsonpath
+ json-path-assert
+ test
+
+
org.springframework
spring-test
diff --git a/microsphere-i18n-spring-cloud/src/test/java/io/microsphere/i18n/spring/cloud/autoconfigure/I18nCloudAutoConfigurationTest.java b/microsphere-i18n-spring-cloud/src/test/java/io/microsphere/i18n/spring/cloud/autoconfigure/I18nCloudAutoConfigurationTest.java
new file mode 100644
index 0000000..f2cb553
--- /dev/null
+++ b/microsphere-i18n-spring-cloud/src/test/java/io/microsphere/i18n/spring/cloud/autoconfigure/I18nCloudAutoConfigurationTest.java
@@ -0,0 +1,33 @@
+package io.microsphere.i18n.spring.cloud.autoconfigure;
+
+import io.microsphere.i18n.spring.boot.autoconfigure.I18nAutoConfiguration;
+import io.microsphere.i18n.spring.cloud.event.ReloadableResourceServiceMessageSourceListener;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.springframework.boot.autoconfigure.AutoConfigurations;
+import org.springframework.boot.test.context.FilteredClassLoader;
+import org.springframework.boot.test.context.runner.ApplicationContextRunner;
+
+import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
+
+
+class I18nCloudAutoConfigurationTest {
+ ApplicationContextRunner applicationContextRunner ;
+
+ @BeforeEach
+ void setup() {
+ applicationContextRunner = new ApplicationContextRunner()
+ .withConfiguration(AutoConfigurations.of(I18nAutoConfiguration.class, I18nCloudAutoConfiguration.class));
+ }
+
+ @Test
+ void shouldContainReloadableResourceServiceMessageSourceListenerBean() {
+ applicationContextRunner.run(context -> assertThat(context).hasSingleBean(ReloadableResourceServiceMessageSourceListener.class));
+ }
+
+ @Test
+ void shouldNotContainReloadableResourceServiceMessageSourceListenerBeanWhenMissingEnvironmentChangeEvent() {
+ applicationContextRunner.withClassLoader(new FilteredClassLoader("org.springframework.cloud.context.environment.EnvironmentChangeEvent"))
+ .run(context -> assertThat(context).doesNotHaveBean(ReloadableResourceServiceMessageSourceListener.class));
+ }
+}
From 90b1bb4461e57ddc449d6aa493ba887476838e93 Mon Sep 17 00:00:00 2001
From: "li.qi" <15868175516@163.com>
Date: Sun, 12 May 2024 12:23:14 +0800
Subject: [PATCH 8/8] test: add Test for MessageSourceIntegrationTest
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
fix I18nEndpoint数据显示
---
.../spring/boot/actuate/I18nEndpoint.java | 6 +-
.../MessageSourceIntegrationTest.java | 64 +++++++++++++++++++
2 files changed, 68 insertions(+), 2 deletions(-)
create mode 100644 microsphere-i18n-spring-cloud/src/test/java/io/microsphere/i18n/spring/cloud/integration/MessageSourceIntegrationTest.java
diff --git a/microsphere-i18n-spring-boot/src/main/java/io/microsphere/i18n/spring/boot/actuate/I18nEndpoint.java b/microsphere-i18n-spring-boot/src/main/java/io/microsphere/i18n/spring/boot/actuate/I18nEndpoint.java
index 2c8ef0f..24766b5 100644
--- a/microsphere-i18n-spring-boot/src/main/java/io/microsphere/i18n/spring/boot/actuate/I18nEndpoint.java
+++ b/microsphere-i18n-spring-boot/src/main/java/io/microsphere/i18n/spring/boot/actuate/I18nEndpoint.java
@@ -24,7 +24,6 @@
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
-import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -86,7 +85,10 @@ public Map> invoke() {
if (subServiceMessageSource instanceof AbstractResourceServiceMessageSource) {
AbstractResourceServiceMessageSource resourceServiceMessageSource = (AbstractResourceServiceMessageSource) subServiceMessageSource;
Map> localizedResourceMessages = resourceServiceMessageSource.getLocalizedResourceMessages();
- allLocalizedResourceMessages.putAll(localizedResourceMessages);
+// allLocalizedResourceMessages.putAll(localizedResourceMessages);
+ localizedResourceMessages.forEach(
+ (k, v) -> allLocalizedResourceMessages.merge(k, v, (oldValue, value) -> value.isEmpty() ? oldValue : value)
+ );
}
}
}
diff --git a/microsphere-i18n-spring-cloud/src/test/java/io/microsphere/i18n/spring/cloud/integration/MessageSourceIntegrationTest.java b/microsphere-i18n-spring-cloud/src/test/java/io/microsphere/i18n/spring/cloud/integration/MessageSourceIntegrationTest.java
new file mode 100644
index 0000000..00eb3f8
--- /dev/null
+++ b/microsphere-i18n-spring-cloud/src/test/java/io/microsphere/i18n/spring/cloud/integration/MessageSourceIntegrationTest.java
@@ -0,0 +1,64 @@
+package io.microsphere.i18n.spring.cloud.integration;
+
+import com.jayway.jsonpath.Configuration;
+import com.jayway.jsonpath.DocumentContext;
+import io.microsphere.i18n.ServiceMessageSource;
+import io.microsphere.i18n.spring.boot.actuate.I18nEndpoint;
+import io.microsphere.i18n.spring.boot.actuate.autoconfigure.I18nEndpointAutoConfiguration;
+import io.microsphere.i18n.spring.boot.autoconfigure.I18nAutoConfiguration;
+import io.microsphere.i18n.spring.cloud.event.ReloadableResourceServiceMessageSourceListener;
+import org.assertj.core.util.Maps;
+import org.assertj.core.util.Sets;
+import org.hamcrest.MatcherAssert;
+import org.junit.jupiter.api.Test;
+import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.annotation.Bean;
+import org.springframework.core.env.ConfigurableEnvironment;
+import org.springframework.core.env.MapPropertySource;
+
+import java.util.Locale;
+
+import static com.jayway.jsonpath.JsonPath.using;
+import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+
+@SpringBootTest(classes = MessageSourceIntegrationTest.Config.class)
+class MessageSourceIntegrationTest {
+
+ @Test
+ void shouldGetMessageWhenChangeMessageSourceProperty(ApplicationContext applicationContext) {
+ String key = "common.i18n_messages_en.properties";
+ ServiceMessageSource serviceMessageSource = applicationContext.getBean("commonServiceMessageSource", ServiceMessageSource.class);
+
+ assertThat(serviceMessageSource.getMessage("common.a", Locale.ENGLISH))
+ .isEqualTo("a");
+ ConfigurableEnvironment environment = (ConfigurableEnvironment) applicationContext.getEnvironment();
+ environment.getPropertySources().addLast(new MapPropertySource("test", Maps.newHashMap(key, "common.a=a.2024")));
+
+ applicationContext.publishEvent(new EnvironmentChangeEvent(Sets.set(key)));
+
+
+ assertThat(serviceMessageSource.getMessage("common.a", Locale.ENGLISH))
+ .isEqualTo("a.2024");
+
+ DocumentContext parse = using(Configuration.defaultConfiguration()).parse(applicationContext.getBean(I18nEndpoint.class).invoke());
+ MatcherAssert.assertThat(parse, hasJsonPath("$.['common.i18n_messages_en.properties'].['common.a']", equalTo("a.2024")));
+ }
+
+ @ImportAutoConfiguration(classes = {
+ I18nAutoConfiguration.class,
+ I18nEndpointAutoConfiguration.class
+ })
+ static class Config {
+
+ @Bean
+ ReloadableResourceServiceMessageSourceListener reloadableResourceServiceMessageSourceListener() {
+ return new ReloadableResourceServiceMessageSourceListener();
+ }
+
+ }
+}