Add unit test for L10nFilter.
[Sone.git] / src / test / java / net / pterodactylus / sone / freenet / L10nFilterTest.java
1 /*
2  * Sone - L10nFilterTest.java - Copyright © 2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.freenet;
19
20 import static freenet.l10n.BaseL10n.LANGUAGE.ENGLISH;
21 import static freenet.l10n.BaseL10n.LANGUAGE.GERMAN;
22 import static org.hamcrest.MatcherAssert.assertThat;
23 import static org.hamcrest.Matchers.is;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
26
27 import java.util.Collections;
28 import java.util.Map;
29
30 import net.pterodactylus.sone.web.WebInterface;
31
32 import freenet.l10n.BaseL10n;
33 import freenet.l10n.BaseL10n.LANGUAGE;
34
35 import com.google.common.collect.ImmutableMap;
36 import org.junit.Before;
37 import org.junit.Test;
38
39 /**
40  * Unit test for {@link L10nFilter}.
41  *
42  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43  */
44 public class L10nFilterTest {
45
46         private final BaseL10n l10n = mock(BaseL10n.class);
47         private final WebInterface webInterface = mock(WebInterface.class);
48         private final L10nFilter l10nFilter = new L10nFilter(webInterface);
49
50         @Before
51         public void setup() {
52                 when(webInterface.getL10n()).thenReturn(l10n);
53                 when(l10n.getString("SingleString")).thenReturn("Translated Value");
54                 when(l10n.getString("MultiString")).thenReturn("{0,number}, {1}");
55         }
56
57         @Test
58         public void formatStringWithoutParameters() {
59                 String translatedValue = l10nFilter.format(null, "SingleString", Collections.<String, Object>emptyMap());
60                 assertThat(translatedValue, is("Translated Value"));
61         }
62
63         @Test
64         public void formatStringWithParametersInEnglish() {
65                 setLanguage(ENGLISH);
66                 Map<String, Object> parameters = ImmutableMap.<String, Object>of("0", 17.123, "1", "Value C");
67                 String translatedValue = l10nFilter.format(null, "MultiString", parameters);
68                 assertThat(translatedValue, is("17.123, Value C"));
69         }
70
71         @Test
72         public void formatStringWithParametersInGerman() {
73                 setLanguage(GERMAN);
74                 Map<String, Object> parameters = ImmutableMap.<String, Object>of("0", 17.123, "1", "Value C");
75                 String translatedValue = l10nFilter.format(null, "MultiString", parameters);
76                 assertThat(translatedValue, is("17,123, Value C"));
77         }
78
79         private void setLanguage(LANGUAGE language) {
80                 when(l10n.getSelectedLanguage()).thenReturn(language);
81         }
82
83 }