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.jexl;
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.junit.Assert;
23  import org.junit.Test;
24  
25  public class JexlEvaluatorTest {
26  
27      private String BAD_EXPRESSION = ">";
28      private Context ctx = new JexlContext();
29  
30      @Test
31      public void testPristine() throws SCXMLExpressionException {
32          Evaluator eval = new JexlEvaluator();
33          Assert.assertTrue(((Boolean) eval.eval(ctx, "1+1 eq 2")).booleanValue());
34      }
35  
36      @Test
37      public void testScript() throws SCXMLExpressionException {
38          Evaluator eval = new JexlEvaluator();
39          ctx.set("x", 3);
40          ctx.set("y", 0);
41          String script = 
42              "if ((x * 2) == 5) {" +
43                  "y = 1;\n" +
44              "} else {\n" +
45                  "y = 2;\n" +
46              "}";
47          Assert.assertEquals(2, eval.evalScript(ctx, script));
48          Assert.assertEquals(2, ctx.get("y"));
49      }
50  
51      @Test
52      public void testErrorMessage() {
53          Evaluator eval = new JexlEvaluator();
54          Assert.assertNotNull(eval);
55          try {
56              eval.eval(ctx, BAD_EXPRESSION);
57              Assert.fail("JexlEvaluator should throw SCXMLExpressionException");
58          } catch (SCXMLExpressionException e) {
59              Assert.assertTrue("JexlEvaluator: Incorrect error message",
60                  e.getMessage().startsWith("eval('" + BAD_EXPRESSION + "'):"));
61          }
62      }
63  
64  }