-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from liqi19950722/addTest
fix: I18nEndpoint
- Loading branch information
Showing
9 changed files
with
341 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...n-spring-boot/src/test/java/io/microsphere/i18n/spring/boot/actuate/I18nEndpointTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, Map<String, String>> 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(); | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...microsphere/i18n/spring/boot/actuate/autoconfigure/I18nEndpointAutoConfigurationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...c/test/java/io/microsphere/i18n/spring/boot/autoconfigure/I18nAutoConfigurationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
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.assertj.core.api.AssertionsForInterfaceTypes.assertThat; | ||
|
||
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 -> { | ||
assertThat(context) | ||
.hasBean("applicationServiceMessageSource") | ||
.getBean("applicationServiceMessageSource") | ||
.hasFieldOrPropertyWithValue("source", "I18nAutoConfigurationTests"); | ||
|
||
|
||
assertThat(context).getBeans(ServiceMessageSourceFactoryBean.class).hasSizeGreaterThanOrEqualTo(1); | ||
|
||
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); | ||
}); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...rc/test/java/io/microsphere/i18n/spring/boot/condition/ConditionalOnI18nEnabledTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.assertj.core.api.AssertionsForInterfaceTypes.assertThat; | ||
|
||
|
||
class ConditionalOnI18nEnabledTests { | ||
|
||
ApplicationContextRunner applicationContextRunner; | ||
|
||
@BeforeEach | ||
void setup() { | ||
applicationContextRunner = new ApplicationContextRunner(); | ||
} | ||
|
||
@Test | ||
void shouldEnableI18nByDefault() { | ||
applicationContextRunner.withUserConfiguration(Config.class) | ||
.run(context -> assertThat(context).hasBean("a")); | ||
} | ||
|
||
@Test | ||
void shouldEnableI18nWhenPropertyIsTrue() { | ||
applicationContextRunner.withUserConfiguration(Config.class) | ||
.withPropertyValues("microsphere.i18n.enabled=true") | ||
.run(context -> assertThat(context).hasBean("a")); | ||
} | ||
|
||
@Test | ||
void shouldDisableI18nWhenPropertyIsFalse() { | ||
applicationContextRunner.withUserConfiguration(Config.class) | ||
.withPropertyValues("microsphere.i18n.enabled=false") | ||
.run(context -> assertThat(context).doesNotHaveBean("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 -> assertThat(context).doesNotHaveBean("a")); | ||
} | ||
|
||
@Configuration | ||
static class Config { | ||
|
||
@Bean | ||
@ConditionalOnI18nEnabled | ||
public String a() { | ||
return "a"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...t/java/io/microsphere/i18n/spring/cloud/autoconfigure/I18nCloudAutoConfigurationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
.../test/java/io/microsphere/i18n/spring/cloud/integration/MessageSourceIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
} | ||
} |