View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.scxml2.env.groovy;
18  
19  import org.apache.commons.scxml2.Context;
20  import org.apache.commons.scxml2.Evaluator;
21  import org.apache.commons.scxml2.SCXMLExpressionException;
22  import org.apache.commons.scxml2.SCXMLSystemContext;
23  import org.apache.commons.scxml2.StateConfiguration;
24  import org.apache.commons.scxml2.Status;
25  import org.apache.commons.scxml2.model.State;
26  import org.junit.Assert;
27  import org.junit.Before;
28  import org.junit.Test;
29  
30  public class GroovyEvaluatorTest {
31  
32      private static final String BAD_EXPRESSION = ">";
33      private Context ctx;
34  
35      @Before
36      public void before() {
37          ctx = new GroovyContext(new SCXMLSystemContext(new GroovyContext()), null);
38      }
39  
40      @Test
41      public void testEval() throws SCXMLExpressionException {
42          Evaluator eval = new GroovyEvaluator();
43          Assert.assertEquals(2, eval.eval(ctx, "1 + 1"));
44      }
45  
46      @Test
47      public void testPristine() throws SCXMLExpressionException {
48          Evaluator eval = new GroovyEvaluator();
49          Assert.assertTrue(eval.evalCond(ctx, "1 + 1 == 2"));
50      }
51  
52      @Test
53      public void testBuiltInFunctions() throws SCXMLExpressionException {
54          Evaluator eval = new GroovyEvaluator();
55          StateConfiguration stateConfiguration = new StateConfiguration();
56          Status status = new Status(stateConfiguration);
57          ctx.getSystemContext().getPlatformVariables().put(SCXMLSystemContext.STATUS_KEY, status);
58  
59          State state1 = new State();
60          state1.setId("state1");
61          stateConfiguration.enterState(state1);
62          Assert.assertTrue(eval.evalCond(ctx, "In('state1')"));
63      }
64  
65      @Test
66      public void testScript() throws SCXMLExpressionException {
67          Evaluator eval = new GroovyEvaluator();
68          ctx.set("x", 3);
69          ctx.set("y", 0);
70          String script = 
71              "if ((x * 2) == 5) {" +
72                  "y = 1;\n" +
73              "} else {\n" +
74                  "y = 2;\n" +
75              "}";
76          Assert.assertEquals(2, eval.evalScript(ctx, script));
77          Assert.assertEquals(2, ctx.get("y"));
78      }
79  
80      @Test
81      public void testErrorMessage() {
82          Evaluator eval = new GroovyEvaluator();
83          Assert.assertNotNull(eval);
84          try {
85              eval.eval(ctx, BAD_EXPRESSION);
86              Assert.fail("GroovyEvaluator should throw SCXMLExpressionException");
87          } catch (SCXMLExpressionException e) {
88              Assert.assertTrue("GroovyEvaluator: Incorrect error message",
89                  e.getMessage().startsWith("eval('" + BAD_EXPRESSION + "'):"));
90          }
91      }
92  
93      @Test
94      public void testPreprocessScript() {
95          GroovyEvaluator evaluator = new GroovyEvaluator();
96          Assert.assertEquals("x &&  x || x  !  x == x <  x <= x != x >  x >= x", evaluator.getScriptPreProcessor().
97                  preProcess("x and x or x not x eq x lt x le x ne x gt x ge x"));
98          Assert.assertEquals("and x OR x\n ! \nx\n== x < \nx(le)x ne. xgt x ge", evaluator.getScriptPreProcessor().
99                   preProcess("and x OR x\nnot\nx\neq x lt\nx(le)x ne. xgt x ge"));
100     }
101 }