diff options
author | Jiri Vanek <[email protected]> | 2013-03-13 14:24:55 +0100 |
---|---|---|
committer | Jiri Vanek <[email protected]> | 2013-03-13 14:24:55 +0100 |
commit | 6d79a2c10d664c8e31c4625348007310fa2bc616 (patch) | |
tree | b2c8f5e04ab5402d54123f3c5610ec1bbb47752b /tests | |
parent | d095c09bff4bd392ba8973d273954b7eddeb19d3 (diff) |
Added tests for German i18n
* tests/reproducers/simple/LocalesTest/testcases/LocalesTestTest.java:
Enhanced to test also German localization .
* tests/netx/unit/net/sourceforge/jnlp/resources/MessagesPropertiesTest.java:
Enhanced to handle de messages and be prepared for locales with one language
but different nations
Diffstat (limited to 'tests')
-rw-r--r-- | tests/netx/unit/net/sourceforge/jnlp/resources/MessagesPropertiesTest.java | 198 | ||||
-rw-r--r-- | tests/reproducers/simple/LocalesTest/testcases/LocalesTestTest.java | 205 |
2 files changed, 294 insertions, 109 deletions
diff --git a/tests/netx/unit/net/sourceforge/jnlp/resources/MessagesPropertiesTest.java b/tests/netx/unit/net/sourceforge/jnlp/resources/MessagesPropertiesTest.java index 7929931..fc80713 100644 --- a/tests/netx/unit/net/sourceforge/jnlp/resources/MessagesPropertiesTest.java +++ b/tests/netx/unit/net/sourceforge/jnlp/resources/MessagesPropertiesTest.java @@ -36,7 +36,9 @@ package net.sourceforge.jnlp.resources; import java.util.ArrayList; +import java.util.Arrays; import java.util.Enumeration; +import java.util.LinkedList; import java.util.List; import java.util.Locale; import java.util.MissingResourceException; @@ -49,61 +51,137 @@ import org.junit.Test; public class MessagesPropertiesTest { - public static final String mainFile = "Messages"; - public static final String[] secondaryCountries = new String[]{"cs"}; - public static final String[] secondaryLanguages = new String[]{"CZ"}; - public static ResourceBundle main; - public static ResourceBundle[] secondary = new ResourceBundle[secondaryCountries.length]; + private final static class LocalesIdentifier { + + public static final LocalesIdentifier DEFAULT = new LocalesIdentifier("",""); + public static final LocalesIdentifier CZ_CS = new LocalesIdentifier("CZ","cs"); + public static final LocalesIdentifier DE = new LocalesIdentifier("de"); + //public static final LocalesIdentifier DE_DE = new LocalesIdentifier("DE","de"); + + public static final String mainFileName = "Messages"; + public static final String pckg = "net.sourceforge.jnlp.resources"; + private final String country; + private final String language; + private final Locale locale; + private final ResourceBundle bundle; + + public LocalesIdentifier(String country, String language) { + this.country = country; + this.language = language; + if (getCountry().equals("") && getLanguage().equals("")){ + locale = new Locale("unknown_so_default", "unknown_so_default"); + } else { + //get default by non existing language and country + locale = new Locale(language, country); + } + bundle = ResourceBundle.getBundle(pckg+"." + mainFileName, locale); + } + + public LocalesIdentifier(String language) { + this.country = null; + this.language = language; + locale = new Locale(language); + bundle = ResourceBundle.getBundle(pckg+"." + mainFileName, locale); + } + + public String getCountry() { + if (country == null) { + return ""; + } + return country.trim(); + } + + public String getLanguage() { + if (language == null) { + return ""; + } + return language.trim(); + } + + public ResourceBundle getBundle() { + return bundle; + } + + public Locale getLocale() { + return locale; + } + + + + public String getId() { + if (getLanguage().equals("")) { + return getCountry(); + } + if (getCountry().equals("")) { + return getLanguage(); + } + return getLanguage() + "_" + getCountry(); + } + + public String getIdentifier() { + if (getId().equals("")) { + return "default"; + } + return getId(); + } + + @Override + public String toString() { + return pckg+"."+mainFileName+"_"+getId(); + } + + + + + + + } + + public static LocalesIdentifier main; + public static LocalesIdentifier[] secondary; @BeforeClass public static void loadResourceBoundels() { - assertTrue("length of countries and languages must be same", secondaryCountries.length == secondaryLanguages.length); //get default by non existing language and country - main = ResourceBundle.getBundle("net.sourceforge.jnlp.resources." + mainFile, new Locale("dfgdfg", "gvff")); - secondary = new ResourceBundle[secondaryCountries.length]; + main = LocalesIdentifier.DEFAULT; assertNotNull(main); - for (int i = 0; i < secondaryCountries.length; i++) { - String country = secondaryCountries[i]; - String language = secondaryLanguages[i]; - secondary[i] = ResourceBundle.getBundle("net.sourceforge.jnlp.resources." + mainFile, new Locale(country, language)); + secondary= new LocalesIdentifier[] {LocalesIdentifier.CZ_CS,LocalesIdentifier.DE}; + assertNotNull(secondary); + for (int i = 0; i < secondary.length; i++) { assertNotNull(secondary[i]); + } } @Test public void allResourcesAreReallyDifferent() { - List<String> ids = new ArrayList<String>(secondary.length + 1); - ids.add("default"); - List<ResourceBundle> bundles = new ArrayList<ResourceBundle>(secondary.length + 1); + List<LocalesIdentifier> bundles = new ArrayList<LocalesIdentifier>(secondary.length + 1); + String detailResults=""; bundles.add(main); int errors = 0; - for (int i = 0; i < secondaryCountries.length; i++) { - String country = secondaryCountries[i]; - String language = secondaryLanguages[i]; - ids.add(country + "_" + language); - bundles.add(secondary[i]); - - } + bundles.addAll(Arrays.asList(secondary)); for (int i = 0; i < bundles.size(); i++) { - ResourceBundle resourceBundle1 = bundles.get(i); - String id1 = ids.get(i); - Enumeration<String> keys1 = resourceBundle1.getKeys(); + LocalesIdentifier resourceBundle1 = bundles.get(i); + Enumeration<String> keys1 = resourceBundle1.getBundle().getKeys(); for (int j = 0; j < bundles.size(); j++) { - if (i == j) { + LocalesIdentifier resourceBundle2 = bundles.get(j); + if (resourceBundle1.getLanguage().equals(resourceBundle2.getLanguage())) { + //do not compare same language groups + allLog("Skipping same language " + resourceBundle1.getLocale() + " x " + resourceBundle2.getLocale() + " (should be " + resourceBundle1.getIdentifier() + " x " + resourceBundle2.getIdentifier() + ")"); break; } - ResourceBundle resourceBundle2 = bundles.get(j); - String id2 = ids.get(j); - outLog("Checking for same items between " + resourceBundle1.getLocale() + " x " + resourceBundle2.getLocale() + " (should be " + id1 + " x " + id2 + ")"); - errLog("Checking for same items between " + resourceBundle1.getLocale() + " x " + resourceBundle2.getLocale() + " (should be " + id1 + " x " + id2 + ")"); + allLog("Checking for same items between " + resourceBundle1.getLocale() + " x " + resourceBundle2.getLocale() + " (should be " + resourceBundle1.getIdentifier() + " x " + resourceBundle2.getIdentifier() + ")"); int localErrors=0; while (keys1.hasMoreElements()) { String key = (String) keys1.nextElement(); - String val1 = getMissingResourceAsEmpty(resourceBundle1, key); - String val2 = getMissingResourceAsEmpty(resourceBundle2, key); + String val1 = getMissingResourceAsEmpty(resourceBundle1.getBundle(), key); + String val2 = getMissingResourceAsEmpty(resourceBundle2.getBundle(), key); outLog("\""+val1+"\" x \""+val2); if (val1.trim().equalsIgnoreCase(val2.trim())) { - if (val1.trim().length() <= 5 /*"ok", "", ...*/ || val1.toLowerCase().contains("://") /*urls...*/) { + if (val1.trim().length() <= 5 /* short words like"ok", "", ...*/ + || val1.toLowerCase().contains("://") /*urls...*/ + || !val1.trim().contains(" ") /*one word*/ + || val1.replaceAll("\\{\\d\\}", "").trim().length()<5 /*only vars and short words*/) { errLog("Warning! Items equals for: " + key + " = " + val1 + " but are in allowed subset"); } else { errors++; @@ -112,11 +190,14 @@ public class MessagesPropertiesTest { } } } - errLog(localErrors+" errors allResourcesAreReallyDifferent fo "+id2+" x "+id1); + if (localErrors > 0){ + detailResults+=resourceBundle1.getIdentifier()+" x "+resourceBundle2.getIdentifier()+": "+localErrors+";"; + } + errLog(localErrors+" errors allResourcesAreReallyDifferent fo "+resourceBundle1.getIdentifier()+" x "+resourceBundle2.getIdentifier()); } } - assertTrue("Several - " + errors + " - items are same in bundles. See error logs for details", errors == 0); + assertTrue("Several - " + errors + " - items are same in bundles. See error logs for details: "+detailResults, errors == 0); } private String getMissingResourceAsEmpty(ResourceBundle res, String key) { @@ -129,20 +210,18 @@ public class MessagesPropertiesTest { @Test //it is not critical that some localisations are missing, however good to know + //and actually this test sis covered by allResourcesAreReallyDifferent, because fallback is geting default value for unknnow localisation public void warnForNotLocalisedStrings() { int errors = 0; - Enumeration<String> keys = main.getKeys(); + Enumeration<String> keys = main.getBundle().getKeys(); for (int i = 0; i < secondary.length; i++) { int localErrors = 0; - ResourceBundle sec = secondary[i]; - String country = secondaryCountries[i]; - String language = secondaryLanguages[i]; - String id = country + "_" + language; - outLog("Checking for missing strings in " + sec.getLocale() + " (should be " + id + ") compared with default"); - errLog("Checking for missing strings in " + sec.getLocale() + " (should be " + id + ") compared with default"); + ResourceBundle sec = secondary[i].getBundle(); + String id = secondary[i].getIdentifier(); + allLog("Checking for missing strings in " + sec.getLocale() + " (should be " + id + ") compared with default"); while (keys.hasMoreElements()) { String key = (String) keys.nextElement(); - String val1 = getMissingResourceAsEmpty(main, key); + String val1 = getMissingResourceAsEmpty(main.getBundle(), key); String val2 = getMissingResourceAsEmpty(sec, key); outLog("\""+val1+"\" x \""+val2); if (val1.trim().isEmpty()) { @@ -165,24 +244,15 @@ public class MessagesPropertiesTest { @Test public void noEmptyResources() { - List<String> ids = new ArrayList<String>(secondary.length + 1); - ids.add("default"); - List<ResourceBundle> bundles = new ArrayList<ResourceBundle>(secondary.length + 1); + List<LocalesIdentifier> bundles = new ArrayList<LocalesIdentifier>(secondary.length + 1); bundles.add(main); int errors = 0; - for (int i = 0; i < secondaryCountries.length; i++) { - String country = secondaryCountries[i]; - String language = secondaryLanguages[i]; - ids.add(country + "_" + language); - bundles.add(secondary[i]); - - } + bundles.addAll(Arrays.asList(secondary)); for (int i = 0; i < bundles.size(); i++) { - ResourceBundle resourceBundle = bundles.get(i); - String id = ids.get(i); + ResourceBundle resourceBundle = bundles.get(i).getBundle(); + String id = bundles.get(i).getIdentifier(); Enumeration<String> keys = resourceBundle.getKeys(); - outLog("Checking for empty items in " + resourceBundle.getLocale() + " (should be " + id + ")"); - errLog("Checking for empty items in " + resourceBundle.getLocale() + " (should be " + id + ")"); + allLog("Checking for empty items in " + resourceBundle.getLocale() + " (should be " + id + ")"); int localErrors=0; while (keys.hasMoreElements()) { String key = (String) keys.nextElement(); @@ -207,16 +277,14 @@ public class MessagesPropertiesTest { int errors = 0; for (int i = 0; i < secondary.length; i++) { int localErrors = 0; - ResourceBundle sec = secondary[i]; + ResourceBundle sec = secondary[i].getBundle(); Enumeration<String> keys = sec.getKeys(); - String country = secondaryCountries[i]; - String language = secondaryLanguages[i]; - String id = country + "_" + language; + String id = secondary[i].getId(); outLog("Checking for redundant keys in " + sec.getLocale() + " (should be " + id + ") compared with default"); errLog("Checking for redundant keys in " + sec.getLocale() + " (should be " + id + ") compared with default"); while (keys.hasMoreElements()) { String key = (String) keys.nextElement(); - String val2 = getMissingResourceAsEmpty(main, key); + String val2 = getMissingResourceAsEmpty(main.getBundle(), key); String val1 = getMissingResourceAsEmpty(sec, key); outLog("\""+val1+"\" x \""+val2); if (val2.trim().isEmpty() && !val1.trim().isEmpty()){ @@ -234,7 +302,13 @@ public class MessagesPropertiesTest { + private void allLog(String string) { + outLog(string); + errLog(string); + } private void errLog(String string) { + //used quite often :) + //System.out.println(string); ServerAccess.logErrorReprint(string); } diff --git a/tests/reproducers/simple/LocalesTest/testcases/LocalesTestTest.java b/tests/reproducers/simple/LocalesTest/testcases/LocalesTestTest.java index c5f7b54..b3c3499 100644 --- a/tests/reproducers/simple/LocalesTest/testcases/LocalesTestTest.java +++ b/tests/reproducers/simple/LocalesTest/testcases/LocalesTestTest.java @@ -110,6 +110,11 @@ public class LocalesTestTest { return l; } + private ResourceBundle getPropertiesDe() throws IOException { + return getProperties("_de"); + } + + public ResourceBundle getPropertiesCz() throws IOException { return getProperties("_cs_CZ"); @@ -132,7 +137,8 @@ public class LocalesTestTest { String[] l = getChangeLocalesForSubproces("en_US.UTF-8"); ProcessResult pr = ServerAccess.executeProcess(javaws, null, null, l); assertEnglish(pr.stdout); - assertNotCzh(pr.stdout); + assertNotCz(pr.stdout); + assertNotDe(pr.stdout); } @Test @@ -140,8 +146,9 @@ public class LocalesTestTest { String[] l = getChangeLocalesForSubproces("cs_CZ"); ProcessResult pr = ServerAccess.executeProcess(javaws, null, null, l); assertNotEnglish(pr.stdout); - assertNotCzh(pr.stdout); - iteratePropertiesForAproxCz(pr.stdout); + assertNotCz(pr.stdout); + assertNotDe(pr.stdout); + iteratePropertiesForAproxCzCs(pr.stdout); } @Test @@ -149,16 +156,71 @@ public class LocalesTestTest { String[] l = getChangeLocalesForSubproces("cs_CZ.UTF-8"); ProcessResult pr = ServerAccess.executeProcess(javaws, null, null, l); assertNotEnglish(pr.stdout); + assertNotDe(pr.stdout); assertCz(pr.stdout); - iteratePropertiesForAproxCz(pr.stdout); + iteratePropertiesForAproxCzCs(pr.stdout); + } + + @Test + public void testLocalesDeDe() throws Exception { + String[] l = getChangeLocalesForSubproces("de_DE"); + ProcessResult pr = ServerAccess.executeProcess(javaws, null, null, l); + assertNotEnglish(pr.stdout); + assertNotCz(pr.stdout); + iteratePropertiesForAproxDe(pr.stdout); + } + + @Test + public void testLocalesDeDeUtf() throws Exception { + String[] l = getChangeLocalesForSubproces("de_DE.UTF-8"); + ProcessResult pr = ServerAccess.executeProcess(javaws, null, null, l); + assertNotEnglish(pr.stdout); + assertNotCz(pr.stdout); + assertDe(pr.stdout); + iteratePropertiesForAproxDe(pr.stdout); + } + + + @Test + public void testLocalesDe_unknowButValidDeLocale() throws Exception { + String[] l = getChangeLocalesForSubproces("de_LU"); + ProcessResult pr = ServerAccess.executeProcess(javaws, null, null, l); + assertNotEnglish(pr.stdout); + assertNotCz(pr.stdout); + iteratePropertiesForAproxDe(pr.stdout); + } + + @Test + public void testLocalesDeUtf_unknowButValidDeLocale() throws Exception { + String[] l = getChangeLocalesForSubproces("de_LU.UTF-8"); + ProcessResult pr = ServerAccess.executeProcess(javaws, null, null, l); + assertNotEnglish(pr.stdout); + assertNotCz(pr.stdout); + assertDe(pr.stdout); + iteratePropertiesForAproxDe(pr.stdout); + } + + + + @Test + public void testLocalesDe_globalDe() throws Exception { + String[] l = getChangeLocalesForSubproces("deutsch"); + ProcessResult pr = ServerAccess.executeProcess(javaws, null, null, l); + assertNotEnglish(pr.stdout); + assertNotCz(pr.stdout); + iteratePropertiesForAproxDe(pr.stdout); } + + + @Test public void testLocalesInvalid() throws Exception { String[] l = getChangeLocalesForSubproces("ax_BU"); ProcessResult pr = ServerAccess.executeProcess(javaws, null, null, l); assertEnglish(pr.stdout); - assertNotCzh(pr.stdout); + assertNotCz(pr.stdout); + assertNotDe(pr.stdout); } private void assertEnglish(String s) throws IOException { @@ -175,11 +237,23 @@ public class LocalesTestTest { ResourceBundle props = getPropertiesCz(); iteratePropertiesFor(props, s, true, "czech"); } + + private void assertDe(String s) throws IOException { + ResourceBundle props = getPropertiesDe(); + iteratePropertiesFor(props, s, true, "de"); + } + - private void assertNotCzh(String s) throws IOException { + private void assertNotCz(String s) throws IOException { ResourceBundle props = getPropertiesCz(); iteratePropertiesFor(props, s, false, "czech"); } + + private void assertNotDe(String s) throws IOException { + ResourceBundle props = getPropertiesDe(); + iteratePropertiesFor(props, s, false, "de"); + } + /** * This method is iterating all keys defined in this class, geting their value in given @@ -216,10 +290,11 @@ public class LocalesTestTest { * given output match/matchnot (depends on value of assertTrue) this string, * * @param outputToBeChecked + * @param props bundle with strings + * @param reg regexter with rules how to handle national characters * @throws IOException */ - private void iteratePropertiesForAproxCz(String outputToBeChecked) throws IOException { - ResourceBundle props = getPropertiesCz(); + private void iteratePropertiesForAprox(String outputToBeChecked, ResourceBundle props, Regexer reg) throws IOException { int keysFound = 0; for (int i = 0; i < keys.length; i++) { String string = keys[i]; @@ -227,55 +302,91 @@ public class LocalesTestTest { if (value == null) { continue; } - value = regexIt(value); + value = reg.regexIt(value); keysFound++; { - Assert.assertTrue("Output must match cz text, failed on " + string, outputToBeChecked.matches(value)); + Assert.assertTrue("Output must match "+reg.getId() +" text, failed on " + string, outputToBeChecked.matches(value)); } } Assert.assertTrue("At least one key must be found, was not", keysFound > 0); } - String[] czEvil = { - "á", - "č", - "ď", - "ě", - "é", - "í", - "ň", - "ó", - "ř", - "š", - "ť", - "ú", - "ů", - "ý", - "ž", - "[", - "]", - "(", - ")"}; - /** - * This method transforms given string to asci-only regex, replacing groups of - * czech characters (defined by czEvil variable) by .+ - * - * @param value - * @return - */ - private String regexIt(String value) { - return regexIt(value, czEvil); + private void iteratePropertiesForAproxCzCs(String stdout) throws IOException { + iteratePropertiesForAprox(stdout, getPropertiesCz(), Regexer.cz); + } + private void iteratePropertiesForAproxDe(String stdout) throws IOException { + iteratePropertiesForAprox(stdout, getPropertiesDe(), Regexer.de); } + + + + private static final class Regexer { + + private static final String[] czEvil = { + "á", + "č", + "ď", + "ě", + "é", + "í", + "ň", + "ó", + "ř", + "š", + "ť", + "ú", + "ů", + "ý", + "ž", + "[", + "]", + "(", + ")"}; + private static final String[] deEvil = { + "ä", + "ö", + "ß", + "ü", + "[", + "]", + "(", + ")"}; + + private static final Regexer cz = new Regexer(czEvil,"cz"); + private static final Regexer de = new Regexer(deEvil,"de"); + + private final String[] map; + private final String id; + + public Regexer(String[] map, String id) { + this.map = map; + this.id = id; + } - private static String regexIt(String value, String[] map) { - for (int i = 0; i < map.length; i++) { - String string = map[i]; - value = value.replace(string, "."); - value = value.replace(string.toUpperCase(), "."); + public String getId() { + return id; } + + + + + /** + * This method transforms given string to asci-only regex, replacing + * groups of national characters (defined by array variable) by .+ + * + * @param value + * @return + */ + public String regexIt(String value) { + for (int i = 0; i < map.length; i++) { + String string = map[i]; + value = value.replace(string, "."); + value = value.replace(string.toUpperCase(), "."); + } - value = value.replaceAll("\\.+", ".+"); - value = "(?s).*" + value + ".*"; - return value; + value = value.replaceAll("\\.+", ".+"); + value = "(?s).*" + value + ".*"; + return value; + } } } |