1 package org.simantics.tests.modelled.utils;
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;
10 import java.util.regex.Pattern;
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;
30 public class STSSuiteTestCollector {
32 private static final Logger LOGGER = LoggerFactory.getLogger(STSSuiteTestCollector.class);
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
40 public static Collection<ModelledSTSSuite> collectTests() throws DatabaseException {
41 Collection<ModelledSTSSuite> suitess = Simantics.getSession().syncRequest(new UniqueRead<Collection<ModelledSTSSuite>>() {
44 public Collection<ModelledSTSSuite> perform(ReadGraph graph) throws DatabaseException {
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) {
58 Collection<Resource> tests = graph.syncRequest(new ObjectsWithType(stsSuite, L0.ConsistsOf, TESTS.STSTest));
62 List<ModelledSTSTest> testRunners = new ArrayList<>(tests.size());
63 for (Resource test : tests)
64 testRunners.add(toModelledTest(graph, test));
66 suites.add(toModelledSuite(graph, stsSuite, testRunners));
67 } catch (Exception e) {
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);
87 String dependencies = graph.getPossibleRelatedValue(test, TESTS.dependencies, Bindings.STRING);
88 String[] actualDeps = dependencies.isEmpty() ? new String[0] : dependencies.split(",");
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);
98 Resource parent = graph.getSingleObject(test, L0.PartOf);
100 String possibleURI = graph.getPossibleURI(parent);
101 if (possibleURI != null)
102 parentName = possibleURI;
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);
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);
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);
122 return new ModelledSTSSuite(suiteName, children, moduleNameFilter, priority != null ? priority : -1, variables);
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);
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());
144 test.setCoverage(CoverageUtils.getCoverage(modules));
145 suite.addCoverage(modules);
146 CoverageUtils.resetCoverage(modules);