001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.scxml2.env.jexl;
018
019import org.apache.commons.scxml2.Context;
020import org.apache.commons.scxml2.Evaluator;
021import org.apache.commons.scxml2.SCXMLExpressionException;
022import org.junit.Assert;
023import org.junit.Test;
024
025public class JexlEvaluatorTest {
026
027    private String BAD_EXPRESSION = ">";
028    private Context ctx = new JexlContext();
029
030    @Test
031    public void testPristine() throws SCXMLExpressionException {
032        Evaluator eval = new JexlEvaluator();
033        Assert.assertTrue(((Boolean) eval.eval(ctx, "1+1 eq 2")).booleanValue());
034    }
035
036    @Test
037    public void testScript() throws SCXMLExpressionException {
038        Evaluator eval = new JexlEvaluator();
039        ctx.set("x", 3);
040        ctx.set("y", 0);
041        String script = 
042            "if ((x * 2) == 5) {" +
043                "y = 1;\n" +
044            "} else {\n" +
045                "y = 2;\n" +
046            "}";
047        Assert.assertEquals(2, eval.evalScript(ctx, script));
048        Assert.assertEquals(2, ctx.get("y"));
049    }
050
051    @Test
052    public void testErrorMessage() {
053        Evaluator eval = new JexlEvaluator();
054        Assert.assertNotNull(eval);
055        try {
056            eval.eval(ctx, BAD_EXPRESSION);
057            Assert.fail("JexlEvaluator should throw SCXMLExpressionException");
058        } catch (SCXMLExpressionException e) {
059            Assert.assertTrue("JexlEvaluator: Incorrect error message",
060                e.getMessage().startsWith("eval('" + BAD_EXPRESSION + "'):"));
061        }
062    }
063
064}