Fixing exclude filter bug & adding query cache flushing
[simantics/platform.git] / bundles / org.simantics.tests.modelled / src / org / simantics / tests / modelled / junit / v2 / ModelledSTSRunner.java
1 package org.simantics.tests.modelled.junit.v2;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.List;
6 import java.util.stream.Collectors;
7
8 import org.eclipse.core.runtime.NullProgressMonitor;
9 import org.junit.runner.Description;
10 import org.junit.runner.Result;
11 import org.junit.runner.notification.RunListener;
12 import org.junit.runner.notification.RunNotifier;
13 import org.junit.runners.ParentRunner;
14 import org.junit.runners.model.InitializationError;
15 import org.simantics.Simantics;
16 import org.simantics.db.layer0.util.Layer0Utils;
17 import org.simantics.db.testing.common.AcornTests;
18 import org.simantics.tests.modelled.utils.ModelledSTSSuite;
19 import org.simantics.tests.modelled.utils.STSSuiteTestCollector;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public class ModelledSTSRunner extends ParentRunner<ModelledSTSSuiteRunner> {
24
25     private static final Logger LOGGER = LoggerFactory.getLogger(ModelledSTSRunner.class);
26     
27     public static final String INCLUSION_FILTER = "org.simantics.tests.modelled.singleTestIncludeFilter";
28     public static final String EXCLUSION_FILTER = "org.simantics.tests.modelled.excludeFilter";
29     
30     private final List<ModelledSTSSuiteRunner> children;
31
32     public ModelledSTSRunner(Class<?> testClass) throws InitializationError {
33         super(testClass);
34
35         try {
36             initialize0();
37             Collection<ModelledSTSSuite> suites = STSSuiteTestCollector.collectTests();
38             
39             // Filter exclusions
40             String exclusionFilter = System.getProperty(EXCLUSION_FILTER);
41             Collection<ModelledSTSSuite> filtered;
42             if (exclusionFilter != null) {
43                 String[] filters = exclusionFilter.split(",");
44                 filtered = suites.stream().filter(s -> !startsWithAny(s, filters)).collect(Collectors.toList());
45             } else {
46                 filtered = suites;
47             }
48             // Filter inclusions
49 //            String inclusionFilter = System.getProperty(INCLUSION_FILTER);
50 //            Collection<ModelledSTSSuite> included;
51 //            if (inclusionFilter != null) {
52 //                String[] filters = inclusionFilter.split(",");
53 //                included = filtered.stream().filter(s -> startsWithAny(s, filters)).collect(Collectors.toList());
54 //            } else {
55 //                included = filtered;
56 //            }
57             
58             // Sort by priority
59 //            List<ModelledSTSSuite> sorted = included.stream().sorted((o1, o2) -> Integer.compare(o1.getPriority(), o2.getPriority())).collect(Collectors.toList());
60             List<ModelledSTSSuite> sorted = filtered.stream().sorted((o1, o2) -> Integer.compare(o1.getPriority(), o2.getPriority())).collect(Collectors.toList());
61             
62             
63             children = new ArrayList<>(sorted.size());
64             for (ModelledSTSSuite suite : sorted) {
65                 children.add(new ModelledSTSSuiteRunner(suite));
66             }
67         } catch (Exception e) {
68             throw new InitializationError(e);
69         }
70     }
71
72     private static boolean startsWithAny(ModelledSTSSuite suite, String[] filters) {
73         for (String filter : filters) {
74             if (suite.getName().contains(filter)) {
75                 return true;
76             }
77         }
78         return false;
79     }
80
81     @Override
82     protected List<ModelledSTSSuiteRunner> getChildren() {
83         return children;
84     }
85
86     @Override
87     protected Description describeChild(ModelledSTSSuiteRunner child) {
88         return child.getDescription();
89     }
90
91     @Override
92     public void run(RunNotifier notifier) {
93         notifier.addListener(new RunListener() {
94
95             @Override
96             public void testRunFinished(Result result) throws Exception {
97                 deinitialize0();
98             }
99         });
100         super.run(notifier);
101     }
102
103     @Override
104     protected void runChild(ModelledSTSSuiteRunner child, RunNotifier notifier) {
105         try {
106             child.run(notifier);
107         } finally {
108             // Clear query cache
109             Layer0Utils.queryDebugSupport("QueryControl.flush");
110         }
111         // TODO: Add coverage reporting to ModelledSTSRunner
112 //        CombinedCoverage cover = child.getCoverage();
113 //        CoverageBuilder b = new CoverageBuilder();
114     }
115
116     public void initialize() throws InitializationError {
117     }
118
119     public void deinitialize() throws Exception {
120     }
121
122     private void initialize0() throws Exception {
123         AcornTests.newSimanticsWorkspace(null, null);
124         org.simantics.debug.browser.internal.Activator.getDefault().startDebugServer();
125         initialize();
126     }
127
128     private void deinitialize0() throws Exception {
129         deinitialize();
130         org.simantics.debug.browser.internal.Activator.getDefault().stopDebugServer();
131         Simantics.shutdown(new NullProgressMonitor());
132     }
133 }