View Javadoc

1   /*
2    * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
3    *
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    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
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.smartics.properties.spi.core.context;
17  
18  import static org.hamcrest.MatcherAssert.assertThat;
19  import static org.hamcrest.Matchers.is;
20  import static org.hamcrest.Matchers.nullValue;
21  
22  import java.util.Locale;
23  
24  import org.junit.Before;
25  import org.junit.Test;
26  
27  import de.smartics.testdoc.annotations.Uut;
28  
29  /**
30   * Tests {@link LocaleFinder#calcLocale(String)}.
31   */
32  public class LocaleFinderCalcLocaleTest
33  {
34    // ********************************* Fields *********************************
35  
36    // --- constants ------------------------------------------------------------
37  
38    // --- members --------------------------------------------------------------
39  
40    @Uut(method = "calcLocale(String)")
41    private LocaleFinder uut;
42  
43    // ****************************** Inner Classes *****************************
44  
45    // ********************************* Methods ********************************
46  
47    // --- prepare --------------------------------------------------------------
48  
49    @Before
50    public void setUp()
51    {
52      this.uut = new LocaleFinder();
53    }
54  
55    // --- helper ---------------------------------------------------------------
56  
57    // --- tests ----------------------------------------------------------------
58  
59    @Test
60    public void returnsNullIfNoLocaleIsProvided()
61    {
62      final String urlString = "fileName.txt";
63      final Locale locale = uut.calcLocale(urlString);
64      assertThat(locale, is(nullValue()));
65    }
66  
67    @Test
68    public void returnsNullIfNoLocaleIsProvidedWithPath()
69    {
70      final String urlString = "my/path/fileName.txt";
71      final Locale locale = uut.calcLocale(urlString);
72      assertThat(locale, is(nullValue()));
73    }
74  
75    @Test
76    public void returnsNullIfNoLocaleIsProvidedOnAFileWithNoExtension()
77    {
78      final String urlString = "fileName";
79      final Locale locale = uut.calcLocale(urlString);
80      assertThat(locale, is(nullValue()));
81    }
82  
83    @Test
84    public void returnsALanguageLocale()
85    {
86      final String urlString = "fileName_de.txt";
87      final Locale locale = uut.calcLocale(urlString);
88      assertThat(locale, is(Locale.GERMAN));
89    }
90  
91    @Test
92    public void returnsACountryLocale()
93    {
94      final String urlString = "fileName_de_DE.txt";
95      final Locale locale = uut.calcLocale(urlString);
96      assertThat(locale, is(Locale.GERMANY));
97    }
98  
99    @Test
100   public void returnsVariantLocale()
101   {
102     final String urlString = "fileName_de_DE_test.txt";
103     final Locale locale = uut.calcLocale(urlString);
104     assertThat(locale, is(new Locale("de", "DE", "test")));
105   }
106 
107   @Test
108   public void returnsRecognizedTheLocaleRelevantPartInAFileName()
109   {
110     final String urlString = "file_name_de.txt";
111     final Locale locale = uut.calcLocale(urlString);
112     assertThat(locale, is(Locale.GERMAN));
113   }
114 
115   @Test
116   public void returnsRecognizedTheLocaleRelevantPartInAFileNameEvenWithoutExtension()
117   {
118     final String urlString = "file_name_de";
119     final Locale locale = uut.calcLocale(urlString);
120     assertThat(locale, is(Locale.GERMAN));
121   }
122 
123   @Test
124   public void separatorWithoutLocaleAtEndOfFileName()
125   {
126     final String urlString = "file_";
127     final Locale locale = uut.calcLocale(urlString);
128     assertThat(locale, is(nullValue()));
129   }
130 
131   @Test
132   public void separatorWithLocaleAtEndOfFileName()
133   {
134     final String urlString = "fileName_de_DE_test_";
135     final Locale locale = uut.calcLocale(urlString);
136     assertThat(locale, is(new Locale("de", "DE", "test_")));
137   }
138 }