Skip to content

Commit

Permalink
Update I18nEndpointTest.java
Browse files Browse the repository at this point in the history
  • Loading branch information
mercyblitz committed Jun 1, 2024
1 parent 2951684 commit 17f60d5
Showing 1 changed file with 31 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.DocumentContext;
import io.microsphere.i18n.spring.annotation.EnableI18n;
import io.microsphere.i18n.spring.boot.actuate.autoconfigure.I18nEndpointAutoConfiguration;
import io.microsphere.i18n.spring.boot.autoconfigure.I18nAutoConfiguration;
import org.junit.jupiter.api.BeforeAll;
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.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

import java.util.Locale;
Expand All @@ -18,37 +21,42 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

@SpringBootTest(
classes = I18nEndpointTest.EndpointConfiguration.class,
properties = {
"management.endpoint.i18n.enabled=true",
"management.endpoints.web.exposure.include=*"
}
)
class I18nEndpointTest {
ApplicationContextRunner applicationContextRunner;

@BeforeAll
static void init() {
Locale.setDefault(Locale.CHINA);
}

@BeforeEach
void setup() {
applicationContextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(EndpointConfiguration.class));
}
@Autowired
private ApplicationContext context;

@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("啊")));
});
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
@ImportAutoConfiguration(classes = {
I18nAutoConfiguration.class,
I18nEndpointAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class
})
static class EndpointConfiguration {
@Bean
I18nEndpoint i18nEndpoint() {
Expand Down

0 comments on commit 17f60d5

Please sign in to comment.