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 CommandSession session = null;
105 // repo.setAdvisor(new ModuleCompilationOptionsAdvisor() {
108 // public ModuleCompilationOptions getOptions(String moduleName) {
109 // // TODO: default to false
110 // boolean coverage = true;
111 // // TODO: add moduleName filters back
112 // // for (Pattern p : getModuleNameFilterPatterns()) {
113 // // if (p.matcher(moduleName.toLowerCase()).find()) {
114 // // coverage = true;
118 // return new ModuleCompilationOptions(coverage);
122 SCLReportingHandler handler = (SCLReportingHandler) SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
123 session = new CommandSession(repo, handler);
125 for (CommandSessionVariable var : vars)
126 session.setVariable(var.getName(), var.getType(), var.getValue());
128 for (Map.Entry<String, String> entry : variables.entrySet())
129 session.setVariable(entry.getKey(), Types.STRING, entry.getValue());
131 new TestScriptExecutor(session, new BufferedReader(new StringReader(code)), handler, true).execute();
132 STSSuiteTestCollector.setTestCoverage(this, session);
134 // Return variables from this session
135 List<CommandSessionVariable> result = new ArrayList<>();
136 for (String var : session.getVariables())
137 result.add(new CommandSessionVariable(var, session.getVariableType(var), session.getVariableValue(var)));
141 // remember to flush this repository
148 public Set<String> getDependencies() {
152 public Set<String> getUnresolvedDependencies() {
153 return unresolvedDependencies;
156 public boolean resolveDependency(String testDep) {
157 return unresolvedDependencies.remove(testDep);
161 public int hashCode() {
162 final int prime = 31;
164 result = prime * result + ((code == null) ? 0 : code.hashCode());
165 result = prime * result + ((coverage == null) ? 0 : coverage.hashCode());
166 result = prime * result + ((dependencies == null) ? 0 : dependencies.hashCode());
167 result = prime * result + (ignored ? 1231 : 1237);
168 result = prime * result + ((name == null) ? 0 : name.hashCode());
169 result = prime * result + priority;
170 result = prime * result + ((unresolvedDependencies == null) ? 0 : unresolvedDependencies.hashCode());
171 result = prime * result + ((variables == null) ? 0 : variables.hashCode());
176 public boolean equals(Object obj) {
181 if (getClass() != obj.getClass())
183 ModelledSTSTest other = (ModelledSTSTest) obj;
185 if (other.code != null)
187 } else if (!code.equals(other.code))
189 if (coverage == null) {
190 if (other.coverage != null)
192 } else if (!coverage.equals(other.coverage))
194 if (dependencies == null) {
195 if (other.dependencies != null)
197 } else if (!dependencies.equals(other.dependencies))
199 if (ignored != other.ignored)
202 if (other.name != null)
204 } else if (!name.equals(other.name))
206 if (priority != other.priority)
208 if (unresolvedDependencies == null) {
209 if (other.unresolvedDependencies != null)
211 } else if (!unresolvedDependencies.equals(other.unresolvedDependencies))
213 if (variables == null) {
214 if (other.variables != null)
216 } else if (!variables.equals(other.variables))