1 package org.simantics.tests.modelled.ui;
3 import java.io.BufferedReader;
4 import java.io.StringReader;
5 import java.util.ArrayList;
6 import java.util.Collections;
8 import java.util.regex.Pattern;
9 import java.util.stream.Collectors;
11 import org.eclipse.swt.graphics.Image;
12 import org.simantics.Simantics;
13 import org.simantics.db.ReadGraph;
14 import org.simantics.db.Resource;
15 import org.simantics.db.common.request.ReadRequest;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.layer0.Layer0;
18 import org.simantics.scl.compiler.commands.CommandSession;
19 import org.simantics.scl.compiler.commands.TestScriptExecutor;
20 import org.simantics.scl.compiler.module.coverage.CombinedCoverage;
21 import org.simantics.scl.compiler.module.options.ModuleCompilationOptions;
22 import org.simantics.scl.compiler.module.options.ModuleCompilationOptionsAdvisor;
23 import org.simantics.scl.compiler.module.repository.ModuleRepository;
24 import org.simantics.scl.osgi.SCLOsgi;
25 import org.simantics.scl.runtime.reporting.AbstractSCLReportingHandler;
26 import org.simantics.tests.modelled.ontology.TestsResource;
27 import org.simantics.tests.modelled.utils.ModelledSTSSuite;
28 import org.simantics.tests.modelled.utils.ModelledSTSTest;
29 import org.simantics.tests.modelled.utils.STSSuiteTestCollector;
31 public class STSTestSuiteModel {
33 static class STSTest {
35 private final ModelledSTSTest test;
37 private final STSSuite parent;
38 private boolean executed = false;
39 private long duration;
40 private boolean failed = false;
41 private boolean isRunning = false;
42 private List<String> output = new ArrayList<>();
44 public STSTest(ModelledSTSTest test, STSSuite parent) {
49 public String getName() {
50 return test.getName();
53 public String getLabel() {
54 StringBuilder sb = new StringBuilder();
56 if (executed || failed)
57 sb.append(" (").append(duration).append(" ms)");
61 public String getDefinition() {
62 return test.getCode();
65 public STSSuite getParent() {
69 public void execute(CommandSession session) {
72 TestScriptExecutor executor = new TestScriptExecutor(session, new BufferedReader(new StringReader(getDefinition())), new AbstractSCLReportingHandler() {
75 public void print(String text) {
76 appendOutput(text + "\n");
80 public void printCommand(String command) {
81 appendOutput("> " + command + "\n");
85 public void printError(String error) {
86 appendOutput(error + "\n");
89 long start = System.currentTimeMillis();
92 parent.startedCount++;
95 } catch (Throwable t) {
98 parent.failureCount++;
102 long end = System.currentTimeMillis();
103 duration = end - start;
108 protected void appendOutput(String text) {
112 public List<String> getOutput() {
116 public void setCoverage(CombinedCoverage coverage) {
117 test.setCoverage(coverage);
120 public CombinedCoverage getCoverage() {
121 return test.getCoverage();
124 public int getPriority() {
125 return test.getPriority();
129 public String toString() {
130 return getName() + " [priority=" + getPriority() + ", executed=" + executed + ", duration=" + duration + "]";
133 public boolean isIgnored() {
134 return test.isIgnored();
138 static class STSSuite {
140 private ModelledSTSSuite suite;
141 private STSTest[] children;
142 private int startedCount;
143 private int errorCount;
144 private int failureCount;
145 public int ignoredCount;
147 public STSSuite(ModelledSTSSuite suite) {
151 public STSTest[] getChildren() {
152 if (children == null)
153 children = suite.getSortedChildren().stream().map(modelledTest -> new STSTest(modelledTest, this)).collect(Collectors.toList()).toArray(new STSTest[suite.getChildren().size()]);
157 public String getName() {
158 return suite.getName();
161 public String getLabel() {
162 StringBuilder sb = new StringBuilder();
163 sb.append(getName());
165 if (getChildren() != null) {
166 for (STSTest test : getChildren()) {
167 if (test.executed || test.failed) {
168 totalTime += test.duration;
173 sb.append(" (").append(totalTime).append(" ms)");
174 return sb.toString();
177 public boolean isRunning() {
178 if (getChildren() != null) {
179 for (STSTest test: getChildren()) {
180 if (test.isRunning) {
188 public boolean executed() {
189 if (getChildren() != null) {
190 for (STSTest test: getChildren()) {
191 if (!test.executed) {
199 public boolean failed() {
200 if (getChildren() != null) {
201 for (STSTest test: getChildren()) {
210 public CombinedCoverage getCoverage() {
211 return suite.getCoverage();
216 private STSSuite suite;
217 private STSTest test;
218 private final List<STSExecutionListener> listeners = new ArrayList<>();
220 public STSTestSuiteModel() {
223 public void addListener(STSExecutionListener listener) {
224 listeners.add(listener);
227 public void removeListener(STSExecutionListener listener) {
228 listeners.remove(listener);
231 public Object[] getElements() {
233 return new Object[] {suite};
234 else if (test != null)
235 return new Object[] {test};
239 public void execute() {
241 ModuleRepository repo = new ModuleRepository(SCLOsgi.SOURCE_REPOSITORY);
243 repo.setAdvisor(new ModuleCompilationOptionsAdvisor() {
246 public ModuleCompilationOptions getOptions(String moduleName) {
247 boolean coverage = false;
248 for (Pattern p : suite.suite.getModuleNameFilterPatterns()) {
249 if (p.matcher(moduleName.toLowerCase()).find()) {
254 return new ModuleCompilationOptions(coverage);
258 CommandSession session = new CommandSession(repo, null);
260 executeSuite(session);
261 } else if (test != null) {
262 executeTest(session);
266 private void testExecuted() {
267 listeners.forEach(listener -> {
268 listener.testExecuted();
272 private void executeSuite(CommandSession session) {
273 for (STSTest test : suite.getChildren()) {
274 if (test.isIgnored()) {
276 test.getParent().ignoredCount++;
279 test.execute(session);
280 STSSuiteTestCollector.setSuiteCoverage(test.test, suite.suite, session);
285 private void executeTest(CommandSession session) {
286 test.execute(session);
288 STSSuiteTestCollector.setTestCoverage(test.test, session);
291 public boolean hasChildren(Object element) {
292 if (element instanceof STSTest) {
294 } else if (element instanceof STSSuite) {
295 STSSuite suite = (STSSuite) element;
296 return (suite.getChildren() != null ? suite.getChildren().length > 0 : false);
298 throw new IllegalArgumentException(element.toString());
302 public Object getParent(Object element) {
303 if (element instanceof STSTest) {
304 return ((STSTest) element).getParent();
305 } else if (element instanceof STSSuite) {
308 throw new IllegalArgumentException(element.toString());
312 public Object[] getChildren(Object parentElement) {
313 if (parentElement instanceof STSTest) {
315 } else if (parentElement instanceof STSSuite) {
316 STSSuite suite = (STSSuite) parentElement;
317 return suite.getChildren();
319 throw new IllegalArgumentException(parentElement.toString());
323 public String getText(Object element) {
324 if (element instanceof STSTest)
325 return ((STSTest)element).getLabel();
326 else if (element instanceof STSSuite)
327 return ((STSSuite)element).getLabel();
329 throw new IllegalArgumentException(element.toString());
332 public Image getImage(Object element) {
333 if (element instanceof STSSuite) {
334 STSSuite suite = (STSSuite) element;
335 if (suite.isRunning())
336 return STSTestSuiteProvider.suiteRunningIcon;
337 else if (suite.executed())
338 return STSTestSuiteProvider.suiteOkIcon;
339 else if (suite.failed())
340 return STSTestSuiteProvider.suiteFailIcon;
342 return STSTestSuiteProvider.suiteIcon;
343 } else if (element instanceof STSTest) {
344 STSTest test = (STSTest) element;
346 return STSTestSuiteProvider.testRunningIcon;
347 else if (test.executed)
348 return STSTestSuiteProvider.testOkIcon;
349 else if (test.failed)
350 return STSTestSuiteProvider.testFailIcon;
352 return STSTestSuiteProvider.testIcon;
357 public void updateInput(Resource root) {
361 Simantics.getSession().syncRequest(new ReadRequest() {
364 public void run(ReadGraph graph) throws DatabaseException {
365 Layer0 L0 = Layer0.getInstance(graph);
366 TestsResource TESTS = TestsResource.getInstance(graph);
367 if (graph.isInstanceOf(root, TESTS.STSTest)) {
368 test = new STSTest(STSSuiteTestCollector.toModelledTest(graph, root), null);
369 } else if (graph.isInstanceOf(root, TESTS.STSSuite)) {
370 List<ModelledSTSTest> tests = new ArrayList<>();
371 for (Resource test : graph.getObjects(root, L0.ConsistsOf))
372 tests.add(STSSuiteTestCollector.toModelledTest(graph, test));
374 suite = new STSSuite(STSSuiteTestCollector.toModelledSuite(graph, root, tests));
376 throw new IllegalArgumentException(root.toString());
380 } catch (DatabaseException e) {
385 public List<String> getOutput(Object element) {
386 if (element instanceof STSTest) {
387 STSTest test = (STSTest) element;
388 return test.getOutput();
390 return Collections.emptyList();
393 public int getStartedCount() {
395 return suite.startedCount;
400 public int getIgnoredCount() {
402 return suite.ignoredCount;
406 public int getTotalCount() {
407 if (suite != null && suite.getChildren() != null)
408 return suite.getChildren().length;
409 else if (test != null)
415 public int getErrorCount() {
417 return suite.errorCount;
421 public int getFailureCount() {
423 return suite.failureCount;
427 public int getAssumptionFailureCount() {
431 public boolean isStopped() {
433 return !suite.isRunning();
435 return test.isRunning;