package org.simantics.tests.modelled.utils; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.regex.Pattern; import org.simantics.Simantics; import org.simantics.databoard.Bindings; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.common.request.ObjectsWithType; import org.simantics.db.common.request.UniqueRead; import org.simantics.db.common.utils.CommonDBUtils; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.QueryIndexUtils; import org.simantics.layer0.Layer0; import org.simantics.modeling.ModelingUtils; import org.simantics.scl.compiler.commands.CommandSession; import org.simantics.scl.compiler.module.Module; import org.simantics.scl.compiler.module.coverage.CoverageUtils; import org.simantics.scl.compiler.runtime.RuntimeModule; import org.simantics.scl.runtime.tuple.Tuple0; import org.simantics.tests.modelled.ontology.TestsResource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class STSSuiteTestCollector { private static final Logger LOGGER = LoggerFactory.getLogger(STSSuiteTestCollector.class); /** * TODO: The idea of this class was to collect all the tests from shared libraries and construct * JUnit tests out of them programmatically and then run them with JUnit to get results * @throws DatabaseException * */ public static Collection collectTests() throws DatabaseException { Collection suitess = Simantics.getSession().syncRequest(new UniqueRead>() { @Override public Collection perform(ReadGraph graph) throws DatabaseException { List sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE); if (LOGGER.isInfoEnabled()) LOGGER.info("Found {} shared ontologies from graph", sharedOntologies.size()); Collection suites = new HashSet<>(); TestsResource TESTS = TestsResource.getInstance(graph); Layer0 L0 = Layer0.getInstance(graph); for (Resource sharedOntology : sharedOntologies) { if (LOGGER.isInfoEnabled()) LOGGER.info("Searching {} for modelled tests", graph.getURI(sharedOntology)); List stsSuites = QueryIndexUtils.searchByType(graph, sharedOntology, TESTS.STSSuite); for (Resource stsSuite : stsSuites) { try { Collection tests = graph.syncRequest(new ObjectsWithType(stsSuite, L0.ConsistsOf, TESTS.STSTest)); if (tests.isEmpty()) continue; List testRunners = new ArrayList<>(tests.size()); for (Resource test : tests) testRunners.add(toModelledTest(graph, test)); suites.add(toModelledSuite(graph, stsSuite, testRunners)); } catch (Exception e) { LOGGER.error("", e); } } } return suites; } }); return suitess; } public static void collectTestImports(ReadGraph graph, Resource resource, Set imports) throws DatabaseException { Layer0 L0 = Layer0.getInstance(graph); TestsResource TESTS = TestsResource.getInstance(graph); if(graph.isInstanceOf(resource, TESTS.STSTest) || graph.isInstanceOf(resource, TESTS.STSSuite)) { for(Resource module : CommonDBUtils.objectsWithType(graph, resource, L0.ConsistsOf, L0.SCLModule)) { String uri = graph.getPossibleURI(module); if(uri != null) imports.add(uri); } Resource parent = graph.getPossibleObject(resource, L0.PartOf); if(parent != null) collectTestImports(graph, parent, imports); } else if(graph.isInstanceOf(resource, L0.IndexRoot)) { Resource sclMain = CommonDBUtils.getPossibleChild(graph, resource, L0.SCLModule, "SCLMain"); if(sclMain != null) imports.add(graph.getURI(sclMain)); } } public static ModelledSTSTest toModelledTest(ReadGraph graph, Resource test) throws DatabaseException { Layer0 L0 = Layer0.getInstance(graph); TestsResource TESTS = TestsResource.getInstance(graph); String testName = graph.getRelatedValue(test, L0.HasName, Bindings.STRING); String code = graph.getRelatedValue(test, TESTS.STSTest_definition, Bindings.STRING); Integer priority = graph.getPossibleRelatedValue(test, TESTS.STSTest_executionPriority, Bindings.INTEGER); Boolean ignored = graph.getPossibleRelatedValue(test, TESTS.ignore, Bindings.BOOLEAN); String dependencies = graph.getPossibleRelatedValue(test, TESTS.dependencies, Bindings.STRING); String[] actualDeps = dependencies.isEmpty() ? new String[0] : dependencies.split(","); // collect variables Collection stsVariables = CommonDBUtils.objectsWithType(graph, test, L0.ConsistsOf, TESTS.STSVariable); Map variables = new HashMap<>(stsVariables.size()); for (Resource stsVariable : stsVariables) { String name = graph.getRelatedValue(stsVariable, L0.HasName, Bindings.STRING); String value = graph.getRelatedValue(stsVariable, TESTS.STSVariable_definition); variables.put(name, value); } Set imports = new HashSet<>(); collectTestImports(graph, test, imports); Resource parent = graph.getSingleObject(test, L0.PartOf); String parentName; String possibleURI = graph.getPossibleURI(parent); if (possibleURI != null) parentName = possibleURI; else parentName = graph.getRelatedValue2(parent, L0.HasName, Bindings.STRING); return new ModelledSTSTest(testName, parentName, code, priority != null ? priority : -1, ignored != null ? ignored : false, imports, new HashSet<>(Arrays.asList(actualDeps)), variables); } public static ModelledSTSSuite toModelledSuite(ReadGraph graph, Resource suite, List children) throws DatabaseException { TestsResource TESTS = TestsResource.getInstance(graph); String suiteName = graph.getURI(suite); String moduleNameFilter = graph.getPossibleRelatedValue2(suite, TESTS.STSSuite_moduleNameFilter, Bindings.STRING); Integer priority = graph.getPossibleRelatedValue2(suite, TESTS.STSTest_executionPriority, Bindings.INTEGER); Layer0 L0 = Layer0.getInstance(graph); Collection stsVariables = graph.sync(new ObjectsWithType(suite, L0.ConsistsOf, TESTS.STSVariable)); Map variables = new HashMap<>(stsVariables.size()); for (Resource stsVariable : stsVariables) { String name = graph.getRelatedValue(stsVariable, L0.HasName, Bindings.STRING); String value = graph.getRelatedValue(stsVariable, TESTS.STSVariable_definition); variables.put(name, value); } return new ModelledSTSSuite(suiteName, children, moduleNameFilter, priority != null ? priority : -1, variables); } public static void setTestCoverage(ModelledSTSTest test, CommandSession session) { Collection runtimeModules = session.getRuntimeEnvironment().getRuntimeModules(); List modules = new ArrayList<>(runtimeModules.size()); for (RuntimeModule module : runtimeModules) modules.add(module.getModule()); test.setCoverage(CoverageUtils.getCoverage(modules)); CoverageUtils.resetCoverage(modules); } public static void setSuiteCoverage(ModelledSTSTest test, ModelledSTSSuite suite, CommandSession session) { Collection runtimeModules = session.getRuntimeEnvironment().getRuntimeModules(); List modules = new ArrayList<>(runtimeModules.size()); for (RuntimeModule module : runtimeModules) { for (Pattern p : suite.getModuleNameFilterPatterns()) { if (p.matcher(module.getModule().getName().toLowerCase()).find()) { modules.add(module.getModule()); } } } test.setCoverage(CoverageUtils.getCoverage(modules)); suite.addCoverage(modules); CoverageUtils.resetCoverage(modules); } }