1 package org.simantics.tests.modelled.ui;
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashMap;
8 import java.util.stream.Collectors;
10 import org.eclipse.core.runtime.IProgressMonitor;
11 import org.eclipse.core.runtime.IStatus;
12 import org.eclipse.core.runtime.Status;
13 import org.eclipse.core.runtime.jobs.Job;
14 import org.eclipse.swt.graphics.Image;
15 import org.simantics.Simantics;
16 import org.simantics.db.ReadGraph;
17 import org.simantics.db.Resource;
18 import org.simantics.db.common.request.ReadRequest;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.layer0.Layer0;
21 import org.simantics.scl.compiler.module.coverage.CombinedCoverage;
22 import org.simantics.scl.runtime.SCLContext;
23 import org.simantics.scl.runtime.reporting.AbstractSCLReportingHandler;
24 import org.simantics.scl.runtime.reporting.SCLReportingHandler;
25 import org.simantics.tests.modelled.ontology.TestsResource;
26 import org.simantics.tests.modelled.utils.ModelledSTSSuite;
27 import org.simantics.tests.modelled.utils.ModelledSTSTest;
28 import org.simantics.tests.modelled.utils.ModelledSTSTest.CommandSessionVariable;
29 import org.simantics.tests.modelled.utils.STSSuiteTestCollector;
31 public class STSTestSuiteModel {
33 private Map<String, List<CommandSessionVariable>> storedVars = new HashMap<>();
37 private final ModelledSTSTest test;
39 private final STSSuite parent;
40 private boolean executed = false;
41 private long duration;
42 private boolean failed = false;
43 private boolean isRunning = false;
44 private List<String> output = new ArrayList<>();
46 public STSTest(ModelledSTSTest test, STSSuite parent) {
51 public String getName() {
52 return test.getName();
55 public String getLabel() {
56 StringBuilder sb = new StringBuilder();
58 if (executed || failed)
59 sb.append(" (").append(duration).append(" ms)");
63 public String getDefinition() {
64 return test.getCode();
67 public STSSuite getParent() {
71 public void execute() {
73 long start = System.currentTimeMillis();
74 Object old = SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
77 parent.startedCount++;
79 SCLContext.getCurrent().put(SCLReportingHandler.REPORTING_HANDLER, new AbstractSCLReportingHandler() {
82 public void print(String text) {
83 appendOutput(text + "\n");
87 public void printCommand(String command) {
88 appendOutput("> " + command + "\n");
92 public void printError(String error) {
93 appendOutput(error + "\n");
96 List<CommandSessionVariable> resolvedVars = new ArrayList<>();
97 for (String deps : test.getDependencies()) {
98 List<CommandSessionVariable> vars = storedVars.get(deps);
100 resolvedVars.addAll(vars);
103 List<CommandSessionVariable> vars = test.run(resolvedVars);
104 storedVars.put(test.getName(), vars);
107 } catch (Throwable t) {
110 parent.failureCount++;
114 long end = System.currentTimeMillis();
115 duration = end - start;
117 SCLContext.getCurrent().put(SCLReportingHandler.REPORTING_HANDLER, old);
121 protected void appendOutput(String text) {
125 public List<String> getOutput() {
129 public void setCoverage(CombinedCoverage coverage) {
130 test.setCoverage(coverage);
133 public CombinedCoverage getCoverage() {
134 return test.getCoverage();
137 public int getPriority() {
138 return test.getPriority();
142 public String toString() {
143 return getName() + " [priority=" + getPriority() + ", executed=" + executed + ", duration=" + duration + "]";
146 public boolean isIgnored() {
147 return test.isIgnored();
153 private ModelledSTSSuite suite;
154 private STSTest[] children;
155 private int startedCount;
156 private int errorCount;
157 private int failureCount;
158 public int ignoredCount;
160 public STSSuite(ModelledSTSSuite suite) {
164 public STSTest[] getChildren() {
165 if (children == null)
166 children = suite.getSortedChildren().stream().map(modelledTest -> new STSTest(modelledTest, this)).collect(Collectors.toList()).toArray(new STSTest[suite.getChildren().size()]);
170 public String getName() {
171 return suite.getName();
174 public String getLabel() {
175 StringBuilder sb = new StringBuilder();
176 sb.append(getName());
178 if (getChildren() != null) {
179 for (STSTest test : getChildren()) {
180 if (test.executed || test.failed) {
181 totalTime += test.duration;
186 sb.append(" (").append(totalTime).append(" ms)");
187 return sb.toString();
190 public boolean isRunning() {
191 if (getChildren() != null) {
192 for (STSTest test: getChildren()) {
193 if (test.isRunning) {
201 public boolean executed() {
202 if (getChildren() != null) {
203 for (STSTest test: getChildren()) {
204 if (!test.executed) {
212 public boolean failed() {
213 if (getChildren() != null) {
214 for (STSTest test: getChildren()) {
223 public CombinedCoverage getCoverage() {
224 return suite.getCoverage();
229 private STSSuite suite;
230 private STSTest test;
231 private final List<STSExecutionListener> listeners = new ArrayList<>();
232 private Job currentJob;
234 public STSTestSuiteModel() {
237 public void addListener(STSExecutionListener listener) {
238 listeners.add(listener);
241 public void removeListener(STSExecutionListener listener) {
242 listeners.remove(listener);
245 public Object[] getElements() {
247 return new Object[] {suite};
248 else if (test != null)
249 return new Object[] {test};
253 public void execute() {
256 command = suite.getName();
258 command = test.getName();
259 if (currentJob != null)
261 currentJob = new Job(command) {
263 protected IStatus run(IProgressMonitor monitor) {
266 } else if (test != null) {
269 return Status.OK_STATUS;
273 protected void canceling() {
274 Thread thread = getThread();
280 } catch (InterruptedException e) {
284 thread = getThread();
289 currentJob.schedule();
292 public void interrupt() {
293 if (currentJob != null)
297 private void testExecuted() {
298 listeners.forEach(listener -> {
299 listener.testExecuted();
303 private void executeSuite() {
304 for (STSTest test : suite.getChildren()) {
305 if (test.isIgnored()) {
307 test.getParent().ignoredCount++;
315 private void executeTest() {
320 public boolean hasChildren(Object element) {
321 if (element instanceof STSTest) {
323 } else if (element instanceof STSSuite) {
324 STSSuite suite = (STSSuite) element;
325 return (suite.getChildren() != null ? suite.getChildren().length > 0 : false);
327 throw new IllegalArgumentException(element.toString());
331 public Object getParent(Object element) {
332 if (element instanceof STSTest) {
333 return ((STSTest) element).getParent();
334 } else if (element instanceof STSSuite) {
337 throw new IllegalArgumentException(element.toString());
341 public Object[] getChildren(Object parentElement) {
342 if (parentElement instanceof STSTest) {
344 } else if (parentElement instanceof STSSuite) {
345 STSSuite suite = (STSSuite) parentElement;
346 return suite.getChildren();
348 throw new IllegalArgumentException(parentElement.toString());
352 public String getText(Object element) {
353 if (element instanceof STSTest)
354 return ((STSTest)element).getLabel();
355 else if (element instanceof STSSuite)
356 return ((STSSuite)element).getLabel();
358 throw new IllegalArgumentException(element.toString());
361 public Image getImage(Object element) {
362 if (element instanceof STSSuite) {
363 STSSuite suite = (STSSuite) element;
364 if (suite.isRunning())
365 return STSTestSuiteProvider.suiteRunningIcon;
366 else if (suite.executed())
367 return STSTestSuiteProvider.suiteOkIcon;
368 else if (suite.failed())
369 return STSTestSuiteProvider.suiteFailIcon;
371 return STSTestSuiteProvider.suiteIcon;
372 } else if (element instanceof STSTest) {
373 STSTest test = (STSTest) element;
375 return STSTestSuiteProvider.testRunningIcon;
376 else if (test.executed)
377 return STSTestSuiteProvider.testOkIcon;
378 else if (test.failed)
379 return STSTestSuiteProvider.testFailIcon;
381 return STSTestSuiteProvider.testIcon;
386 public void updateInput(Resource root) {
390 Simantics.getSession().syncRequest(new ReadRequest() {
393 public void run(ReadGraph graph) throws DatabaseException {
394 Layer0 L0 = Layer0.getInstance(graph);
395 TestsResource TESTS = TestsResource.getInstance(graph);
396 if (graph.isInstanceOf(root, TESTS.STSTest)) {
397 test = new STSTest(STSSuiteTestCollector.toModelledTest(graph, root), null);
398 } else if (graph.isInstanceOf(root, TESTS.STSSuite)) {
399 List<ModelledSTSTest> tests = new ArrayList<>();
400 for (Resource test : graph.getObjects(root, L0.ConsistsOf))
401 tests.add(STSSuiteTestCollector.toModelledTest(graph, test));
403 suite = new STSSuite(STSSuiteTestCollector.toModelledSuite(graph, root, tests));
405 throw new IllegalArgumentException(root.toString());
409 } catch (DatabaseException e) {
414 public List<String> getOutput(Object element) {
415 if (element instanceof STSTest) {
416 STSTest test = (STSTest) element;
417 return test.getOutput();
419 return Collections.emptyList();
422 public int getStartedCount() {
424 return suite.startedCount;
429 public int getIgnoredCount() {
431 return suite.ignoredCount;
435 public int getTotalCount() {
436 if (suite != null && suite.getChildren() != null)
437 return suite.getChildren().length;
438 else if (test != null)
444 public int getErrorCount() {
446 return suite.errorCount;
450 public int getFailureCount() {
452 return suite.failureCount;
456 public int getAssumptionFailureCount() {
460 public boolean isStopped() {
462 return !suite.isRunning();
464 return test.isRunning;