1 package org.simantics.tests.modelled.utils;
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.StringReader;
6 import java.util.ArrayList;
7 import java.util.HashSet;
12 import org.simantics.scl.compiler.commands.CommandSession;
13 import org.simantics.scl.compiler.commands.TestScriptExecutor;
14 import org.simantics.scl.compiler.elaboration.java.Builtins;
15 import org.simantics.scl.compiler.elaboration.java.JavaModule;
16 import org.simantics.scl.compiler.module.coverage.CombinedCoverage;
17 import org.simantics.scl.compiler.module.repository.ModuleRepository;
18 import org.simantics.scl.compiler.types.Type;
19 import org.simantics.scl.compiler.types.Types;
20 import org.simantics.scl.osgi.SCLOsgi;
21 import org.simantics.scl.runtime.SCLContext;
22 import org.simantics.scl.runtime.reporting.SCLReportingHandler;
24 public class ModelledSTSTest {
26 private final String name;
27 private final String parentName;
28 private final String code;
29 private final int priority;
30 private final boolean ignored;
31 private Set<String> imports;
32 private Set<String> dependencies;
33 private Set<String> unresolvedDependencies;
35 private CombinedCoverage coverage;
36 private Map<String, String> variables;
38 ModelledSTSTest(String name, String parentName, String code, int priority, boolean ignored, Set<String> imports, Set<String> dependencies, Map<String, String> variables) {
40 this.parentName = parentName;
42 this.priority = priority;
43 this.ignored = ignored;
44 this.imports = imports;
45 this.dependencies = dependencies;
46 this.unresolvedDependencies = new HashSet<>(dependencies);
47 this.variables = variables;
50 public String getName() {
54 public String getParentName() {
58 public String getCode() {
62 public int getPriority() {
66 public boolean isIgnored() {
67 return (ignored || !unresolvedDependencies.isEmpty());
70 public void setCoverage(CombinedCoverage coverage) {
71 this.coverage = coverage;
74 public CombinedCoverage getCoverage() {
78 public static class CommandSessionVariable {
80 private final String name;
81 private final Type type;
82 private final Object value;
84 public CommandSessionVariable(String name, Type type, Object value) {
90 public String getName() {
94 public Type getType() {
98 public Object getValue() {
103 public List<CommandSessionVariable> run(List<CommandSessionVariable> vars) throws IOException {
104 // ModuleRepository repo = new ModuleRepository(SCLOsgi.SOURCE_REPOSITORY);
105 ModuleRepository repo = SCLOsgi.MODULE_REPOSITORY;
106 CommandSession session = null;
108 // repo.setAdvisor(new ModuleCompilationOptionsAdvisor() {
111 // public ModuleCompilationOptions getOptions(String moduleName) {
112 // // TODO: default to false
113 // boolean coverage = true;
114 // // TODO: add moduleName filters back
115 // // for (Pattern p : getModuleNameFilterPatterns()) {
116 // // if (p.matcher(moduleName.toLowerCase()).find()) {
117 // // coverage = true;
121 // return new ModuleCompilationOptions(coverage);
125 SCLReportingHandler handler = (SCLReportingHandler) SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
126 session = new CommandSession(repo, handler);
128 for(String imp : imports) {
129 session.execute("import \"" + imp + "\"");
132 for (CommandSessionVariable var : vars)
133 session.setVariable(var.getName(), var.getType(), var.getValue());
135 for (Map.Entry<String, String> entry : variables.entrySet())
136 session.setVariable(entry.getKey(), Types.STRING, entry.getValue());
138 new TestScriptExecutor(session, new BufferedReader(new StringReader(code)), handler, false).execute();
139 STSSuiteTestCollector.setTestCoverage(this, session);
141 // Return variables from this session
142 List<CommandSessionVariable> result = new ArrayList<>();
143 for (String var : session.getVariables())
144 result.add(new CommandSessionVariable(var, session.getVariableType(var), session.getVariableValue(var)));
148 // remember to flush this repository
155 public Set<String> getDependencies() {
159 public Set<String> getUnresolvedDependencies() {
160 return unresolvedDependencies;
163 public boolean resolveDependency(String testDep) {
164 return unresolvedDependencies.remove(testDep);
168 public int hashCode() {
169 final int prime = 31;
171 result = prime * result + ((code == null) ? 0 : code.hashCode());
172 result = prime * result + ((coverage == null) ? 0 : coverage.hashCode());
173 result = prime * result + ((imports == null) ? 0 : imports.hashCode());
174 result = prime * result + ((dependencies == null) ? 0 : dependencies.hashCode());
175 result = prime * result + (ignored ? 1231 : 1237);
176 result = prime * result + ((name == null) ? 0 : name.hashCode());
177 result = prime * result + priority;
178 result = prime * result + ((unresolvedDependencies == null) ? 0 : unresolvedDependencies.hashCode());
179 result = prime * result + ((variables == null) ? 0 : variables.hashCode());
184 public boolean equals(Object obj) {
189 if (getClass() != obj.getClass())
191 ModelledSTSTest other = (ModelledSTSTest) obj;
193 if (other.code != null)
195 } else if (!code.equals(other.code))
197 if (coverage == null) {
198 if (other.coverage != null)
200 } else if (!coverage.equals(other.coverage))
202 if (imports== null) {
203 if (other.imports != null)
205 } else if (!imports.equals(other.imports))
207 if (dependencies == null) {
208 if (other.dependencies != null)
210 } else if (!dependencies.equals(other.dependencies))
212 if (ignored != other.ignored)
215 if (other.name != null)
217 } else if (!name.equals(other.name))
219 if (priority != other.priority)
221 if (unresolvedDependencies == null) {
222 if (other.unresolvedDependencies != null)
224 } else if (!unresolvedDependencies.equals(other.unresolvedDependencies))
226 if (variables == null) {
227 if (other.variables != null)
229 } else if (!variables.equals(other.variables))