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.classpath.ClassPathContext;
33
34
35
36
37 public class LocaleFinderTest
38 {
39
40
41
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
50
51 @Uut
52 private LocaleFinder uut;
53
54
55
56
57
58
59
60 @Before
61 public void setUp()
62 {
63 this.uut = new LocaleFinder();
64 }
65
66
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
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 }