e1eb08baf0321c200af4d79b06034ceb5b4469b3
[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 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         CommandSession session = null;
104         try {
105 //            repo.setAdvisor(new ModuleCompilationOptionsAdvisor() {
106 //                
107 //                @Override
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;
115 //    //                        break;
116 //    //                    }
117 //    //                }
118 //                    return new ModuleCompilationOptions(coverage);
119 //                }
120 //            });
121             
122             SCLReportingHandler handler = (SCLReportingHandler) SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
123             session = new CommandSession(repo, handler);
124             
125             for (CommandSessionVariable var : vars)
126                 session.setVariable(var.getName(), var.getType(), var.getValue());
127             
128             for (Map.Entry<String, String> entry : variables.entrySet())
129                 session.setVariable(entry.getKey(), Types.STRING, entry.getValue());
130             
131             new TestScriptExecutor(session, new BufferedReader(new StringReader(code)), handler, true).execute();
132             STSSuiteTestCollector.setTestCoverage(this, session);
133             
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)));
138             
139             return result;
140         } finally {
141             // remember to flush this repository
142             repo.flush();
143         }
144     }
145
146     public Set<String> getDependencies() {
147         return dependencies;
148     }
149     
150     public Set<String> getUnresolvedDependencies() {
151         return unresolvedDependencies;
152     }
153     
154     public boolean resolveDependency(String testDep) {
155         return unresolvedDependencies.remove(testDep);
156     }
157
158     @Override
159     public int hashCode() {
160         final int prime = 31;
161         int result = 1;
162         result = prime * result + ((code == null) ? 0 : code.hashCode());
163         result = prime * result + ((coverage == null) ? 0 : coverage.hashCode());
164         result = prime * result + ((dependencies == null) ? 0 : dependencies.hashCode());
165         result = prime * result + (ignored ? 1231 : 1237);
166         result = prime * result + ((name == null) ? 0 : name.hashCode());
167         result = prime * result + priority;
168         result = prime * result + ((unresolvedDependencies == null) ? 0 : unresolvedDependencies.hashCode());
169         result = prime * result + ((variables == null) ? 0 : variables.hashCode());
170         return result;
171     }
172
173     @Override
174     public boolean equals(Object obj) {
175         if (this == obj)
176             return true;
177         if (obj == null)
178             return false;
179         if (getClass() != obj.getClass())
180             return false;
181         ModelledSTSTest other = (ModelledSTSTest) obj;
182         if (code == null) {
183             if (other.code != null)
184                 return false;
185         } else if (!code.equals(other.code))
186             return false;
187         if (coverage == null) {
188             if (other.coverage != null)
189                 return false;
190         } else if (!coverage.equals(other.coverage))
191             return false;
192         if (dependencies == null) {
193             if (other.dependencies != null)
194                 return false;
195         } else if (!dependencies.equals(other.dependencies))
196             return false;
197         if (ignored != other.ignored)
198             return false;
199         if (name == null) {
200             if (other.name != null)
201                 return false;
202         } else if (!name.equals(other.name))
203             return false;
204         if (priority != other.priority)
205             return false;
206         if (unresolvedDependencies == null) {
207             if (other.unresolvedDependencies != null)
208                 return false;
209         } else if (!unresolvedDependencies.equals(other.unresolvedDependencies))
210             return false;
211         if (variables == null) {
212             if (other.variables != null)
213                 return false;
214         } else if (!variables.equals(other.variables))
215             return false;
216         return true;
217     }
218 }