package org.simantics.tests.modelled.ui;
-import java.io.BufferedReader;
-import java.io.StringReader;
import java.util.ArrayList;
-import java.util.Collection;
import java.util.Collections;
+import java.util.HashMap;
import java.util.List;
-import java.util.regex.Pattern;
-import java.util.regex.PatternSyntaxException;
+import java.util.Map;
+import java.util.stream.Collectors;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.swt.graphics.Image;
import org.simantics.Simantics;
-import org.simantics.databoard.Bindings;
import org.simantics.db.ReadGraph;
import org.simantics.db.Resource;
import org.simantics.db.common.request.ReadRequest;
import org.simantics.db.exception.DatabaseException;
import org.simantics.layer0.Layer0;
-import org.simantics.scl.compiler.commands.CommandSession;
-import org.simantics.scl.compiler.commands.TestScriptExecutor;
-import org.simantics.scl.compiler.module.Module;
import org.simantics.scl.compiler.module.coverage.CombinedCoverage;
-import org.simantics.scl.compiler.module.coverage.CoverageBuilder;
-import org.simantics.scl.compiler.module.coverage.CoverageUtils;
-import org.simantics.scl.compiler.module.options.ModuleCompilationOptions;
-import org.simantics.scl.compiler.module.options.ModuleCompilationOptionsAdvisor;
-import org.simantics.scl.compiler.module.repository.ModuleRepository;
-import org.simantics.scl.compiler.runtime.RuntimeModule;
-import org.simantics.scl.osgi.SCLOsgi;
+import org.simantics.scl.runtime.SCLContext;
import org.simantics.scl.runtime.reporting.AbstractSCLReportingHandler;
+import org.simantics.scl.runtime.reporting.SCLReportingHandler;
import org.simantics.tests.modelled.ontology.TestsResource;
-import org.simantics.utils.strings.AlphanumComparator;
+import org.simantics.tests.modelled.utils.ModelledSTSSuite;
+import org.simantics.tests.modelled.utils.ModelledSTSTest;
+import org.simantics.tests.modelled.utils.ModelledSTSTest.CommandSessionVariable;
+import org.simantics.tests.modelled.utils.STSSuiteTestCollector;
public class STSTestSuiteModel {
- static class STSTest {
+ private Map<String, List<CommandSessionVariable>> storedVars = new HashMap<>();
+
+ class STSTest {
+
+ private final ModelledSTSTest test;
- private final Resource test;
private final STSSuite parent;
- private final String definition;
- private final String name;
private boolean executed = false;
private long duration;
private boolean failed = false;
private boolean isRunning = false;
private List<String> output = new ArrayList<>();
- private CombinedCoverage coverage;
- private int priority;
- public STSTest(Resource test, STSSuite parent, String definition, String name, int executionPrioprity) {
+ public STSTest(ModelledSTSTest test, STSSuite parent) {
this.test = test;
this.parent = parent;
- this.definition = definition;
- this.name = name;
- this.priority = executionPrioprity;
}
public String getName() {
- return name;
+ return test.getName();
}
public String getLabel() {
StringBuilder sb = new StringBuilder();
- sb.append(name);
+ sb.append(getName());
if (executed || failed)
sb.append(" (").append(duration).append(" ms)");
return sb.toString();
}
public String getDefinition() {
- return definition;
+ return test.getCode();
}
public STSSuite getParent() {
return parent;
}
- public void execute(CommandSession session) {
+ public void execute() {
isRunning = true;
-
- TestScriptExecutor executor = new TestScriptExecutor(session, new BufferedReader(new StringReader(definition)), new AbstractSCLReportingHandler() {
-
- @Override
- public void print(String text) {
- appendOutput(text + "\n");
- }
-
- @Override
- public void printCommand(String command) {
- appendOutput("> " + command + "\n");
- }
-
- @Override
- public void printError(String error) {
- appendOutput(error + "\n");
- }
- });
long start = System.currentTimeMillis();
+ Object old = SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
try {
if (parent != null)
parent.startedCount++;
- executor.execute();
+
+ SCLContext.getCurrent().put(SCLReportingHandler.REPORTING_HANDLER, new AbstractSCLReportingHandler() {
+
+ @Override
+ public void print(String text) {
+ appendOutput(text + "\n");
+ }
+
+ @Override
+ public void printCommand(String command) {
+ appendOutput("> " + command + "\n");
+ }
+
+ @Override
+ public void printError(String error) {
+ appendOutput(error + "\n");
+ }
+ });
+ List<CommandSessionVariable> resolvedVars = new ArrayList<>();
+ for (String deps : test.getDependencies()) {
+ List<CommandSessionVariable> vars = storedVars.get(deps);
+ if (vars != null)
+ resolvedVars.addAll(vars);
+ }
+
+ List<CommandSessionVariable> vars = test.run(resolvedVars);
+ storedVars.put(test.getName(), vars);
+
executed = true;
} catch (Throwable t) {
t.printStackTrace();
isRunning = false;
long end = System.currentTimeMillis();
duration = end - start;
+
+ SCLContext.getCurrent().put(SCLReportingHandler.REPORTING_HANDLER, old);
}
-
}
protected void appendOutput(String text) {
}
public void setCoverage(CombinedCoverage coverage) {
- this.coverage = coverage;
+ test.setCoverage(coverage);
}
public CombinedCoverage getCoverage() {
- return coverage;
+ return test.getCoverage();
}
-
+
+ public int getPriority() {
+ return test.getPriority();
+ }
+
@Override
public String toString() {
- return name + " [priority=" + priority + ", executed=" + executed + ", duration=" + duration + "]";
+ return getName() + " [priority=" + getPriority() + ", executed=" + executed + ", duration=" + duration + "]";
+ }
+
+ public boolean isIgnored() {
+ return test.isIgnored();
}
}
- static class STSSuite {
-
- private List<Pattern> moduleNameFilterPatterns = new ArrayList<>();
- private final Resource suite;
- private final String name;
+ class STSSuite {
+
+ private ModelledSTSSuite suite;
private STSTest[] children;
private int startedCount;
private int errorCount;
private int failureCount;
- private CoverageBuilder coverageBuilder;
+ public int ignoredCount;
- public STSSuite(Resource suite, String name, String moduleNameFilter) {
+ public STSSuite(ModelledSTSSuite suite) {
this.suite = suite;
- this.name = name;
- for (String s : moduleNameFilter.split(",")) {
- try {
- s = s.trim().replaceAll("\\*", "\\\\w*").toLowerCase();
- moduleNameFilterPatterns.add(Pattern.compile(s));
- } catch (PatternSyntaxException e) {
- e.printStackTrace();
- }
- }
- }
-
- public void children(STSTest[] children) {
- this.children = children;
}
public STSTest[] getChildren() {
+ if (children == null)
+ children = suite.getSortedChildren().stream().map(modelledTest -> new STSTest(modelledTest, this)).collect(Collectors.toList()).toArray(new STSTest[suite.getChildren().size()]);
return children;
}
public String getName() {
- return name;
+ return suite.getName();
}
public String getLabel() {
StringBuilder sb = new StringBuilder();
- sb.append(name);
+ sb.append(getName());
long totalTime = 0;
- if (children != null) {
- for (STSTest test : children) {
+ if (getChildren() != null) {
+ for (STSTest test : getChildren()) {
if (test.executed || test.failed) {
totalTime += test.duration;
}
}
public boolean isRunning() {
- boolean running = false;
- if (children != null) {
- for (STSTest test: children) {
+ if (getChildren() != null) {
+ for (STSTest test: getChildren()) {
if (test.isRunning) {
- running = true;
- break;
+ return true;
}
}
}
- return running;
+ return false;
}
public boolean executed() {
- boolean executed = true;
- if (children != null) {
- for (STSTest test: children) {
+ if (getChildren() != null) {
+ for (STSTest test: getChildren()) {
if (!test.executed) {
- executed = false;
- break;
+ return false;
}
}
}
- return executed;
+ return true;
}
public boolean failed() {
- boolean failed = false;
- if (children != null) {
- for (STSTest test: children) {
+ if (getChildren() != null) {
+ for (STSTest test: getChildren()) {
if (test.failed) {
- failed = true;
- break;
+ return true;
}
}
}
- return failed;
- }
-
- public void addCoverage(List<Module> modules) {
- if (coverageBuilder == null) {
- coverageBuilder = new CoverageBuilder();
- }
- for (Module module : modules)
- coverageBuilder.addCoverage(module, true);
+ return false;
}
public CombinedCoverage getCoverage() {
- if (coverageBuilder == null)
- return null;
- return coverageBuilder.getCoverage();
+ return suite.getCoverage();
}
}
private STSSuite suite;
private STSTest test;
private final List<STSExecutionListener> listeners = new ArrayList<>();
+ private Job currentJob;
public STSTestSuiteModel() {
}
}
public void execute() {
-
- ModuleRepository repo = new ModuleRepository(SCLOsgi.SOURCE_REPOSITORY);
- if (suite != null) {
- repo.setAdvisor(new ModuleCompilationOptionsAdvisor() {
+ String command;
+ if (suite != null)
+ command = suite.getName();
+ else
+ command = test.getName();
+ if (currentJob != null)
+ currentJob.cancel();
+ currentJob = new Job(command) {
+ @Override
+ protected IStatus run(IProgressMonitor monitor) {
+ if (suite != null) {
+ executeSuite();
+ } else if (test != null) {
+ executeTest();
+ }
+ return Status.OK_STATUS;
+ }
+
+ @Override
+ protected void canceling() {
+ Thread thread = getThread();
+ if(thread != null)
+ thread.interrupt();
- @Override
- public ModuleCompilationOptions getOptions(String moduleName) {
- boolean coverage = false;
- for (Pattern p : suite.moduleNameFilterPatterns) {
- if (p.matcher(moduleName.toLowerCase()).find()) {
- coverage = true;
- break;
- }
- }
- return new ModuleCompilationOptions(coverage);
+ try {
+ Thread.sleep(1000);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
}
- });
- }
- CommandSession session = new CommandSession(repo, null);
- if (suite != null) {
- executeSuite(session);
- } else if (test != null) {
- executeTest(session);
- }
+
+ thread = getThread();
+ if(thread != null)
+ thread.stop();
+ }
+ };
+ currentJob.schedule();
+ }
+
+ public void interrupt() {
+ if (currentJob != null)
+ currentJob.cancel();
}
private void testExecuted() {
});
}
- private void executeSuite(CommandSession session) {
-
+ private void executeSuite() {
for (STSTest test : suite.getChildren()) {
- test.execute(session);
-
- Collection<RuntimeModule> runtimeModules = session.getRuntimeEnvironment().getRuntimeModules();
- List<Module> modules = new ArrayList<>(runtimeModules.size());
- for (RuntimeModule module : runtimeModules) {
- for (Pattern p : suite.moduleNameFilterPatterns) {
- if (p.matcher(module.getModule().getName().toLowerCase()).find()) {
- modules.add(module.getModule());
- }
- }
+ if (test.isIgnored()) {
+ testExecuted();
+ test.getParent().ignoredCount++;
+ continue;
}
- test.setCoverage(CoverageUtils.getCoverage(modules));
- suite.addCoverage(modules);
-
- CoverageUtils.resetCoverage(modules);
-
+ test.execute();
testExecuted();
}
-
}
-
- private void executeTest(CommandSession session) {
-
- test.execute(session);
- testExecuted();
-
- Collection<RuntimeModule> runtimeModules = session.getRuntimeEnvironment().getRuntimeModules();
- List<Module> modules = new ArrayList<>(runtimeModules.size());
- for (RuntimeModule module : runtimeModules) {
- modules.add(module.getModule());
- }
- test.setCoverage(CoverageUtils.getCoverage(modules));
-
- CoverageUtils.resetCoverage(modules);
+ private void executeTest() {
+ test.execute();
+ testExecuted();
}
-
public boolean hasChildren(Object element) {
if (element instanceof STSTest) {
Layer0 L0 = Layer0.getInstance(graph);
TestsResource TESTS = TestsResource.getInstance(graph);
if (graph.isInstanceOf(root, TESTS.STSTest)) {
- String testName = graph.getRelatedValue2(root, L0.HasName, Bindings.STRING);
- String definition = graph.getRelatedValue2(root, TESTS.STSTest_definition, Bindings.STRING);
- Integer executionPrioprity = graph.getRelatedValue2(root, TESTS.STSTest_executionPriority, Bindings.INTEGER);
- test = new STSTest(root, null, definition, testName, executionPrioprity);
+ test = new STSTest(STSSuiteTestCollector.toModelledTest(graph, root), null);
} else if (graph.isInstanceOf(root, TESTS.STSSuite)) {
- String suiteName = graph.getRelatedValue2(root, L0.HasName, Bindings.STRING);
- String moduleNameFilter = graph.getPossibleRelatedValue2(root, TESTS.STSSuite_moduleNameFilter, Bindings.STRING);
- suite = new STSSuite(root, suiteName, moduleNameFilter);
- List<STSTest> tests = new ArrayList<>();
- for (Resource test : graph.getObjects(root, L0.ConsistsOf)) {
- String testName = graph.getRelatedValue2(test, L0.HasName, Bindings.STRING);
- String definition = graph.getRelatedValue2(test, TESTS.STSTest_definition, Bindings.STRING);
- Integer executionPrioprity = graph.getRelatedValue2(test, TESTS.STSTest_executionPriority, Bindings.INTEGER);
- tests.add(new STSTest(test, suite, definition, testName, executionPrioprity));
- }
- Collections.sort(tests, (o1, o2) -> {
- if (o1.priority < o2.priority)
- return -1;
- else if (o1.priority > o2.priority)
- return 1;
- else return AlphanumComparator.COMPARATOR.compare(o1.name, o2.name);
- });
- suite.children(tests.toArray(new STSTest[tests.size()]));
+ List<ModelledSTSTest> tests = new ArrayList<>();
+ for (Resource test : graph.getObjects(root, L0.ConsistsOf))
+ tests.add(STSSuiteTestCollector.toModelledTest(graph, test));
+
+ suite = new STSSuite(STSSuiteTestCollector.toModelledSuite(graph, root, tests));
} else {
throw new IllegalArgumentException(root.toString());
}
}
public int getIgnoredCount() {
+ if (suite != null)
+ return suite.ignoredCount;
return 0;
}