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