1 package org.simantics.tests.modelled;
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;
9 import java.util.regex.Pattern;
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;
30 public class TestsGraphUtils {
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";
36 private TestsGraphUtils() {}
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();
42 Layer0 L0 = Layer0.getInstance(graph);
43 TestsResource TESTS = TestsResource.getInstance(graph);
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);
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();
56 Layer0 L0 = Layer0.getInstance(graph);
57 TestsResource TESTS = TestsResource.getInstance(graph);
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);
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();
70 Layer0 L0 = Layer0.getInstance(graph);
71 TestsResource TESTS = TestsResource.getInstance(graph);
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);
79 public static CombinedCoverage runSTSTestDefinition(String definition, List<Pattern> moduleNameFilters, SCLReportingHandler handler) throws IOException {
81 // ModuleRepository repo = SCLOsgi.MODULE_REPOSITORY;
82 ModuleRepository repo = new ModuleRepository(SCLOsgi.SOURCE_REPOSITORY);
83 repo.setAdvisor(new ModuleCompilationOptionsAdvisor() {
86 public ModuleCompilationOptions getOptions(String moduleName) {
87 boolean coverage = false;
88 for (Pattern p : moduleNameFilters) {
89 if (p.matcher(moduleName.toLowerCase()).find()) {
94 return new ModuleCompilationOptions(coverage);
98 CommandSession session = new CommandSession(repo, handler);
99 TestScriptExecutor executor = new TestScriptExecutor(session, new BufferedReader(new StringReader(definition)), handler);
102 } catch (Throwable t) {
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());
110 CombinedCoverage coverage = CoverageUtils.getCoverage(modules);