Search in sources :

Example 1 with FlexibleMapAccessor

use of org.apache.ofbiz.base.util.collections.FlexibleMapAccessor 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));
}
Also used : HashMap(java.util.HashMap) FlexibleStringExpander(org.apache.ofbiz.base.util.string.FlexibleStringExpander) FlexibleMapAccessor(org.apache.ofbiz.base.util.collections.FlexibleMapAccessor) HashSet(java.util.HashSet)

Example 2 with FlexibleMapAccessor

use of org.apache.ofbiz.base.util.collections.FlexibleMapAccessor in project ofbiz-framework by apache.

the class EntityFinderUtil method makeFieldMap.

public static Map<FlexibleMapAccessor<Object>, Object> makeFieldMap(Element element) {
    Map<FlexibleMapAccessor<Object>, Object> fieldMap = null;
    List<? extends Element> fieldMapElementList = UtilXml.childElementList(element, "field-map");
    if (fieldMapElementList.size() > 0) {
        fieldMap = new HashMap<>(fieldMapElementList.size());
        for (Element fieldMapElement : fieldMapElementList) {
            // set the env-name for each field-name, noting that if no field-name is specified it defaults to the env-name
            String fieldName = fieldMapElement.getAttribute("field-name");
            String envName = fieldMapElement.getAttribute("from-field");
            if (envName.isEmpty()) {
                envName = fieldMapElement.getAttribute("env-name");
            }
            String value = fieldMapElement.getAttribute("value");
            if (fieldName.isEmpty()) {
                // no fieldName, use envName for both
                fieldMap.put(FlexibleMapAccessor.getInstance(envName), FlexibleMapAccessor.getInstance(envName));
            } else {
                if (!value.isEmpty()) {
                    fieldMap.put(FlexibleMapAccessor.getInstance(fieldName), FlexibleStringExpander.getInstance(value));
                } else {
                    // at this point we have a fieldName and no value, do we have a envName?
                    if (!envName.isEmpty()) {
                        fieldMap.put(FlexibleMapAccessor.getInstance(fieldName), FlexibleMapAccessor.getInstance(envName));
                    } else {
                        // no envName, use fieldName for both
                        fieldMap.put(FlexibleMapAccessor.getInstance(fieldName), FlexibleMapAccessor.getInstance(fieldName));
                    }
                }
            }
        }
    }
    return fieldMap;
}
Also used : Element(org.w3c.dom.Element) FlexibleMapAccessor(org.apache.ofbiz.base.util.collections.FlexibleMapAccessor)

Aggregations

FlexibleMapAccessor (org.apache.ofbiz.base.util.collections.FlexibleMapAccessor)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 FlexibleStringExpander (org.apache.ofbiz.base.util.string.FlexibleStringExpander)1 Element (org.w3c.dom.Element)1