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.groovy;
018
019import org.apache.commons.scxml2.Context;
020import org.apache.commons.scxml2.Evaluator;
021import org.apache.commons.scxml2.SCXMLExpressionException;
022import org.apache.commons.scxml2.SCXMLSystemContext;
023import org.apache.commons.scxml2.StateConfiguration;
024import org.apache.commons.scxml2.Status;
025import org.apache.commons.scxml2.model.State;
026import org.junit.Assert;
027import org.junit.Before;
028import org.junit.Test;
029
030public class GroovyEvaluatorTest {
031
032    private static final String BAD_EXPRESSION = ">";
033    private Context ctx;
034
035    @Before
036    public void before() {
037        ctx = new GroovyContext(new SCXMLSystemContext(new GroovyContext()), null);
038    }
039
040    @Test
041    public void testEval() throws SCXMLExpressionException {
042        Evaluator eval = new GroovyEvaluator();
043        Assert.assertEquals(2, eval.eval(ctx, "1 + 1"));
044    }
045
046    @Test
047    public void testPristine() throws SCXMLExpressionException {
048        Evaluator eval = new GroovyEvaluator();
049        Assert.assertTrue(eval.evalCond(ctx, "1 + 1 == 2"));
050    }
051
052    @Test
053    public void testBuiltInFunctions() throws SCXMLExpressionException {
054        Evaluator eval = new GroovyEvaluator();
055        StateConfiguration stateConfiguration = new StateConfiguration();
056        Status status = new Status(stateConfiguration);
057        ctx.getSystemContext().getPlatformVariables().put(SCXMLSystemContext.STATUS_KEY, status);
058
059        State state1 = new State();
060        state1.setId("state1");
061        stateConfiguration.enterState(state1);
062        Assert.assertTrue(eval.evalCond(ctx, "In('state1')"));
063    }
064
065    @Test
066    public void testScript() throws SCXMLExpressionException {
067        Evaluator eval = new GroovyEvaluator();
068        ctx.set("x", 3);
069        ctx.set("y", 0);
070        String script = 
071            "if ((x * 2) == 5) {" +
072                "y = 1;\n" +
073            "} else {\n" +
074                "y = 2;\n" +
075            "}";
076        Assert.assertEquals(2, eval.evalScript(ctx, script));
077        Assert.assertEquals(2, ctx.get("y"));
078    }
079
080    @Test
081    public void testErrorMessage() {
082        Evaluator eval = new GroovyEvaluator();
083        Assert.assertNotNull(eval);
084        try {
085            eval.eval(ctx, BAD_EXPRESSION);
086            Assert.fail("GroovyEvaluator should throw SCXMLExpressionException");
087        } catch (SCXMLExpressionException e) {
088            Assert.assertTrue("GroovyEvaluator: Incorrect error message",
089                e.getMessage().startsWith("eval('" + BAD_EXPRESSION + "'):"));
090        }
091    }
092
093    @Test
094    public void testPreprocessScript() {
095        GroovyEvaluator evaluator = new GroovyEvaluator();
096        Assert.assertEquals("x &&  x || x  !  x == x <  x <= x != x >  x >= x", evaluator.getScriptPreProcessor().
097                preProcess("x and x or x not x eq x lt x le x ne x gt x ge x"));
098        Assert.assertEquals("and x OR x\n ! \nx\n== x < \nx(le)x ne. xgt x ge", evaluator.getScriptPreProcessor().
099                 preProcess("and x OR x\nnot\nx\neq x lt\nx(le)x ne. xgt x ge"));
100    }
101}