View Javadoc
1   /**
2    * SpamSchutz - simple way to protect your mail addresses from naïve spammers
3    * Copyright (C) 2011, Aiki IT
4    * <p/>
5    * This program is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    * <p/>
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   * <p/>
15   * You should have received a copy of the GNU General Public License
16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  package de.aikiit.spamprotector.util;
19  
20  import org.junit.Test;
21  
22  import java.util.Locale;
23  
24  import static de.aikiit.spamprotector.util.LocalizationHelper.getBundleString;
25  import static de.aikiit.spamprotector.util.LocalizationHelper.getLanguage;
26  import static de.aikiit.spamprotector.util.LocalizationHelper.getLocale;
27  import static de.aikiit.spamprotector.util.LocalizationHelper.getParameterizedBundleString;
28  import static de.aikiit.spamprotector.util.LocalizationHelper.setLocale;
29  import static org.assertj.core.api.Assertions.assertThat;
30  
31  /**
32   * Testing resource bundling and i18n handling.
33   */
34  public class LocalizationHelperTest {
35  
36      /**
37       * Retrieve a i18n-value with parameters.
38       */
39      @Test
40      public final void checkParametrizedValueExtraction() {
41          assertThat(getParameterizedBundleString("spamschutz.test.param", "Erfolg", 7))
42                  .isEqualTo("Erfolg und dann folgt noch die 7");
43          // ignore warning, we want to test what happens here! An empty String or null changes the output.
44          assertThat(getParameterizedBundleString("spamschutz.test.param", new Object[]{}))
45                  .isEqualTo("{0} und dann folgt noch die {1}");
46      }
47  
48      @Test
49      public final void fallbackLocale() {
50          // WHEN: reset system properties
51          System.setProperty("user.language", "");
52          System.setProperty("user.country", "");
53          setLocale();
54  
55          assertThat(getLocale()).isEqualTo(Locale.GERMANY);
56          assertThat(getLanguage()).isEqualTo("de");
57      }
58  
59      @Test
60      public final void setLocaleViaSystemProperties() {
61          // WHEN: reset system properties
62          final String french = "fr";
63          System.setProperty("user.language", french);
64          System.setProperty("user.country", "CA");
65          setLocale();
66  
67          assertThat(getLocale()).isEqualTo(Locale.CANADA_FRENCH);
68          assertThat(getLanguage()).isEqualTo(french);
69      }
70  
71      @Test
72      public final void umlautEncodingWorksCorrectly() {
73          assertThat(getBundleString("spamschutz.test.umlauts")).isEqualTo("ßäü");
74      }
75  
76      @Test
77      public final void unknownKey() {
78          String unknown = "abcde.unknown";
79          assertThat(getBundleString(unknown)).isEqualTo(unknown);
80      }
81  }