]> gerrit.simantics Code Review - simantics/platform.git/blob
06b81687ec025f1cc17a1bbf5cbb0dbf8155589f
[simantics/platform.git] /
1 package org.simantics.tests.modelled.junit;\r
2 \r
3 import java.util.ArrayList;\r
4 import java.util.Collection;\r
5 import java.util.Collections;\r
6 import java.util.HashSet;\r
7 import java.util.List;\r
8 import java.util.Set;\r
9 \r
10 import org.simantics.Simantics;\r
11 import org.simantics.databoard.Bindings;\r
12 import org.simantics.db.ReadGraph;\r
13 import org.simantics.db.Resource;\r
14 import org.simantics.db.common.request.ObjectsWithType;\r
15 import org.simantics.db.common.request.UniqueRead;\r
16 import org.simantics.db.exception.DatabaseException;\r
17 import org.simantics.layer0.Layer0;\r
18 import org.simantics.modeling.ModelingUtils;\r
19 import org.simantics.scl.runtime.tuple.Tuple0;\r
20 import org.simantics.tests.modelled.ontology.TestsResource;\r
21 \r
22 public class RuntimeTestCollector {\r
23 \r
24     \r
25     /**\r
26      * TODO: The idea of this class was to collect all the tests from shared libraries and construct\r
27      * JUnit tests out of them programmatically and then run them with JUnit to get results\r
28      * \r
29      */\r
30     private static Collection<RuntimeSTSSuiteRunner> collectTestsFromGraph() {\r
31         try {\r
32             Collection<RuntimeSTSSuiteRunner> suitess = Simantics.getSession().syncRequest(new UniqueRead<Collection<RuntimeSTSSuiteRunner>>() {\r
33 \r
34                 @Override\r
35                 public Collection<RuntimeSTSSuiteRunner> perform(ReadGraph graph) throws DatabaseException {\r
36                     \r
37                     List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);\r
38 \r
39                     Set<RuntimeSTSSuiteRunner> suites = new HashSet<>();\r
40                     TestsResource TESTS = TestsResource.getInstance(graph);\r
41                     Layer0 L0 = Layer0.getInstance(graph);\r
42                     for (Resource sharedOntology : sharedOntologies) {\r
43                         List<Resource> stsSuites = ModelingUtils.searchByType(graph, sharedOntology, TESTS.STSSuite);\r
44                         for (Resource stsSuite : stsSuites) {\r
45                             try {\r
46                                 String suiteName = graph.getURI(stsSuite);\r
47     \r
48                                 Collection<Resource> tests = graph.syncRequest(new ObjectsWithType(stsSuite, L0.ConsistsOf, TESTS.STSTest));\r
49                                 if (tests.isEmpty())\r
50                                     continue;\r
51     \r
52                                 RuntimeSTSSuiteRunner suite = new RuntimeSTSSuiteRunner(RuntimeSTSSuiteRunner.class, suiteName);\r
53                                 List<RuntimeSTSTestRunner> testRunners = new ArrayList<>();\r
54                                 for (Resource test : tests) {\r
55                                     String testName = graph.getRelatedValue(test, L0.HasName, Bindings.STRING);\r
56                                     String code = graph.getRelatedValue(test, TESTS.STSTest_definition, Bindings.STRING);\r
57                                     Integer priority = graph.getPossibleRelatedValue(test, TESTS.STSTest_executionPriority, Bindings.INTEGER);\r
58                                     RuntimeSTSTestRunner testCase = new RuntimeSTSTestRunner(testName, code, priority);\r
59                                     testRunners.add(testCase);\r
60                                 }\r
61                                 \r
62                                 testRunners.sort((test1, test2) -> {\r
63                                     if (test1.getPriority() <= test2.getPriority())\r
64                                         return -1;\r
65                                     else\r
66                                         return 1;\r
67                                 });\r
68                                 suite.addChildren(testRunners);\r
69                                 suites.add(suite);\r
70                             } catch (Exception e) {\r
71                                 e.printStackTrace();\r
72                             }\r
73                         }\r
74                     }\r
75                     return suites;\r
76                 }\r
77             });\r
78             return suitess;\r
79         } catch (DatabaseException e) {\r
80             e.printStackTrace();\r
81             return Collections.emptyList();\r
82         }\r
83     }\r
84 \r
85     public static List<RuntimeSTSSuiteRunner> collectTests() {\r
86         return new ArrayList<>(collectTestsFromGraph());\r
87     }\r
88 }\r