Search in sources :

Example 16 with ExpressionRuntimeException

use of com.googlecode.aviator.exception.ExpressionRuntimeException in project aviatorscript by killme2008.

the class AviatorJavaTypeUnitTest method testCompareDate.

@Test
public void testCompareDate() {
    Map<String, Object> env = new HashMap<String, Object>();
    final Date date = new Date();
    String dateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS").format(date);
    env.put("date", date);
    env.put("dateStr", dateStr);
    assertEquals(0, new AviatorJavaType("date").innerCompare(new AviatorJavaType("date"), env));
    assertEquals(0, new AviatorJavaType("date").innerCompare(new AviatorString(dateStr), env));
    assertEquals(1, new AviatorJavaType("date").innerCompare(new AviatorString("1990-03-21 04:56:30:0"), env));
    assertEquals(-1, new AviatorJavaType("date").innerCompare(new AviatorString("2200-03-21 04:56:30:0"), env));
    assertEquals(0, new AviatorJavaType("date").innerCompare(new AviatorJavaType("dateStr"), env));
    try {
        new AviatorJavaType("date").innerCompare(AviatorNumber.valueOf(191), env);
        Assert.fail();
    } catch (ExpressionRuntimeException e) {
    }
    try {
        new AviatorJavaType("date").innerCompare(new AviatorString("2200-03 21 04:56:30:0"), env);
        Assert.fail();
    } catch (ExpressionRuntimeException e) {
    }
    try {
        new AviatorJavaType("date").innerCompare(AviatorBoolean.TRUE, env);
        Assert.fail();
    } catch (ExpressionRuntimeException e) {
    }
}
Also used : ExpressionRuntimeException(com.googlecode.aviator.exception.ExpressionRuntimeException) HashMap(java.util.HashMap) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 17 with ExpressionRuntimeException

use of com.googlecode.aviator.exception.ExpressionRuntimeException in project aviatorscript by killme2008.

the class AviatorJavaTypeUnitTest method testNeg.

@Test
public void testNeg() {
    AviatorJavaType intType = new AviatorJavaType("intType");
    AviatorJavaType boolType = new AviatorJavaType("boolType");
    AviatorJavaType stringType = new AviatorJavaType("stringType");
    AviatorJavaType charType = new AviatorJavaType("charType");
    AviatorJavaType dateType = new AviatorJavaType("dateType");
    Map<String, Object> env = new HashMap<String, Object>();
    env.put("intType", 3);
    env.put("boolType", Boolean.FALSE);
    env.put("stringType", "hello");
    env.put("charType", 'c');
    env.put("dateType", new Date());
    assertEquals(-3L, intType.neg(env).getValue(env));
    try {
        boolType.neg(env);
        Assert.fail();
    } catch (ExpressionRuntimeException e) {
    }
    try {
        stringType.neg(env);
        Assert.fail();
    } catch (ExpressionRuntimeException e) {
    }
    try {
        charType.neg(env);
        Assert.fail();
    } catch (ExpressionRuntimeException e) {
    }
    try {
        dateType.neg(env);
        Assert.fail();
    } catch (ExpressionRuntimeException e) {
    }
}
Also used : ExpressionRuntimeException(com.googlecode.aviator.exception.ExpressionRuntimeException) HashMap(java.util.HashMap) Date(java.util.Date) Test(org.junit.Test)

Example 18 with ExpressionRuntimeException

use of com.googlecode.aviator.exception.ExpressionRuntimeException in project aviatorscript by killme2008.

the class SeqPredicateFunction method call.

@Override
public AviatorObject call(final Map<String, Object> env, AviatorObject arg1) {
    if (this.propertyName != null) {
        String propertyNameStr = this.propertyName.stringValue(env);
        Object target = arg1.getValue(env);
        try {
            Object property = Reflector.getProperty(target, propertyNameStr);
            arg1 = AviatorRuntimeJavaType.valueOf(property);
        } catch (NoSuchPropertyException e) {
            throw new IllegalArgumentException("Fail to get property <" + propertyNameStr + "> from <" + arg1.desc(env) + ">", e);
        }
    }
    switch(this.opType) {
        case EQ:
            return arg1.compare(this.value, env) == 0 ? AviatorBoolean.TRUE : AviatorBoolean.FALSE;
        case NEQ:
            return arg1.compare(this.value, env) != 0 ? AviatorBoolean.TRUE : AviatorBoolean.FALSE;
        case LT:
            return arg1.compare(this.value, env) < 0 ? AviatorBoolean.TRUE : AviatorBoolean.FALSE;
        case LE:
            return arg1.compare(this.value, env) <= 0 ? AviatorBoolean.TRUE : AviatorBoolean.FALSE;
        case GE:
            return arg1.compare(this.value, env) >= 0 ? AviatorBoolean.TRUE : AviatorBoolean.FALSE;
        case GT:
            return arg1.compare(this.value, env) > 0 ? AviatorBoolean.TRUE : AviatorBoolean.FALSE;
        default:
            throw new ExpressionRuntimeException(getName() + " is not a relation operator");
    }
}
Also used : ExpressionRuntimeException(com.googlecode.aviator.exception.ExpressionRuntimeException) NoSuchPropertyException(com.googlecode.aviator.exception.NoSuchPropertyException) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject)

