Skip to content

Commit a913673

Browse files
committed
Add Java unit tests for the execute script and command actions
1 parent 3fd0de6 commit a913673

File tree

3 files changed

+161
-0
lines changed

3 files changed

+161
-0
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>
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+
}

0 commit comments

Comments
 (0)