Search in sources :

Example 1 with VirtualAttributeEval

use of org.beetl.core.VirtualAttributeEval in project beetl2.0 by javamonkey.

the class VirtualAttributeTest method testVirtualAttribute.

@Test
public void testVirtualAttribute() throws Exception {
    gt.registerVirtualAttributeEval(new VirtualAttributeEval() {

        @Override
        public Object eval(Object o, String attributeName, Context ctx) {
            // TODO Auto-generated method stub
            return attributeName;
        }

        @Override
        public boolean isSupport(Class c, String attributeName) {
            if (attributeName.equals("hello"))
                return true;
            else
                return false;
        }
    });
    List list = User.getTestUsers();
    Template t = gt.getTemplate("/va/va_virtual_template.html");
    this.bind(t, "list", list, "user", new Object());
    String str = t.render();
    AssertJUnit.assertEquals(this.getFileContent("/va/va_virtual_expected.html"), str);
    t = gt.getTemplate("/va/va_virtual_template.html");
    this.bind(t, "list", list, "user", new Object());
    str = t.render();
    AssertJUnit.assertEquals(this.getFileContent("/va/va_virtual_expected.html"), str);
}
Also used : Context(org.beetl.core.Context) VirtualAttributeEval(org.beetl.core.VirtualAttributeEval) JSONObject(com.alibaba.fastjson.JSONObject) List(java.util.List) Template(org.beetl.core.Template) Test(org.testng.annotations.Test)

Example 2 with VirtualAttributeEval

use of org.beetl.core.VirtualAttributeEval in project beetl2.0 by javamonkey.

the class VirtualAttributeTest method testJacksonVirtualAttribute.

@Test
public void testJacksonVirtualAttribute() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    String json = "{\"name\":\"lijz\",\"id\":10}";
    JsonNode node = mapper.readTree(json);
    gt.registerVirtualAttributeEval(new VirtualAttributeEval() {

        @Override
        public Object eval(Object o, String attributeName, Context ctx) {
            JsonNode node = (JsonNode) o;
            return node.get(attributeName);
        }

        @Override
        public boolean isSupport(Class c, String attributeName) {
            if (JsonNode.class.isAssignableFrom(c))
                return true;
            else
                return false;
        }
    });
    // String str = "${json.name}"; jaskson 支持get方法,因此不用虚拟属性也能访问
    String str = "${json.~name}";
    Template template = gt.getTemplate(str, new StringTemplateResourceLoader());
    template.binding("json", node);
    String ret = template.render();
    AssertJUnit.assertEquals("\"lijz\"", ret);
}
Also used : Context(org.beetl.core.Context) JsonNode(com.fasterxml.jackson.databind.JsonNode) VirtualAttributeEval(org.beetl.core.VirtualAttributeEval) JSONObject(com.alibaba.fastjson.JSONObject) StringTemplateResourceLoader(org.beetl.core.resource.StringTemplateResourceLoader) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Template(org.beetl.core.Template) Test(org.testng.annotations.Test)

Example 3 with VirtualAttributeEval

use of org.beetl.core.VirtualAttributeEval in project beetl2.0 by javamonkey.

the class VirtualAttributeTest method testFastjosnVirtualAttribute.

@Test
public void testFastjosnVirtualAttribute() throws Exception {
    String json = "{\"name\":\"lijz\",\"id\":10}";
    JSONObject node = (JSONObject) JSON.parse(json);
    gt.registerVirtualAttributeEval(new VirtualAttributeEval() {

        @Override
        public Object eval(Object o, String attributeName, Context ctx) {
            return ((JSONObject) o).get(attributeName);
        }

        @Override
        public boolean isSupport(Class c, String attributeName) {
            if (c == JSONObject.class || c == JSONArray.class) {
                return true;
            } else {
                return false;
            }
        }
    });
    String str = "${json.~name}";
    Template template = gt.getTemplate(str, new StringTemplateResourceLoader());
    template.binding("json", node);
    String ret = template.render();
    AssertJUnit.assertEquals("lijz", ret);
}
Also used : Context(org.beetl.core.Context) JSONObject(com.alibaba.fastjson.JSONObject) VirtualAttributeEval(org.beetl.core.VirtualAttributeEval) JSONObject(com.alibaba.fastjson.JSONObject) StringTemplateResourceLoader(org.beetl.core.resource.StringTemplateResourceLoader) Template(org.beetl.core.Template) Test(org.testng.annotations.Test)

Aggregations

JSONObject (com.alibaba.fastjson.JSONObject)3 Context (org.beetl.core.Context)3 Template (org.beetl.core.Template)3 VirtualAttributeEval (org.beetl.core.VirtualAttributeEval)3 Test (org.testng.annotations.Test)3 StringTemplateResourceLoader (org.beetl.core.resource.StringTemplateResourceLoader)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 List (java.util.List)1