]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.tests.modelled/src/org/simantics/tests/modelled/TestsGraphUtils.java
Simantics integration STS
[simantics/platform.git] / bundles / org.simantics.tests.modelled / src / org / simantics / tests / modelled / TestsGraphUtils.java
index c5253bd5a37ad138d159b4b7b938ad3a159cbf6e..846261938c090de6f4eefc1724c6ed17a7e06eb3 100644 (file)
@@ -3,12 +3,14 @@ package org.simantics.tests.modelled;
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.StringReader;
+import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 import java.util.regex.Pattern;
 
 import org.simantics.databoard.Bindings;
+import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.WriteGraph;
 import org.simantics.db.common.utils.NameUtils;
@@ -27,49 +29,54 @@ import org.simantics.scl.osgi.SCLOsgi;
 import org.simantics.scl.runtime.reporting.SCLReportingHandler;
 import org.simantics.tests.modelled.ontology.TestsResource;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 public class TestsGraphUtils {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(TestsGraphUtils.class);
+
     private static final String STS_TEST_PREFIX = "STSTest";
     private static final String STS_SUITE_PREFIX = "STSSuite";
     private static final String STS_VARIABLE_PREFIX = "STSVariable";
-    
+
     private TestsGraphUtils() {}
-    
+
     public static Resource createSTSVariable(WriteGraph graph, Resource parent) throws DatabaseException {
         String name = NameUtils.findFreshEscapedName(graph, STS_VARIABLE_PREFIX, parent);
         Resource stsVariable = graph.newResource();
-        
+
         Layer0 L0 = Layer0.getInstance(graph);
         TestsResource TESTS = TestsResource.getInstance(graph);
-        
+
         graph.claim(parent, L0.ConsistsOf, L0.PartOf, stsVariable);
         graph.claim(stsVariable, L0.InstanceOf, TESTS.STSVariable);
         graph.claimLiteral(stsVariable, L0.HasName, name, Bindings.STRING);
         graph.claimLiteral(stsVariable, TESTS.STSVariable_definition, "", Bindings.STRING);
         return stsVariable;
     }
-    
+
     public static Resource createSTSTest(WriteGraph graph, Resource parent) throws DatabaseException {
         String name = NameUtils.findFreshEscapedName(graph, STS_TEST_PREFIX, parent);
         Resource stsTest = graph.newResource();
-        
+
         Layer0 L0 = Layer0.getInstance(graph);
         TestsResource TESTS = TestsResource.getInstance(graph);
-        
+
         graph.claim(parent, L0.ConsistsOf, L0.PartOf, stsTest);
         graph.claim(stsTest, L0.InstanceOf, TESTS.STSTest);
         graph.claimLiteral(stsTest, L0.HasName, name, Bindings.STRING);
         graph.claimLiteral(stsTest, TESTS.STSTest_definition, "", Bindings.STRING);
         return stsTest;
     }
-    
+
     public static Resource createSTSSuite(WriteGraph graph, Resource parent) throws DatabaseException {
         String name = NameUtils.findFreshEscapedName(graph, STS_SUITE_PREFIX, parent);
         Resource stsSuite = graph.newResource();
-        
+
         Layer0 L0 = Layer0.getInstance(graph);
         TestsResource TESTS = TestsResource.getInstance(graph);
-        
+
         graph.claim(parent, L0.ConsistsOf, L0.PartOf, stsSuite);
         graph.claim(stsSuite, L0.InstanceOf, TESTS.STSSuite);
         graph.claimLiteral(stsSuite, L0.HasName, name, Bindings.STRING);
@@ -77,11 +84,11 @@ public class TestsGraphUtils {
     }
 
     public static CombinedCoverage runSTSTestDefinition(String definition, List<Pattern> moduleNameFilters, SCLReportingHandler handler) throws IOException {
-        
+
 //        ModuleRepository repo = SCLOsgi.MODULE_REPOSITORY;
         ModuleRepository repo = new ModuleRepository(SCLOsgi.SOURCE_REPOSITORY);
         repo.setAdvisor(new ModuleCompilationOptionsAdvisor() {
-            
+
             @Override
             public ModuleCompilationOptions getOptions(String moduleName) {
                 boolean coverage = false;
@@ -94,13 +101,13 @@ public class TestsGraphUtils {
                 return new ModuleCompilationOptions(coverage);
             }
         });
-        
+
         CommandSession session = new CommandSession(repo, handler);
         TestScriptExecutor executor = new TestScriptExecutor(session, new BufferedReader(new StringReader(definition)), handler);
         try {
             executor.execute();
         } catch (Throwable t) {
-            t.printStackTrace();
+            LOGGER.error("Failed to execute test definition:\n{}", definition, t);
         }
         Collection<RuntimeModule> runtimeModules = session.getRuntimeEnvironment().getRuntimeModules();
         List<Module> modules = new ArrayList<>(runtimeModules.size());
@@ -110,4 +117,11 @@ public class TestsGraphUtils {
         CombinedCoverage coverage = CoverageUtils.getCoverage(modules);
         return coverage;
     }
+
+    public static byte[] stsTestContentDump(ReadGraph graph, Resource test) throws DatabaseException {
+        TestsResource TESTS = TestsResource.getInstance(graph);
+        String def = graph.getRelatedValue(test, TESTS.STSTest_definition, Bindings.STRING);
+        return def.getBytes(StandardCharsets.UTF_8);
+    }
+
 }