package com.qmth.boot.test.tools.freemarker;

import com.qmth.boot.tools.freemarker.FreemarkerUtil;
import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class FreemarkerUtilTest {

    @Test
    public void test() {
        Map<String, Object> map = new HashMap<>();
        map.put("name", "ABC");
        map.put("address", new HashMap<String, Object>() {{
            put("city", "wuhan");
        }});
        map.put("child", new ArrayList<Object>() {{
            add(new HashMap<String, Object>() {{
                put("name", "c1");
                put("age", 3);
            }});
            add(new HashMap<String, Object>() {{
                put("name", "c2");
                put("age", 4);
            }});
        }});
        Assert.assertEquals("ABC", FreemarkerUtil.getValue("${name}", map));
        Assert.assertEquals("wuhan", FreemarkerUtil.getValue("${address.city}", map));
        Assert.assertEquals("4", FreemarkerUtil.getValue("${child[1].age}", map));
        Assert.assertEquals("", FreemarkerUtil.getValue("${child[3].name}", map, ""));
    }
}
