码迷,mamicode.com
首页 > 其他好文 > 详细

Jexl表达式引擎(3)-属性篇

时间:2019-03-28 00:17:36      阅读:922      评论:0      收藏:0      [点我收藏+]

标签:create   表达式   imp   ble   inner   port   ant   .class   ati   

import org.apache.commons.jexl3.*;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;


/**
 * Created by Main on 2019/3/27.
 */
public class PublicFieldsTest {


    private static final JexlEngine JEXL_ENGINE = new JexlBuilder().strict(true).silent(false).cache(32).create();

    // some constants
    private static final String LOWER42 = "fourty-two";
    private static final String UPPER42 = "FOURTY-TWO";
    /**
     * An Inner class.
     */
    public static class Inner {
        public double aDouble = 42.0;
        public static double NOT42 = -42.0;
    }

    /**
     * A Struct, all fields public
     */
    public static class Struct {
        public Inner inner = new Inner();
        public int anInt = 42;
        public String aString = LOWER42;
    }

    // a pub instance
    private Struct pub;
    // the JexlContext to use
    private JexlContext ctxt;

    @Before
    public void setUp() {
        pub = new Struct();
        ctxt = new MapContext();
        ctxt.set("pub", pub);
    }

    @Test
    public void testGetInt() throws Exception {
        JexlExpression get = JEXL_ENGINE.createExpression("pub.anInt");
        Assert.assertEquals(42, get.evaluate(ctxt));
        JEXL_ENGINE.setProperty(pub, "anInt", -42);
        Assert.assertEquals(-42, get.evaluate(ctxt));
    }

    @Test
    public void testSetInt() throws Exception {
        JexlExpression set = JEXL_ENGINE.createExpression("pub.anInt = value");
        ctxt.set("value", -42);
        Assert.assertEquals(-42, set.evaluate(ctxt));
        Assert.assertEquals(-42, JEXL_ENGINE.getProperty(pub, "anInt"));
        ctxt.set("value", 42);
        Assert.assertEquals(42, set.evaluate(ctxt));
        Assert.assertEquals(42, JEXL_ENGINE.getProperty(pub, "anInt"));
        try {
            ctxt.set("value", UPPER42);
            Assert.assertEquals(null, set.evaluate(ctxt));
            Assert.fail("should have thrown");
        } catch(JexlException xjexl) {}
    }

    @Test
    public void testGetString() throws Exception {
        JexlExpression get = JEXL_ENGINE.createExpression("pub.aString");
        Assert.assertEquals(LOWER42, get.evaluate(ctxt));
        JEXL_ENGINE.setProperty(pub, "aString", UPPER42);
        Assert.assertEquals(UPPER42, get.evaluate(ctxt));
    }

    @Test
    public void testSetString() throws Exception {
        JexlExpression set = JEXL_ENGINE.createExpression("pub.aString = value");
        ctxt.set("value", UPPER42);
        Assert.assertEquals(UPPER42, set.evaluate(ctxt));
        Assert.assertEquals(UPPER42, JEXL_ENGINE.getProperty(pub, "aString"));
        ctxt.set("value", LOWER42);
        Assert.assertEquals(LOWER42, set.evaluate(ctxt));
        Assert.assertEquals(LOWER42, JEXL_ENGINE.getProperty(pub, "aString"));
    }

    @Test
    public void testGetInnerDouble() throws Exception {
        JexlExpression get = JEXL_ENGINE.createExpression("pub.inner.aDouble");
        Assert.assertEquals(42.0, get.evaluate(ctxt));
        JEXL_ENGINE.setProperty(pub, "inner.aDouble", -42);
        Assert.assertEquals(-42.0, get.evaluate(ctxt));
    }

    @Test
    public void testSetInnerDouble() throws Exception {
        JexlExpression set = JEXL_ENGINE.createExpression("pub.inner.aDouble = value");
        ctxt.set("value", -42.0);
        Assert.assertEquals(-42.0, set.evaluate(ctxt));
        Assert.assertEquals(-42.0, JEXL_ENGINE.getProperty(pub, "inner.aDouble"));
        ctxt.set("value", 42.0);
        Assert.assertEquals(42.0, set.evaluate(ctxt));
        Assert.assertEquals(42.0, JEXL_ENGINE.getProperty(pub, "inner.aDouble"));
        try {
            ctxt.set("value", UPPER42);
            Assert.assertEquals(null, set.evaluate(ctxt));
            Assert.fail("should have thrown");
        } catch(JexlException xjexl) {}
    }

    public enum Gender { MALE, FEMALE };

    @Test
    public void testGetEnum() throws Exception {
        ctxt.set("com.jexl.gender", Gender.class);
        String src = "x = com.jexl.gender.FEMALE";
        JexlScript script = JEXL_ENGINE.createScript(src);
        Object result = script.execute(ctxt);
        Assert.assertEquals(Gender.FEMALE, result);
        Assert.assertEquals(Gender.FEMALE, ctxt.get("x"));
    }

    @Test
    public void testGetStaticField() throws Exception {
        ctxt.set("com.jexl", Inner.class);
        String src = "x = com.jexl.NOT42";
        JexlScript script = JEXL_ENGINE.createScript(src);
        Object result = script.execute(ctxt);
        Assert.assertEquals(Inner.NOT42, result);
        Assert.assertEquals(Inner.NOT42, ctxt.get("x"));
    }

}

Jexl表达式引擎(3)-属性篇

标签:create   表达式   imp   ble   inner   port   ant   .class   ati   

原文地址:https://www.cnblogs.com/Jmmm/p/10611908.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!