|
| 1 | +/* |
| 2 | + * Copyright (C) 2024 European Spallation Source ERIC. |
| 3 | + */ |
| 4 | +package org.csstudio.display.actions; |
| 5 | + |
| 6 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 7 | +import static org.junit.jupiter.api.Assertions.fail; |
| 8 | + |
| 9 | +import java.io.ByteArrayInputStream; |
| 10 | +import java.io.File; |
| 11 | +import java.io.FileOutputStream; |
| 12 | +import java.io.InputStream; |
| 13 | +import java.nio.charset.Charset; |
| 14 | +import java.nio.file.Files; |
| 15 | +import java.nio.file.Path; |
| 16 | + |
| 17 | +import org.csstudio.display.builder.model.DisplayModel; |
| 18 | +import org.csstudio.display.builder.model.persist.ModelReader; |
| 19 | +import org.csstudio.display.builder.model.persist.ModelWriter; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +/** |
| 23 | + * Test Execute Script actions |
| 24 | + * |
| 25 | + * @author Becky Auger-Williams |
| 26 | + */ |
| 27 | +public class ExecuteScriptTest { |
| 28 | + |
| 29 | + private static final String TMP_OUTFILE = "/tmp/outfile.bob"; |
| 30 | + |
| 31 | + @Test |
| 32 | + public void execute_embedded_javascript() { |
| 33 | + String xml = "<display typeId=\"org.csstudio.opibuilder.Display\" version=\"1.0.0\">" |
| 34 | + + "<widget typeId=\"org.csstudio.opibuilder.widgets.ActionButton\" version=\"2.0.0\">" |
| 35 | + + "<actions hook=\"false\" hook_all=\"true\"><action type=\"EXECUTE_JAVASCRIPT\"><path></path>" |
| 36 | + + "<scriptText><![CDATA[importPackage(Packages.org.csstudio.opibuilder.scriptUtil);]]></scriptText>" |
| 37 | + + "<embedded>true</embedded><description></description></action></actions>" |
| 38 | + + "</widget></display>"; |
| 39 | + |
| 40 | + InputStream stream = new ByteArrayInputStream(xml.getBytes(Charset.forName("UTF-8"))); |
| 41 | + try (FileOutputStream outStream = new FileOutputStream(TMP_OUTFILE); |
| 42 | + ModelWriter writer = new ModelWriter(outStream);) { |
| 43 | + |
| 44 | + ModelReader reader = new ModelReader(stream); |
| 45 | + DisplayModel model = reader.readModel(); |
| 46 | + assertTrue(model.isClean()); |
| 47 | + |
| 48 | + writer.writeModel(model); |
| 49 | + |
| 50 | + // Check the model gets written correctly |
| 51 | + String file_content = Files.readString(Path.of(TMP_OUTFILE)).strip(); |
| 52 | + assertTrue(file_content.contains("<text><![CDATA[importPackage(Packages.org.csstudio.opibuilder.scriptUtil);]]></text>")); |
| 53 | + } catch (Exception e) { |
| 54 | + e.printStackTrace(); |
| 55 | + fail("Exception thrown" + e.getLocalizedMessage()); |
| 56 | + } |
| 57 | + // Test clean up |
| 58 | + File tmpfile = new File(TMP_OUTFILE); |
| 59 | + tmpfile.delete(); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void execute_embedded_pythonscript() { |
| 64 | + String xml = "<display typeId=\"org.csstudio.opibuilder.Display\" version=\"1.0.0\">" |
| 65 | + + "<widget typeId=\"org.csstudio.opibuilder.widgets.ActionButton\" version=\"2.0.0\">" |
| 66 | + + "<actions hook=\"false\" hook_all=\"true\"><action type=\"EXECUTE_PYTHONSCRIPT\"><path></path>" |
| 67 | + + "<scriptText><![CDATA[from org.csstudio.opibuilder.scriptUtil import PVUtil\n" |
| 68 | + + "]]></scriptText>" |
| 69 | + + "<embedded>true</embedded><description></description></action></actions>" |
| 70 | + + "</widget></display>"; |
| 71 | + |
| 72 | + InputStream stream = new ByteArrayInputStream(xml.getBytes(Charset.forName("UTF-8"))); |
| 73 | + try (FileOutputStream outStream = new FileOutputStream(TMP_OUTFILE); |
| 74 | + ModelWriter writer = new ModelWriter(outStream);) { |
| 75 | + |
| 76 | + ModelReader reader = new ModelReader(stream); |
| 77 | + DisplayModel model = reader.readModel(); |
| 78 | + assertTrue(model.isClean()); |
| 79 | + |
| 80 | + writer.writeModel(model); |
| 81 | + |
| 82 | + // Check the model gets written correctly |
| 83 | + String file_content = Files.readString(Path.of(TMP_OUTFILE)).strip(); |
| 84 | + assertTrue(file_content.contains("<text><![CDATA[from org.csstudio.opibuilder.scriptUtil import PVUtil\n" |
| 85 | + + "]]></text>")); |
| 86 | + } catch (Exception e) { |
| 87 | + e.printStackTrace(); |
| 88 | + fail("Exception thrown" + e.getLocalizedMessage()); |
| 89 | + } |
| 90 | + // Test clean up |
| 91 | + File tmpfile = new File(TMP_OUTFILE); |
| 92 | + tmpfile.delete(); |
| 93 | + } |
| 94 | +} |
0 commit comments