use of org.structr.schema.action.ActionContext in project structr by structr.
the class ScriptingTest method testIncludeJs.
@Test
public void testIncludeJs() {
final String script = "${{ Structr.includeJs('test'); }}\n";
try (final Tx tx = app.tx()) {
final ActionContext ctx = new ActionContext(securityContext, null);
// just run without an error, that's enough for this test
Scripting.evaluate(ctx, null, script, "test");
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception.");
}
}
use of org.structr.schema.action.ActionContext in project structr by structr.
the class ScriptingTest method testPropertyConversion.
@Test
public void testPropertyConversion() {
TestOne testOne = null;
// setup phase
try (final Tx tx = app.tx()) {
testOne = app.create(TestOne.class);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
// test phase, check value conversion
try (final Tx tx = app.tx()) {
final ActionContext actionContext = new ActionContext(securityContext);
Scripting.evaluate(actionContext, testOne, "${{ var e = Structr.get('this'); e.aString = 12; }}", "test");
assertEquals("Invalid scripted property conversion result", "12", testOne.getProperty(TestOne.aString));
Scripting.evaluate(actionContext, testOne, "${{ var e = Structr.get('this'); e.anInt = '12'; }}", "test");
assertEquals("Invalid scripted property conversion result", 12L, (long) testOne.getProperty(TestOne.anInt));
Scripting.evaluate(actionContext, testOne, "${{ var e = Structr.get('this'); e.aDouble = '12.2342'; }}", "test");
assertEquals("Invalid scripted property conversion result", 12.2342, (double) testOne.getProperty(TestOne.aDouble), 0.0);
Scripting.evaluate(actionContext, testOne, "${{ var e = Structr.get('this'); e.aDouble = 2; }}", "test");
assertEquals("Invalid scripted property conversion result", 2.0, (double) testOne.getProperty(TestOne.aDouble), 0.0);
Scripting.evaluate(actionContext, testOne, "${{ var e = Structr.get('this'); e.aLong = 2352343457252; }}", "test");
assertEquals("Invalid scripted property conversion result", 2352343457252L, (long) testOne.getProperty(TestOne.aLong));
Scripting.evaluate(actionContext, testOne, "${{ var e = Structr.get('this'); e.aBoolean = true; }}", "test");
assertEquals("Invalid scripted property conversion result", true, (boolean) testOne.getProperty(TestOne.aBoolean));
tx.success();
} catch (UnlicensedException | FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
}
use of org.structr.schema.action.ActionContext in project structr by structr.
the class ScriptingTest method testDateOutput.
@Test
public void testDateOutput() {
final Date now = new Date();
final SimpleDateFormat isoDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
try (final Tx tx = app.tx()) {
final ActionContext ctx = new ActionContext(securityContext, null);
// Copy dates with/without format in StructrScript
TestOne testOne = createTestNode(TestOne.class);
testOne.setProperty(TestOne.aDate, now);
final String expectedDateOutput = isoDateFormat.format(now);
final String dateOutput1 = Scripting.replaceVariables(ctx, testOne, "${this.aDate}");
final String dateOutput2 = Scripting.replaceVariables(ctx, testOne, "${print(this.aDate)}");
final String dateOutput3 = Scripting.replaceVariables(ctx, testOne, "${{Structr.print(Structr.this.aDate)}}");
assertEquals("${this.aDate} should yield ISO 8601 date format", expectedDateOutput, dateOutput1);
assertEquals("${print(this.aDate)} should yield ISO 8601 date format", expectedDateOutput, dateOutput2);
assertEquals("${Structr.print(Structr.this.aDate)} should yield ISO 8601 date format", expectedDateOutput, dateOutput3);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail(fex.getMessage());
}
}
use of org.structr.schema.action.ActionContext in project structr by structr.
the class ScriptingTest method testPython.
@Test
public void testPython() {
try (final Tx tx = app.tx()) {
final Principal testUser = createTestNode(Principal.class, "testuser");
final ActionContext ctx = new ActionContext(SecurityContext.getInstance(testUser, AccessMode.Backend));
// assertEquals("Invalid python scripting evaluation result", "Hello World from Python!\n", Scripting.evaluate(ctx, null, "${python{print \"Hello World from Python!\"}}"));
System.out.println(Scripting.evaluate(ctx, null, "${python{print(Structr.get('me').id)}}", "test"));
tx.success();
} catch (UnlicensedException | FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
}
Aggregations