This is an automated email from the ASF dual-hosted git repository.
gtchaboussie pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git
The following commit(s) were added to refs/heads/trunk by this push:
new 2aff62ff1f Improved : Rewrites FlexibleMapAccessorTest (OFBIZ-13345)
2aff62ff1f is described below
commit 2aff62ff1fbb26c1baacaea9e24fa0ad59c71aca
Author: Gaetan <[email protected]>
AuthorDate: Wed Feb 18 16:08:01 2026 +0100
Improved : Rewrites FlexibleMapAccessorTest (OFBIZ-13345)
- Rewrites the FlexibleMapAccessorTest in groovy
- Explodes the previous big test in several test cases, a cases for each
feature of the FMA.
- Removes some of the testing lines that were not relevant :
- Testing that a FMA in a set is correctly contained
- Testing a FSE instance in this class. FSE has its own test cases.
- Some of the lines were repeated, only with the two compared objects
inverted.
- Creates a dedicated class for the utility test map.
Thanks Jacques for the testing.
Co-authored-by: cgaetan <[email protected]>
---
.../util/collection/FlexibleMapAccessorTest.groovy | 164 ++++++++++++++++
.../ofbiz/base/util/string/tool/TestingMap.groovy | 39 ++++
.../util/collections/FlexibleMapAccessorTests.java | 206 ---------------------
3 files changed, 203 insertions(+), 206 deletions(-)
diff --git
a/framework/base/src/test/groovy/org/apache/ofbiz/base/util/collection/FlexibleMapAccessorTest.groovy
b/framework/base/src/test/groovy/org/apache/ofbiz/base/util/collection/FlexibleMapAccessorTest.groovy
new file mode 100644
index 0000000000..d60756c79a
--- /dev/null
+++
b/framework/base/src/test/groovy/org/apache/ofbiz/base/util/collection/FlexibleMapAccessorTest.groovy
@@ -0,0 +1,164 @@
+/*******************************************************************************
+ * 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.base.util.collection
+
+import junit.framework.TestCase
+import org.apache.ofbiz.base.util.collections.FlexibleMapAccessor
+import org.apache.ofbiz.base.util.string.tool.TestException
+import org.apache.ofbiz.base.util.string.tool.TestingMap
+import org.junit.Assert
+import org.junit.Test
+
+/* codenarc-disable GStringExpressionWithinString,
UnnecessaryBigDecimalInstantiation */
+
+class FlexibleMapAccessorTest extends TestCase {
+
+ private static final Locale LOCALE_TO_TEST = new Locale('en', 'US')
+ private static final FlexibleMapAccessor<?> EMPTY_FMA =
FlexibleMapAccessor.getInstance('')
+ private static final FlexibleMapAccessor<?> NULL_FMA =
FlexibleMapAccessor.getInstance(null)
+
+ @Test
+ void testNestedExpressionDetection() {
+ assert FlexibleMapAccessor.getInstance('Hello
${parameters.var}!').containsNestedExpression()
+ assert !FlexibleMapAccessor.getInstance('Hello
World!').containsNestedExpression()
+ }
+
+ @Test
+ void testBasis() {
+ List stringsToTest = ['parameters.var',
+ 'parameters.someList[0]',
+ 'para${\'meter\'}s.var',
+ 'The total is ${total?currency(USD)}.']
+ for (stringToTest in stringsToTest) {
+ FlexibleMapAccessor fmaInstance =
FlexibleMapAccessor.getInstance(stringToTest)
+ assert stringToTest == fmaInstance.getOriginalName()
+ assert stringToTest == fmaInstance.toString()
+ assert !fmaInstance.isEmpty()
+ assert fmaInstance ===
FlexibleMapAccessor.getInstance(stringToTest)
+ assert EMPTY_FMA != fmaInstance
+ assert NULL_FMA != fmaInstance
+ }
+ }
+
+ @Test
+ void testAscendingTest() {
+ String fmaInput = 'parameters.var'
+ FlexibleMapAccessor ascendingFma = FlexibleMapAccessor.getInstance('+'
+ fmaInput)
+ assert '+' + fmaInput == ascendingFma.toString()
+ assert ascendingFma.getIsAscending()
+ FlexibleMapAccessor descendingFma =
FlexibleMapAccessor.getInstance('-' + fmaInput)
+ assert '-' + fmaInput == descendingFma.toString()
+ assert !descendingFma.getIsAscending()
+ }
+
+ @Test
+ void testFmaPutCRUD() {
+ String fmaInput = 'parameters.var'
+ Object var = 'Foo'
+ Map map = [:]
+ FlexibleMapAccessor fmaInstance =
FlexibleMapAccessor.getInstance(fmaInput)
+ assert fmaInstance.get(map) == null
+ fmaInstance.put(map, var)
+ assert !map.isEmpty()
+ assert var == fmaInstance.get(map)
+ assert var == fmaInstance.remove(map)
+ assert fmaInstance.remove(map) == null
+ }
+
+ @Test
+ void testFmaWithLocale() {
+ String fmaInput = '\'The total is ${total?currency(USD)}.\''
+ String output = 'The total is $12,345,678.90.'
+ Object variable = new BigDecimal('12345678.90')
+ Map map = [:]
+ FlexibleMapAccessor fmaInstance =
FlexibleMapAccessor.getInstance(fmaInput)
+ FlexibleMapAccessor fmaVarInstance =
FlexibleMapAccessor.getInstance('total')
+ fmaVarInstance.put(map, variable)
+ assert !map.isEmpty()
+ assert output == fmaInstance.get(map, LOCALE_TO_TEST)
+ assert output == fmaInstance.get(map, null)
+ // TODO BUG: fmaGet modifies testMap, even tho it shouldn't ?
+ // Is this bug still relevant ?
+ }
+
+ @Test
+ void testAutoVivify() {
+ String fmaInput = 'parameters.var'
+ Map map = [:]
+ FlexibleMapAccessor fmaVarInstance =
FlexibleMapAccessor.getInstance(fmaInput)
+ fmaVarInstance.put(map, null)
+ Map outParameters = map.parameters
+ assert !outParameters.isEmpty()
+ assert outParameters.keySet().contains('var')
+ assert outParameters.('var') === null
+ Assert.assertThrows(IllegalArgumentException, () ->
fmaVarInstance.put(null, 'Foo'))
+ }
+
+ @Test
+ void testAutoVivifyList() {
+ String fmaInput = 'parameters.someList[0]'
+ Map map = [:]
+ FlexibleMapAccessor fmaVarInstance =
FlexibleMapAccessor.getInstance(fmaInput)
+ fmaVarInstance.put(map, null)
+ Map parameters = map.parameters
+ assert !parameters.isEmpty()
+ assert parameters.keySet().contains('someList')
+ assert parameters.('someList') == []
+ Assert.assertThrows(IllegalArgumentException, () ->
fmaVarInstance.put(null, 'Foo'))
+ }
+
+ @Test
+ void testFmaAndExceptions() {
+ Map map = new TestingMap()
+ map.put('exception', new TestException())
+ Object result =
FlexibleMapAccessor.getInstance('exception.value').get(map)
+ assert result == null
+ FlexibleMapAccessor.getInstance('exception.value').put(map, 'Foo')
+ FlexibleMapAccessor.getInstance('exception').remove(map)
+ assert null != map.get('exception')
+ }
+
+// codenarc-disable JUnitTestMethodWithoutAssert
+ @Test
+ void testEmptyOrNullFma() {
+ doEmptyFmaTest('')
+ doEmptyFmaTest(null)
+ doEmptyFmaTest('null')
+ }
+// codenarc-enable JUnitTestMethodWithoutAssert
+
+ private static void doEmptyFmaTest(String text) {
+ FlexibleMapAccessor fma = FlexibleMapAccessor.getInstance(text)
+ assert fma.isEmpty()
+ Map testMap = [:]
+ assert fma.get(null) == null
+ assert fma.get(testMap) == null
+ assert testMap.isEmpty()
+ fma.put(testMap, FlexibleMapAccessorTest)
+ assert testMap.isEmpty()
+ fma.put(null, FlexibleMapAccessorTest)
+ assert testMap.isEmpty()
+ assert NULL_FMA === fma
+ assert EMPTY_FMA === fma
+ assert fma.getOriginalName() == ''
+ assert fma.remove(testMap) == null
+ assert fma.toString() != null
+ }
+
+}
diff --git
a/framework/base/src/test/groovy/org/apache/ofbiz/base/util/string/tool/TestingMap.groovy
b/framework/base/src/test/groovy/org/apache/ofbiz/base/util/string/tool/TestingMap.groovy
new file mode 100644
index 0000000000..821c43f6ba
--- /dev/null
+++
b/framework/base/src/test/groovy/org/apache/ofbiz/base/util/string/tool/TestingMap.groovy
@@ -0,0 +1,39 @@
+/*******************************************************************************
+ * 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.base.util.string.tool
+
+/**
+ * Small custom map implementation used for testing
+ */
+class TestingMap<K, V> extends HashMap<K, V> {
+
+ @Override
+ V get(Object key) {
+ return super.get(key)
+ }
+
+ @Override
+ V put(K key, V value) {
+ if (value == null) {
+ throw new IllegalArgumentException()
+ }
+ return super.put(key, value)
+ }
+
+}
diff --git
a/framework/base/src/test/java/org/apache/ofbiz/base/util/collections/FlexibleMapAccessorTests.java
b/framework/base/src/test/java/org/apache/ofbiz/base/util/collections/FlexibleMapAccessorTests.java
deleted file mode 100644
index 0a0527df32..0000000000
---
a/framework/base/src/test/java/org/apache/ofbiz/base/util/collections/FlexibleMapAccessorTests.java
+++ /dev/null
@@ -1,206 +0,0 @@
-/*******************************************************************************
- * 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.base.util.collections;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertSame;
-import static org.junit.Assert.assertTrue;
-
-import java.math.BigDecimal;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Locale;
-import java.util.Map;
-import java.util.Set;
-
-import org.apache.ofbiz.base.util.Debug;
-import org.apache.ofbiz.base.util.string.FlexibleStringExpander;
-import org.junit.Test;
-
-public class FlexibleMapAccessorTests {
- private static final Locale LOCALE_TO_TEST = new Locale("en", "US");
- private static FlexibleMapAccessor<?> fmaEmpty =
FlexibleMapAccessor.getInstance("");
- private static FlexibleMapAccessor<?> fmaNull =
FlexibleMapAccessor.getInstance(null);
-
- private static <T> void fmaTest(String label, String getText, String
fseText, T var, String value) {
- fmaTest(label, getText, getText, fseText, null, var, value);
- }
-
- private static <T> void fmaTest(String label, String getText, String
putText, String fseText, Locale locale, T var, String value) {
- Map<String, Object> testMap = new HashMap<>();
- FlexibleMapAccessor<T> fmaGet =
FlexibleMapAccessor.getInstance(getText);
- assertEquals(label + ":get-original-name", getText,
fmaGet.getOriginalName());
- assertEquals(label + ":get-isEmpty", false, fmaGet.isEmpty());
- assertEquals(label + ":get-instance-equals", fmaGet,
FlexibleMapAccessor.getInstance(getText));
- assertEquals(label + ":toString", getText, fmaGet.toString());
- assertNotEquals(label + ":get-not-equals-empty", fmaEmpty, fmaGet);
- assertNotEquals(label + ":get-not-equals-null", fmaNull, fmaGet);
- assertNotEquals(label + ":empty-not-equals-get", fmaGet, fmaEmpty);
- assertNotEquals(label + ":null-not-equals-get", fmaGet, fmaNull);
- assertNotEquals(label + ":get-not-equals-other", fmaGet,
FlexibleMapAccessorTests.class);
- assertEquals(label + ":get-toString", getText, fmaGet.toString());
- FlexibleMapAccessor<T> fmaGetAscending =
FlexibleMapAccessor.getInstance("+" + getText);
- assertEquals(label + ":get-ascending-toString", "+" + getText,
fmaGetAscending.toString());
- assertTrue(label + ":get-ascending-isAscending",
fmaGetAscending.getIsAscending());
- FlexibleMapAccessor<T> fmaGetDescending =
FlexibleMapAccessor.getInstance("-" + getText);
- assertEquals(label + ":get-descending-toString", "-" + getText,
fmaGetDescending.toString());
- assertFalse(label + ":get-decending-isAscending",
fmaGetDescending.getIsAscending());
- FlexibleMapAccessor<T> fmaPut =
FlexibleMapAccessor.getInstance(putText);
- assertEquals(label + ":put-toString", putText, fmaPut.toString());
- assertEquals(label + ":put-original-name", putText,
fmaPut.getOriginalName());
- assertEquals(label + ":put-isEmpty", false, fmaPut.isEmpty());
- assertEquals(label + ":put-instance-equals", fmaPut,
FlexibleMapAccessor.getInstance(putText));
- assertNotEquals(label + ":put-not-equals-other", fmaPut,
FlexibleMapAccessorTests.class);
-
- FlexibleStringExpander fse =
FlexibleStringExpander.getInstance(fseText);
- if (locale == null) {
- assertNull(label + ":get-initial", fmaGet.get(testMap));
- fmaPut.put(testMap, var);
- assertFalse(label + ":testMap-not-empty", testMap.isEmpty());
- assertEquals(label + ":get", var, fmaGet.get(testMap));
- assertEquals(label, value, fse.expandString(testMap));
- assertEquals(label + ":remove", var, fmaGet.remove(testMap));
- assertNull(label + ":remove-not-exist", fmaGet.remove(testMap));
- } else {
- fmaPut.put(testMap, var);
- assertFalse(label + ":testMap-not-empty", testMap.isEmpty());
- assertEquals(label + ":get", value, fmaGet.get(testMap, locale));
- // BUG: fmaGet modifies testMap, even tho it shouldn't
- assertEquals(label + ":get", value, fmaGet.get(testMap, null));
- assertEquals(label, value, fse.expandString(testMap, locale));
- }
-
- testMap.clear();
- fmaPut.put(testMap, null);
- assertFalse(label + ":testMap-not-empty-put-null", testMap.isEmpty());
- if (locale == null) {
- assertNull(label + ":get-put-null", fmaGet.get(testMap));
- }
- testMap.clear();
- Exception caught = null;
- try {
- fmaPut.put(null, var);
- } catch (Exception e) {
- caught = e;
- } finally {
- assertNotNull(label + ":put-null-map", caught);
- assertTrue(label + ":put-null-map-isEmpty", testMap.isEmpty());
- }
- Set<FlexibleMapAccessor<?>> set = new HashSet<>();
- assertFalse(label + ":not-in-set", set.contains(fmaGet));
- set.add(fmaGet);
- assertTrue(label + ":in-set", set.contains(fmaGet));
- }
-
- private static void fmaEmptyTest(String label, String text) {
- FlexibleMapAccessor<Class<?>> fma =
FlexibleMapAccessor.getInstance(text);
- assertTrue(label + ":isEmpty", fma.isEmpty());
- Map<String, Object> testMap = new HashMap<>();
- assertNull(label + ":get", fma.get(null));
- assertNull(label + ":get", fma.get(testMap));
- assertTrue(label + ":map-isEmpty-initial", testMap.isEmpty());
- fma.put(testMap, FlexibleMapAccessorTests.class);
- assertTrue(label + ":map-isEmpty-map", testMap.isEmpty());
- fma.put(null, FlexibleMapAccessorTests.class);
- assertTrue(label + ":map-isEmpty-null", testMap.isEmpty());
- assertSame(label + ":same-null", fmaNull, fma);
- assertSame(label + ":same-empty", fmaEmpty, fma);
- assertEquals(label + ":original-name", "", fma.getOriginalName());
- assertNull(label + ":remove", fma.remove(testMap));
- assertNotNull(label + ":toString", fma.toString());
- }
-
- @Test
- /** These tests rely upon FlexibleStringExpander, so they should follow
the FlexibleStringExpander tests. */
- public void testFlexibleMapAccessor() {
- fmaEmptyTest("fmaEmpty", "");
- fmaEmptyTest("fmaNull", null);
- fmaEmptyTest("fma\"null\"", "null");
- fmaTest("UEL auto-vivify Map", "parameters.var", "Hello
${parameters.var}!", "World", "Hello World!");
- fmaTest("UEL auto-vivify List", "parameters.someList[0]",
"parameters.someList[+0]", "Hello ${parameters.someList[0]}!",
- null, "World", "Hello World!");
- fmaTest("fse", "para${'meter'}s.var", "Hello ${parameters.var}!",
"World", "Hello World!");
- fmaTest("foo", "'The total is ${total?currency(USD)}.'", "total", "The
total is ${total?currency(USD)}.",
- LOCALE_TO_TEST, new BigDecimal("12345678.90"), "The total is
$12,345,678.90.");
- assertTrue("containsNestedExpression method returns true",
FlexibleMapAccessor.getInstance("Hello ${parameters.var}!")
- .containsNestedExpression());
- assertFalse("containsNestedExpression method returns false",
FlexibleMapAccessor.getInstance("Hello World!").containsNestedExpression());
- }
-
- public static class ThrowException {
- /**
- * Gets value.
- * @return the value
- * @throws Exception the exception
- */
- public Object getValue() throws Exception {
- throw new Exception();
- }
-
- /**
- * Sets value.
- * @param value the value
- * @throws Exception the exception
- */
- public void setValue(Object value) throws Exception {
- throw new Exception();
- }
- }
-
- @SuppressWarnings("serial")
- public static class CantRemoveMap<K, V> extends HashMap<K, V> {
- @Override
- public V get(Object key) {
- return super.get(key);
- }
-
- @Override
- public V put(K key, V value) {
- if (value == null) {
- throw new IllegalArgumentException();
- }
- return super.put(key, value);
- }
- }
-
- @Test
- /**
- * Test verbosity and errors.
- */
- public void testVerbosityAndErrors() {
- boolean isVerbose = Debug.isOn(Debug.VERBOSE);
- try {
- Debug.set(Debug.VERBOSE, true);
- Map<String, Object> testMap = new CantRemoveMap<>();
- testMap.put("throwException", new ThrowException());
- assertNull("no var",
FlexibleMapAccessor.getInstance("var").get(testMap));
- Object result =
FlexibleMapAccessor.getInstance("throwException.value").get(testMap);
- assertNull("get null var", result);
-
FlexibleMapAccessor.getInstance("throwException.value").put(testMap, this);
- FlexibleMapAccessor.getInstance("throwException").remove(testMap);
- assertNotNull("not removed", testMap.get("throwException"));
- } finally {
- Debug.set(Debug.VERBOSE, isVerbose);
- }
- }
-}