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