Search in sources :

Example 26 with CompiledMangoJavaScript

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());
    }
}
Also used : HashMap(java.util.HashMap) IDataPointValueSource(com.serotonin.m2m2.rt.dataImage.IDataPointValueSource) CompiledMangoJavaScript(com.infiniteautomation.mango.util.script.CompiledMangoJavaScript) IOException(java.io.IOException) Test(org.junit.Test)

Example 27 with CompiledMangoJavaScript

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());
    }
}
Also used : StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) IDataPointValueSource(com.serotonin.m2m2.rt.dataImage.IDataPointValueSource) CompiledMangoJavaScript(com.infiniteautomation.mango.util.script.CompiledMangoJavaScript) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 28 with CompiledMangoJavaScript

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());
    }
}
Also used : HashMap(java.util.HashMap) MangoJavaScriptResult(com.infiniteautomation.mango.util.script.MangoJavaScriptResult) IOException(java.io.IOException) StringWriter(java.io.StringWriter) IDataPointValueSource(com.serotonin.m2m2.rt.dataImage.IDataPointValueSource) IDataPoint(com.serotonin.m2m2.vo.IDataPoint) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) CompiledMangoJavaScript(com.infiniteautomation.mango.util.script.CompiledMangoJavaScript) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Aggregations

CompiledMangoJavaScript (com.infiniteautomation.mango.util.script.CompiledMangoJavaScript)28 IDataPointValueSource (com.serotonin.m2m2.rt.dataImage.IDataPointValueSource)24 HashMap (java.util.HashMap)24 IOException (java.io.IOException)20 Test (org.junit.Test)18 MangoJavaScriptResult (com.infiniteautomation.mango.util.script.MangoJavaScriptResult)13 PrintWriter (java.io.PrintWriter)13 StringWriter (java.io.StringWriter)13 IDataPoint (com.serotonin.m2m2.vo.IDataPoint)12 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)11 ResultTypeException (com.serotonin.m2m2.rt.script.ResultTypeException)10 ScriptError (com.serotonin.m2m2.rt.script.ScriptError)10 Matcher (java.util.regex.Matcher)9 Pattern (java.util.regex.Pattern)9 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)8 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)8 ScriptLog (com.serotonin.m2m2.rt.script.ScriptLog)8 ScriptPermissionsException (com.serotonin.m2m2.rt.script.ScriptPermissionsException)8 File (java.io.File)8 IntStringPair (com.serotonin.db.pair.IntStringPair)6