Example 19 with ExpressionRuntimeException

use of com.googlecode.aviator.exception.ExpressionRuntimeException in project aviatorscript by killme2008.

the class GrammarUnitTest method testTernaryOperator.

/*
   * 测试三元表达式
   */
@Test
public void testTernaryOperator() {
    Map<String, Object> env = new HashMap<String, Object>();
    int i = 0;
    float f = 3.14f;
    String email = "killme2008@gmail.com";
    char ch = 'a';
    boolean t = true;
    env.put("i", i);
    env.put("f", f);
    env.put("email", email);
    env.put("ch", ch);
    env.put("t", t);
    assertEquals(1, this.instance.execute("2>1?1:0"));
    assertEquals(0, this.instance.execute("2<1?1:0"));
    assertEquals(f, (Float) this.instance.execute("false?i:f", env), 0.001);
    assertEquals(i, this.instance.execute("true?i:f", env));
    assertEquals("killme2008", this.instance.execute("email=~/([\\w0-9]+)@\\w+\\.\\w+/ ? $1:'unknow'", env));
    assertEquals(f, (Float) this.instance.execute("ch!='a'?i:f", env), 0.001);
    assertEquals(i, this.instance.execute("ch=='a'?i:f", env));
    assertEquals(email, this.instance.execute("t?email:ch", env));
    // 多层嵌套
    assertEquals(ch, this.instance.execute("t? i>0? f:ch : email", env));
    assertEquals(email, this.instance.execute("!t? i>0? f:ch : f>3?email:ch", env));
    // 使用括号
    assertEquals(email, this.instance.execute("!t? (i>0? f:ch) :( f>3?email:ch)", env));
    // 测试错误情况
    try {
        this.instance.execute("f?1:0", env);
        Assert.fail();
    } catch (ExpressionRuntimeException e) {
    }
    try {
        this.instance.execute("'hello'?1:0");
        Assert.fail();
    } catch (ExpressionRuntimeException e) {
    }
    try {
        this.instance.execute("/test/?1:0");
        Assert.fail();
    } catch (ExpressionRuntimeException e) {
    }
    try {
        this.instance.execute("!t? (i>0? f:ch) : f>3?email:ch)", env);
        Assert.fail();
    } catch (ExpressionSyntaxErrorException e) {
    }
    try {
        this.instance.execute("!t? (i>0? f:ch : (f>3?email:ch)", env);
        Assert.fail();
    } catch (ExpressionSyntaxErrorException e) {
    }
}
Also used : ExpressionRuntimeException(com.googlecode.aviator.exception.ExpressionRuntimeException) HashMap(java.util.HashMap) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) ExpressionSyntaxErrorException(com.googlecode.aviator.exception.ExpressionSyntaxErrorException) Test(org.junit.Test)

Aggregations

ExpressionRuntimeException (com.googlecode.aviator.exception.ExpressionRuntimeException)19 Date (java.util.Date)7 Test (org.junit.Test)7 AviatorObject (com.googlecode.aviator.runtime.type.AviatorObject)6 HashMap (java.util.HashMap)6 AviatorString (com.googlecode.aviator.runtime.type.AviatorString)4 SimpleDateFormat (java.text.SimpleDateFormat)4 ID (cn.devezhao.persist4j.engine.ID)1 MissingMetaExcetion (cn.devezhao.persist4j.metadata.MissingMetaExcetion)1 BaseExpression (com.googlecode.aviator.BaseExpression)1 CompileExpressionErrorException (com.googlecode.aviator.exception.CompileExpressionErrorException)1 ExpressionSyntaxErrorException (com.googlecode.aviator.exception.ExpressionSyntaxErrorException)1 NoSuchPropertyException (com.googlecode.aviator.exception.NoSuchPropertyException)1 NumberToken (com.googlecode.aviator.lexer.token.NumberToken)1 AviatorFunction (com.googlecode.aviator.runtime.type.AviatorFunction)1 AviatorJavaType (com.googlecode.aviator.runtime.type.AviatorJavaType)1 AviatorPattern (com.googlecode.aviator.runtime.type.AviatorPattern)1 Env (com.googlecode.aviator.utils.Env)1 RebuildException (com.rebuild.core.RebuildException)1 RepeatedRecordsException (com.rebuild.core.service.general.RepeatedRecordsException)1