|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved. |
| 3 | + * This file is a part of the ModelEngine Project. |
| 4 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 5 | + *--------------------------------------------------------------------------------------------*/ |
| 6 | + |
| 7 | +package modelengine.fitframework.util.i18n; |
| 8 | + |
| 9 | +import static org.assertj.core.api.Assertions.assertThat; |
| 10 | + |
| 11 | +import java.util.Locale; |
| 12 | + |
| 13 | +import org.junit.jupiter.api.AfterEach; |
| 14 | +import org.junit.jupiter.api.DisplayName; |
| 15 | +import org.junit.jupiter.api.Nested; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | + |
| 18 | +/** |
| 19 | + * {@link LocaleContextHolder} 的单元测试。 |
| 20 | + * |
| 21 | + * @author 阮睿 |
| 22 | + * @since 2025-09-09 |
| 23 | + */ |
| 24 | +@DisplayName("测试 LocaleContextHolder") |
| 25 | +public class LocaleContextHolderTest { |
| 26 | + @AfterEach |
| 27 | + void tearDown() { |
| 28 | + LocaleContextHolder.clear(); |
| 29 | + } |
| 30 | + |
| 31 | + @Nested |
| 32 | + @DisplayName("Test method: setLocaleContext and getLocaleContext") |
| 33 | + class TestSetAndGetLocaleContext { |
| 34 | + |
| 35 | + @Test |
| 36 | + @DisplayName("Given locale context with zh_CN then return the same locale context") |
| 37 | + void givenLocaleContextWithZhCNThenReturnSameLocaleContext() { |
| 38 | + LocaleContext localeContext = new LocaleContext(Locale.SIMPLIFIED_CHINESE); |
| 39 | + LocaleContextHolder.setLocaleContext(localeContext); |
| 40 | + assertThat(LocaleContextHolder.getLocaleContext()).isEqualTo(localeContext); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + @DisplayName("Given locale context with en_US then return the same locale context") |
| 45 | + void givenLocaleContextWithEnUSThenReturnSameLocaleContext() { |
| 46 | + LocaleContext localeContext = new LocaleContext(Locale.US); |
| 47 | + LocaleContextHolder.setLocaleContext(localeContext); |
| 48 | + assertThat(LocaleContextHolder.getLocaleContext()).isEqualTo(localeContext); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + @DisplayName("Given null locale context then not set and return null") |
| 53 | + void givenNullLocaleContextThenReturnNull() { |
| 54 | + LocaleContextHolder.setLocaleContext(null); |
| 55 | + assertThat(LocaleContextHolder.getLocaleContext()).isNull(); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Nested |
| 60 | + @DisplayName("Test method: getLocale") |
| 61 | + class TestGetLocale { |
| 62 | + |
| 63 | + @Test |
| 64 | + @DisplayName("Given locale context with zh_CN then return zh_CN locale") |
| 65 | + void givenLocaleContextWithZhCNThenReturnZhCNLocale() { |
| 66 | + LocaleContext localeContext = new LocaleContext(Locale.SIMPLIFIED_CHINESE); |
| 67 | + LocaleContextHolder.setLocaleContext(localeContext); |
| 68 | + assertThat(LocaleContextHolder.getLocale()).isEqualTo(Locale.SIMPLIFIED_CHINESE); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + @DisplayName("Given locale context with en_US then return en_US locale") |
| 73 | + void givenLocaleContextWithEnUSThenReturnEnUSLocale() { |
| 74 | + LocaleContext localeContext = new LocaleContext(Locale.US); |
| 75 | + LocaleContextHolder.setLocaleContext(localeContext); |
| 76 | + assertThat(LocaleContextHolder.getLocale()).isEqualTo(Locale.US); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + @DisplayName("Given no locale context then return null") |
| 81 | + void givenNoLocaleContextThenReturnNull() { |
| 82 | + LocaleContextHolder.clear(); |
| 83 | + assertThat(LocaleContextHolder.getLocale()).isNull(); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Nested |
| 88 | + @DisplayName("Test method: clear") |
| 89 | + class TestClear { |
| 90 | + |
| 91 | + @Test |
| 92 | + @DisplayName("Given existing locale context then clear it") |
| 93 | + void givenExistingLocaleContextThenClearIt() { |
| 94 | + LocaleContext localeContext = new LocaleContext(Locale.SIMPLIFIED_CHINESE); |
| 95 | + LocaleContextHolder.setLocaleContext(localeContext); |
| 96 | + assertThat(LocaleContextHolder.getLocaleContext()).isNotNull(); |
| 97 | + |
| 98 | + LocaleContextHolder.clear(); |
| 99 | + assertThat(LocaleContextHolder.getLocaleContext()).isNull(); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments