You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ServerI18nTest.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.l18n;
  21. import java.util.List;
  22. import java.util.Locale;
  23. import java.util.stream.Stream;
  24. import org.junit.Before;
  25. import org.junit.Test;
  26. import org.sonar.api.utils.internal.TestSystem2;
  27. import org.sonar.core.extension.CoreExtension;
  28. import org.sonar.core.extension.CoreExtensionRepository;
  29. import org.sonar.core.platform.PluginInfo;
  30. import org.sonar.core.platform.PluginRepository;
  31. import static java.util.Collections.singletonList;
  32. import static org.assertj.core.api.Assertions.assertThat;
  33. import static org.mockito.Mockito.mock;
  34. import static org.mockito.Mockito.when;
  35. public class ServerI18nTest {
  36. private TestSystem2 system2 = new TestSystem2();
  37. private ServerI18n underTest;
  38. @Before
  39. public void before() {
  40. PluginRepository pluginRepository = mock(PluginRepository.class);
  41. List<PluginInfo> plugins = singletonList(newPlugin("checkstyle"));
  42. when(pluginRepository.getPluginInfos()).thenReturn(plugins);
  43. CoreExtensionRepository coreExtensionRepository = mock(CoreExtensionRepository.class);
  44. Stream<CoreExtension> coreExtensions = Stream.of(newCoreExtension("coreext"), newCoreExtension("othercorext"));
  45. when(coreExtensionRepository.loadedCoreExtensions()).thenReturn(coreExtensions);
  46. underTest = new ServerI18n(pluginRepository, system2, coreExtensionRepository);
  47. underTest.doStart(getClass().getClassLoader());
  48. }
  49. @Test
  50. public void get_english_labels() {
  51. assertThat(underTest.message(Locale.ENGLISH, "any", null)).isEqualTo("Any");
  52. assertThat(underTest.message(Locale.ENGLISH, "coreext.rule1.name", null)).isEqualTo("Rule one");
  53. }
  54. @Test
  55. public void get_english_labels_when_default_locale_is_not_english() {
  56. Locale defaultLocale = Locale.getDefault();
  57. try {
  58. Locale.setDefault(Locale.FRENCH);
  59. assertThat(underTest.message(Locale.ENGLISH, "any", null)).isEqualTo("Any");
  60. assertThat(underTest.message(Locale.ENGLISH, "coreext.rule1.name", null)).isEqualTo("Rule one");
  61. } finally {
  62. Locale.setDefault(defaultLocale);
  63. }
  64. }
  65. @Test
  66. public void get_labels_from_french_pack() {
  67. assertThat(underTest.message(Locale.FRENCH, "coreext.rule1.name", null)).isEqualTo("Rule un");
  68. assertThat(underTest.message(Locale.FRENCH, "any", null)).isEqualTo("Tous");
  69. }
  70. private static PluginInfo newPlugin(String key) {
  71. PluginInfo plugin = mock(PluginInfo.class);
  72. when(plugin.getKey()).thenReturn(key);
  73. return plugin;
  74. }
  75. private static CoreExtension newCoreExtension(String name) {
  76. CoreExtension res = mock(CoreExtension.class);
  77. when(res.getName()).thenReturn(name);
  78. return res;
  79. }
  80. }