]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.tests.modelled/src/org/simantics/tests/modelled/junit/RuntimeTestCollector.java
Merge "(refs #7216) Removed OldTransferableGraph1 and all referring code"
[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
23 public class RuntimeTestCollector {
24
25     
26     /**
27      * TODO: The idea of this class was to collect all the tests from shared libraries and construct
28      * JUnit tests out of them programmatically and then run them with JUnit to get results
29      * 
30      */
31     private static Collection<RuntimeSTSSuiteRunner> collectTestsFromGraph() {
32         try {
33             Collection<RuntimeSTSSuiteRunner> suitess = Simantics.getSession().syncRequest(new UniqueRead<Collection<RuntimeSTSSuiteRunner>>() {
34
35                 @Override
36                 public Collection<RuntimeSTSSuiteRunner> perform(ReadGraph graph) throws DatabaseException {
37                     
38                     List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
39
40                     Set<RuntimeSTSSuiteRunner> suites = new HashSet<>();
41                     TestsResource TESTS = TestsResource.getInstance(graph);
42                     Layer0 L0 = Layer0.getInstance(graph);
43                     for (Resource sharedOntology : sharedOntologies) {
44                         List<Resource> stsSuites = ModelingUtils.searchByType(graph, sharedOntology, TESTS.STSSuite);
45                         for (Resource stsSuite : stsSuites) {
46                             try {
47                                 String suiteName = graph.getURI(stsSuite);
48     
49                                 Collection<Resource> tests = graph.syncRequest(new ObjectsWithType(stsSuite, L0.ConsistsOf, TESTS.STSTest));
50                                 if (tests.isEmpty())
51                                     continue;
52     
53                                 RuntimeSTSSuiteRunner suite = new RuntimeSTSSuiteRunner(RuntimeSTSSuiteRunner.class, suiteName);
54                                 List<RuntimeSTSTestRunner> testRunners = new ArrayList<>();
55                                 for (Resource test : tests) {
56                                     String testName = graph.getRelatedValue(test, L0.HasName, Bindings.STRING);
57                                     String code = graph.getRelatedValue(test, TESTS.STSTest_definition, Bindings.STRING);
58                                     Integer priority = graph.getPossibleRelatedValue(test, TESTS.STSTest_executionPriority, Bindings.INTEGER);
59                                     RuntimeSTSTestRunner testCase = new RuntimeSTSTestRunner(testName, code, priority);
60                                     testRunners.add(testCase);
61                                 }
62                                 
63                                 testRunners.sort((test1, test2) -> {
64                                     if (test1.getPriority() < test2.getPriority())
65                                         return -1;
66                                     else if (test1.getPriority() > test2.getPriority())
67                                         return 1;
68                                     else return AlphanumComparator.COMPARATOR.compare(test1.getName(), test2.getName());
69                                 });
70                                 suite.addChildren(testRunners);
71                                 suites.add(suite);
72                             } catch (Exception e) {
73                                 e.printStackTrace();
74                             }
75                         }
76                     }
77                     return suites;
78                 }
79             });
80             return suitess;
81         } catch (DatabaseException e) {
82             e.printStackTrace();
83             return Collections.emptyList();
84         }
85     }
86
87     public static List<RuntimeSTSSuiteRunner> collectTests() {
88         return new ArrayList<>(collectTestsFromGraph());
89     }
90 }