1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.i18n;
19
20 import junit.framework.TestCase;
21
22 import java.util.Locale;
23 import java.util.Map;
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 public abstract class MessageProviderTestBase extends TestCase {
78
79 protected MessageProviderTestBase() {
80 }
81
82 protected MessageProviderTestBase(String testName) {
83 super(testName);
84 }
85
86
87
88
89
90
91
92 public void setUp() throws Exception {
93
94 Locale.setDefault(Locale.ENGLISH);
95 }
96
97 public void tearDown() throws Exception {
98
99 MessageManager.clearMessageProviders();
100 }
101
102
103
104
105
106
107 protected void testGetText(MessageProvider messageProvider) {
108
109 assertEnglishTexts(messageProvider, Locale.ENGLISH);
110
111
112 assertEnglishTexts(messageProvider, Locale.US);
113
114
115 assertEnglishTexts(messageProvider, new Locale("en", "", "scottish"));
116
117
118 assertEnglishTexts(messageProvider, new Locale("en", "GB", "scottish"));
119
120
121 assertGermanTexts(messageProvider, Locale.GERMAN);
122
123
124 assertGermanTexts(messageProvider, Locale.GERMANY);
125
126
127 assertGermanTexts(messageProvider, new Locale("de", "", "foo"));
128
129
130 assertGermanTexts(messageProvider, new Locale("de", "CH", "foo"));
131
132
133 assertEnglishTexts(messageProvider, Locale.JAPANESE);
134
135
136
137 assertNull(messageProvider.getText("helloWorld", "nonExistentEntry", Locale.ENGLISH));
138
139
140
141
142
143
144 try {
145 assertNull(messageProvider.getText("nonExistentId", "foo", Locale.ENGLISH));
146 }
147 catch(MessageNotFoundException mnfex) {
148 assertEquals("No message entries found for bundle with key nonExistentId", mnfex.getMessage());
149 }
150 }
151
152 private void assertEnglishTexts(MessageProvider messageProvider, Locale locale) {
153 assertEquals("Locale = " + locale + ", title",
154 "Hello World", messageProvider.getText("helloWorld", "title", locale));
155 assertEquals("Locale = " + locale + ", text",
156 "Hello World, we are in {0}.", messageProvider.getText("helloWorld", "text", locale));
157 }
158
159 private void assertGermanTexts(MessageProvider messageProvider, Locale locale) {
160 assertEquals("Locale = " + locale + ", title",
161 "Hallo Welt", messageProvider.getText("helloWorld", "title", locale));
162 assertEquals("Locale = " + locale + ", text",
163 "Hallo Welt, wir sind in {0}.", messageProvider.getText("helloWorld", "text", locale));
164 }
165
166
167 protected void testGetEntries(MessageProvider messageProvider, boolean hasNonTranslatedEntry) {
168
169
170
171 assertEnglishEntries(messageProvider, Locale.ENGLISH, hasNonTranslatedEntry);
172
173
174 assertEnglishEntries(messageProvider, Locale.US, hasNonTranslatedEntry);
175
176
177 assertEnglishEntries(messageProvider, new Locale("en", "", "scottish"), hasNonTranslatedEntry);
178
179 assertGermanEntries(messageProvider, Locale.GERMAN);
180
181
182 assertGermanEntries(messageProvider, Locale.GERMANY);
183
184
185 assertEnglishEntries(messageProvider, Locale.JAPANESE, hasNonTranslatedEntry);
186
187
188 try {
189 messageProvider.getEntries("nonExistentId", Locale.ENGLISH);
190 fail("Non-existent ID should cause exception");
191 }
192 catch(MessageNotFoundException mnfex) {
193 assertEquals("No message entries found for bundle with key nonExistentId", mnfex.getMessage());
194 }
195 }
196
197 private static void assertEnglishEntries(MessageProvider messageProvider, Locale locale, boolean hasNonTranslatedEntry) {
198 Map entries = messageProvider.getEntries("helloWorld", locale);
199 if(hasNonTranslatedEntry) {
200 assertEquals("Locale = " + locale + ", No of entries", 3, entries.size());
201 assertEquals("Locale = " + locale, "This entry is not translated to any other languages",
202 entries.get("notTranslated"));
203 }
204 else
205 assertEquals("Locale = " + locale + ", No of entries", 2, entries.size());
206 assertEquals("Locale = " + locale, "Hello World", (String)entries.get("title"));
207 assertEquals("Locale = " + locale, "Hello World, we are in {0}.", entries.get("text"));
208 }
209
210 private static void assertGermanEntries(MessageProvider messageProvider, Locale locale) {
211 Map entries = messageProvider.getEntries("helloWorld", locale);
212
213
214 if(entries.size() == 3)
215 assertEquals("This entry is not translated to any other languages", entries.get("notTranslated"));
216 else if(entries.size() != 2)
217 fail("No of entries should be 2 or 3!");
218
219 assertEquals("Hallo Welt", (String)entries.get("title"));
220 assertEquals("Hallo Welt, wir sind in {0}.", entries.get("text"));
221 }
222 }