1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
36
37 @SuppressWarnings("unchecked")
38 public class LocaleFinderTest
39 {
40
41
42
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
51
52 @Uut
53 private LocaleFinder uut;
54
55
56
57
58
59
60
61 @Before
62 public void setUp()
63 {
64 this.uut = new LocaleFinder();
65 }
66
67
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
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 }