use of com.infiniteautomation.mango.util.script.CompiledMangoJavaScript in project ma-core-public by infiniteautomation.
the class ScriptingTest method testScriptLogNullWriter.
@Test
public void testScriptLogNullWriter() {
String script = "LOG.trace('Trace message');";
script += "LOG.debug('Debug message');";
script += "LOG.info('Info message');";
script += "LOG.warn('Warn message');";
script += "LOG.error('Error message');";
script += "LOG.fatal('Fatal message');";
try {
Map<String, IDataPointValueSource> context = new HashMap<String, IDataPointValueSource>();
try (ScriptLog scriptLog = new ScriptLog("testNullWriter")) {
ScriptPointValueSetter setter = null;
CompiledMangoJavaScript compiled = new CompiledMangoJavaScript(setter, scriptLog, new ArrayList<>(), admin);
compiled.compile(script, true);
compiled.initialize(context);
compiled.execute(Common.timer.currentTimeMillis(), Common.timer.currentTimeMillis(), DataType.NUMERIC);
Assert.assertTrue(!scriptLog.getFile().exists());
}
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of com.infiniteautomation.mango.util.script.CompiledMangoJavaScript in project ma-core-public by infiniteautomation.
the class ScriptingTest method testScriptContextWriter.
@Test
public void testScriptContextWriter() {
String script = "print('testing context writer');";
try {
Map<String, IDataPointValueSource> context = new HashMap<String, IDataPointValueSource>();
final StringWriter scriptOut = new StringWriter();
final PrintWriter scriptWriter = new PrintWriter(scriptOut);
try (ScriptLog scriptLog = new ScriptLog("testContextWriter-", LogLevel.TRACE, scriptWriter)) {
ScriptPointValueSetter setter = null;
CompiledMangoJavaScript compiled = new CompiledMangoJavaScript(setter, scriptLog, new ArrayList<>(), admin);
compiled.compile(script, true);
compiled.initialize(context);
compiled.execute(Common.timer.currentTimeMillis(), Common.timer.currentTimeMillis(), DataType.NUMERIC);
String result = scriptOut.toString();
Assert.assertEquals("testing context writer" + System.lineSeparator(), result);
}
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of com.infiniteautomation.mango.util.script.CompiledMangoJavaScript in project ma-core-public by infiniteautomation.
the class ScriptingTest method testAnalogStatistics.
@Test
public void testAnalogStatistics() {
String script = "var a = p1.past(MINUTE,50);";
script += "return a.average;";
try {
Map<String, IDataPointValueSource> context = new HashMap<String, IDataPointValueSource>();
List<IDataPoint> vos = createMockDataPoints(1, true, new MangoPermission(), new MangoPermission());
ScriptContextVariable p1 = new ScriptContextVariable();
p1.setContextUpdate(true);
p1.setDataPointId(vos.get(0).getId());
p1.setVariableName("p1");
ScriptingTestPointValueRT p1Rt = new ScriptingTestPointValueRT((DataPointVO) vos.get(0));
context.put(p1.getVariableName(), p1Rt);
final StringWriter scriptOut = new StringWriter();
final PrintWriter scriptWriter = new PrintWriter(scriptOut);
try (ScriptLog scriptLog = new ScriptLog("testScriptLogger", LogLevel.TRACE, scriptWriter)) {
ScriptPointValueSetter setter = null;
CompiledMangoJavaScript compiled = new CompiledMangoJavaScript(setter, scriptLog, new ArrayList<>(), admin);
compiled.compile(script, true);
compiled.initialize(context);
MangoJavaScriptResult result = compiled.execute(Common.timer.currentTimeMillis(), Common.timer.currentTimeMillis(), DataType.NUMERIC);
PointValueTime pvt = (PointValueTime) result.getResult();
assertNotNull(pvt);
}
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
Aggregations