]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.tests.modelled/src/org/simantics/tests/modelled/junit/RuntimeTestCollector.java
Merge changes Ib64cf026,I238948da
[simantics/platform.git] / bundles / org.simantics.tests.modelled / src / org / simantics / tests / modelled / junit / RuntimeTestCollector.java
1 package org.simantics.tests.modelled.junit;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.HashSet;
7 import java.util.List;
8 import java.util.Set;
9
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;
24
25 @Deprecated
26 public class RuntimeTestCollector {
27
28     private static final Logger LOGGER = LoggerFactory.getLogger(RuntimeTestCollector.class);
29     
30     /**
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
33      * 
34      */
35     private static Collection<RuntimeSTSSuiteRunner> collectTestsFromGraph() {
36         try {
37             Collection<RuntimeSTSSuiteRunner> suitess = Simantics.getSession().syncRequest(new UniqueRead<Collection<RuntimeSTSSuiteRunner>>() {
38
39                 @Override
40                 public Collection<RuntimeSTSSuiteRunner> perform(ReadGraph graph) throws DatabaseException {
41                     
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) {
53                             try {
54                                 String suiteName = graph.getURI(stsSuite);
55     
56                                 Collection<Resource> tests = graph.syncRequest(new ObjectsWithType(stsSuite, L0.ConsistsOf, TESTS.STSTest));
57                                 if (tests.isEmpty())
58                                     continue;
59     
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);
68                                 }
69                                 
70                                 testRunners.sort((test1, test2) -> {
71                                     if (test1.getPriority() < test2.getPriority())
72                                         return -1;
73                                     else if (test1.getPriority() > test2.getPriority())
74                                         return 1;
75                                     else return AlphanumComparator.COMPARATOR.compare(test1.getName(), test2.getName());
76                                 });
77                                 suite.addChildren(testRunners);
78                                 suites.add(suite);
79                             } catch (Exception e) {
80                                 e.printStackTrace();
81                             }
82                         }
83                     }
84                     return suites;
85                 }
86             });
87             return suitess;
88         } catch (DatabaseException e) {
89             e.printStackTrace();
90             return Collections.emptyList();
91         }
92     }
93
94     public static List<RuntimeSTSSuiteRunner> collectTests() {
95         return new ArrayList<>(collectTestsFromGraph());
96     }
97 }