]> gerrit.simantics Code Review - simantics/platform.git/blob
8917d16462425ddf65e93b20636d19dcaa766a78
[simantics/platform.git] /
1 package org.simantics.tests.modelled.utils;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Collection;
6 import java.util.HashMap;
7 import java.util.HashSet;
8 import java.util.List;
9 import java.util.Map;
10 import java.util.regex.Pattern;
11
12 import org.simantics.Simantics;
13 import org.simantics.databoard.Bindings;
14 import org.simantics.db.ReadGraph;
15 import org.simantics.db.Resource;
16 import org.simantics.db.common.request.ObjectsWithType;
17 import org.simantics.db.common.request.UniqueRead;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.layer0.Layer0;
20 import org.simantics.modeling.ModelingUtils;
21 import org.simantics.scl.compiler.commands.CommandSession;
22 import org.simantics.scl.compiler.module.Module;
23 import org.simantics.scl.compiler.module.coverage.CoverageUtils;
24 import org.simantics.scl.compiler.runtime.RuntimeModule;
25 import org.simantics.scl.runtime.tuple.Tuple0;
26 import org.simantics.tests.modelled.ontology.TestsResource;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public class STSSuiteTestCollector {
31
32     private static final Logger LOGGER = LoggerFactory.getLogger(STSSuiteTestCollector.class);
33     
34     /**
35      * TODO: The idea of this class was to collect all the tests from shared libraries and construct
36      * JUnit tests out of them programmatically and then run them with JUnit to get results
37      * @throws DatabaseException 
38      * 
39      */
40     public static Collection<ModelledSTSSuite> collectTests() throws DatabaseException {
41         Collection<ModelledSTSSuite> suitess = Simantics.getSession().syncRequest(new UniqueRead<Collection<ModelledSTSSuite>>() {
42
43             @Override
44             public Collection<ModelledSTSSuite> perform(ReadGraph graph) throws DatabaseException {
45                 
46                 List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
47                 if (LOGGER.isInfoEnabled())
48                     LOGGER.info("Found {} shared ontologies from graph",  sharedOntologies.size());
49                 Collection<ModelledSTSSuite> suites = new HashSet<>();
50                 TestsResource TESTS = TestsResource.getInstance(graph);
51                 Layer0 L0 = Layer0.getInstance(graph);
52                 for (Resource sharedOntology : sharedOntologies) {
53                     if (LOGGER.isInfoEnabled())
54                         LOGGER.info("Searching {} for modelled tests", graph.getURI(sharedOntology));
55                     List<Resource> stsSuites = ModelingUtils.searchByType(graph, sharedOntology, TESTS.STSSuite);
56                     for (Resource stsSuite : stsSuites) {
57                         try {
58                             Collection<Resource> tests = graph.syncRequest(new ObjectsWithType(stsSuite, L0.ConsistsOf, TESTS.STSTest));
59                             if (tests.isEmpty())
60                                 continue;
61
62                             List<ModelledSTSTest> testRunners = new ArrayList<>(tests.size());
63                             for (Resource test : tests)
64                                 testRunners.add(toModelledTest(graph, test));
65
66                             suites.add(toModelledSuite(graph, stsSuite, testRunners));
67                         } catch (Exception e) {
68                             LOGGER.error("", e);
69                         }
70                     }
71                 }
72                 return suites;
73             }
74         });
75         return suitess;
76     }
77     
78
79     public static ModelledSTSTest toModelledTest(ReadGraph graph, Resource test) throws DatabaseException {
80         Layer0 L0 = Layer0.getInstance(graph);
81         TestsResource TESTS = TestsResource.getInstance(graph);
82         String testName = graph.getRelatedValue(test, L0.HasName, Bindings.STRING);
83         String code = graph.getRelatedValue(test, TESTS.STSTest_definition, Bindings.STRING);
84         Integer priority = graph.getPossibleRelatedValue(test, TESTS.STSTest_executionPriority, Bindings.INTEGER);
85         Boolean ignored = graph.getPossibleRelatedValue(test, TESTS.ignore, Bindings.BOOLEAN);
86         
87         String dependencies = graph.getPossibleRelatedValue(test, TESTS.dependencies, Bindings.STRING);
88         String[] actualDeps = dependencies.isEmpty() ? new String[0] : dependencies.split(",");
89         
90         // collect variables
91         Collection<Resource> stsVariables = graph.getObjects(test, L0.ConsistsOf);
92         Map<String, String> variables = new HashMap<>(stsVariables.size());
93         for (Resource stsVariable : stsVariables) {
94             String name = graph.getRelatedValue(stsVariable, L0.HasName, Bindings.STRING);
95             String value = graph.getRelatedValue(stsVariable, TESTS.STSVariable_definition);
96             variables.put(name, value);
97         }
98         return new ModelledSTSTest(testName, code, priority != null ? priority : -1, ignored != null ? ignored : false, new HashSet<>(Arrays.asList(actualDeps)), variables);
99     }
100
101     public static ModelledSTSSuite toModelledSuite(ReadGraph graph, Resource suite, List<ModelledSTSTest> children) throws DatabaseException {
102         TestsResource TESTS = TestsResource.getInstance(graph);
103         String suiteName = graph.getURI(suite);
104         String moduleNameFilter = graph.getPossibleRelatedValue2(suite, TESTS.STSSuite_moduleNameFilter, Bindings.STRING);
105         
106         Layer0 L0 = Layer0.getInstance(graph);
107         Collection<Resource> stsVariables = graph.sync(new ObjectsWithType(suite, L0.ConsistsOf, TESTS.STSVariable));
108         Map<String, String> variables = new HashMap<>(stsVariables.size());
109         for (Resource stsVariable : stsVariables) {
110             String name = graph.getRelatedValue(stsVariable, L0.HasName, Bindings.STRING);
111             String value = graph.getRelatedValue(stsVariable, TESTS.STSVariable_definition);
112             variables.put(name, value);
113         }
114         return new ModelledSTSSuite(suiteName, children, moduleNameFilter, variables);
115     }
116
117     public static void setTestCoverage(ModelledSTSTest test, CommandSession session) {
118         Collection<RuntimeModule> runtimeModules = session.getRuntimeEnvironment().getRuntimeModules();
119         List<Module> modules = new ArrayList<>(runtimeModules.size());
120         for (RuntimeModule module : runtimeModules)
121             modules.add(module.getModule());
122         test.setCoverage(CoverageUtils.getCoverage(modules));
123         CoverageUtils.resetCoverage(modules);
124     }
125
126     public static void setSuiteCoverage(ModelledSTSTest test, ModelledSTSSuite suite, CommandSession session) {
127         Collection<RuntimeModule> runtimeModules = session.getRuntimeEnvironment().getRuntimeModules();
128         List<Module> modules = new ArrayList<>(runtimeModules.size());
129         for (RuntimeModule module : runtimeModules) {
130             for (Pattern p : suite.getModuleNameFilterPatterns()) {
131                 if (p.matcher(module.getModule().getName().toLowerCase()).find()) {
132                     modules.add(module.getModule());
133                 }
134             }
135         }
136         test.setCoverage(CoverageUtils.getCoverage(modules));
137         suite.addCoverage(modules);
138         CoverageUtils.resetCoverage(modules);
139     }
140 }