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