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