Search in sources :

Example 1 with ScriptExecutor

use of org.springframework.integration.scripting.ScriptExecutor in project spring-integration-samples by spring-projects.

the class ScriptTests method testRuby.

@Test
public void testRuby() {
    ScriptExecutor executor = ScriptExecutorFactory.getScriptExecutor("ruby");
    Order order = new Order(0);
    order.addItem(DrinkType.LATTE, 2, false);
    Map<String, Object> variables = new HashMap<String, Object>();
    variables.put("payload", order.getItems().get(0));
    variables.put("timeToPrepare", 1L);
    Object obj = executor.executeScript(new ResourceScriptSource(new FileSystemResource("scripts/ruby/barista.rb")), variables);
    assertNotNull(obj);
    assertTrue(obj instanceof Drink);
}
Also used : ResourceScriptSource(org.springframework.scripting.support.ResourceScriptSource) HashMap(java.util.HashMap) FileSystemResource(org.springframework.core.io.FileSystemResource) ScriptExecutor(org.springframework.integration.scripting.ScriptExecutor) Test(org.junit.Test)

Example 2 with ScriptExecutor

use of org.springframework.integration.scripting.ScriptExecutor in project spring-integration-samples by spring-projects.

the class ScriptTests method testPython.

@Test
@Ignore
public void testPython() {
    ScriptExecutor executor = ScriptExecutorFactory.getScriptExecutor("python");
    Order order = new Order(0);
    order.addItem(DrinkType.LATTE, 2, false);
    Map<String, Object> variables = new HashMap<String, Object>();
    variables.put("payload", order.getItems().get(0));
    variables.put("timeToPrepare", "1");
    Object obj = executor.executeScript(new ResourceScriptSource(new FileSystemResource("scripts/python/barista.py")), variables);
    assertNotNull(obj);
    assertTrue(obj instanceof Drink);
}
Also used : ResourceScriptSource(org.springframework.scripting.support.ResourceScriptSource) HashMap(java.util.HashMap) FileSystemResource(org.springframework.core.io.FileSystemResource) ScriptExecutor(org.springframework.integration.scripting.ScriptExecutor) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with ScriptExecutor

use of org.springframework.integration.scripting.ScriptExecutor in project spring-integration by spring-projects.

the class Jsr223ScriptExecutorTests method test.

@Test
public void test() {
    ScriptExecutor executor = ScriptExecutorFactory.getScriptExecutor("jruby");
    executor.executeScript(new StaticScriptSource("'hello, world'"));
    executor.executeScript(new StaticScriptSource("'hello, again'"));
    Map<String, Object> variables = new HashMap<String, Object>();
    Map<String, Object> headers = new HashMap<String, Object>();
    headers.put("one", 1);
    headers.put("two", "two");
    headers.put("three", 3);
    variables.put("payload", "payload");
    variables.put("headers", headers);
    Resource resource = new ClassPathResource("/org/springframework/integration/scripting/jsr223/print_message.rb");
    String result = (String) executor.executeScript(new ResourceScriptSource(resource), variables);
    assertEquals("payload modified", result.substring(0, "payload modified".length()));
}
Also used : StaticScriptSource(org.springframework.scripting.support.StaticScriptSource) ResourceScriptSource(org.springframework.scripting.support.ResourceScriptSource) HashMap(java.util.HashMap) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ScriptExecutor(org.springframework.integration.scripting.ScriptExecutor) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 4 with ScriptExecutor

use of org.springframework.integration.scripting.ScriptExecutor in project spring-integration by spring-projects.

the class Jsr223ScriptExecutorTests method testJs.

@Test
public void testJs() {
    ScriptExecutor executor = ScriptExecutorFactory.getScriptExecutor("js");
    Object obj = executor.executeScript(new StaticScriptSource("function js(){ return 'js';} js();"));
    assertEquals("js", obj.toString());
}
Also used : StaticScriptSource(org.springframework.scripting.support.StaticScriptSource) ScriptExecutor(org.springframework.integration.scripting.ScriptExecutor) Test(org.junit.Test)

Example 5 with ScriptExecutor

use of org.springframework.integration.scripting.ScriptExecutor in project spring-integration by spring-projects.

the class Jsr223ScriptExecutorTests method testPython.

@Test
public void testPython() {
    ScriptExecutor executor = ScriptExecutorFactory.getScriptExecutor("python");
    Object obj = executor.executeScript(new StaticScriptSource("x=2"));
    assertEquals(2, obj);
    obj = executor.executeScript(new StaticScriptSource("def foo(y):\n\tx=y\n\treturn y\nz=foo(2)"));
    assertEquals(2, obj);
}
Also used : StaticScriptSource(org.springframework.scripting.support.StaticScriptSource) ScriptExecutor(org.springframework.integration.scripting.ScriptExecutor) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)5 ScriptExecutor (org.springframework.integration.scripting.ScriptExecutor)5 HashMap (java.util.HashMap)3 ResourceScriptSource (org.springframework.scripting.support.ResourceScriptSource)3 StaticScriptSource (org.springframework.scripting.support.StaticScriptSource)3 FileSystemResource (org.springframework.core.io.FileSystemResource)2 Ignore (org.junit.Ignore)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1 Resource (org.springframework.core.io.Resource)1