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 git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/branches/release17.12@1835283 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user