[fyi] [icedtea-web] enhanced locales tests for upcoming DE translation
Adam Domurad
adomurad at redhat.com
Tue Feb 5 07:43:56 PST 2013
On 02/05/2013 07:16 AM, Jiri Vanek wrote:
> Hi!
>
> I have enhanced our testsuite for localisation. The changes are little
> bit greater because I have to handle tests for "any German" and for
> "specific German"
>
>
> This should be pushed together with locales from Jacob.
>
> J.
> diff -r fd01cd1c2bbc
> tests/netx/unit/net/sourceforge/jnlp/resources/MessagesPropertiesTest.java
> ---
> a/tests/netx/unit/net/sourceforge/jnlp/resources/MessagesPropertiesTest.java
> Tue Feb 05 09:51:44 2013 +0100
> +++
> b/tests/netx/unit/net/sourceforge/jnlp/resources/MessagesPropertiesTest.java
> Tue Feb 05 12:49:32 2013 +0100
> @@ -36,6 +36,7 @@
> package net.sourceforge.jnlp.resources;
>
> import java.util.ArrayList;
> +import java.util.Arrays;
> import java.util.Enumeration;
> import java.util.List;
> import java.util.Locale;
> @@ -49,61 +50,128 @@
>
> 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("fdbgvfdbg", "ftgjhuj");
> + } 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();
> + }
> +
> +
> +
> +
> + }
> +
> + 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,LocalesIdentifier.DE_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);
> 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*/) {
> errLog("Warning! Items equals for: " +
> key + " = " + val1 + " but are in allowed subset");
> } else {
> errors++;
> @@ -112,7 +180,7 @@
> }
> }
> }
> - errLog(localErrors+" errors
> allResourcesAreReallyDifferent fo "+id2+" x "+id1);
> + errLog(localErrors+" errors
> allResourcesAreReallyDifferent fo "+resourceBundle1.getIdentifier()+"
> x "+resourceBundle2.getIdentifier());
>
> }
> }
> @@ -129,20 +197,18 @@
>
> @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 +231,15 @@
>
> @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 +264,14 @@
> 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 +289,13 @@
>
>
>
> + 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 -r fd01cd1c2bbc
> tests/reproducers/simple/LocalesTest/testcases/LocalesTestTest.java
> ---
> a/tests/reproducers/simple/LocalesTest/testcases/LocalesTestTest.java
> Tue Feb 05 09:51:44 2013 +0100
> +++
> b/tests/reproducers/simple/LocalesTest/testcases/LocalesTestTest.java
> Tue Feb 05 12:49:32 2013 +0100
> @@ -110,6 +110,13 @@
> return l;
> }
>
> + private ResourceBundle getPropertiesDe() throws IOException {
> + return getProperties("_de");
> + }
> +
> + private ResourceBundle getPropertiesDeDe() throws IOException {
> + return getProperties("_de_DE");
> + }
> public ResourceBundle getPropertiesCz() throws IOException {
> return getProperties("_cs_CZ");
>
> @@ -132,7 +139,9 @@
> String[] l = getChangeLocalesForSubproces("en_US.UTF-8");
> ProcessResult pr = ServerAccess.executeProcess(javaws, null,
> null, l);
> assertEnglish(pr.stdout);
> - assertNotCzh(pr.stdout);
> + assertNotCz(pr.stdout);
> + assertNotDeDe(pr.stdout);
> + assertNotDe(pr.stdout);
> }
>
> @Test
> @@ -140,8 +149,10 @@
> 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);
> + assertNotDeDe(pr.stdout);
> + assertNotDe(pr.stdout);
> + iteratePropertiesForAproxCzCs(pr.stdout);
> }
>
> @Test
> @@ -149,16 +160,73 @@
> String[] l = getChangeLocalesForSubproces("cs_CZ.UTF-8");
> ProcessResult pr = ServerAccess.executeProcess(javaws, null,
> null, l);
> assertNotEnglish(pr.stdout);
> + assertNotDeDe(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);
> + iteratePropertiesForAproxDeDe(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);
> + assertDeDe(pr.stdout);
> + iteratePropertiesForAproxDeDe(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);
> + assertNotDeDe(pr.stdout);
> + assertNotDe(pr.stdout);
> }
>
> private void assertEnglish(String s) throws IOException {
> @@ -175,11 +243,29 @@
> 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 assertDeDe(String s) throws IOException {
> + ResourceBundle props = getPropertiesDeDe();
> + iteratePropertiesFor(props, s, true, "de_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");
> + }
> + private void assertNotDeDe(String s) throws IOException {
> + ResourceBundle props = getPropertiesDeDe();
> + iteratePropertiesFor(props, s, false, "de_de");
> + }
>
> /**
> * This method is iterating all keys defined in this class,
> geting their value in given
> @@ -216,10 +302,11 @@
> * 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 +314,95 @@
> 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 void iteratePropertiesForAproxDeDe(String stdout)
> throws IOException {
> + iteratePropertiesForAprox(stdout, getPropertiesDeDe(),
> Regexer.de);
> }
>
> - 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(), ".");
> +
> +
> + private static final class Regexer {
> +
> + private static final String[] czEvil = {
> + "á",
> + "č",
> + "ď",
> + "ě",
> + "é",
> + "í",
> + "ň",
> + "ó",
> + "ř",
> + "š",
> + "ť",
> + "ú",
> + "ů",
> + "ý",
> + "ž",
> + "[",
> + "]",
> + "(",
> + ")"};
> + private static final String[] deEvil = {
> + "ä",
> + "ö",
> + "ß",
> + "ü",
> + "[",
> + "]",
> + "(",
> + ")"};
I notice a pattern in your naming :-)
> +
> + 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;
> }
>
> - value = value.replaceAll("\\.+", ".+");
> - value = "(?s).*" + value + ".*";
> - return value;
> + 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;
> + }
> }
> }
Looks fine overall.
-Adam
More information about the distro-pkg-dev
mailing list