Skip to content

Commit dcc692c

Browse files
authored
Merge pull request #3341 from rjwills28/opi_converter_fix
Fix conversion of OPI actions to execute scripts/commands into BOB
2 parents 7d8db27 + a913673 commit dcc692c

File tree

6 files changed

+173
-5
lines changed

6 files changed

+173
-5
lines changed

app/display/actions/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,11 @@
3131
<artifactId>app-display-model</artifactId>
3232
<version>4.7.4-SNAPSHOT</version>
3333
</dependency>
34+
<dependency>
35+
<groupId>org.junit.jupiter</groupId>
36+
<artifactId>junit-jupiter</artifactId>
37+
<version>${junit.version}</version>
38+
<scope>test</scope>
39+
</dependency>
3440
</dependencies>
3541
</project>

app/display/actions/src/main/java/org/csstudio/display/actions/ExecuteCommandAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public void writeToXML(ModelWriter modelWriter, XMLStreamWriter writer) throws E
9393
@Override
9494
public boolean matchesAction(String actionId) {
9595
return actionId.equalsIgnoreCase(EXECUTE_COMMAND) ||
96-
"EXECUTE_CMD".equalsIgnoreCase(type);
96+
actionId.equalsIgnoreCase("EXECUTE_CMD");
9797
}
9898

9999
public String getCommand() {

app/display/actions/src/main/java/org/csstudio/display/actions/ExecuteScriptAction.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public Image getImage() {
5858

5959
@Override
6060
public void readFromXML(ModelReader modelReader, Element actionXml) throws Exception {
61+
String type = actionXml.getAttribute(XMLTags.TYPE);
6162
if (type.equalsIgnoreCase(EXECUTE_SCRIPT)) {
6263
// <script file="EmbeddedPy">
6364
// <text> the embedded text </text>
@@ -105,10 +106,10 @@ public void writeToXML(ModelWriter modelWriter, XMLStreamWriter writer) throws E
105106
writer.writeAttribute(XMLTags.TYPE, EXECUTE_SCRIPT);
106107
writeDescriptionToXML(writer, description);
107108
writer.writeStartElement(XMLTags.SCRIPT);
108-
writer.writeAttribute(XMLTags.FILE, path);
109+
writer.writeAttribute(XMLTags.FILE, scriptInfo.getPath());
109110
// The controller updates the text and path not the scriptInfo
110-
if (this.path.equals(ScriptInfo.EMBEDDED_PYTHON) ||
111-
this.path.equals(ScriptInfo.EMBEDDED_JAVASCRIPT)) {
111+
if (scriptInfo.getPath().equals(ScriptInfo.EMBEDDED_PYTHON) ||
112+
scriptInfo.getPath().equals(ScriptInfo.EMBEDDED_JAVASCRIPT)) {
112113
final String text = this.text;
113114
if (text != null) {
114115
writer.writeStartElement(XMLTags.TEXT);
@@ -119,6 +120,13 @@ public void writeToXML(ModelWriter modelWriter, XMLStreamWriter writer) throws E
119120
writer.writeEndElement();
120121
}
121122

123+
@Override
124+
public boolean matchesAction(String actionId) {
125+
return actionId.equalsIgnoreCase(EXECUTE_SCRIPT) ||
126+
actionId.equalsIgnoreCase(EXECUTE_PYTHONSCRIPT) ||
127+
actionId.equalsIgnoreCase(EXECUTE_JAVASCRIPT);
128+
}
129+
122130
public ScriptInfo getScriptInfo() {
123131
return scriptInfo;
124132
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 ExecuteCommandTest {
28+
29+
private static final String TMP_OUTFILE = "/tmp/outfile.bob";
30+
31+
@Test
32+
public void execute_command() {
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_CMD\">"
36+
+ "<command>echo hello</command><command_directory>$(user.home)</command_directory>"
37+
+ "<wait_time>10</wait_time><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("<command>echo hello</command>"));
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+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
}

app/display/model/src/main/java/org/csstudio/display/builder/model/Converter.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ private void convert(final File infile, final File outfile) throws Exception {
175175
final ModelReader reader = new ModelReader(new FileInputStream(infile));
176176
DisplayModel model = reader.readModel();
177177
writer.writeModel(model);
178-
writer.close();
179178
} catch (Exception e) {
180179
throw e;
181180
}

0 commit comments

Comments
 (0)