You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by pg...@apache.org on 2018/07/06 21:10:08 UTC

svn commit: r1835282 - in /ofbiz/ofbiz-framework/trunk/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/

Author: pgil
Date: Fri Jul  6 21:10:07 2018
New Revision: 1835282

URL: http://svn.apache.org/viewvc?rev=1835282&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/trunk/framework/common/src/test/
    ofbiz/ofbiz-framework/trunk/framework/common/src/test/java/
    ofbiz/ofbiz-framework/trunk/framework/common/src/test/java/org/
    ofbiz/ofbiz-framework/trunk/framework/common/src/test/java/org/apache/
    ofbiz/ofbiz-framework/trunk/framework/common/src/test/java/org/apache/ofbiz/
    ofbiz/ofbiz-framework/trunk/framework/common/src/test/java/org/apache/ofbiz/common/
    ofbiz/ofbiz-framework/trunk/framework/common/src/test/java/org/apache/ofbiz/common/GetLocaleListTests.java   (with props)
Modified:
    ofbiz/ofbiz-framework/trunk/framework/common/groovyScripts/GetLocaleList.groovy

Modified: ofbiz/ofbiz-framework/trunk/framework/common/groovyScripts/GetLocaleList.groovy
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/common/groovyScripts/GetLocaleList.groovy?rev=1835282&r1=1835281&r2=1835282&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/common/groovyScripts/GetLocaleList.groovy (original)
+++ ofbiz/ofbiz-framework/trunk/framework/common/groovyScripts/GetLocaleList.groovy Fri Jul  6 21:10:07 2018
@@ -17,35 +17,22 @@
  * 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()
+// Check that `a` contains `b` when ignoring case.
+boolean contains(String a, String b) {
+    b && a.toUpperCase().contains(b.toUpperCase())
+}
 
-// Debug.logInfo(parameters.localeString + "==" +  parameters.localeName)
+hasNoFilters = !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)
-        }
+context.locales = availableLocales()
+    .stream()
+    .map { [localeName: it.getDisplayName(it), localeString: it.toString()] }
+    .filter {
+        hasNoFilters ||
+        contains(it.localeString, parameters.localeString) ||
+        contains(it.localeName, parameters.localeName)
     }
-}
-
-context.locales = locales
+    .collect toList()

Added: ofbiz/ofbiz-framework/trunk/framework/common/src/test/java/org/apache/ofbiz/common/GetLocaleListTests.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/common/src/test/java/org/apache/ofbiz/common/GetLocaleListTests.java?rev=1835282&view=auto
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/common/src/test/java/org/apache/ofbiz/common/GetLocaleListTests.java (added)
+++ ofbiz/ofbiz-framework/trunk/framework/common/src/test/java/org/apache/ofbiz/common/GetLocaleListTests.java Fri Jul  6 21:10:07 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/trunk/framework/common/src/test/java/org/apache/ofbiz/common/GetLocaleListTests.java
------------------------------------------------------------------------------
    svn:eol-style = native

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

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