]> gerrit.simantics Code Review - simantics/platform.git/blob
58aa323e1e98e5053380b1579eaf2c27823f4e85
[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         Integer priority = graph.getPossibleRelatedValue2(suite, TESTS.STSTest_executionPriority, Bindings.INTEGER);
106         
107         Layer0 L0 = Layer0.getInstance(graph);
108         Collection<Resource> stsVariables = graph.sync(new ObjectsWithType(suite, L0.ConsistsOf, TESTS.STSVariable));
109         Map<String, String> variables = new HashMap<>(stsVariables.size());
110         for (Resource stsVariable : stsVariables) {
111             String name = graph.getRelatedValue(stsVariable, L0.HasName, Bindings.STRING);
112             String value = graph.getRelatedValue(stsVariable, TESTS.STSVariable_definition);
113             variables.put(name, value);
114         }
115         return new ModelledSTSSuite(suiteName, children, moduleNameFilter, priority != null ? priority : -1, variables);
116     }
117
118     public static void setTestCoverage(ModelledSTSTest test, CommandSession session) {
119         Collection<RuntimeModule> runtimeModules = session.getRuntimeEnvironment().getRuntimeModules();
120         List<Module> modules = new ArrayList<>(runtimeModules.size());
121         for (RuntimeModule module : runtimeModules)
122             modules.add(module.getModule());
123         test.setCoverage(CoverageUtils.getCoverage(modules));
124         CoverageUtils.resetCoverage(modules);
125     }
126
127     public static void setSuiteCoverage(ModelledSTSTest test, ModelledSTSSuite suite, CommandSession session) {
128         Collection<RuntimeModule> runtimeModules = session.getRuntimeEnvironment().getRuntimeModules();
129         List<Module> modules = new ArrayList<>(runtimeModules.size());
130         for (RuntimeModule module : runtimeModules) {
131             for (Pattern p : suite.getModuleNameFilterPatterns()) {
132                 if (p.matcher(module.getModule().getName().toLowerCase()).find()) {
133                     modules.add(module.getModule());
134                 }
135             }
136         }
137         test.setCoverage(CoverageUtils.getCoverage(modules));
138         suite.addCoverage(modules);
139         CoverageUtils.resetCoverage(modules);
140     }
141 }