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