]> gerrit.simantics Code Review - simantics/platform.git/blob
10530e185b6dfcc833dc77ab5a262899cba76c40
[simantics/platform.git] /
1 package org.simantics.tests.modelled.utils;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.HashSet;
7 import java.util.List;
8 import java.util.regex.Pattern;
9
10 import org.simantics.Simantics;
11 import org.simantics.databoard.Bindings;
12 import org.simantics.db.ReadGraph;
13 import org.simantics.db.Resource;
14 import org.simantics.db.common.request.ObjectsWithType;
15 import org.simantics.db.common.request.UniqueRead;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.layer0.Layer0;
18 import org.simantics.modeling.ModelingUtils;
19 import org.simantics.scl.compiler.commands.CommandSession;
20 import org.simantics.scl.compiler.module.Module;
21 import org.simantics.scl.compiler.module.coverage.CoverageUtils;
22 import org.simantics.scl.compiler.runtime.RuntimeModule;
23 import org.simantics.scl.runtime.tuple.Tuple0;
24 import org.simantics.tests.modelled.ontology.TestsResource;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public class STSSuiteTestCollector {
29
30     private static final Logger LOGGER = LoggerFactory.getLogger(STSSuiteTestCollector.class);
31     
32     /**
33      * TODO: The idea of this class was to collect all the tests from shared libraries and construct
34      * JUnit tests out of them programmatically and then run them with JUnit to get results
35      * 
36      */
37     private static Collection<ModelledSTSSuite> collectTestsFromGraph() {
38         try {
39             Collection<ModelledSTSSuite> suitess = Simantics.getSession().syncRequest(new UniqueRead<Collection<ModelledSTSSuite>>() {
40
41                 @Override
42                 public Collection<ModelledSTSSuite> perform(ReadGraph graph) throws DatabaseException {
43                     
44                     List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
45                     if (LOGGER.isInfoEnabled())
46                         LOGGER.info("Found {} shared ontologies from graph",  sharedOntologies.size());
47                     Collection<ModelledSTSSuite> suites = new HashSet<>();
48                     TestsResource TESTS = TestsResource.getInstance(graph);
49                     Layer0 L0 = Layer0.getInstance(graph);
50                     for (Resource sharedOntology : sharedOntologies) {
51                         if (LOGGER.isInfoEnabled())
52                             LOGGER.info("Searching {} for modelled tests", graph.getURI(sharedOntology));
53                         List<Resource> stsSuites = ModelingUtils.searchByType(graph, sharedOntology, TESTS.STSSuite);
54                         for (Resource stsSuite : stsSuites) {
55                             try {
56                                 Collection<Resource> tests = graph.syncRequest(new ObjectsWithType(stsSuite, L0.ConsistsOf, TESTS.STSTest));
57                                 if (tests.isEmpty())
58                                     continue;
59     
60                                 List<ModelledSTSTest> testRunners = new ArrayList<>(tests.size());
61                                 for (Resource test : tests)
62                                     testRunners.add(toModelledTest(graph, test));
63
64                                 suites.add(toModelledSuite(graph, stsSuite, testRunners));
65                             } catch (Exception e) {
66                                 LOGGER.error("", e);
67                             }
68                         }
69                     }
70                     return suites;
71                 }
72             });
73             return suitess;
74         } catch (DatabaseException e) {
75             LOGGER.error("Could not find modelled tests", e);
76             return Collections.emptyList();
77         }
78     }
79     
80
81     public static ModelledSTSTest toModelledTest(ReadGraph graph, Resource test) throws DatabaseException {
82         TestsResource TESTS = TestsResource.getInstance(graph);
83         String testName = graph.getRelatedValue(test, Layer0.getInstance(graph).HasName, Bindings.STRING);
84         String code = graph.getRelatedValue(test, TESTS.STSTest_definition, Bindings.STRING);
85         Integer priority = graph.getPossibleRelatedValue(test, TESTS.STSTest_executionPriority, Bindings.INTEGER);
86         Boolean ignored = graph.getPossibleRelatedValue(test, TESTS.ignore, Bindings.BOOLEAN);
87         return new ModelledSTSTest(testName, code, priority != null ? priority : -1, ignored != null ? ignored : false);
88     }
89
90     public static ModelledSTSSuite toModelledSuite(ReadGraph graph, Resource suite, List<ModelledSTSTest> children) throws DatabaseException {
91         TestsResource TESTS = TestsResource.getInstance(graph);
92         String suiteName = graph.getURI(suite);
93         String moduleNameFilter = graph.getPossibleRelatedValue2(suite, TESTS.STSSuite_moduleNameFilter, Bindings.STRING);
94         return new ModelledSTSSuite(suiteName, children, moduleNameFilter);
95     }
96
97     public static void setTestCoverage(ModelledSTSTest test, CommandSession session) {
98         Collection<RuntimeModule> runtimeModules = session.getRuntimeEnvironment().getRuntimeModules();
99         List<Module> modules = new ArrayList<>(runtimeModules.size());
100         for (RuntimeModule module : runtimeModules)
101             modules.add(module.getModule());
102         test.setCoverage(CoverageUtils.getCoverage(modules));
103         CoverageUtils.resetCoverage(modules);
104     }
105     
106     public static List<ModelledSTSSuite> collectTests() {
107         return new ArrayList<>(collectTestsFromGraph());
108     }
109
110
111     public static void setSuiteCoverage(ModelledSTSTest test, ModelledSTSSuite suite, CommandSession session) {
112         Collection<RuntimeModule> runtimeModules = session.getRuntimeEnvironment().getRuntimeModules();
113         List<Module> modules = new ArrayList<>(runtimeModules.size());
114         for (RuntimeModule module : runtimeModules) {
115             for (Pattern p : suite.getModuleNameFilterPatterns()) {
116                 if (p.matcher(module.getModule().getName().toLowerCase()).find()) {
117                     modules.add(module.getModule());
118                 }
119             }
120         }
121         test.setCoverage(CoverageUtils.getCoverage(modules));
122         suite.addCoverage(modules);
123         CoverageUtils.resetCoverage(modules);
124     }
125 }