Fixing exclude filter bug & adding query cache flushing
[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.HashMap;
5 import java.util.List;
6 import java.util.Map;
7
8 import org.junit.runner.Description;
9 import org.junit.runner.notification.Failure;
10 import org.junit.runner.notification.RunNotifier;
11 import org.junit.runners.ParentRunner;
12 import org.junit.runners.model.InitializationError;
13 import org.simantics.scl.compiler.module.coverage.CombinedCoverage;
14 import org.simantics.tests.modelled.utils.ModelledSTSSuite;
15 import org.simantics.tests.modelled.utils.ModelledSTSTest;
16 import org.simantics.tests.modelled.utils.ModelledSTSTest.CommandSessionVariable;
17
18 public class ModelledSTSSuiteRunner extends ParentRunner<ModelledSTSTestRunner> {
19
20     private final ModelledSTSSuite suite;
21     private final List<ModelledSTSTestRunner> children;
22     private Map<String, List<CommandSessionVariable>> storedVariables = new HashMap<>();
23
24     public ModelledSTSSuiteRunner(ModelledSTSSuite suite) throws InitializationError {
25         super(ModelledSTSSuiteRunner.class);
26         this.suite = suite;
27         this.children = new ArrayList<>(suite.getChildren().size());
28         
29         // Do possible filtering
30         // Filter exclusions
31         String exclusionFilter = System.getProperty(ModelledSTSRunner.EXCLUSION_FILTER);
32         // Filter inclusions
33         String inclusionFilter = System.getProperty(ModelledSTSRunner.INCLUSION_FILTER);
34         for (ModelledSTSTest test : suite.getSortedChildren()) {
35             boolean add = true;
36             if (exclusionFilter != null) {
37                 String[] filters = exclusionFilter.split(",");
38                 if (startsWithAny(test, filters)) {
39                     add = false;
40                 }
41             }
42             if (inclusionFilter != null) {
43                 String[] filters = inclusionFilter.split(",");
44                 if (!startsWithAny(test, filters)) {
45                     add = false;
46                 }
47             }
48             if (add)
49                 children.add(new ModelledSTSTestRunner(test));
50         }
51     }
52
53     private static boolean startsWithAny(ModelledSTSTest test, String[] filters) {
54         for (String filter : filters) {
55             if (test.getName().contains(filter)) {
56                 return true;
57             }
58         }
59         return false;
60     }
61     
62     @Override
63     protected String getName() {
64         return suite.getName();
65     }
66
67     @Override
68     protected List<ModelledSTSTestRunner> getChildren() {
69         return children;
70     }
71
72     @Override
73     protected Description describeChild(ModelledSTSTestRunner child) {
74         return child.getDescription();
75     }
76
77     @Override
78     protected void runChild(ModelledSTSTestRunner child, RunNotifier notifier) {
79         Description description = describeChild(child);
80         if (isIgnored(child)) {
81             notifier.fireTestIgnored(description);
82         } else {
83             try {
84                 List<CommandSessionVariable> variables = new ArrayList<>();
85                 for (String dep : child.getTest().getDependencies()) {
86                     List<CommandSessionVariable> storedVars = storedVariables.get(dep);
87                     if (storedVars != null) {
88                         variables.addAll(storedVars);
89                     }
90                 }
91                 notifier.fireTestStarted(description);
92                 List<CommandSessionVariable> newVars = child.runWithVars(variables);
93 //                TODO: FIX THIS BY NOT ADDING ALL VARIABLES TO MEMORY (CAN BE HUGE)
94 //                storedVariables.put(child.getTest().getName(), newVars);
95                 notifier.fireTestFinished(description);
96             } catch (Throwable e) {
97                 notifier.fireTestFailure(new Failure(description, e));
98             }
99         } 
100     }
101
102     @Override
103     protected boolean isIgnored(ModelledSTSTestRunner child) {
104         return child.isIgnored();
105     }
106
107     public CombinedCoverage getCoverage() {
108         return suite.getCoverage();
109     }
110
111 }