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 return new ModelledSTSTest(testName, code, priority != null ? priority : -1, ignored != null ? ignored : false, new HashSet<>(Arrays.asList(actualDeps)), variables);
101 public static ModelledSTSSuite toModelledSuite(ReadGraph graph, Resource suite, List<ModelledSTSTest> children) throws DatabaseException {
102 TestsResource TESTS = TestsResource.getInstance(graph);
103 String suiteName = graph.getURI(suite);
104 String moduleNameFilter = graph.getPossibleRelatedValue2(suite, TESTS.STSSuite_moduleNameFilter, Bindings.STRING);
106 Layer0 L0 = Layer0.getInstance(graph);
107 Collection<Resource> stsVariables = graph.sync(new ObjectsWithType(suite, L0.ConsistsOf, TESTS.STSVariable));
108 Map<String, String> variables = new HashMap<>(stsVariables.size());
109 for (Resource stsVariable : stsVariables) {
110 String name = graph.getRelatedValue(stsVariable, L0.HasName, Bindings.STRING);
111 String value = graph.getRelatedValue(stsVariable, TESTS.STSVariable_definition);
112 variables.put(name, value);
114 return new ModelledSTSSuite(suiteName, children, moduleNameFilter, variables);
117 public static void setTestCoverage(ModelledSTSTest test, CommandSession session) {
118 Collection<RuntimeModule> runtimeModules = session.getRuntimeEnvironment().getRuntimeModules();
119 List<Module> modules = new ArrayList<>(runtimeModules.size());
120 for (RuntimeModule module : runtimeModules)
121 modules.add(module.getModule());
122 test.setCoverage(CoverageUtils.getCoverage(modules));
123 CoverageUtils.resetCoverage(modules);
126 public static void setSuiteCoverage(ModelledSTSTest test, ModelledSTSSuite suite, CommandSession session) {
127 Collection<RuntimeModule> runtimeModules = session.getRuntimeEnvironment().getRuntimeModules();
128 List<Module> modules = new ArrayList<>(runtimeModules.size());
129 for (RuntimeModule module : runtimeModules) {
130 for (Pattern p : suite.getModuleNameFilterPatterns()) {
131 if (p.matcher(module.getModule().getName().toLowerCase()).find()) {
132 modules.add(module.getModule());
136 test.setCoverage(CoverageUtils.getCoverage(modules));
137 suite.addCoverage(modules);
138 CoverageUtils.resetCoverage(modules);