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.allOf;
20  import static org.hamcrest.Matchers.hasItem;
21  
22  import java.net.URL;
23  import java.net.URLClassLoader;
24  import java.util.List;
25  import java.util.Locale;
26  
27  import org.junit.Before;
28  import org.junit.Test;
29  
30  import de.smartics.properties.api.core.domain.ConfigException;
31  import de.smartics.testdoc.annotations.Uut;
32  import de.smartics.util.lang.classpath.ClassPathContext;
33  
34  /**
35   * Tests {@link LocaleFinder}.
36   */
37  public class LocaleFinderTest
38  {
39    // ********************************* Fields *********************************
40  
41    // --- constants ------------------------------------------------------------
42  
43    private static final Locale DEFAULT_LOCALE = new Locale("");
44  
45    private static final Locale VARIANT_LOCALE = new Locale("de", "DE", "test");
46  
47    private static final String TEST_ID = "test";
48  
49    // --- members --------------------------------------------------------------
50  
51    @Uut
52    private LocaleFinder uut;
53  
54    // ****************************** Inner Classes *****************************
55  
56    // ********************************* Methods ********************************
57  
58    // --- prepare --------------------------------------------------------------
59  
60    @Before
61    public void setUp()
62    {
63      this.uut = new LocaleFinder();
64    }
65  
66    // --- helper ---------------------------------------------------------------
67  
68    private static ClassLoader createClassLoader(final String jar)
69    {
70      final URL jarUrl = LocaleFinderTest.class.getResource(jar + ".jar");
71      final ClassLoader loader = new URLClassLoader(new URL[] { jarUrl });
72      return loader;
73    }
74  
75    private List<Locale> runTest(final String jar) throws ConfigException
76    {
77      final ClassLoader loader = createClassLoader(jar);
78      final ClassPathContext context = new ClassPathContext(loader, null);
79      final List<Locale> locales = uut.find(TEST_ID, context);
80      return locales;
81    }
82  
83    // --- tests ----------------------------------------------------------------
84  
85    @Test
86    public void returnsTheDefaultLocaleIfNoPropertySetIsFound()
87    {
88      final List<Locale> locales = runTest("no-property-set");
89      assertThat(locales, hasItem(DEFAULT_LOCALE));
90    }
91  
92    @Test
93    public void returnsTheDefaultLocaleIfOnlyPropertySetsAreFoundWithoutSuffix()
94    {
95      final List<Locale> locales = runTest("no-locale");
96      assertThat(locales, hasItem(DEFAULT_LOCALE));
97    }
98  
99    @Test
100   public void returnsAllLocalesOfAPropertySet()
101   {
102     final List<Locale> locales = runTest("two-property-sets-full-locale");
103     assertThat(
104         locales,
105         allOf(hasItem(DEFAULT_LOCALE), hasItem(Locale.ENGLISH),
106             hasItem(Locale.GERMAN), hasItem(Locale.GERMANY),
107             hasItem(VARIANT_LOCALE)));
108   }
109 
110   @Test
111   public void returnsAllLocalesOfDifferentPropertySetsEvenIfNotEveryLocaleIsSupportedByEveryPropertySet()
112   {
113     final List<Locale> locales = runTest("two-property-sets-missing-locales");
114     assertThat(
115         locales,
116         allOf(hasItem(DEFAULT_LOCALE), hasItem(Locale.ENGLISH),
117             hasItem(Locale.GERMAN), hasItem(Locale.GERMANY),
118             hasItem(VARIANT_LOCALE)));
119   }
120 }