]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.tests.modelled/src/org/simantics/tests/modelled/utils/ModelledSTSTest.java
c0ed9b191420604d49cfc73edd636d9f00928920
[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.module.coverage.CombinedCoverage;
15 import org.simantics.scl.compiler.module.options.ModuleCompilationOptions;
16 import org.simantics.scl.compiler.module.options.ModuleCompilationOptionsAdvisor;
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 code;
28     private final int priority;
29     private final boolean ignored;
30     private Set<String> dependencies;
31     private Set<String> unresolvedDependencies;
32     
33     private CombinedCoverage coverage;
34     private Map<String, String> variables;
35
36     ModelledSTSTest(String name, String code, int priority, boolean ignored, Set<String> dependencies, Map<String, String> variables) {
37         this.name = name;
38         this.code = code;
39         this.priority = priority;
40         this.ignored = ignored;
41         this.dependencies = dependencies;
42         this.unresolvedDependencies = new HashSet<>(dependencies);
43         this.variables = variables;
44     }
45
46     public String getName() {
47         return name;
48     }
49
50     public String getCode() {
51         return code;
52     }
53
54     public int getPriority() {
55         return priority;
56     }
57
58     public boolean isIgnored() {
59         return (ignored || !unresolvedDependencies.isEmpty());
60     }
61
62     public void setCoverage(CombinedCoverage coverage) {
63         this.coverage = coverage;
64     }
65
66     public CombinedCoverage getCoverage() {
67         return coverage;
68     }
69
70     public static class CommandSessionVariable {
71
72         private final String name;
73         private final Type type;
74         private final Object value;
75
76         public CommandSessionVariable(String name, Type type, Object value) {
77             this.name = name;
78             this.type = type;
79             this.value = value;
80         }
81
82         public String getName() {
83             return name;
84         }
85
86         public Type getType() {
87             return type;
88         }
89
90         public Object getValue() {
91             return value;
92         }
93     }
94     
95     public List<CommandSessionVariable> run(List<CommandSessionVariable> vars) throws IOException {
96         ModuleRepository repo = new ModuleRepository(SCLOsgi.SOURCE_REPOSITORY);
97         CommandSession session = null;
98         try {
99             repo.setAdvisor(new ModuleCompilationOptionsAdvisor() {
100                 
101                 @Override
102                 public ModuleCompilationOptions getOptions(String moduleName) {
103                     // TODO: default to false
104                     boolean coverage = true;
105                     // TODO: add moduleName filters back
106     //                for (Pattern p : getModuleNameFilterPatterns()) {
107     //                    if (p.matcher(moduleName.toLowerCase()).find()) {
108     //                        coverage = true;
109     //                        break;
110     //                    }
111     //                }
112                     return new ModuleCompilationOptions(coverage);
113                 }
114             });
115             
116             SCLReportingHandler handler = (SCLReportingHandler) SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
117             session = new CommandSession(repo, handler);
118             
119             for (CommandSessionVariable var : vars)
120                 session.setVariable(var.getName(), var.getType(), var.getValue());
121             
122             for (Map.Entry<String, String> entry : variables.entrySet())
123                 session.setVariable(entry.getKey(), Types.STRING, entry.getValue());
124             
125             new TestScriptExecutor(session, new BufferedReader(new StringReader(code)), handler, true).execute();
126             STSSuiteTestCollector.setTestCoverage(this, session);
127             
128             // Return variables from this session
129             List<CommandSessionVariable> result = new ArrayList<>();
130             for (String var : session.getVariables())
131                 result.add(new CommandSessionVariable(var, session.getVariableType(var), session.getVariableValue(var)));
132             
133             return result;
134         } finally {
135             // remember to flush this repository
136             repo.flush();
137         }
138     }
139
140     public Set<String> getDependencies() {
141         return dependencies;
142     }
143     
144     public Set<String> getUnresolvedDependencies() {
145         return unresolvedDependencies;
146     }
147     
148     public boolean resolveDependency(String testDep) {
149         return unresolvedDependencies.remove(testDep);
150     }
151
152     @Override
153     public int hashCode() {
154         final int prime = 31;
155         int result = 1;
156         result = prime * result + ((code == null) ? 0 : code.hashCode());
157         result = prime * result + ((coverage == null) ? 0 : coverage.hashCode());
158         result = prime * result + ((dependencies == null) ? 0 : dependencies.hashCode());
159         result = prime * result + (ignored ? 1231 : 1237);
160         result = prime * result + ((name == null) ? 0 : name.hashCode());
161         result = prime * result + priority;
162         result = prime * result + ((unresolvedDependencies == null) ? 0 : unresolvedDependencies.hashCode());
163         result = prime * result + ((variables == null) ? 0 : variables.hashCode());
164         return result;
165     }
166
167     @Override
168     public boolean equals(Object obj) {
169         if (this == obj)
170             return true;
171         if (obj == null)
172             return false;
173         if (getClass() != obj.getClass())
174             return false;
175         ModelledSTSTest other = (ModelledSTSTest) obj;
176         if (code == null) {
177             if (other.code != null)
178                 return false;
179         } else if (!code.equals(other.code))
180             return false;
181         if (coverage == null) {
182             if (other.coverage != null)
183                 return false;
184         } else if (!coverage.equals(other.coverage))
185             return false;
186         if (dependencies == null) {
187             if (other.dependencies != null)
188                 return false;
189         } else if (!dependencies.equals(other.dependencies))
190             return false;
191         if (ignored != other.ignored)
192             return false;
193         if (name == null) {
194             if (other.name != null)
195                 return false;
196         } else if (!name.equals(other.name))
197             return false;
198         if (priority != other.priority)
199             return false;
200         if (unresolvedDependencies == null) {
201             if (other.unresolvedDependencies != null)
202                 return false;
203         } else if (!unresolvedDependencies.equals(other.unresolvedDependencies))
204             return false;
205         if (variables == null) {
206             if (other.variables != null)
207                 return false;
208         } else if (!variables.equals(other.variables))
209             return false;
210         return true;
211     }
212 }