5b7a8ef0ac3d5b352052476055bd160b7c58531b
[simantics/platform.git] / bundles / org.simantics.tests.modelled / src / org / simantics / tests / modelled / junit / v2 / ModelledSTSSuiteRunner.java
1 package org.simantics.tests.modelled.junit.v2;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.Map;
6
7 import org.junit.runner.Description;
8 import org.junit.runner.notification.Failure;
9 import org.junit.runner.notification.RunNotifier;
10 import org.junit.runners.ParentRunner;
11 import org.junit.runners.model.InitializationError;
12 import org.simantics.scl.compiler.module.coverage.CombinedCoverage;
13 import org.simantics.tests.modelled.utils.ModelledSTSSuite;
14 import org.simantics.tests.modelled.utils.ModelledSTSTest;
15 import org.simantics.tests.modelled.utils.ModelledSTSTest.CommandSessionVariable;
16
17 public class ModelledSTSSuiteRunner extends ParentRunner<ModelledSTSTestRunner> {
18
19     private final ModelledSTSSuite suite;
20     private final List<ModelledSTSTestRunner> children;
21     private Map<String, List<CommandSessionVariable>> storedVariables;
22
23     public ModelledSTSSuiteRunner(ModelledSTSSuite suite) throws InitializationError {
24         super(ModelledSTSSuiteRunner.class);
25         this.suite = suite;
26         this.children = new ArrayList<>(suite.getChildren().size());
27         for (ModelledSTSTest test : suite.getSortedChildren()) {
28             children.add(new ModelledSTSTestRunner(test));
29         }
30     }
31
32     @Override
33     protected String getName() {
34         return suite.getName();
35     }
36
37     @Override
38     protected List<ModelledSTSTestRunner> getChildren() {
39         return children;
40     }
41
42     @Override
43     protected Description describeChild(ModelledSTSTestRunner child) {
44         return child.getDescription();
45     }
46
47     @Override
48     protected void runChild(ModelledSTSTestRunner child, RunNotifier notifier) {
49         Description description = describeChild(child);
50         if (isIgnored(child)) {
51             notifier.fireTestIgnored(description);
52         } else {
53             try {
54                 List<CommandSessionVariable> variables = new ArrayList<>();
55                 for (String dep : child.getTest().getDependencies()) {
56                     List<CommandSessionVariable> storedVars = storedVariables.get(dep);
57                     if (storedVars != null) {
58                         variables.addAll(storedVars);
59                     }
60                 }
61                 List<CommandSessionVariable> newVars = child.runWithVars(variables);
62                 storedVariables.put(child.getTest().getName(), newVars);
63                 notifier.fireTestFinished(description);
64             } catch (Exception e) {
65                 notifier.fireTestFailure(new Failure(description, e));
66             }
67         } 
68     }
69
70     @Override
71     protected boolean isIgnored(ModelledSTSTestRunner child) {
72         return child.isIgnored();
73     }
74
75     public CombinedCoverage getCoverage() {
76         return suite.getCoverage();
77     }
78
79 }