aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api/src/test/java/org/sonar/api/utils/LocalizedMessagesTest.java
blob: a34d27ce2701ecdf63a4333c615e64e92e6695e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * SonarQube
 * Copyright (C) 2009-2019 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.api.utils;

import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.Enumeration;
import java.util.List;
import java.util.Locale;
import java.util.MissingResourceException;

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

public class LocalizedMessagesTest {
  private static final Locale DEFAULT_LOCALE = Locale.getDefault();

  @BeforeClass
  public static void beforeAll() {
    Locale.setDefault(Locale.ENGLISH);
  }

  @AfterClass
  public static void afterAll() {
    Locale.setDefault(DEFAULT_LOCALE);
  }

  @Test
  public void mergeBundles() {
    LocalizedMessages messages = new LocalizedMessages(Locale.ENGLISH, "Test", "PluginFoo");

    assertThat(messages.getString("test.one")).isEqualTo("One");
    assertThat(messages.getString("test.two")).isEqualTo("Two");
    assertThat(messages.getString("foo.hello")).isEqualTo("Hello");
  }

  @Test
  public void mergeBundlesByLocale() {
    LocalizedMessages messages = new LocalizedMessages(Locale.FRENCH, "Test", "PluginFoo");

    assertThat(messages.getString("test.one")).isEqualTo("Un");
    assertThat(messages.getString("test.two")).isEqualTo("Deux");
    assertThat(messages.getString("foo.hello")).isEqualTo("Hello");// not in french, use the default locale
  }

  @Test
  public void useDefaultWhenMissingLocale() {
    LocalizedMessages messages = new LocalizedMessages(Locale.JAPANESE, "Test", "PluginFoo");

    assertThat(messages.getString("test.one")).isEqualTo("One");
    assertThat(messages.getString("foo.hello")).isEqualTo("Hello");
  }

  @Test(expected = MissingResourceException.class)
  public void failIfMissingKey() {
    LocalizedMessages messages = new LocalizedMessages(Locale.FRENCH, "Test", "PluginFoo");
    messages.getString("unknown");
  }

  @Test
  public void format() {
    LocalizedMessages messages = new LocalizedMessages(Locale.ENGLISH, "Test", "PluginFoo");
    assertThat(messages.format("test.one")).isEqualTo("One");
  }

  @Test
  public void formatNeverFails() {
    LocalizedMessages messages = new LocalizedMessages(Locale.ENGLISH, "Test", "PluginFoo");
    assertThat(messages.format("unknown")).isEqualTo("unknown");
  }

  @Test
  public void formatParameters() {
    LocalizedMessages messages = new LocalizedMessages(Locale.ENGLISH, "Test", "PluginFoo");
    assertThat(messages.format("with.string.params", "inspection", "rock")).isEqualTo("Continuous inspection will rock !");
    assertThat(messages.format("with.string.params", "rock", "inspection")).isEqualTo("Continuous rock will inspection !");
  }

  @Test
  public void getKeys() {
    LocalizedMessages messages = new LocalizedMessages(Locale.ENGLISH, "Test", "PluginFoo");
    assertThat(toList(messages.getKeys())).contains("test.one", "test.two", "foo.hello");

    LocalizedMessages spanishMessages = new LocalizedMessages(new Locale("es"), "Test", "PluginFoo");
    assertThat(toList(spanishMessages.getKeys())).contains("test.one", "only.in.spanish");
  }

  private List<String> toList(Enumeration<String> enumeration) {
    return Lists.newArrayList(Iterators.forEnumeration(enumeration));
  }
}