]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.tests.modelled/src/org/simantics/tests/modelled/utils/ModelledSTSTest.java
Simantics integration STS
[simantics/platform.git] / bundles / org.simantics.tests.modelled / src / org / simantics / tests / modelled / utils / ModelledSTSTest.java
1 package org.simantics.tests.modelled.utils;
2
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;
8 import java.util.List;
9 import java.util.Map;
10 import java.util.Set;
11
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;
23
24 public class ModelledSTSTest {
25
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;
34
35     private CombinedCoverage coverage;
36     private Map<String, String> variables;
37
38     ModelledSTSTest(String name, String parentName, String code, int priority, boolean ignored, Set<String> imports, Set<String> dependencies, Map<String, String> variables) {
39         this.name = name;
40         this.parentName = parentName;
41         this.code = code;
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;
48     }
49
50     public String getName() {
51         return name;
52     }
53
54     public String getParentName() {
55         return parentName;
56     }
57
58     public String getCode() {
59         return code;
60     }
61
62     public int getPriority() {
63         return priority;
64     }
65
66     public boolean isIgnored() {
67         return (ignored || !unresolvedDependencies.isEmpty());
68     }
69
70     public void setCoverage(CombinedCoverage coverage) {
71         this.coverage = coverage;
72     }
73
74     public CombinedCoverage getCoverage() {
75         return coverage;
76     }
77
78     public static class CommandSessionVariable {
79
80         private final String name;
81         private final Type type;
82         private final Object value;
83
84         public CommandSessionVariable(String name, Type type, Object value) {
85             this.name = name;
86             this.type = type;
87             this.value = value;
88         }
89
90         public String getName() {
91             return name;
92         }
93
94         public Type getType() {
95             return type;
96         }
97
98         public Object getValue() {
99             return value;
100         }
101     }
102
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;
107         try {
108 //            repo.setAdvisor(new ModuleCompilationOptionsAdvisor() {
109 //
110 //                @Override
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;
118 //    //                        break;
119 //    //                    }
120 //    //                }
121 //                    return new ModuleCompilationOptions(coverage);
122     //                    }
123 //            });
124
125             SCLReportingHandler handler = (SCLReportingHandler) SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
126             session = new CommandSession(repo, handler);
127
128             for(String imp : imports) {
129                 session.execute("import \"" + imp + "\"");
130             }
131
132             for (CommandSessionVariable var : vars)
133                 session.setVariable(var.getName(), var.getType(), var.getValue());
134
135             for (Map.Entry<String, String> entry : variables.entrySet())
136                 session.setVariable(entry.getKey(), Types.STRING, entry.getValue());
137
138             new TestScriptExecutor(session, new BufferedReader(new StringReader(code)), handler, false).execute();
139             STSSuiteTestCollector.setTestCoverage(this, session);
140
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)));
145
146             return result;
147         } finally {
148             // remember to flush this repository
149 //            repo.flush();
150             Builtins.flush();
151             JavaModule.flush();
152         }
153     }
154
155     public Set<String> getDependencies() {
156         return dependencies;
157     }
158
159     public Set<String> getUnresolvedDependencies() {
160         return unresolvedDependencies;
161     }
162
163     public boolean resolveDependency(String testDep) {
164         return unresolvedDependencies.remove(testDep);
165     }
166
167     @Override
168     public int hashCode() {
169         final int prime = 31;
170         int result = 1;
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());
180         return result;
181     }
182
183     @Override
184     public boolean equals(Object obj) {
185         if (this == obj)
186             return true;
187         if (obj == null)
188             return false;
189         if (getClass() != obj.getClass())
190             return false;
191         ModelledSTSTest other = (ModelledSTSTest) obj;
192         if (code == null) {
193             if (other.code != null)
194                 return false;
195         } else if (!code.equals(other.code))
196             return false;
197         if (coverage == null) {
198             if (other.coverage != null)
199                 return false;
200         } else if (!coverage.equals(other.coverage))
201             return false;
202         if (imports== null) {
203             if (other.imports != null)
204                 return false;
205         } else if (!imports.equals(other.imports))
206             return false;
207         if (dependencies == null) {
208             if (other.dependencies != null)
209                 return false;
210         } else if (!dependencies.equals(other.dependencies))
211             return false;
212         if (ignored != other.ignored)
213             return false;
214         if (name == null) {
215             if (other.name != null)
216                 return false;
217         } else if (!name.equals(other.name))
218             return false;
219         if (priority != other.priority)
220             return false;
221         if (unresolvedDependencies == null) {
222             if (other.unresolvedDependencies != null)
223                 return false;
224         } else if (!unresolvedDependencies.equals(other.unresolvedDependencies))
225             return false;
226         if (variables == null) {
227             if (other.variables != null)
228                 return false;
229         } else if (!variables.equals(other.variables))
230             return false;
231         return true;
232     }
233 }