29628cec061e8b4ef5117da84d45947524cd3893
[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.testing.common.AcornTests;
17 import org.simantics.tests.modelled.utils.ModelledSTSSuite;
18 import org.simantics.tests.modelled.utils.STSSuiteTestCollector;
19
20 public class ModelledSTSRunner extends ParentRunner<ModelledSTSSuiteRunner> {
21
22     public static final String EXCLUSION_FILTER = "org.simantics.tests.modelled.excludeFilter";
23     
24     private final List<ModelledSTSSuiteRunner> children;
25
26     public ModelledSTSRunner(Class<?> testClass) throws InitializationError {
27         super(testClass);
28         try {
29             initialize0();
30             Collection<ModelledSTSSuite> suites = STSSuiteTestCollector.collectTests();
31             String exclusionFilter = System.getProperty(EXCLUSION_FILTER);
32             Collection<ModelledSTSSuite> filtered;
33             if (exclusionFilter != null) {
34                 String[] filters = exclusionFilter.split(",");
35                 filtered = suites.stream().filter(s -> !startsWithAny(s, filters)).collect(Collectors.toList());
36             } else {
37                 filtered = suites;
38             }
39             // Sort by priority
40             List<ModelledSTSSuite> sorted = filtered.stream().sorted((o1, o2) -> Integer.compare(o1.getPriority(), o2.getPriority())).collect(Collectors.toList());
41             
42             
43             children = new ArrayList<>(sorted.size());
44             for (ModelledSTSSuite suite : sorted) {
45                 children.add(new ModelledSTSSuiteRunner(suite));
46             }
47         } catch (Exception e) {
48             throw new InitializationError(e);
49         }
50     }
51
52     private static boolean startsWithAny(ModelledSTSSuite suite, String[] filters) {
53         for (String filter : filters) {
54             if (suite.getName().contains(filter)) {
55                 return true;
56             }
57         }
58         return false;
59     }
60
61     @Override
62     protected List<ModelledSTSSuiteRunner> getChildren() {
63         return children;
64     }
65
66     @Override
67     protected Description describeChild(ModelledSTSSuiteRunner child) {
68         return child.getDescription();
69     }
70
71     @Override
72     public void run(RunNotifier notifier) {
73         notifier.addListener(new RunListener() {
74
75             @Override
76             public void testRunFinished(Result result) throws Exception {
77                 deinitialize0();
78             }
79         });
80         super.run(notifier);
81     }
82
83     @Override
84     protected void runChild(ModelledSTSSuiteRunner child, RunNotifier notifier) {
85         child.run(notifier);
86         // TODO: Add coverage reporting to ModelledSTSRunner
87 //        CombinedCoverage cover = child.getCoverage();
88 //        CoverageBuilder b = new CoverageBuilder();
89     }
90
91     public void initialize() throws InitializationError {
92     }
93
94     public void deinitialize() throws Exception {
95     }
96
97     private void initialize0() throws Exception {
98         AcornTests.newSimanticsWorkspace(null, null);
99         initialize();
100     }
101
102     private void deinitialize0() throws Exception {
103         deinitialize();
104         Simantics.shutdown(new NullProgressMonitor());
105     }
106 }