svn commit: r1835283 - in /ofbiz/ofbiz-framework/branches/release17.12/framework/common: groovyScripts/ src/test/ src/test/java/ src/test/java/org/ src/test/java/org/apache/ src/test/java/org/apache/ofbiz/ src/test/java/org/apache/ofbiz/common/

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1835283 - in /ofbiz/ofbiz-framework/branches/release17.12/framework/common: groovyScripts/ src/test/ src/test/java/ src/test/java/org/ src/test/java/org/apache/ src/test/java/org/apache/ofbiz/ src/test/java/org/apache/ofbiz/common/

pgil
Author: pgil
Date: Fri Jul  6 21:10:50 2018
New Revision: 1835283

URL: http://svn.apache.org/viewvc?rev=1835283&view=rev
Log:
Fixed: GetLocaleList call can provide duplicate results
(OFBIZ-10458)

A new Junit test is implemented to validate that no duplicates are present.
Groovy service has been refactored to use a more functional style

Thanks Mathieu Lirzin for reporting and providing the patch

Added:
    ofbiz/ofbiz-framework/branches/release17.12/framework/common/src/test/
    ofbiz/ofbiz-framework/branches/release17.12/framework/common/src/test/java/
    ofbiz/ofbiz-framework/branches/release17.12/framework/common/src/test/java/org/
    ofbiz/ofbiz-framework/branches/release17.12/framework/common/src/test/java/org/apache/
    ofbiz/ofbiz-framework/branches/release17.12/framework/common/src/test/java/org/apache/ofbiz/
    ofbiz/ofbiz-framework/branches/release17.12/framework/common/src/test/java/org/apache/ofbiz/common/
    ofbiz/ofbiz-framework/branches/release17.12/framework/common/src/test/java/org/apache/ofbiz/common/GetLocaleListTests.java   (with props)
Modified:
    ofbiz/ofbiz-framework/branches/release17.12/framework/common/groovyScripts/GetLocaleList.groovy

Modified: ofbiz/ofbiz-framework/branches/release17.12/framework/common/groovyScripts/GetLocaleList.groovy
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/branches/release17.12/framework/common/groovyScripts/GetLocaleList.groovy?rev=1835283&r1=1835282&r2=1835283&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/branches/release17.12/framework/common/groovyScripts/GetLocaleList.groovy (original)
+++ ofbiz/ofbiz-framework/branches/release17.12/framework/common/groovyScripts/GetLocaleList.groovy Fri Jul  6 21:10:50 2018
@@ -17,35 +17,25 @@
  * under the License.
  */
 
-import java.util.List
-import org.apache.ofbiz.base.util.*
-import org.apache.ofbiz.base.util.string.*
-import org.apache.ofbiz.base.util.UtilMisc
+import static java.util.stream.Collectors.toList
+import static org.apache.ofbiz.base.util.UtilMisc.availableLocales
 
-locales = [] as LinkedList
-availableLocales = UtilMisc.availableLocales()
+import org.apache.ofbiz.base.util.UtilValidate as Uv
 
