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    *   https://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  
18  package org.apache.commons.jexl3.scripting;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertInstanceOf;
22  
23  import java.io.StringWriter;
24  
25  import javax.script.Compilable;
26  import javax.script.CompiledScript;
27  import javax.script.ScriptEngine;
28  import javax.script.ScriptEngineManager;
29  
30  import org.apache.commons.jexl3.JexlBuilder;
31  import org.apache.commons.jexl3.JexlTestCase;
32  import org.junit.jupiter.api.BeforeAll;
33  import org.junit.jupiter.api.Test;
34  
35  class JexlScriptEngineOptionalTest {
36      private final JexlScriptEngineFactory factory = new JexlScriptEngineFactory();
37      private final ScriptEngineManager manager = new ScriptEngineManager();
38      private final ScriptEngine engine = manager.getEngineByName("jexl");
39  
40      @BeforeAll
41      static void setUpClass() {
42          JexlBuilder.setDefaultPermissions(JexlTestCase.TEST_PERMS);
43      }
44  
45      @Test
46      void testCompilable() throws Exception {
47          assertInstanceOf(Compilable.class, engine, "Engine should implement Compilable");
48          final Compilable cengine = (Compilable) engine;
49          final CompiledScript script = cengine.compile("40 + 2");
50          assertEquals(42, script.eval());
51          assertEquals(42, script.eval());
52      }
53  
54      @Test
55      void testError() throws Exception {
56          final String error = "JEXL.err.print('ERROR')";
57          // redirect error to capture evaluation result
58          final StringWriter outContent = new StringWriter();
59          engine.getContext().setErrorWriter(outContent);
60          engine.eval(error);
61          assertEquals("ERROR", outContent.toString());
62      }
63  
64      @Test
65      void testOutput() throws Exception {
66          final String output = factory.getOutputStatement("foo\u00a9bar");
67          assertEquals("JEXL.out.print('foo\\u00a9bar')", output);
68          // redirect output to capture evaluation result
69          final StringWriter outContent = new StringWriter();
70          engine.getContext().setWriter(outContent);
71          engine.eval(output);
72          assertEquals("foo\u00a9bar", outContent.toString());
73      }
74  }