From 9c866c82a391db00ae11913c61d39d7dc46b14d0 Mon Sep 17 00:00:00 2001 From: jsimomaa Date: Mon, 5 Jun 2017 22:28:33 +0300 Subject: [PATCH] Fixing NPE with modelled tests refs #7277 Change-Id: Ie3dc4c982fe01e469a0e302f3ace0e5f484737f6 --- .../graph/Tests.pgraph | 1 + .../modelled/junit/v2/ModelledSTSRunner.java | 28 ++++++- .../junit/v2/ModelledSTSSuiteRunner.java | 6 +- .../modelled/utils/ModelledSTSSuite.java | 8 +- .../tests/modelled/utils/ModelledSTSTest.java | 73 ++++++++++--------- .../modelled/utils/STSSuiteTestCollector.java | 3 +- 6 files changed, 79 insertions(+), 40 deletions(-) diff --git a/bundles/org.simantics.tests.modelled.ontology/graph/Tests.pgraph b/bundles/org.simantics.tests.modelled.ontology/graph/Tests.pgraph index e3aa1f61f..59d9e578c 100644 --- a/bundles/org.simantics.tests.modelled.ontology/graph/Tests.pgraph +++ b/bundles/org.simantics.tests.modelled.ontology/graph/Tests.pgraph @@ -41,6 +41,7 @@ TESTS.STSSuite { + public static final String EXCLUSION_FILTER = "org.simantics.tests.modelled.excludeFilter"; + private final List children; public ModelledSTSRunner(Class testClass) throws InitializationError { @@ -25,8 +28,20 @@ public class ModelledSTSRunner extends ParentRunner { try { initialize0(); Collection suites = STSSuiteTestCollector.collectTests(); - children = new ArrayList<>(suites.size()); - for (ModelledSTSSuite suite : suites) { + String exclusionFilter = System.getProperty(EXCLUSION_FILTER); + Collection filtered; + if (exclusionFilter != null) { + String[] filters = exclusionFilter.split(","); + filtered = suites.stream().filter(s -> !startsWithAny(s, filters)).collect(Collectors.toList()); + } else { + filtered = suites; + } + // Sort by priority + List sorted = filtered.stream().sorted((o1, o2) -> Integer.compare(o1.getPriority(), o2.getPriority())).collect(Collectors.toList()); + + + children = new ArrayList<>(sorted.size()); + for (ModelledSTSSuite suite : sorted) { children.add(new ModelledSTSSuiteRunner(suite)); } } catch (Exception e) { @@ -34,6 +49,15 @@ public class ModelledSTSRunner extends ParentRunner { } } + private static boolean startsWithAny(ModelledSTSSuite suite, String[] filters) { + for (String filter : filters) { + if (suite.getName().contains(filter)) { + return true; + } + } + return false; + } + @Override protected List getChildren() { return children; diff --git a/bundles/org.simantics.tests.modelled/src/org/simantics/tests/modelled/junit/v2/ModelledSTSSuiteRunner.java b/bundles/org.simantics.tests.modelled/src/org/simantics/tests/modelled/junit/v2/ModelledSTSSuiteRunner.java index 5b7a8ef0a..11931d824 100644 --- a/bundles/org.simantics.tests.modelled/src/org/simantics/tests/modelled/junit/v2/ModelledSTSSuiteRunner.java +++ b/bundles/org.simantics.tests.modelled/src/org/simantics/tests/modelled/junit/v2/ModelledSTSSuiteRunner.java @@ -1,6 +1,7 @@ package org.simantics.tests.modelled.junit.v2; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -18,7 +19,7 @@ public class ModelledSTSSuiteRunner extends ParentRunner private final ModelledSTSSuite suite; private final List children; - private Map> storedVariables; + private Map> storedVariables = new HashMap<>(); public ModelledSTSSuiteRunner(ModelledSTSSuite suite) throws InitializationError { super(ModelledSTSSuiteRunner.class); @@ -58,10 +59,11 @@ public class ModelledSTSSuiteRunner extends ParentRunner variables.addAll(storedVars); } } + notifier.fireTestStarted(description); List newVars = child.runWithVars(variables); storedVariables.put(child.getTest().getName(), newVars); notifier.fireTestFinished(description); - } catch (Exception e) { + } catch (Throwable e) { notifier.fireTestFailure(new Failure(description, e)); } } diff --git a/bundles/org.simantics.tests.modelled/src/org/simantics/tests/modelled/utils/ModelledSTSSuite.java b/bundles/org.simantics.tests.modelled/src/org/simantics/tests/modelled/utils/ModelledSTSSuite.java index 7ccb3ccd3..87f95ca1f 100644 --- a/bundles/org.simantics.tests.modelled/src/org/simantics/tests/modelled/utils/ModelledSTSSuite.java +++ b/bundles/org.simantics.tests.modelled/src/org/simantics/tests/modelled/utils/ModelledSTSSuite.java @@ -20,14 +20,16 @@ public class ModelledSTSSuite { private String name; private List children; + private int priority; private String moduleNameFilter; private List moduleNameFilterPatterns = new ArrayList<>(); private CoverageBuilder coverageBuilder; private Map variables; - ModelledSTSSuite(String name, List children, String moduleNameFilter, Map variables) { + ModelledSTSSuite(String name, List children, String moduleNameFilter, int priority, Map variables) { this.name = name; + this.priority = priority; this.children = children; this.variables = variables; this.moduleNameFilter = moduleNameFilter; @@ -121,4 +123,8 @@ public class ModelledSTSSuite { return moduleNameFilterPatterns; } + public int getPriority() { + return priority; + } + } diff --git a/bundles/org.simantics.tests.modelled/src/org/simantics/tests/modelled/utils/ModelledSTSTest.java b/bundles/org.simantics.tests.modelled/src/org/simantics/tests/modelled/utils/ModelledSTSTest.java index 3430696b8..6d9e4f4fe 100644 --- a/bundles/org.simantics.tests.modelled/src/org/simantics/tests/modelled/utils/ModelledSTSTest.java +++ b/bundles/org.simantics.tests.modelled/src/org/simantics/tests/modelled/utils/ModelledSTSTest.java @@ -95,41 +95,46 @@ public class ModelledSTSTest { public List run(List vars) throws IOException { ModuleRepository repo = new ModuleRepository(SCLOsgi.SOURCE_REPOSITORY); - repo.setAdvisor(new ModuleCompilationOptionsAdvisor() { + try { + repo.setAdvisor(new ModuleCompilationOptionsAdvisor() { + + @Override + public ModuleCompilationOptions getOptions(String moduleName) { + // TODO: default to false + boolean coverage = true; + // TODO: add moduleName filters back + // for (Pattern p : getModuleNameFilterPatterns()) { + // if (p.matcher(moduleName.toLowerCase()).find()) { + // coverage = true; + // break; + // } + // } + return new ModuleCompilationOptions(coverage); + } + }); - @Override - public ModuleCompilationOptions getOptions(String moduleName) { - // TODO: default to false - boolean coverage = true; - // TODO: add moduleName filters back -// for (Pattern p : getModuleNameFilterPatterns()) { -// if (p.matcher(moduleName.toLowerCase()).find()) { -// coverage = true; -// break; -// } -// } - return new ModuleCompilationOptions(coverage); - } - }); - - SCLReportingHandler handler = (SCLReportingHandler) SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER); - CommandSession session = new CommandSession(repo, handler); - - for (CommandSessionVariable var : vars) - session.setVariable(var.getName(), var.getType(), var.getValue()); - - for (Map.Entry entry : variables.entrySet()) - session.setVariable(entry.getKey(), Types.STRING, entry.getValue()); - - new TestScriptExecutor(session, new BufferedReader(new StringReader(code)), handler, true).execute(); - STSSuiteTestCollector.setTestCoverage(this, session); - - // Return variables from this session - List result = new ArrayList<>(); - for (String var : session.getVariables()) - result.add(new CommandSessionVariable(var, session.getVariableType(var), session.getVariableValue(var))); - - return result; + SCLReportingHandler handler = (SCLReportingHandler) SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER); + CommandSession session = new CommandSession(repo, handler); + + for (CommandSessionVariable var : vars) + session.setVariable(var.getName(), var.getType(), var.getValue()); + + for (Map.Entry entry : variables.entrySet()) + session.setVariable(entry.getKey(), Types.STRING, entry.getValue()); + + new TestScriptExecutor(session, new BufferedReader(new StringReader(code)), handler, true).execute(); + STSSuiteTestCollector.setTestCoverage(this, session); + + // Return variables from this session + List result = new ArrayList<>(); + for (String var : session.getVariables()) + result.add(new CommandSessionVariable(var, session.getVariableType(var), session.getVariableValue(var))); + + return result; + } finally { + // remember to flush this repository + repo.flush(); + } } public Set getDependencies() { diff --git a/bundles/org.simantics.tests.modelled/src/org/simantics/tests/modelled/utils/STSSuiteTestCollector.java b/bundles/org.simantics.tests.modelled/src/org/simantics/tests/modelled/utils/STSSuiteTestCollector.java index 8917d1646..58aa323e1 100644 --- a/bundles/org.simantics.tests.modelled/src/org/simantics/tests/modelled/utils/STSSuiteTestCollector.java +++ b/bundles/org.simantics.tests.modelled/src/org/simantics/tests/modelled/utils/STSSuiteTestCollector.java @@ -102,6 +102,7 @@ public class STSSuiteTestCollector { TestsResource TESTS = TestsResource.getInstance(graph); String suiteName = graph.getURI(suite); String moduleNameFilter = graph.getPossibleRelatedValue2(suite, TESTS.STSSuite_moduleNameFilter, Bindings.STRING); + Integer priority = graph.getPossibleRelatedValue2(suite, TESTS.STSTest_executionPriority, Bindings.INTEGER); Layer0 L0 = Layer0.getInstance(graph); Collection stsVariables = graph.sync(new ObjectsWithType(suite, L0.ConsistsOf, TESTS.STSVariable)); @@ -111,7 +112,7 @@ public class STSSuiteTestCollector { String value = graph.getRelatedValue(stsVariable, TESTS.STSVariable_definition); variables.put(name, value); } - return new ModelledSTSSuite(suiteName, children, moduleNameFilter, variables); + return new ModelledSTSSuite(suiteName, children, moduleNameFilter, priority != null ? priority : -1, variables); } public static void setTestCoverage(ModelledSTSTest test, CommandSession session) { -- 2.47.1