Debian Codegraph @

tests/reproducers/simple/LocalesTest/testcases/LocalesTestTest.java

raw
1 /* LocalisedInformationElementTest.java
2 Copyright (C) 2012 Red Hat, Inc.
3
4 This file is part of IcedTea.
5
6 IcedTea is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, version 2.
9
10 IcedTea is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with IcedTea; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 02110-1301 USA.
19
20 Linking this library statically or dynamically with other modules is
21 making a combined work based on this library. Thus, the terms and
22 conditions of the GNU General Public License cover the whole
23 combination.
24
25 As a special exception, the copyright holders of this library give you
26 permission to link this library with independent modules to produce an
27 executable, regardless of the license terms of these independent
28 modules, and to copy and distribute the resulting executable under
29 terms of your choice, provided that you also meet, for each linked
30 independent module, the terms and conditions of the license of that
31 module. An independent module is a module which is not derived from
32 or based on this library. If you modify this library, you may extend
33 this exception to your version of the library, but you are not
34 obligated to do so. If you do not wish to do so, delete this
35 exception statement from your version.
36 */
37
38 import java.io.IOException;
39 import java.io.InputStream;
40 import java.util.ArrayList;
41 import java.util.Arrays;
42 import java.util.Collections;
43 import java.util.Comparator;
44 import java.util.Iterator;
45 import java.util.List;
46 import java.util.Map;
47 import java.util.Map.Entry;
48 import java.util.PropertyResourceBundle;
49 import java.util.ResourceBundle;
50 import java.util.Set;
51 import net.sourceforge.jnlp.ProcessResult;
52 import net.sourceforge.jnlp.ServerAccess;
53 import org.junit.Assert;
54 import org.junit.Test;
55
56 public class LocalesTestTest {
57
58 private static final ServerAccess server = new ServerAccess();
59 String[] keys = {
60 "BOUsage",
61 "BOUsage2",
62 "BOArg",
63 "BOParam",
64 "BOProperty",
65 "BOLicense",
66 "BOVerbose",
67 "BOAbout",
68 "BONosecurity",
69 "BONoupdate",
70 "BOHeadless",
71 "BOStrict",
72 "BOViewer",
73 "BXnofork",
74 "BXclearcache",
75 "BOHelp1"};
76
77 /**
78 * this will prepare new set of variables with wanted locale, which can be
79 * then passed to subprocess
80 *
81 * @param locale - locale to be set to LANG variable, eg cs_CZ.UTF-8
82 */
83 public static String[] getChangeLocalesForSubproces(String locale) {
84 ServerAccess.logOutputReprint("Setting locales");
85 Map<String, String> p = System.getenv();
86 Set<Entry<String, String>> r = p.entrySet();
87 List<Entry<String, String>> rr = new ArrayList<Entry<String, String>>(r);
88 Collections.sort(rr, new Comparator<Entry<String, String>>() {
89
90 @Override
91 public int compare(Entry<String, String> o1, Entry<String, String> o2) {
92 return o1.getKey().compareTo(o2.getKey());
93 }
94 });
95 String[] l = new String[rr.size()];
96 int i = 0;
97 for (Iterator<Entry<String, String>> it = rr.iterator(); it.hasNext(); i++) {
98 Entry<String, String> entry = it.next();
99 String v = entry.getValue();
100 String s = entry.getKey() + "=" + v;
101 //System.out.println(s);
102 if (entry.getKey().equals("LANG")) {
103 ServerAccess.logOutputReprint("was " + v);
104 v = locale;
105 ServerAccess.logOutputReprint("set " + v);
106 }
107 s = entry.getKey() + "=" + v;
108 l[i] = s;
109 }
110 return l;
111 }
112
113 private ResourceBundle getPropertiesDe() throws IOException {
114 return getProperties("_de");
115 }
116
117
118 public ResourceBundle getPropertiesCz() throws IOException {
119 return getProperties("_cs");
120
121 }
122
123 public ResourceBundle getPropertiesPl() throws IOException {
124 return getProperties("_pl");
125
126 }
127
128 public ResourceBundle getPropertiesEn() throws IOException {
129 return getProperties("");
130
131 }
132
133 public ResourceBundle getProperties(String s) throws IOException {
134 ClassLoader cl = this.getClass().getClassLoader();
135 ServerAccess.logOutputReprint(cl==null?"null":cl.toString());
136 if (cl == null) {
137 cl = ClassLoader.getSystemClassLoader();
138 ServerAccess.logOutputReprint(cl==null?"null":cl.toString());
139 }
140 InputStream q = cl.getResourceAsStream("net/sourceforge/jnlp/resources/Messages" + s + ".properties");
141 return new PropertyResourceBundle(q);
142
143 }
144 //just launching javaws -about to see if messages are corectly localised
145 List<String> javaws = Arrays.asList(new String[]{server.getJavawsLocation(), "-help", ServerAccess.HEADLES_OPTION});
146
147 @Test
148 public void testLocalesEnUsUtf() throws Exception {
149 String[] l = getChangeLocalesForSubproces("en_US.UTF-8");
150 ProcessResult pr = ServerAccess.executeProcess(javaws, null, null, l);
151 assertEnglish(pr.stdout);
152 assertNotCz(pr.stdout);
153 assertNotDe(pr.stdout);
154 assertNotPl(pr.stdout);
155 }
156
157 @Test
158 public void testLocalesCsCz() throws Exception {
159 String[] l = getChangeLocalesForSubproces("cs_CZ");
160 ProcessResult pr = ServerAccess.executeProcess(javaws, null, null, l);
161 assertNotEnglish(pr.stdout);
162 assertNotCz(pr.stdout);
163 assertNotDe(pr.stdout);
164 assertNotPl(pr.stdout);
165 iteratePropertiesForAproxCzCs(pr.stdout);
166 }
167
168 @Test
169 public void testLocalesCsCzUtf() throws Exception {
170 String[] l = getChangeLocalesForSubproces("cs_CZ.UTF-8");
171 ProcessResult pr = ServerAccess.executeProcess(javaws, null, null, l);
172 assertNotEnglish(pr.stdout);
173 assertNotDe(pr.stdout);
174 assertCz(pr.stdout);
175 assertNotPl(pr.stdout);
176 iteratePropertiesForAproxCzCs(pr.stdout);
177 }
178
179 @Test
180 public void testLocalesPlPL() throws Exception {
181 String[] l = getChangeLocalesForSubproces("pl_PL");
182 ProcessResult pr = ServerAccess.executeProcess(javaws, null, null, l);
183 assertNotEnglish(pr.stdout);
184 assertNotCz(pr.stdout);
185 assertNotDe(pr.stdout);
186 iteratePropertiesForAproxPl(pr.stdout);
187 }
188
189 @Test
190 public void testLocalesPlPLUtf() throws Exception {
191 String[] l = getChangeLocalesForSubproces("pl_PL.UTF-8");
192 ProcessResult pr = ServerAccess.executeProcess(javaws, null, null, l);
193 assertNotEnglish(pr.stdout);
194 assertNotDe(pr.stdout);
195 assertNotCz(pr.stdout);
196 assertPl(pr.stdout);
197 iteratePropertiesForAproxPl(pr.stdout);
198 }
199
200 @Test
201 public void testLocalesDeDe() throws Exception {
202 String[] l = getChangeLocalesForSubproces("de_DE");
203 ProcessResult pr = ServerAccess.executeProcess(javaws, null, null, l);
204 assertNotEnglish(pr.stdout);
205 assertNotCz(pr.stdout);
206 assertNotPl(pr.stdout);
207 iteratePropertiesForAproxDe(pr.stdout);
208 }
209
210 @Test
211 public void testLocalesDeDeUtf() throws Exception {
212 String[] l = getChangeLocalesForSubproces("de_DE.UTF-8");
213 ProcessResult pr = ServerAccess.executeProcess(javaws, null, null, l);
214 assertNotEnglish(pr.stdout);
215 assertNotCz(pr.stdout);
216 assertDe(pr.stdout);
217 assertNotPl(pr.stdout);
218 iteratePropertiesForAproxDe(pr.stdout);
219 }
220
221
222 @Test
223 public void testLocalesDe_unknowButValidDeLocale() throws Exception {
224 String[] l = getChangeLocalesForSubproces("de_LU");
225 ProcessResult pr = ServerAccess.executeProcess(javaws, null, null, l);
226 assertNotEnglish(pr.stdout);
227 assertNotCz(pr.stdout);
228 assertNotPl(pr.stdout);
229 iteratePropertiesForAproxDe(pr.stdout);
230 }
231
232 @Test
233 public void testLocalesDeUtf_unknowButValidDeLocale() throws Exception {
234 String[] l = getChangeLocalesForSubproces("de_LU.UTF-8");
235 ProcessResult pr = ServerAccess.executeProcess(javaws, null, null, l);
236 assertNotEnglish(pr.stdout);
237 assertNotCz(pr.stdout);
238 assertDe(pr.stdout);
239 assertNotPl(pr.stdout);
240 iteratePropertiesForAproxDe(pr.stdout);
241 }
242
243
244
245 @Test
246 public void testLocalesDe_globalDe() throws Exception {
247 String[] l = getChangeLocalesForSubproces("deutsch");
248 ProcessResult pr = ServerAccess.executeProcess(javaws, null, null, l);
249 assertNotEnglish(pr.stdout);
250 assertNotCz(pr.stdout);
251 assertNotPl(pr.stdout);
252 iteratePropertiesForAproxDe(pr.stdout);
253 }
254
255
256
257
258 @Test
259 public void testLocalesInvalid() throws Exception {
260 String[] l = getChangeLocalesForSubproces("ax_BU");
261 ProcessResult pr = ServerAccess.executeProcess(javaws, null, null, l);
262 assertEnglish(pr.stdout);
263 assertNotCz(pr.stdout);
264 assertNotDe(pr.stdout);
265 assertNotPl(pr.stdout);
266 }
267
268 private void assertEnglish(String s) throws IOException {
269 ResourceBundle props = getPropertiesEn();
270 iteratePropertiesFor(props, s, true, "english");
271 }
272
273 private void assertNotEnglish(String s) throws IOException {
274 ResourceBundle props = getPropertiesEn();
275 iteratePropertiesFor(props, s, false, "english");
276 }
277
278 private void assertCz(String s) throws IOException {
279 ResourceBundle props = getPropertiesCz();
280 iteratePropertiesFor(props, s, true, "czech");
281 }
282
283 private void assertPl(String s) throws IOException {
284 ResourceBundle props = getPropertiesPl();
285 iteratePropertiesFor(props, s, true, "polish");
286 }
287
288 private void assertDe(String s) throws IOException {
289 ResourceBundle props = getPropertiesDe();
290 iteratePropertiesFor(props, s, true, "de");
291 }
292
293
294 private void assertNotCz(String s) throws IOException {
295 ResourceBundle props = getPropertiesCz();
296 iteratePropertiesFor(props, s, false, "czech");
297 }
298
299 private void assertNotPl(String s) throws IOException {
300 ResourceBundle props = getPropertiesPl();
301 iteratePropertiesFor(props, s, false, "polish");
302 }
303
304 private void assertNotDe(String s) throws IOException {
305 ResourceBundle props = getPropertiesDe();
306 iteratePropertiesFor(props, s, false, "de");
307 }
308
309
310 /**
311 * This method is iterating all keys defined in this class, geting their value in given
312 * properties, and then checking if given output have/have not (depends on value of assertTrue)
313 * this string contained.
314 *
315 * @param props
316 * @param outputToExamine
317 * @param assertTrue
318 * @param languageId
319 */
320 private void iteratePropertiesFor(ResourceBundle props, String outputToExamine, boolean assertTrue, String languageId) {
321 int keysFound = 0;
322 for (int i = 0; i < keys.length; i++) {
323 String string = keys[i];
324 String value = props.getString(string);
325 if (value == null) {
326 continue;
327 }
328 keysFound++;
329 if (assertTrue) {
330 Assert.assertTrue("Output must contains " + languageId + " text, failed on " + string, outputToExamine.contains(value));
331 } else {
332 Assert.assertFalse("Output must NOT contains " + languageId + " text, failed on " + string, outputToExamine.contains(value));
333
334 }
335 }
336 Assert.assertTrue("At least one key must be found, was not", keysFound > 0);
337 }
338
339 /**
340 * This method is iterating all keys defined in this class, geting their value in given
341 * properties, transforming this to asci-ionly regex and then checking if
342 * given output match/matchnot (depends on value of assertTrue) this string,
343 *
344 * @param outputToBeChecked
345 * @param props bundle with strings
346 * @param reg regexter with rules how to handle national characters
347 * @throws IOException
348 */
349 private void iteratePropertiesForAprox(String outputToBeChecked, ResourceBundle props, Regexer reg) throws IOException {
350 int keysFound = 0;
351 for (int i = 0; i < keys.length; i++) {
352 String string = keys[i];
353 String value = props.getString(string);
354 if (value == null) {
355 continue;
356 }
357 value = reg.regexIt(value);
358 keysFound++;
359 {
360 Assert.assertTrue("Output must match "+reg.getId() +" text, failed on " + string, outputToBeChecked.matches(value));
361 }
362 }
363 Assert.assertTrue("At least one key must be found, was not", keysFound > 0);
364 }
365
366 private void iteratePropertiesForAproxCzCs(String stdout) throws IOException {
367 iteratePropertiesForAprox(stdout, getPropertiesCz(), Regexer.cz);
368 }
369 private void iteratePropertiesForAproxDe(String stdout) throws IOException {
370 iteratePropertiesForAprox(stdout, getPropertiesDe(), Regexer.de);
371 }
372
373 private void iteratePropertiesForAproxPl(String stdout) throws IOException {
374 iteratePropertiesForAprox(stdout, getPropertiesPl(), Regexer.pl);
375 }
376
377
378
379 private static final class Regexer {
380
381 private static final String[] czEvil = {
382 "á",
383 "č",
384 "ď",
385 "ě",
386 "é",
387 "í",
388 "ň",
389 "ó",
390 "ř",
391 "š",
392 "ť",
393 "ú",
394 "ů",
395 "ý",
396 "ž",
397 "[",
398 "]",
399 "(",
400 ")"};
401 private static final String[] deEvil = {
402 "ä",
403 "ö",
404 "ß",
405 "ü",
406 "[",
407 "]",
408 "(",
409 ")"};
410
411 private static final String[] plEvil = {
412 "ó",
413 "ą",
414 "ę",
415 "ó",
416 "ł",
417 "ć",
418 "ś",
419 "ź",
420 "ż",
421 "ń",
422 "[",
423 "]",
424 "(",
425 ")"};
426
427 private static final Regexer cz = new Regexer(czEvil,"cz");
428 private static final Regexer de = new Regexer(deEvil,"de");
429 private static final Regexer pl = new Regexer(plEvil,"pl");
430
431 private final String[] map;
432 private final String id;
433
434 public Regexer(String[] map, String id) {
435 this.map = map;
436 this.id = id;
437 }
438
439 public String getId() {
440 return id;
441 }
442
443
444
445
446 /**
447 * This method transforms given string to asci-only regex, replacing
448 * groups of national characters (defined by array variable) by .+
449 *
450 * @param value
451 * @return
452 */
453 public String regexIt(String value) {
454 for (int i = 0; i < map.length; i++) {
455 String string = map[i];
456 value = value.replace(string, ".");
457 value = value.replace(string.toUpperCase(), ".");
458 }
459
460 value = value.replaceAll("\\.+", ".+");
461 value = "(?s).*" + value + ".*";
462 return value;
463 }
464 }
465 }