]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.tests.modelled/src/org/simantics/tests/modelled/utils/ModelledSTSTest.java
Merge "(refs #7375) Replaced forVariables by a visitor"
[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> dependencies;
32     private Set<String> unresolvedDependencies;
33     
34     private CombinedCoverage coverage;
35     private Map<String, String> variables;
36
37     ModelledSTSTest(String name, String parentName, String code, int priority, boolean ignored, Set<String> dependencies, Map<String, String> variables) {
38         this.name = name;
39         this.parentName = parentName;
40         this.code = code;
41         this.priority = priority;
42         this.ignored = ignored;
43         this.dependencies = dependencies;
44         this.unresolvedDependencies = new HashSet<>(dependencies);
45         this.variables = variables;
46     }
47
48     public String getName() {
49         return name;
50     }
51
52     public String getParentName() {
53         return parentName;
54     }
55
56     public String getCode() {
57         return code;
58     }
59
60     public int getPriority() {
61         return priority;
62     }
63
64     public boolean isIgnored() {
65         return (ignored || !unresolvedDependencies.isEmpty());
66     }
67
68     public void setCoverage(CombinedCoverage coverage) {
69         this.coverage = coverage;
70     }
71
72     public CombinedCoverage getCoverage() {
73         return coverage;
74     }
75
76     public static class CommandSessionVariable {
77
78         private final String name;
79         private final Type type;
80         private final Object value;
81
82         public CommandSessionVariable(String name, Type type, Object value) {
83             this.name = name;
84             this.type = type;
85             this.value = value;
86         }
87
88         public String getName() {
89             return name;
90         }
91
92         public Type getType() {
93             return type;
94         }
95
96         public Object getValue() {
97             return value;
98         }
99     }
100     
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;
105         try {
106 //            repo.setAdvisor(new ModuleCompilationOptionsAdvisor() {
107 //                
108 //                @Override
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;
116 //    //                        break;
117 //    //                    }
118 //    //                }
119 //                    return new ModuleCompilationOptions(coverage);
120 //                }
121 //            });
122             
123             SCLReportingHandler handler = (SCLReportingHandler) SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
124             session = new CommandSession(repo, handler);
125             
126             for (CommandSessionVariable var : vars)
127                 session.setVariable(var.getName(), var.getType(), var.getValue());
128             
129             for (Map.Entry<String, String> entry : variables.entrySet())
130                 session.setVariable(entry.getKey(), Types.STRING, entry.getValue());
131             
132             new TestScriptExecutor(session, new BufferedReader(new StringReader(code)), handler, true).execute();
133             STSSuiteTestCollector.setTestCoverage(this, session);
134             
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)));
139             
140             return result;
141         } finally {
142             // remember to flush this repository
143 //            repo.flush();
144             Builtins.flush();
145             JavaModule.flush();
146         }
147     }
148
149     public Set<String> getDependencies() {
150         return dependencies;
151     }
152     
153     public Set<String> getUnresolvedDependencies() {
154         return unresolvedDependencies;
155     }
156     
157     public boolean resolveDependency(String testDep) {
158         return unresolvedDependencies.remove(testDep);
159     }
160
161     @Override
162     public int hashCode() {
163         final int prime = 31;
164         int result = 1;
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());
173         return result;
174     }
175
176     @Override
177     public boolean equals(Object obj) {
178         if (this == obj)
179             return true;
180         if (obj == null)
181             return false;
182         if (getClass() != obj.getClass())
183             return false;
184         ModelledSTSTest other = (ModelledSTSTest) obj;
185         if (code == null) {
186             if (other.code != null)
187                 return false;
188         } else if (!code.equals(other.code))
189             return false;
190         if (coverage == null) {
191             if (other.coverage != null)
192                 return false;
193         } else if (!coverage.equals(other.coverage))
194             return false;
195         if (dependencies == null) {
196             if (other.dependencies != null)
197                 return false;
198         } else if (!dependencies.equals(other.dependencies))
199             return false;
200         if (ignored != other.ignored)
201             return false;
202         if (name == null) {
203             if (other.name != null)
204                 return false;
205         } else if (!name.equals(other.name))
206             return false;
207         if (priority != other.priority)
208             return false;
209         if (unresolvedDependencies == null) {
210             if (other.unresolvedDependencies != null)
211                 return false;
212         } else if (!unresolvedDependencies.equals(other.unresolvedDependencies))
213             return false;
214         if (variables == null) {
215             if (other.variables != null)
216                 return false;
217         } else if (!variables.equals(other.variables))
218             return false;
219         return true;
220     }
221 }