]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.tests.modelled.ui/src/org/simantics/tests/modelled/ui/STSTestSuiteModel.java
Ignore multiple modelled tests via context menu action
[simantics/platform.git] / bundles / org.simantics.tests.modelled.ui / src / org / simantics / tests / modelled / ui / STSTestSuiteModel.java
1 package org.simantics.tests.modelled.ui;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.stream.Collectors;
9
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;
30
31 public class STSTestSuiteModel {
32
33     private Map<String, List<CommandSessionVariable>> storedVars = new HashMap<>();
34     
35     class STSTest {
36         
37         private final ModelledSTSTest test;
38         
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<>();
45         
46         public STSTest(ModelledSTSTest test, STSSuite parent) {
47             this.test = test;
48             this.parent = parent;
49         }
50         
51         public String getName() {
52             return test.getName();
53         }
54         
55         public String getLabel() {
56             StringBuilder sb = new StringBuilder();
57             sb.append(getName());
58             if (executed || failed)
59                 sb.append(" (").append(duration).append(" ms)");
60             return sb.toString();
61         }
62         
63         public String getDefinition() {
64             return test.getCode();
65         }
66
67         public STSSuite getParent() {
68             return parent;
69         }
70
71         public void execute() {
72             isRunning = true;
73             long start = System.currentTimeMillis();
74             Object old = SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
75             try {
76                 if (parent != null)
77                     parent.startedCount++;
78
79                 SCLContext.getCurrent().put(SCLReportingHandler.REPORTING_HANDLER, new AbstractSCLReportingHandler() {
80                     
81                     @Override
82                     public void print(String text) {
83                         appendOutput(text + "\n");
84                     }
85                     
86                     @Override
87                     public void printCommand(String command) {
88                         appendOutput("> " + command + "\n");
89                     }
90                     
91                     @Override
92                     public void printError(String error) {
93                         appendOutput(error + "\n");
94                     }
95                 });
96                 List<CommandSessionVariable> resolvedVars = new ArrayList<>();
97                 for (String deps : test.getDependencies()) {
98                     List<CommandSessionVariable> vars = storedVars.get(deps);
99                     if (vars != null)
100                         resolvedVars.addAll(vars);
101                 }
102                 
103                 List<CommandSessionVariable> vars = test.run(resolvedVars);
104                 storedVars.put(test.getName(), vars);
105                 
106                 executed = true;
107             } catch (Throwable t) {
108                 t.printStackTrace();
109                 if (parent != null)
110                     parent.failureCount++;
111                 failed = true;
112             } finally {
113                 isRunning = false;
114                 long end = System.currentTimeMillis();
115                 duration = end - start;
116                 
117                 SCLContext.getCurrent().put(SCLReportingHandler.REPORTING_HANDLER, old);
118             }
119         }
120
121         protected void appendOutput(String text) {
122             output.add(text);
123         }
124
125         public List<String> getOutput() {
126             return output;
127         }
128
129         public void setCoverage(CombinedCoverage coverage) {
130             test.setCoverage(coverage);
131         }
132         
133         public CombinedCoverage getCoverage() {
134             return test.getCoverage();
135         }
136
137         public int getPriority() {
138             return test.getPriority();
139         }
140
141         @Override
142         public String toString() {
143             return getName() + " [priority=" + getPriority() + ", executed=" + executed + ", duration=" + duration + "]";
144         }
145
146         public boolean isIgnored() {
147             return test.isIgnored();
148         }
149     }
150
151     class STSSuite {
152
153         private ModelledSTSSuite suite;
154         private STSTest[] children;
155         private int startedCount;
156         private int errorCount;
157         private int failureCount;
158         public int ignoredCount;
159         
160         public STSSuite(ModelledSTSSuite suite) {
161             this.suite = suite;
162         }
163
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()]);
167             return children;
168         }
169
170         public String getName() {
171             return suite.getName();
172         }
173
174         public String getLabel() {
175             StringBuilder sb = new StringBuilder();
176             sb.append(getName());
177             long totalTime = 0; 
178             if (getChildren() != null) {
179                 for (STSTest test : getChildren()) {
180                     if (test.executed || test.failed) {
181                         totalTime += test.duration;
182                     }
183                 }
184             }
185             if (totalTime != 0)
186                 sb.append(" (").append(totalTime).append(" ms)");
187             return sb.toString();
188         }
189
190         public boolean isRunning() {
191             if (getChildren() != null) {
192                 for (STSTest test: getChildren()) {
193                     if (test.isRunning) {
194                         return true;
195                     }
196                 }
197             }
198             return false;
199         }
200
201         public boolean executed() {
202             if (getChildren() != null) {
203                 for (STSTest test: getChildren()) {
204                     if (!test.executed) {
205                         return false;
206                     }
207                 }
208             }
209             return true;
210         }
211
212         public boolean failed() {
213             if (getChildren() != null) {
214                 for (STSTest test: getChildren()) {
215                     if (test.failed) {
216                         return true;
217                     }
218                 }
219             }
220             return false;
221         }
222
223         public CombinedCoverage getCoverage() {
224             return suite.getCoverage();
225         }
226     }
227
228     
229     private STSSuite suite;
230     private STSTest test;
231     private final List<STSExecutionListener> listeners = new ArrayList<>();
232     private Job currentJob;
233     
234     public STSTestSuiteModel() {
235     }
236     
237     public void addListener(STSExecutionListener listener) {
238         listeners.add(listener);
239     }
240     
241     public void removeListener(STSExecutionListener listener) {
242         listeners.remove(listener);
243     }
244
245     public Object[] getElements() {
246         if (suite != null)
247             return new Object[] {suite};
248         else if (test != null)
249             return new Object[] {test};
250         else return null;
251     }
252
253     public void execute() {
254         String command;
255         if (suite != null)
256             command = suite.getName();
257         else
258             command = test.getName();
259         if (currentJob != null)
260             currentJob.cancel();
261         currentJob = new Job(command) {
262             @Override
263             protected IStatus run(IProgressMonitor monitor) {
264                 if (suite != null) {
265                     executeSuite();
266                 } else if (test != null) {
267                     executeTest();
268                 }
269                 return Status.OK_STATUS;
270             }
271             
272             @Override
273             protected void canceling() {
274                 Thread thread = getThread();
275                 if(thread != null)
276                     thread.interrupt();
277                 
278                 try {
279                     Thread.sleep(1000);
280                 } catch (InterruptedException e) {
281                     e.printStackTrace();
282                 }
283                 
284                 thread = getThread();
285                 if(thread != null)
286                     thread.stop();
287             }
288         };
289         currentJob.schedule();
290     }
291     
292     public void interrupt() {
293         if (currentJob != null)
294             currentJob.cancel();
295     }
296     
297     private void testExecuted() {
298         listeners.forEach(listener -> {
299             listener.testExecuted();
300         });
301     }
302     
303     private void executeSuite() {
304         for (STSTest test : suite.getChildren()) {
305             if (test.isIgnored()) {
306                 testExecuted();
307                 test.getParent().ignoredCount++;
308                 continue;
309             }
310             test.execute();
311             testExecuted();
312         }
313     }
314
315     private void executeTest() {
316         test.execute();
317         testExecuted();
318     }
319
320     public boolean hasChildren(Object element) {
321         if (element instanceof STSTest) {
322             return false;
323         } else if (element instanceof STSSuite) {
324             STSSuite suite = (STSSuite) element;
325             return (suite.getChildren() != null ? suite.getChildren().length > 0 : false);
326         } else {
327             throw new IllegalArgumentException(element.toString());
328         }
329     }
330
331     public Object getParent(Object element) {
332         if (element instanceof STSTest) {
333             return ((STSTest) element).getParent();
334         } else if (element instanceof STSSuite) {
335             return null;
336         } else {
337             throw new IllegalArgumentException(element.toString());
338         }
339     }
340
341     public Object[] getChildren(Object parentElement) {
342         if (parentElement instanceof STSTest) {
343             return null;
344         } else if (parentElement instanceof STSSuite) {
345             STSSuite suite = (STSSuite) parentElement;
346             return suite.getChildren();
347         } else {
348             throw new IllegalArgumentException(parentElement.toString());
349         }
350     }
351
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();
357         else
358             throw new IllegalArgumentException(element.toString());
359     }
360
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;
370             else
371                 return STSTestSuiteProvider.suiteIcon;
372         } else if (element instanceof STSTest) {
373             STSTest test = (STSTest) element;
374             if (test.isRunning)
375                 return STSTestSuiteProvider.testRunningIcon;
376             else if (test.executed)
377                 return STSTestSuiteProvider.testOkIcon;
378             else if (test.failed)
379                 return STSTestSuiteProvider.testFailIcon;
380             else
381                 return STSTestSuiteProvider.testIcon;
382         }
383         return null;
384     }
385
386     public void updateInput(Resource root) {
387         suite = null;
388         test = null;
389         try {
390             Simantics.getSession().syncRequest(new ReadRequest() {
391                 
392                 @Override
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));
402                         
403                         suite = new STSSuite(STSSuiteTestCollector.toModelledSuite(graph, root, tests));
404                     } else {
405                         throw new IllegalArgumentException(root.toString());
406                     }
407                 }
408             });
409         } catch (DatabaseException e) {
410             e.printStackTrace();
411         }
412     }
413
414     public List<String> getOutput(Object element) {
415         if (element instanceof STSTest) {
416             STSTest test = (STSTest) element;
417             return test.getOutput();
418         }
419         return Collections.emptyList();
420     }
421
422     public int getStartedCount() {
423         if (suite != null)
424             return suite.startedCount;
425         else
426             return 0;
427     }
428
429     public int getIgnoredCount() {
430         if (suite != null)
431             return suite.ignoredCount;
432         return 0;
433     }
434
435     public int getTotalCount() {
436         if (suite != null && suite.getChildren() != null)
437             return suite.getChildren().length;
438         else if (test != null)
439             return 1;
440         else
441             return 0;
442     }
443
444     public int getErrorCount() {
445         if (suite != null)
446             return suite.errorCount;
447         return 0;
448     }
449
450     public int getFailureCount() {
451         if (suite != null)
452             return suite.failureCount;
453         return 0;
454     }
455
456     public int getAssumptionFailureCount() {
457         return 0;
458     }
459
460     public boolean isStopped() {
461         if (suite != null)
462             return !suite.isRunning();
463         else
464             return test.isRunning;
465     }
466 }
467