1 package org.simantics.tests.modelled.junit;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.HashSet;
10 import org.simantics.Simantics;
11 import org.simantics.databoard.Bindings;
12 import org.simantics.db.ReadGraph;
13 import org.simantics.db.Resource;
14 import org.simantics.db.common.request.ObjectsWithType;
15 import org.simantics.db.common.request.UniqueRead;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.layer0.Layer0;
18 import org.simantics.modeling.ModelingUtils;
19 import org.simantics.scl.runtime.tuple.Tuple0;
20 import org.simantics.tests.modelled.ontology.TestsResource;
21 import org.simantics.utils.strings.AlphanumComparator;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 public class RuntimeTestCollector {
28 private static final Logger LOGGER = LoggerFactory.getLogger(RuntimeTestCollector.class);
31 * TODO: The idea of this class was to collect all the tests from shared libraries and construct
32 * JUnit tests out of them programmatically and then run them with JUnit to get results
35 private static Collection<RuntimeSTSSuiteRunner> collectTestsFromGraph() {
37 Collection<RuntimeSTSSuiteRunner> suitess = Simantics.getSession().syncRequest(new UniqueRead<Collection<RuntimeSTSSuiteRunner>>() {
40 public Collection<RuntimeSTSSuiteRunner> perform(ReadGraph graph) throws DatabaseException {
42 List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
43 if (LOGGER.isInfoEnabled())
44 LOGGER.info("Found {} shared ontologies from graph", sharedOntologies.size());
45 Set<RuntimeSTSSuiteRunner> suites = new HashSet<>();
46 TestsResource TESTS = TestsResource.getInstance(graph);
47 Layer0 L0 = Layer0.getInstance(graph);
48 for (Resource sharedOntology : sharedOntologies) {
49 if (LOGGER.isInfoEnabled())
50 LOGGER.info("Searching {} for modelled tests", graph.getURI(sharedOntology));
51 List<Resource> stsSuites = ModelingUtils.searchByType(graph, sharedOntology, TESTS.STSSuite);
52 for (Resource stsSuite : stsSuites) {
54 String suiteName = graph.getURI(stsSuite);
56 Collection<Resource> tests = graph.syncRequest(new ObjectsWithType(stsSuite, L0.ConsistsOf, TESTS.STSTest));
60 RuntimeSTSSuiteRunner suite = new RuntimeSTSSuiteRunner(RuntimeSTSSuiteRunner.class, suiteName);
61 List<RuntimeSTSTestRunner> testRunners = new ArrayList<>();
62 for (Resource test : tests) {
63 String testName = graph.getRelatedValue(test, L0.HasName, Bindings.STRING);
64 String code = graph.getRelatedValue(test, TESTS.STSTest_definition, Bindings.STRING);
65 Integer priority = graph.getPossibleRelatedValue(test, TESTS.STSTest_executionPriority, Bindings.INTEGER);
66 RuntimeSTSTestRunner testCase = new RuntimeSTSTestRunner(testName, code, priority);
67 testRunners.add(testCase);
70 testRunners.sort((test1, test2) -> {
71 if (test1.getPriority() < test2.getPriority())
73 else if (test1.getPriority() > test2.getPriority())
75 else return AlphanumComparator.COMPARATOR.compare(test1.getName(), test2.getName());
77 suite.addChildren(testRunners);
79 } catch (Exception e) {
88 } catch (DatabaseException e) {
90 return Collections.emptyList();
94 public static List<RuntimeSTSSuiteRunner> collectTests() {
95 return new ArrayList<>(collectTestsFromGraph());