-// Debug.logInfo(parameters.localeString + "==" +  parameters.localeName)
-
-if (availableLocales) {
-    availableLocales.each { availableLocale ->
-        locale = [:]
-        locale.localeName = availableLocale.getDisplayName(availableLocale)
-        locale.localeString = availableLocale.toString()
-        if (UtilValidate.isNotEmpty(parameters.localeString)) {
-            if (locale.localeString.toUpperCase().contains(parameters.localeString.toUpperCase())) {
-                locales.add(locale)
-            }
-        }
-        if (UtilValidate.isNotEmpty(parameters.localeName)) {
-            if (locale.localeName.toUpperCase().contains(parameters.localeName.toUpperCase())) {
-                locales.add(locale)
-            }
-        }
-        if (UtilValidate.isEmpty(parameters.localeString) && UtilValidate.isEmpty(parameters.localeName)) {
-            locales.add(locale)
-        }
-    }
+// Check that `a` contains `b` when ignoring case.
+boolean contains(String a, String b) {
+    Uv.isNotEmpty(b) && a.toUpperCase().contains(b.toUpperCase())
 }
 
-context.locales = locales
+hasNoFilters = Uv.isEmpty(parameters.localeString) &&
+    Uv.isEmpty(parameters.localeName)
+
+context.locales = availableLocales()
+    .stream()
+    .map { [localeName: it.getDisplayName(it), localeString: it.toString()] }
+    .filter {
+        hasNoFilters ||
+        contains(it.localeString, parameters.localeString) ||
+        contains(it.localeName, parameters.localeName)
+    }
+    .collect toList()

Added: ofbiz/ofbiz-framework/branches/release17.12/framework/common/src/test/java/org/apache/ofbiz/common/GetLocaleListTests.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/branches/release17.12/framework/common/src/test/java/org/apache/ofbiz/common/GetLocaleListTests.java?rev=1835283&view=auto
==============================================================================
--- ofbiz/ofbiz-framework/branches/release17.12/framework/common/src/test/java/org/apache/ofbiz/common/GetLocaleListTests.java (added)
+++ ofbiz/ofbiz-framework/branches/release17.12/framework/common/src/test/java/org/apache/ofbiz/common/GetLocaleListTests.java Fri Jul  6 21:10:50 2018
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ofbiz.common;
+
+import static org.junit.Assert.*;
+import static org.hamcrest.CoreMatchers.*;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+import org.apache.ofbiz.base.util.GroovyUtil;
+import org.apache.ofbiz.base.util.ScriptUtil;
+import org.junit.Before;
+import org.junit.Test;
+
+import groovy.util.GroovyScriptEngine;
+
+public class GetLocaleListTests {
+    private Map<String, Object> params;
+    private GroovyScriptEngine engine;
+
+    @Before
+    public void setUp() throws Exception {
+        params = new HashMap<>();
+        engine = new GroovyScriptEngine(System.getProperty("user.dir"));
+    }
+
+    @SuppressWarnings("unchecked")
+    private List<Map<String, String>> runScript() throws Exception {
+        Map<String, Object> gContext = new HashMap<>();
+        gContext.put(ScriptUtil.PARAMETERS_KEY, params);
+        engine.run("framework/common/groovyScripts/GetLocaleList.groovy", GroovyUtil.getBinding(gContext));
+        return (List<Map<String, String>>) gContext.get("locales");
+    }
+
+    static private List<String> localeStrings(List<Map<String, String>> locales) {
+        return locales.stream()
+            .map(m -> m.get("localeString"))
+            .collect(Collectors.toList());
+    }
+
+    @Test
+    public void frenchLocaleName() throws Exception {
+        params.put("localeName", "fr");
+        List<Map<String, String>> res = runScript();
+        assertThat(localeStrings(res), hasItems("en_ZA", "fr", "fr_BE", "fr_CA", "fr_FR", "fr_LU", "fr_CH"));
+    }
+
+    @Test
+    public void frenchLocaleString() throws Exception {
+        params.put("localeString", "fr");
+        List<Map<String, String>> res = runScript();
+        assertThat(localeStrings(res),
+                both(hasItems("fr", "fr_BE", "fr_CA", "fr_FR", "fr_LU", "fr_CH")).and(not(hasItem("en_ZA"))));
+    }
+
+    @Test
+    public void frenchNoDuplicates() throws Exception {
+        params.put("localeName", "fr");
+        params.put("localeString", "fr");
+        List<Map<String, String>> res = runScript();
+        assertThat(localeStrings(res), hasItems("en_ZA", "fr", "fr_BE", "fr_CA", "fr_FR", "fr_LU", "fr_CH"));
+        assertEquals(new HashSet<String>(localeStrings(res)).size(), localeStrings(res).size());
+    }
+}

Propchange: ofbiz/ofbiz-framework/branches/release17.12/framework/common/src/test/java/org/apache/ofbiz/common/GetLocaleListTests.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: ofbiz/ofbiz-framework/branches/release17.12/framework/common/src/test/java/org/apache/ofbiz/common/GetLocaleListTests.java
------------------------------------------------------------------------------
    svn:keywords = Date Rev Author URL Id

Propchange: ofbiz/ofbiz-framework/branches/release17.12/framework/common/src/test/java/org/apache/ofbiz/common/GetLocaleListTests.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain