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> dependencies;
32 private Set<String> unresolvedDependencies;
34 private CombinedCoverage coverage;
35 private Map<String, String> variables;
37 ModelledSTSTest(String name, String parentName, String code, int priority, boolean ignored, Set<String> dependencies, Map<String, String> variables) {
39 this.parentName = parentName;
41 this.priority = priority;
42 this.ignored = ignored;
43 this.dependencies = dependencies;
44 this.unresolvedDependencies = new HashSet<>(dependencies);
45 this.variables = variables;
48 public String getName() {
52 public String getParentName() {
56 public String getCode() {
60 public int getPriority() {
64 public boolean isIgnored() {
65 return (ignored || !unresolvedDependencies.isEmpty());
68 public void setCoverage(CombinedCoverage coverage) {
69 this.coverage = coverage;
72 public CombinedCoverage getCoverage() {
76 public static class CommandSessionVariable {
78 private final String name;
79 private final Type type;
80 private final Object value;
82 public CommandSessionVariable(String name, Type type, Object value) {
88 public String getName() {
92 public Type getType() {
96 public Object getValue() {
101 public List<CommandSessionVariable> run(List<CommandSessionVariable> vars) throws IOException {
102 // ModuleRepository repo = new ModuleRepository(SCLOsgi.SOURCE_REPOSITORY);
103 ModuleRepository repo = SCLOsgi.MODULE_REPOSITORY;
104 CommandSession session = null;
106 // repo.setAdvisor(new ModuleCompilationOptionsAdvisor() {
109 // public ModuleCompilationOptions getOptions(String moduleName) {
110 // // TODO: default to false
111 // boolean coverage = true;
112 // // TODO: add moduleName filters back
113 // // for (Pattern p : getModuleNameFilterPatterns()) {
114 // // if (p.matcher(moduleName.toLowerCase()).find()) {
115 // // coverage = true;
119 // return new ModuleCompilationOptions(coverage);
123 SCLReportingHandler handler = (SCLReportingHandler) SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
124 session = new CommandSession(repo, handler);
126 for (CommandSessionVariable var : vars)
127 session.setVariable(var.getName(), var.getType(), var.getValue());
129 for (Map.Entry<String, String> entry : variables.entrySet())
130 session.setVariable(entry.getKey(), Types.STRING, entry.getValue());
132 new TestScriptExecutor(session, new BufferedReader(new StringReader(code)), handler, true).execute();
133 STSSuiteTestCollector.setTestCoverage(this, session);
135 // Return variables from this session
136 List<CommandSessionVariable> result = new ArrayList<>();
137 for (String var : session.getVariables())
138 result.add(new CommandSessionVariable(var, session.getVariableType(var), session.getVariableValue(var)));
142 // remember to flush this repository
149 public Set<String> getDependencies() {
153 public Set<String> getUnresolvedDependencies() {
154 return unresolvedDependencies;
157 public boolean resolveDependency(String testDep) {
158 return unresolvedDependencies.remove(testDep);
162 public int hashCode() {
163 final int prime = 31;
165 result = prime * result + ((code == null) ? 0 : code.hashCode());
166 result = prime * result + ((coverage == null) ? 0 : coverage.hashCode());
167 result = prime * result + ((dependencies == null) ? 0 : dependencies.hashCode());
168 result = prime * result + (ignored ? 1231 : 1237);
169 result = prime * result + ((name == null) ? 0 : name.hashCode());
170 result = prime * result + priority;
171 result = prime * result + ((unresolvedDependencies == null) ? 0 : unresolvedDependencies.hashCode());
172 result = prime * result + ((variables == null) ? 0 : variables.hashCode());
177 public boolean equals(Object obj) {
182 if (getClass() != obj.getClass())
184 ModelledSTSTest other = (ModelledSTSTest) obj;
186 if (other.code != null)
188 } else if (!code.equals(other.code))
190 if (coverage == null) {
191 if (other.coverage != null)
193 } else if (!coverage.equals(other.coverage))
195 if (dependencies == null) {
196 if (other.dependencies != null)
198 } else if (!dependencies.equals(other.dependencies))
200 if (ignored != other.ignored)
203 if (other.name != null)
205 } else if (!name.equals(other.name))
207 if (priority != other.priority)
209 if (unresolvedDependencies == null) {
210 if (other.unresolvedDependencies != null)
212 } else if (!unresolvedDependencies.equals(other.unresolvedDependencies))
214 if (variables == null) {
215 if (other.variables != null)
217 } else if (!variables.equals(other.variables))