X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=bundles%2Forg.simantics.tests.modelled%2Fsrc%2Forg%2Fsimantics%2Ftests%2Fmodelled%2Futils%2FSTSSuiteTestCollector.java;fp=bundles%2Forg.simantics.tests.modelled%2Fsrc%2Forg%2Fsimantics%2Ftests%2Fmodelled%2Futils%2FSTSSuiteTestCollector.java;h=10530e185b6dfcc833dc77ab5a262899cba76c40;hb=f03893d9b643eae3f03debf7a656edbfa5b9b501;hp=0000000000000000000000000000000000000000;hpb=51006ffec13cbf8e0d9c8b07212d69478e4bdd4e;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.tests.modelled/src/org/simantics/tests/modelled/utils/STSSuiteTestCollector.java b/bundles/org.simantics.tests.modelled/src/org/simantics/tests/modelled/utils/STSSuiteTestCollector.java new file mode 100644 index 000000000..10530e185 --- /dev/null +++ b/bundles/org.simantics.tests.modelled/src/org/simantics/tests/modelled/utils/STSSuiteTestCollector.java @@ -0,0 +1,125 @@ +package org.simantics.tests.modelled.utils; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +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.exception.DatabaseException; +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 + * + */ + private static Collection collectTestsFromGraph() { + try { + 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 = ModelingUtils.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; + } catch (DatabaseException e) { + LOGGER.error("Could not find modelled tests", e); + return Collections.emptyList(); + } + } + + + public static ModelledSTSTest toModelledTest(ReadGraph graph, Resource test) throws DatabaseException { + TestsResource TESTS = TestsResource.getInstance(graph); + String testName = graph.getRelatedValue(test, Layer0.getInstance(graph).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); + return new ModelledSTSTest(testName, code, priority != null ? priority : -1, ignored != null ? ignored : false); + } + + 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); + return new ModelledSTSSuite(suiteName, children, moduleNameFilter); + } + + 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 List collectTests() { + return new ArrayList<>(collectTestsFromGraph()); + } + + + 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); + } +}