use of org.apache.ofbiz.base.util.string.FlexibleStringExpander in project ofbiz-framework by apache.
the class FlexibleMapAccessorTests method fmaTest.
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<String, Object>();
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<FlexibleMapAccessor<?>>();
assertFalse(label + ":not-in-set", set.contains(fmaGet));
set.add(fmaGet);
assertTrue(label + ":in-set", set.contains(fmaGet));
}
use of org.apache.ofbiz.base.util.string.FlexibleStringExpander in project ofbiz-framework by apache.
the class FlexibleStringExpanderTests method fseTest.
private static void fseTest(String label, String input, Map<String, Object> context, TimeZone timeZone, Locale locale, String compare, Object expand, boolean isEmpty) {
FlexibleStringExpander fse = FlexibleStringExpander.getInstance(input);
doFseTest(label, input, fse, context, timeZone, locale, compare, expand, isEmpty);
assertEquals("static expandString:" + label, compare, FlexibleStringExpander.expandString(input, context, timeZone, locale));
if (input == null) {
assertEquals("static expandString(null, null):" + label, "", FlexibleStringExpander.expandString(input, null));
assertEquals("static expandString(null, null):" + label, "", FlexibleStringExpander.expandString(input, null, locale));
} else {
assertEquals("static expandString(input, null):" + label, input, FlexibleStringExpander.expandString(input, null));
assertEquals("static expandString(input, null):" + label, input, FlexibleStringExpander.expandString(input, null, locale));
}
if (!fse.isEmpty()) {
fse = FlexibleStringExpander.getInstance(input, false);
doFseTest(label, input, fse, context, timeZone, locale, compare, expand, isEmpty);
}
}
use of org.apache.ofbiz.base.util.string.FlexibleStringExpander in project ofbiz-framework by apache.
the class EntityFinderUtil method makeSelectFieldExpanderList.
public static List<FlexibleStringExpander> makeSelectFieldExpanderList(Element element) {
List<FlexibleStringExpander> selectFieldExpanderList = null;
List<? extends Element> selectFieldElementList = UtilXml.childElementList(element, "select-field");
if (selectFieldElementList.size() > 0) {
selectFieldExpanderList = new ArrayList<>(selectFieldElementList.size());
for (Element selectFieldElement : selectFieldElementList) {
selectFieldExpanderList.add(FlexibleStringExpander.getInstance(selectFieldElement.getAttribute("field-name")));
}
}
return selectFieldExpanderList;
}
use of org.apache.ofbiz.base.util.string.FlexibleStringExpander in project ofbiz-framework by apache.
the class ContentWorker method checkWhen.
/**
* Returns a boolean, result of whenStr evaluation with context.
* If whenStr is empty return defaultReturn.
* @param context A <code>Map</code> containing initial variables
* @param whenStr A <code>String</code> condition expression
* @param defaultReturn A <code>boolean</code> default return value
* @return A <code>boolan</code> result of evaluation
*/
public static boolean checkWhen(Map<String, Object> context, String whenStr, boolean defaultReturn) {
boolean isWhen = defaultReturn;
if (UtilValidate.isNotEmpty(whenStr)) {
FlexibleStringExpander fse = FlexibleStringExpander.getInstance(whenStr);
String newWhen = fse.expandString(context);
try {
Object retVal = GroovyUtil.eval(newWhen, context);
// retVal should be a Boolean, if not something weird is up...
if (retVal instanceof Boolean) {
Boolean boolVal = (Boolean) retVal;
isWhen = boolVal.booleanValue();
} else {
throw new IllegalArgumentException("Return value from use-when condition eval was not a Boolean: " + (retVal != null ? retVal.getClass().getName() : "null") + " [" + retVal + "]");
}
} catch (CompilationFailedException e) {
Debug.logError("Error in evaluating :" + whenStr + " : " + e.getMessage(), null);
throw new RuntimeException(e.getMessage());
}
}
return isWhen;
}
use of org.apache.ofbiz.base.util.string.FlexibleStringExpander in project ofbiz-framework by apache.
the class FlexibleStringExpanderTests method parserTest.
private static void parserTest(String label, String input, boolean checkCache, String toString) {
FlexibleStringExpander fse = FlexibleStringExpander.getInstance(input, false);
assertEquals(label + ":toString(no-cache)", toString, fse.toString());
fse = FlexibleStringExpander.getInstance(input, true);
assertEquals(label + ":toString(cache)", toString, fse.toString());
if (checkCache) {
assertEquals(label + ":same-cache", fse, FlexibleStringExpander.getInstance(input, true));
}
}
Aggregations