X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.tests.modelled.ui%2Fsrc%2Forg%2Fsimantics%2Ftests%2Fmodelled%2Fui%2FSTSTestSuiteModel.java;h=b6cda3fdc817aaafc77e1560b89a16195eb2c3ab;hp=569f6fbdc7a6d8df1d11fb63876624cf79f97175;hb=1ec0193a5a5b8f368b03adb24acd762838ddf8ea;hpb=0ae2b770234dfc3cbb18bd38f324125cf0faca07 diff --git a/bundles/org.simantics.tests.modelled.ui/src/org/simantics/tests/modelled/ui/STSTestSuiteModel.java b/bundles/org.simantics.tests.modelled.ui/src/org/simantics/tests/modelled/ui/STSTestSuiteModel.java index 569f6fbdc..b6cda3fdc 100644 --- a/bundles/org.simantics.tests.modelled.ui/src/org/simantics/tests/modelled/ui/STSTestSuiteModel.java +++ b/bundles/org.simantics.tests.modelled.ui/src/org/simantics/tests/modelled/ui/STSTestSuiteModel.java @@ -1,106 +1,108 @@ 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> 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 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 resolvedVars = new ArrayList<>(); + for (String deps : test.getDependencies()) { + List vars = storedVars.get(deps); + if (vars != null) + resolvedVars.addAll(vars); + } + + List vars = test.run(resolvedVars); + storedVars.put(test.getName(), vars); + executed = true; } catch (Throwable t) { t.printStackTrace(); @@ -111,8 +113,9 @@ public class STSTestSuiteModel { isRunning = false; long end = System.currentTimeMillis(); duration = end - start; + + SCLContext.getCurrent().put(SCLReportingHandler.REPORTING_HANDLER, old); } - } protected void appendOutput(String text) { @@ -124,61 +127,56 @@ public class STSTestSuiteModel { } 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 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; } @@ -190,56 +188,40 @@ public class STSTestSuiteModel { } 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 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(); } } @@ -247,6 +229,7 @@ public class STSTestSuiteModel { private STSSuite suite; private STSTest test; private final List listeners = new ArrayList<>(); + private Job currentJob; public STSTestSuiteModel() { } @@ -268,30 +251,47 @@ public class 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() { @@ -300,46 +300,22 @@ public class STSTestSuiteModel { }); } - private void executeSuite(CommandSession session) { - + private void executeSuite() { for (STSTest test : suite.getChildren()) { - test.execute(session); - - Collection runtimeModules = session.getRuntimeEnvironment().getRuntimeModules(); - List 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 runtimeModules = session.getRuntimeEnvironment().getRuntimeModules(); - List 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) { @@ -418,29 +394,13 @@ public class STSTestSuiteModel { 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 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 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()); } @@ -467,6 +427,8 @@ public class STSTestSuiteModel { } public int getIgnoredCount() { + if (suite != null) + return suite.ignoredCount; return 0; }