Search in sources :

Example 1 with JxltEngine

use of org.apache.commons.jexl3.JxltEngine in project commons-jexl by apache.

the class Issues200Test method testTemplate6565a.

@Test
public void testTemplate6565a() throws Exception {
    final JexlEngine jexl = new JexlBuilder().create();
    final JxltEngine jexlt = jexl.createJxltEngine();
    final String source = "$$ var res = '';\n" + "$$ var meta = session.data['METADATA'];\n" + "$$ if (meta) {\n" + "$$   var entry = meta['ID'];\n" + "$$   if (entry) {\n" + "$$     var value = session.data[entry];\n" + "$$     res = value?: '';\n" + "$$   }\n" + "$$ }\n" + "${res}\n";
    final JxltEngine.Template script = jexlt.createTemplate("$$", new StringReader(source));
    Assert.assertNotNull(script);
    final TemplateDebugger dbg = new TemplateDebugger();
    final String refactored = dbg.debug(script) ? dbg.toString() : "";
    Assert.assertNotNull(refactored);
    Assert.assertEquals(source, refactored);
}
Also used : StringReader(java.io.StringReader) TemplateDebugger(org.apache.commons.jexl3.internal.TemplateDebugger) Test(org.junit.Test)

Example 2 with JxltEngine

use of org.apache.commons.jexl3.JxltEngine in project commons-jexl by apache.

the class JXLTTest method testSanboxedTemplate.

@Test
public void testSanboxedTemplate() throws Exception {
    final String src = "Hello ${user}";
    final JexlContext ctxt = new MapContext();
    ctxt.set("user", "Francesco");
    // / this uberspect can not access jexl3 classes (besides test)
    Uberspect uberspect = new Uberspect(LogFactory.getLog(JXLTTest.class), null, NOJEXL3);
    Method method = uberspect.getMethod(TemplateInterpreter.class, "print", new Object[] { Integer.TYPE });
    Assert.assertNull(method);
    // ensures JXLT sandboxed still executes
    final JexlEngine jexl = new JexlBuilder().uberspect(uberspect).create();
    final JxltEngine jxlt = jexl.createJxltEngine();
    JxltEngine.Template tmplt = jxlt.createTemplate(src);
    Writer strw = new StringWriter();
    tmplt.evaluate(ctxt, strw);
    String result = strw.toString();
    Assert.assertEquals("Hello Francesco", result);
}
Also used : StringWriter(java.io.StringWriter) JexlMethod(org.apache.commons.jexl3.introspection.JexlMethod) Method(java.lang.reflect.Method) Uberspect(org.apache.commons.jexl3.internal.introspection.Uberspect) PrintWriter(java.io.PrintWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer) Test(org.junit.Test)

Example 3 with JxltEngine

use of org.apache.commons.jexl3.JxltEngine in project syncope by apache.

the class JexlUtilsTest method newJxltEngine.

@Test
public void newJxltEngine() {
    JxltEngine engine = JexlUtils.newJxltEngine();
    assertNotNull(engine);
}
Also used : JxltEngine(org.apache.commons.jexl3.JxltEngine) AbstractTest(org.apache.syncope.core.provisioning.api.AbstractTest) Test(org.junit.jupiter.api.Test)

Example 4 with JxltEngine

use of org.apache.commons.jexl3.JxltEngine in project commons-jexl by apache.

the class JXLTTest method testSanboxed311i.

@Test
public void testSanboxed311i() throws Exception {
    // / this uberspect can not access jexl3 classes (besides test)
    Uberspect uberspect = new Uberspect(LogFactory.getLog(JXLTTest.class), null, NOJEXL3);
    Method method = uberspect.getMethod(TemplateInterpreter.class, "print", new Object[] { Integer.TYPE });
    final JexlEngine jexl = new JexlBuilder().uberspect(uberspect).create();
    final JxltEngine jxlt = jexl.createJxltEngine();
    final JexlContext ctx311 = new Context311();
    final String rpt = "$$var u = 'Universe'; exec('4').execute((a, b)->{" + "\n<p>${u} ${a}${b}</p>" + "\n$$}, '2')";
    final JxltEngine.Template t = jxlt.createTemplate("$$", new StringReader(rpt));
    final StringWriter strw = new StringWriter();
    t.evaluate(ctx311, strw, 42);
    final String output = strw.toString();
    Assert.assertEquals("<p>Universe 42</p>\n", output);
}
Also used : StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) JexlMethod(org.apache.commons.jexl3.introspection.JexlMethod) Method(java.lang.reflect.Method) Uberspect(org.apache.commons.jexl3.internal.introspection.Uberspect) Test(org.junit.Test)

Example 5 with JxltEngine

use of org.apache.commons.jexl3.JxltEngine in project commons-jexl by apache.

the class Issues200Test method testTemplate6565b.

@Test
public void testTemplate6565b() throws Exception {
    final JexlEngine jexl = new JexlBuilder().create();
    final JxltEngine jexlt = jexl.createJxltEngine();
    final String source = "$$ var res = '';\n" + "$$ var meta = session.data['METADATA'];\n" + "$$ if (meta) {\n" + "$$   var entry = meta['ID'];\n" + "$$   if (entry) {\n" + "$$     var value = session.data[entry];\n" + "$$     res = value?: '';\n" + "${res}\n" + "$$   }\n" + "$$ }\n";
    final JxltEngine.Template script = jexlt.createTemplate("$$", new StringReader(source));
    Assert.assertNotNull(script);
    final TemplateDebugger dbg = new TemplateDebugger();
    final String refactored = dbg.debug(script) ? dbg.toString() : "";
    Assert.assertNotNull(refactored);
    Assert.assertEquals(source, refactored);
}
Also used : StringReader(java.io.StringReader) TemplateDebugger(org.apache.commons.jexl3.internal.TemplateDebugger) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)4 StringReader (java.io.StringReader)3 StringWriter (java.io.StringWriter)2 Method (java.lang.reflect.Method)2 TemplateDebugger (org.apache.commons.jexl3.internal.TemplateDebugger)2 Uberspect (org.apache.commons.jexl3.internal.introspection.Uberspect)2 JexlMethod (org.apache.commons.jexl3.introspection.JexlMethod)2 PrintWriter (java.io.PrintWriter)1 Writer (java.io.Writer)1 JxltEngine (org.apache.commons.jexl3.JxltEngine)1 AbstractTest (org.apache.syncope.core.provisioning.api.AbstractTest)1 Test (org.junit.jupiter.api.Test)1