Search in sources :

Example 1 with TemplateHashModelEx2

use of freemarker.template.TemplateHashModelEx2 in project freemarker by apache.

the class DeepUnwrapTest method testHashEx2Unwrapping.

@SuppressWarnings("rawtypes")
@Test
public void testHashEx2Unwrapping() throws Exception {
    Map<Object, Object> map = new LinkedHashMap<Object, Object>();
    map.put("k1", "v1");
    map.put("k2", null);
    map.put(3, "v3");
    map.put(null, "v4");
    DefaultObjectWrapper dow = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_27).build();
    TemplateModel model = dow.wrap(map);
    assertSame(map, DeepUnwrap.unwrap(model));
    Object unwrapped = DeepUnwrap.unwrap(new PurelyTemplateHashModelEx2((TemplateHashModelEx2) model));
    assertNotSame(map, unwrapped);
    assertEquals(map, unwrapped);
    // Order is kept:
    assertArrayEquals(new Object[] { "k1", "k2", 3, null }, ((Map) unwrapped).keySet().toArray());
}
Also used : TemplateHashModelEx2(freemarker.template.TemplateHashModelEx2) DefaultObjectWrapper(freemarker.template.DefaultObjectWrapper) TemplateModel(freemarker.template.TemplateModel) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) DefaultObjectWrapperBuilder(freemarker.template.DefaultObjectWrapperBuilder) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 2 with TemplateHashModelEx2

use of freemarker.template.TemplateHashModelEx2 in project freemarker by apache.

the class Listables method getMapsWrappedAsEx2.

/**
 * Returns the map wrapped on various ways.
 */
private List<TemplateHashModelEx2> getMapsWrappedAsEx2(Map<?, ?> map) throws TemplateModelException {
    List<TemplateHashModelEx2> maps = new ArrayList<TemplateHashModelEx2>();
    maps.add((SimpleHash) new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_0).build().wrap(map));
    maps.add((DefaultMapAdapter) new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_24).build().wrap(map));
    BeansWrapperBuilder bwb = new BeansWrapperBuilder(Configuration.VERSION_2_3_24);
    bwb.setSimpleMapWrapper(true);
    maps.add((TemplateHashModelEx2) bwb.build().wrap(map));
    return maps;
}
Also used : TemplateHashModelEx2(freemarker.template.TemplateHashModelEx2) ArrayList(java.util.ArrayList) BeansWrapperBuilder(freemarker.ext.beans.BeansWrapperBuilder) DefaultObjectWrapperBuilder(freemarker.template.DefaultObjectWrapperBuilder)

Aggregations

DefaultObjectWrapperBuilder (freemarker.template.DefaultObjectWrapperBuilder)2 TemplateHashModelEx2 (freemarker.template.TemplateHashModelEx2)2 BeansWrapperBuilder (freemarker.ext.beans.BeansWrapperBuilder)1 DefaultObjectWrapper (freemarker.template.DefaultObjectWrapper)1 TemplateModel (freemarker.template.TemplateModel)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Test (org.junit.Test)1