]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.tests.modelled/src/org/simantics/tests/modelled/junit/RuntimeTestCollector.java
Sync git svn branch with SVN repository r33364.
[simantics/platform.git] / bundles / org.simantics.tests.modelled / src / org / simantics / tests / modelled / junit / RuntimeTestCollector.java
diff --git a/bundles/org.simantics.tests.modelled/src/org/simantics/tests/modelled/junit/RuntimeTestCollector.java b/bundles/org.simantics.tests.modelled/src/org/simantics/tests/modelled/junit/RuntimeTestCollector.java
new file mode 100644 (file)
index 0000000..81d1ef7
--- /dev/null
@@ -0,0 +1,89 @@
+package org.simantics.tests.modelled.junit;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+import java.util.HashSet;\r
+import java.util.List;\r
+import java.util.Set;\r
+\r
+import org.junit.runner.Runner;\r
+import org.simantics.Simantics;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.ObjectsWithType;\r
+import org.simantics.db.common.request.UniqueRead;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.modeling.ModelingUtils;\r
+import org.simantics.scl.runtime.tuple.Tuple0;\r
+import org.simantics.tests.modelled.ontology.TestsResource;\r
+\r
+public class RuntimeTestCollector {\r
+\r
+    \r
+    /**\r
+     * TODO: The idea of this class was to collect all the tests from shared libraries and construct\r
+     * JUnit tests out of them programmatically and then run them with JUnit to get results\r
+     * \r
+     */\r
+    private static Collection<RuntimeSTSSuiteRunner> collectTestsFromGraph() {\r
+        try {\r
+            Collection<RuntimeSTSSuiteRunner> suitess = Simantics.getSession().syncRequest(new UniqueRead<Collection<RuntimeSTSSuiteRunner>>() {\r
+\r
+                @Override\r
+                public Collection<RuntimeSTSSuiteRunner> perform(ReadGraph graph) throws DatabaseException {\r
+                    \r
+                    List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);\r
+\r
+                    Set<RuntimeSTSSuiteRunner> suites = new HashSet<>();\r
+                    TestsResource TESTS = TestsResource.getInstance(graph);\r
+                    Layer0 L0 = Layer0.getInstance(graph);\r
+                    for (Resource sharedOntology : sharedOntologies) {\r
+                        List<Resource> stsSuites = ModelingUtils.searchByType(graph, sharedOntology, TESTS.STSSuite);\r
+                        for (Resource stsSuite : stsSuites) {\r
+                            try {\r
+                                String suiteName = graph.getURI(stsSuite);\r
+    \r
+                                Collection<Resource> tests = graph.syncRequest(new ObjectsWithType(stsSuite, L0.ConsistsOf, TESTS.STSTest));\r
+                                if (tests.isEmpty())\r
+                                    continue;\r
+    \r
+                                RuntimeSTSSuiteRunner suite = new RuntimeSTSSuiteRunner(suiteName);\r
+                                List<RuntimeSTSTestRunner> testRunners = new ArrayList<>();\r
+                                for (Resource test : tests) {\r
+                                    String testName = graph.getRelatedValue(test, L0.HasName, Bindings.STRING);\r
+                                    String code = graph.getRelatedValue(test, TESTS.STSTest_definition, Bindings.STRING);\r
+                                    Integer priority = graph.getPossibleRelatedValue(test, TESTS.STSTest_executionPriority, Bindings.INTEGER);\r
+                                    RuntimeSTSTestRunner testCase = new RuntimeSTSTestRunner(testName, code, priority);\r
+                                    testRunners.add(testCase);\r
+                                }\r
+                                \r
+                                testRunners.sort((test1, test2) -> {\r
+                                    if (test1.getPriority() <= test2.getPriority())\r
+                                        return -1;\r
+                                    else\r
+                                        return 1;\r
+                                });\r
+                                suite.addChildren(testRunners);\r
+                                suites.add(suite);\r
+                            } catch (Exception e) {\r
+                                e.printStackTrace();\r
+                            }\r
+                        }\r
+                    }\r
+                    return suites;\r
+                }\r
+            });\r
+            return suitess;\r
+        } catch (DatabaseException e) {\r
+            e.printStackTrace();\r
+            return Collections.emptyList();\r
+        }\r
+    }\r
+\r
+    public static List<Runner> collectTests() {\r
+        return new ArrayList<>(collectTestsFromGraph());\r
+    }\r
+}\r