Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void teardown() throws IOException {

@Test
@DisplayName("测试法文地区的验证消息")
void shouldReturnChineseValidationMessage() {
void shouldReturnFrenchValidationMessage() {
Company invalidCompany = new Company(null);

MockRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/validation/locale/simple")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void teardown() throws IOException {

@Test
@DisplayName("测试法文地区的验证消息")
void shouldReturnChineseValidationMessage() {
void shouldReturnFrenchValidationMessage() {
Company invalidCompany = new Company(null);

MockRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/validation/locale/simple")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved.
* This file is a part of the ModelEngine Project.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

package modelengine.fitframework.util.i18n;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Locale;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

/**
* {@link LocaleContextHolder} 的单元测试。
*
* @author 阮睿
* @since 2025-09-09
*/
@DisplayName("测试 LocaleContextHolder")
public class LocaleContextHolderTest {
@AfterEach
void tearDown() {
LocaleContextHolder.clear();
}

@Nested
@DisplayName("Test method: setLocaleContext and getLocaleContext")
class TestSetAndGetLocaleContext {

@Test
@DisplayName("Given locale context with zh_CN then return the same locale context")
void givenLocaleContextWithZhCNThenReturnSameLocaleContext() {
LocaleContext localeContext = new LocaleContext(Locale.SIMPLIFIED_CHINESE);
LocaleContextHolder.setLocaleContext(localeContext);
assertThat(LocaleContextHolder.getLocaleContext()).isEqualTo(localeContext);
}

@Test
@DisplayName("Given locale context with en_US then return the same locale context")
void givenLocaleContextWithEnUSThenReturnSameLocaleContext() {
LocaleContext localeContext = new LocaleContext(Locale.US);
LocaleContextHolder.setLocaleContext(localeContext);
assertThat(LocaleContextHolder.getLocaleContext()).isEqualTo(localeContext);
}

@Test
@DisplayName("Given null locale context then not set and return null")
void givenNullLocaleContextThenReturnNull() {
LocaleContextHolder.setLocaleContext(null);
assertThat(LocaleContextHolder.getLocaleContext()).isNull();
}
}

@Nested
@DisplayName("Test method: getLocale")
class TestGetLocale {

@Test
@DisplayName("Given locale context with zh_CN then return zh_CN locale")
void givenLocaleContextWithZhCNThenReturnZhCNLocale() {
LocaleContext localeContext = new LocaleContext(Locale.SIMPLIFIED_CHINESE);
LocaleContextHolder.setLocaleContext(localeContext);
assertThat(LocaleContextHolder.getLocale()).isEqualTo(Locale.SIMPLIFIED_CHINESE);
}

@Test
@DisplayName("Given locale context with en_US then return en_US locale")
void givenLocaleContextWithEnUSThenReturnEnUSLocale() {
LocaleContext localeContext = new LocaleContext(Locale.US);
LocaleContextHolder.setLocaleContext(localeContext);
assertThat(LocaleContextHolder.getLocale()).isEqualTo(Locale.US);
}

@Test
@DisplayName("Given no locale context then return null")
void givenNoLocaleContextThenReturnNull() {
LocaleContextHolder.clear();
assertThat(LocaleContextHolder.getLocale()).isNull();
}
}

@Nested
@DisplayName("Test method: clear")
class TestClear {

@Test
@DisplayName("Given existing locale context then clear it")
void givenExistingLocaleContextThenClearIt() {
LocaleContext localeContext = new LocaleContext(Locale.SIMPLIFIED_CHINESE);
LocaleContextHolder.setLocaleContext(localeContext);
assertThat(LocaleContextHolder.getLocaleContext()).isNotNull();

LocaleContextHolder.clear();
assertThat(LocaleContextHolder.getLocaleContext()).isNull();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved.
* This file is a part of the ModelEngine Project.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

package modelengine.fitframework.util.i18n;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Locale;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

/**
* {@link LocaleContext} 的单元测试。
*
* @author 阮睿
* @since 2025-09-09
*/
@DisplayName("测试 LocaleContext")
public class LocaleContextTest {
@Nested
@DisplayName("Test method: constructor and getLocale()")
class TestConstructorAndGetLocale {

@Test
@DisplayName("Given locale is zh_CN then return the same locale")
void givenZhCNThenReturnSameLocale() {
Locale locale = Locale.SIMPLIFIED_CHINESE;
LocaleContext localeContext = new LocaleContext(locale);
assertThat(localeContext.getLocale()).isEqualTo(locale);
}

@Test
@DisplayName("Given locale is en_US then return the same locale")
void givenEnUSThenReturnSameLocale() {
Locale locale = Locale.US;
LocaleContext localeContext = new LocaleContext(locale);
assertThat(localeContext.getLocale()).isEqualTo(locale);
}

@Test
@DisplayName("Given locale is null then return null")
void givenNullThenReturnNull() {
LocaleContext localeContext = new LocaleContext(null);
assertThat(localeContext.getLocale()).isNull();
}
}
}

Loading