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.*;
25  
26  import static org.junit.Assert.assertEquals;
27  
28  /**
29   * Testing resource bundling and i18n handling.
30   */
31  public class LocalizationHelperTest {
32  
33      /**
34       * Retrieve a i18n-value with parameters.
35       */
36      @Test
37      public final void checkParametrizedValueExtraction() {
38          assertEquals("Erfolg und dann folgt noch die 7", getParameterizedBundleString("spamschutz.test.param",
39                  "Erfolg", 7));
40          // ignore warning, we want to test what happens here! An empty String or null changes the output.
41          assertEquals("{0} und dann folgt noch die {1}", getParameterizedBundleString("spamschutz.test.param",
42                  new Object[]{}));
43      }
44  
45      @Test
46      public final void fallbackLocale() {
47          // WHEN: reset system properties
48          System.setProperty("user.language", "");
49          System.setProperty("user.country", "");
50          setLocale();
51  
52          assertEquals(Locale.GERMANY, getLocale());
53          assertEquals("de", getLanguage());
54      }
55  
56      @Test
57      public final void setLocaleViaSystemProperties() {
58          // WHEN: reset system properties
59          final String french = "fr";
60          System.setProperty("user.language", french);
61          System.setProperty("user.country", "CA");
62          setLocale();
63  
64          assertEquals(Locale.CANADA_FRENCH, getLocale());
65          assertEquals(french, getLanguage());
66      }
67  
68      @Test
69      public final void umlautEncodingWorksCorrectly() {
70          assertEquals("ßäü", getBundleString("spamschutz.test.umlauts"));
71      }
72  
73      @Test
74      public final void unknownKey() {
75          String unknown = "abcde.unknown";
76          assertEquals(unknown, getBundleString(unknown));
77      }
78  }