]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.tests.modelled/src/org/simantics/tests/modelled/TestsGraphUtils.java
Adding system property support for starting SCL REST server on startup
[simantics/platform.git] / bundles / org.simantics.tests.modelled / src / org / simantics / tests / modelled / TestsGraphUtils.java
1 package org.simantics.tests.modelled;
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.StringReader;
6 import java.util.ArrayList;
7 import java.util.Collection;
8 import java.util.List;
9 import java.util.regex.Pattern;
10
11 import org.simantics.databoard.Bindings;
12 import org.simantics.db.Resource;
13 import org.simantics.db.WriteGraph;
14 import org.simantics.db.common.utils.NameUtils;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.layer0.Layer0;
17 import org.simantics.scl.compiler.commands.CommandSession;
18 import org.simantics.scl.compiler.commands.TestScriptExecutor;
19 import org.simantics.scl.compiler.module.Module;
20 import org.simantics.scl.compiler.module.coverage.CombinedCoverage;
21 import org.simantics.scl.compiler.module.coverage.CoverageUtils;
22 import org.simantics.scl.compiler.module.options.ModuleCompilationOptions;
23 import org.simantics.scl.compiler.module.options.ModuleCompilationOptionsAdvisor;
24 import org.simantics.scl.compiler.module.repository.ModuleRepository;
25 import org.simantics.scl.compiler.runtime.RuntimeModule;
26 import org.simantics.scl.osgi.SCLOsgi;
27 import org.simantics.scl.runtime.reporting.SCLReportingHandler;
28 import org.simantics.tests.modelled.ontology.TestsResource;
29
30 public class TestsGraphUtils {
31
32     private static final String STS_TEST_PREFIX = "STSTest";
33     private static final String STS_SUITE_PREFIX = "STSSuite";
34     private static final String STS_VARIABLE_PREFIX = "STSVariable";
35     
36     private TestsGraphUtils() {}
37     
38     public static Resource createSTSVariable(WriteGraph graph, Resource parent) throws DatabaseException {
39         String name = NameUtils.findFreshEscapedName(graph, STS_VARIABLE_PREFIX, parent);
40         Resource stsVariable = graph.newResource();
41         
42         Layer0 L0 = Layer0.getInstance(graph);
43         TestsResource TESTS = TestsResource.getInstance(graph);
44         
45         graph.claim(parent, L0.ConsistsOf, L0.PartOf, stsVariable);
46         graph.claim(stsVariable, L0.InstanceOf, TESTS.STSVariable);
47         graph.claimLiteral(stsVariable, L0.HasName, name, Bindings.STRING);
48         graph.claimLiteral(stsVariable, TESTS.STSVariable_definition, "", Bindings.STRING);
49         return stsVariable;
50     }
51     
52     public static Resource createSTSTest(WriteGraph graph, Resource parent) throws DatabaseException {
53         String name = NameUtils.findFreshEscapedName(graph, STS_TEST_PREFIX, parent);
54         Resource stsTest = graph.newResource();
55         
56         Layer0 L0 = Layer0.getInstance(graph);
57         TestsResource TESTS = TestsResource.getInstance(graph);
58         
59         graph.claim(parent, L0.ConsistsOf, L0.PartOf, stsTest);
60         graph.claim(stsTest, L0.InstanceOf, TESTS.STSTest);
61         graph.claimLiteral(stsTest, L0.HasName, name, Bindings.STRING);
62         graph.claimLiteral(stsTest, TESTS.STSTest_definition, "", Bindings.STRING);
63         return stsTest;
64     }
65     
66     public static Resource createSTSSuite(WriteGraph graph, Resource parent) throws DatabaseException {
67         String name = NameUtils.findFreshEscapedName(graph, STS_SUITE_PREFIX, parent);
68         Resource stsSuite = graph.newResource();
69         
70         Layer0 L0 = Layer0.getInstance(graph);
71         TestsResource TESTS = TestsResource.getInstance(graph);
72         
73         graph.claim(parent, L0.ConsistsOf, L0.PartOf, stsSuite);
74         graph.claim(stsSuite, L0.InstanceOf, TESTS.STSSuite);
75         graph.claimLiteral(stsSuite, L0.HasName, name, Bindings.STRING);
76         return stsSuite;
77     }
78
79     public static CombinedCoverage runSTSTestDefinition(String definition, List<Pattern> moduleNameFilters, SCLReportingHandler handler) throws IOException {
80         
81 //        ModuleRepository repo = SCLOsgi.MODULE_REPOSITORY;
82         ModuleRepository repo = new ModuleRepository(SCLOsgi.SOURCE_REPOSITORY);
83         repo.setAdvisor(new ModuleCompilationOptionsAdvisor() {
84             
85             @Override
86             public ModuleCompilationOptions getOptions(String moduleName) {
87                 boolean coverage = false;
88                 for (Pattern p : moduleNameFilters) {
89                     if (p.matcher(moduleName.toLowerCase()).find()) {
90                         coverage = true;
91                         break;
92                     }
93                 }
94                 return new ModuleCompilationOptions(coverage);
95             }
96         });
97         
98         CommandSession session = new CommandSession(repo, handler);
99         TestScriptExecutor executor = new TestScriptExecutor(session, new BufferedReader(new StringReader(definition)), handler);
100         try {
101             executor.execute();
102         } catch (Throwable t) {
103             t.printStackTrace();
104         }
105         Collection<RuntimeModule> runtimeModules = session.getRuntimeEnvironment().getRuntimeModules();
106         List<Module> modules = new ArrayList<>(runtimeModules.size());
107         for (RuntimeModule module : runtimeModules) {
108             modules.add(module.getModule());
109         }
110         CombinedCoverage coverage = CoverageUtils.getCoverage(modules);
111         return coverage;
112     }
113 }