View Javadoc
1   /**
2    * Copyright 2011, Aiki IT, FotoRenamer
3    * <p/>
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * <p/>
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * <p/>
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.aikiit.fotorenamer.util;
17  
18  import org.junit.jupiter.api.Test;
19  
20  import java.util.Locale;
21  
22  import static de.aikiit.fotorenamer.util.LocalizationHelper.*;
23  import static org.junit.jupiter.api.Assertions.assertEquals;
24  
25  /**
26   * Testing resource bundling.
27   *
28   * @author hirsch
29   * @version 23.08.11
30   */
31  public class LocalizationHelperTest {
32  
33      /**
34       * Retrieve a plain i18n-value.
35       */
36      @Test
37      void checkValueRetrievingFromBundle() {
38          assertEquals("Fortschritt", getBundleString("fotorenamer.ui.progress"));
39      }
40  
41      /**
42       * Retrieve a i18n-value with parameters set.
43       */
44      @SuppressWarnings("RedundantArrayCreation")
45      @Test
46      void checkParametrizedValueExtraction() {
47          assertEquals("Erfolg und dann folgt noch die 7", getParameterizedBundleString("fotorenamer.test.param",
48                  "Erfolg", 7));
49          // ignore warning, we want to test what happens here! An empty String or null changes the output.
50          assertEquals("{0} und dann folgt noch die {1}", getParameterizedBundleString("fotorenamer.test.param",
51                  new Object[]{}));
52      }
53  
54      @Test
55      void fallbackLocale() {
56          // WHEN: reset system properties
57          System.setProperty("user.language", "");
58          System.setProperty("user.country", "");
59          setLocale();
60  
61          assertEquals(Locale.GERMANY, getLocale());
62          assertEquals("de", getLanguage());
63      }
64  
65      @Test
66      void setLocaleViaSystemProperties() {
67          // WHEN: reset system properties
68          final String french = "fr";
69          System.setProperty("user.language", french);
70          System.setProperty("user.country", "CA");
71          setLocale();
72  
73          assertEquals(Locale.CANADA_FRENCH, getLocale());
74          assertEquals(french, getLanguage());
75      }
76  
77      @Test
78      void umlautEncodingWorksCorrectly() {
79          assertEquals("ßäü", getBundleString("fotorenamer.test.umlauts"));
80      }
81  
82      @Test
83      void unknownKey() {
84          String unknown = "abcde.unknown";
85          assertEquals(unknown, getBundleString(unknown));
86      }
87  }