]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Enhancements to modelled tests 99/599/2
authorjsimomaa <jani.simomaa@gmail.com>
Mon, 5 Jun 2017 12:14:49 +0000 (15:14 +0300)
committerJani Simomaa <jani.simomaa@semantum.fi>
Mon, 5 Jun 2017 17:53:48 +0000 (20:53 +0300)
* Single test has now an isolated and own SCL CommandSession
* Tests can depend on one another and the SCL variables are available
for the depenent tests
* Added support for declaring SCL variables under the test definition
(suitable e.g. for large literals for comparison)

refs #7277

Change-Id: I5089bca479f681b42ee955851ab50ce24a3158a3

15 files changed:
bundles/org.simantics.tests.modelled.ontology/graph/Tests.pgraph
bundles/org.simantics.tests.modelled.ui.ontology/graph/TestsUI.pgraph
bundles/org.simantics.tests.modelled.ui/scl/Simantics/TestsUI.scl
bundles/org.simantics.tests.modelled.ui/src/org/simantics/tests/modelled/ui/STSCounterPanel.java
bundles/org.simantics.tests.modelled.ui/src/org/simantics/tests/modelled/ui/STSTestEditor.java
bundles/org.simantics.tests.modelled.ui/src/org/simantics/tests/modelled/ui/STSTestRunnerView.java
bundles/org.simantics.tests.modelled.ui/src/org/simantics/tests/modelled/ui/STSTestSuiteModel.java
bundles/org.simantics.tests.modelled/scl/Simantics/Tests.scl
bundles/org.simantics.tests.modelled/src/org/simantics/tests/modelled/TestsGraphUtils.java
bundles/org.simantics.tests.modelled/src/org/simantics/tests/modelled/junit/v2/ModelledSTSRunner.java
bundles/org.simantics.tests.modelled/src/org/simantics/tests/modelled/junit/v2/ModelledSTSSuiteRunner.java
bundles/org.simantics.tests.modelled/src/org/simantics/tests/modelled/junit/v2/ModelledSTSTestRunner.java
bundles/org.simantics.tests.modelled/src/org/simantics/tests/modelled/utils/ModelledSTSSuite.java
bundles/org.simantics.tests.modelled/src/org/simantics/tests/modelled/utils/ModelledSTSTest.java
bundles/org.simantics.tests.modelled/src/org/simantics/tests/modelled/utils/STSSuiteTestCollector.java

index 8364abd1875c6cec0b9a99f27345a80d983a78e4..e3aa1f61f0a3aacd47b0ec72be8030506f191b26 100644 (file)
@@ -31,6 +31,7 @@ TESTS.STSTest <T L0.Entity
     @L0.assert TESTS.STSTest.executionPriority -1
     @L0.assert TESTS.ignore false
     @L0.assert TESTS.ignoreReason ""
+    @L0.assert TESTS.dependencies ""
 
 TESTS.STSTest : MOD.TypeWithChangeInformation 
 TESTS.STSTest : L0.TypeWithIdentifier
@@ -39,6 +40,18 @@ TESTS.STSSuite <T L0.Entity
     >-- TESTS.STSSuite.moduleNameFilter --> L0.String <R L0.HasProperty : L0.TotalFunction : SEL.GenericParameterType
     @L0.assert TESTS.STSSuite.moduleNameFilter ""
     @L0.assert TESTS.ignore false
+    @L0.assert TESTS.dependencies ""
 
 TESTS.STSSuite : MOD.TypeWithChangeInformation 
-TESTS.STSSuite : L0.TypeWithIdentifier
\ No newline at end of file
+TESTS.STSSuite : L0.TypeWithIdentifier
+
+TESTS.STSVariable <T L0.Entity
+    TESTS.STSVariable.definition ""
+
+TESTS.STSVariable.definition <R L0.HasProperty : L0.TotalFunction : SEL.GenericParameterType
+    L0.HasRange L0.String
+    L0.HasLabel "Variable definition"
+
+TESTS.dependencies <R L0.HasProperty : L0.TotalFunction : SEL.GenericParameterType
+    L0.HasRange L0.String
+    L0.HasLabel "Test dependencies"
index f92524b797ae40dd43dee90286f60a2e01c8db5f..a0e60d0b31e2882935a98a802a8bf7f82776757f 100644 (file)
@@ -36,6 +36,8 @@ MBC
     @VP.namedConstantImageRule    TestsUI.Contributions.SuiteImage          TESTS.STSSuite                         TestsUI.testSuiteImage
     @VP.namedRelationChildRule    TestsUI.Contributions.STSTests            L0.Entity                              L0.ConsistsOf TESTS.STSTest
     @VP.customImageRule           TESTS.STSTest                             MBC.TestImageRule
+    @VP.namedRelationChildRule    TestsUI.Contributions.STSVariables        L0.Entity                              L0.ConsistsOf TESTS.STSVariable
+    @VP.namedConstantImageRule    TestsUI.Contributions.VariableImage       TESTS.STSVariable                      SILK.page_white_edit
 
 MBC
     VP.BrowseContext.HasVisualsContribution _ : VP.VisualsContribution
@@ -55,6 +57,8 @@ ACTIONS.NewSTSTest
   @MOD.sclAction "createSTSTestAction"
 ACTIONS.RunSTSTest
   @MOD.sclAction "runSTSTestAction"
+ACTIONS.NewSTSVariable
+  @MOD.sclAction "createSTSVariableAction"
 
 MAC
     VP.BrowseContext.HasActionContribution TestsUI.Contributions.NewSTSSuite : VP.ActionContribution
@@ -76,10 +80,12 @@ MAC
         VP.ActionContribution.HasImage SILK.control_play
         VP.ActionContribution.HasCategory VP.EditActionCategory
         VP.ActionContribution.HasNodeType TESTS.STSSuite
+        VP.ActionContribution.HasNodeType TESTS.STSTest
         VP.ActionContribution.HasAction ACTIONS.RunSTSTest
     VP.BrowseContext.HasActionContribution _ : VP.ActionContribution
-        L0.HasLabel "Run"
-        VP.ActionContribution.HasImage SILK.control_play
-        VP.ActionContribution.HasCategory VP.EditActionCategory
+        L0.HasLabel "STS Variable"
+        VP.ActionContribution.HasImage SILK.page_white_edit
+        VP.ActionContribution.HasCategory VP.NewActionCategory
+        VP.ActionContribution.HasNodeType TESTS.STSSuite
         VP.ActionContribution.HasNodeType TESTS.STSTest
-        VP.ActionContribution.HasAction ACTIONS.RunSTSTest
\ No newline at end of file
+        VP.ActionContribution.HasAction ACTIONS.NewSTSVariable
index d834c36756fe5a8d3ab254dc0ff34b608af9d23f..53218224a089feac163ae5e475d245afe05a25e2 100644 (file)
@@ -11,4 +11,9 @@ importJava "org.simantics.tests.modelled.ui.TestsUIUtils" where
 createSTSSuiteAction :: Resource -> <Proc> ()
 createSTSSuiteAction res = do
   syncWrite (\() -> createSTSSuite res)
+  ()
+
+createSTSVariableAction :: Resource -> <Proc> ()
+createSTSVariableAction res = do
+  syncWrite(\() -> createSTSVariable res)
   ()
\ No newline at end of file
index e93c793e74aa90e94362392770edb0440a61b5fe..d4d58129d61ce0f42f13d106ab3d983d121fe4bd 100644 (file)
@@ -28,6 +28,7 @@ import org.eclipse.swt.widgets.Text;
  */
 public class STSCounterPanel extends Composite {
     protected Text numberOfErrors;
+    protected Text numberOfSkipped;
     protected Text numberOfFailures;
     protected Text numberOfRuns;
     protected int total;
@@ -46,6 +47,7 @@ public class STSCounterPanel extends Composite {
         setLayout(gridLayout);
 
         numberOfRuns= createLabel("Runs:", null, " 0/0  "); //$NON-NLS-1$
+        numberOfSkipped = createLabel("Skipped:", null," 0 ");
         numberOfErrors= createLabel("Errors:", fErrorIcon, " 0 "); //$NON-NLS-1$
         numberOfFailures= createLabel("Failures:", fFailureIcon, " 0 "); //$NON-NLS-1$
 
@@ -129,6 +131,11 @@ public class STSCounterPanel extends Composite {
         assumptionFailedCount= assumptionFailureCount;
     }
 
+    public void setIgnoredValue(int value) {
+        numberOfSkipped.setText(Integer.toString(value));
+        redraw();
+    }
+    
     public void setErrorValue(int value) {
         numberOfErrors.setText(Integer.toString(value));
         redraw();
index 21bb5e4d7011bd87b1d6efefb13112cae22623de..ea00d8f07ab2f61e695826b831e77cb8edcff5e7 100644 (file)
@@ -4,5 +4,11 @@ import org.simantics.scl.ui.editor2.SCLModuleEditor2;
 
 public class STSTestEditor extends SCLModuleEditor2 {
 
+    
+    @Override
+    public void setFocus() {
+        // TODO Auto-generated method stub
+        super.setFocus();
+    }
 
 }
index 203f2832501c57d98aa48bd54e344c833fb373d3..b8b8094491da298706a44b976731a051b487674e 100644 (file)
@@ -7,6 +7,7 @@ import javax.annotation.PostConstruct;
 import javax.annotation.PreDestroy;
 
 import org.eclipse.e4.ui.di.Focus;
+import org.eclipse.e4.ui.model.application.ui.basic.MPart;
 import org.eclipse.jface.layout.TreeColumnLayout;
 import org.eclipse.jface.viewers.CellLabelProvider;
 import org.eclipse.jface.viewers.ColumnWeightData;
@@ -32,6 +33,9 @@ import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Event;
 import org.eclipse.swt.widgets.Layout;
+import org.eclipse.swt.widgets.Listener;
+import org.eclipse.swt.widgets.Menu;
+import org.eclipse.swt.widgets.MenuItem;
 import org.eclipse.swt.widgets.Tree;
 import org.eclipse.swt.widgets.TreeColumn;
 import org.eclipse.ui.part.PageBook;
@@ -54,19 +58,21 @@ public class STSTestRunnerView {
     private Composite counterComposite;
 
     @PostConstruct
-    void createView(Composite parent) {
+    void createView(MPart part, Composite parent) {
         
         GridLayout gridLayout= new GridLayout();
         gridLayout.marginWidth= 0;
         gridLayout.marginHeight= 0;
         parent.setLayout(gridLayout);
         
-        counterComposite= createProgressCountPanel(parent);
+        counterComposite = createProgressCountPanel(parent);
         counterComposite.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
         counterComposite.pack();
         
         SashForm sashForm = createSashForm(parent);
         sashForm.setLayoutData(new GridData(GridData.FILL_BOTH));
+        
+//        IToolBarManager toolBarManager = getViewSite().getActionBars().getToolBarManager();
     }
     
     private SashForm createSashForm(Composite parent) {
@@ -335,6 +341,26 @@ public class STSTestRunnerView {
         treeViewer.setContentProvider(provider);
         treeViewer.setLabelProvider(provider);
 
+        
+        Menu menu = new Menu(treeViewer.getControl());
+        
+        MenuItem item1 = new MenuItem(menu, SWT.PUSH);
+        item1.setText("Stop");
+        item1.addListener(SWT.Selection, new Listener() {
+            
+            @Override
+            public void handleEvent(Event event) {
+                TreeSelection selec = (TreeSelection) treeViewer.getSelection();
+                if (!selec.isEmpty()) {
+                    Object elem = selec.getFirstElement();
+                    provider.getModel().interrupt();
+                    
+                }
+            }
+        });
+        
+        treeViewer.getControl().setMenu(menu);
+        
 
         viewerbook.showPage(treeViewer.getTree());
         
@@ -388,6 +414,7 @@ public class STSTestRunnerView {
 
        counterPanel.setTotal(totalCount);
        counterPanel.setRunValue(startedCount, ignoredCount, assumptionFailureCount);
+       counterPanel.setIgnoredValue(ignoredCount);
        counterPanel.setErrorValue(errorCount);
        counterPanel.setFailureValue(failureCount);
 
index 976efa2c2d2a0aab10b40125ed664672c2b4f9d9..b6cda3fdc817aaafc77e1560b89a16195eb2c3ab 100644 (file)
@@ -1,13 +1,16 @@
 package org.simantics.tests.modelled.ui;
 
-import java.io.BufferedReader;
-import java.io.StringReader;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
-import java.util.regex.Pattern;
+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.db.ReadGraph;
@@ -15,22 +18,21 @@ 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.coverage.CombinedCoverage;
-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.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.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;
         
@@ -66,31 +68,41 @@ public class STSTestSuiteModel {
             return parent;
         }
 
-        public void execute(CommandSession session) {
+        public void execute() {
             isRunning = true;
-            
-            TestScriptExecutor executor = new TestScriptExecutor(session, new BufferedReader(new StringReader(getDefinition())), 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");
-                }
-            }, true);
             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();
@@ -101,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) {
@@ -135,7 +148,7 @@ public class STSTestSuiteModel {
         }
     }
 
-    static class STSSuite {
+    class STSSuite {
 
         private ModelledSTSSuite suite;
         private STSTest[] children;
@@ -216,6 +229,7 @@ public class STSTestSuiteModel {
     private STSSuite suite;
     private STSTest test;
     private final List<STSExecutionListener> listeners = new ArrayList<>();
+    private Job currentJob;
     
     public STSTestSuiteModel() {
     }
@@ -237,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.suite.getModuleNameFilterPatterns()) {
-                        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() {
@@ -269,23 +300,21 @@ public class STSTestSuiteModel {
         });
     }
     
-    private void executeSuite(CommandSession session) {
+    private void executeSuite() {
         for (STSTest test : suite.getChildren()) {
             if (test.isIgnored()) {
                 testExecuted();
                 test.getParent().ignoredCount++;
                 continue;
             }
-            test.execute(session);
-            STSSuiteTestCollector.setSuiteCoverage(test.test, suite.suite, session);
+            test.execute();
             testExecuted();
         }
     }
 
-    private void executeTest(CommandSession session) {
-        test.execute(session);
+    private void executeTest() {
+        test.execute();
         testExecuted();
-        STSSuiteTestCollector.setTestCoverage(test.test, session);
     }
 
     public boolean hasChildren(Object element) {
index fe80e1303cb4ae115e742312fcc2c3aea36c971f..ce3cfb1548a0b316571afd533c443f84087558ea 100644 (file)
@@ -1,5 +1,6 @@
 include "Simantics/DB"
 
 importJava "org.simantics.tests.modelled.TestsGraphUtils" where
-    createSTSTest :: Resource -> <Proc, WriteGraph> Resource
-    createSTSSuite :: Resource -> <WriteGraph, Proc> ()
+    createSTSTest :: Resource -> <WriteGraph, Proc> Resource
+    createSTSSuite :: Resource -> <WriteGraph, Proc> Resource
+    createSTSVariable :: Resource -> <WriteGraph, Proc> Resource
index e9ce7d06ff24d775992a896969cb71ccb034a0a7..c5253bd5a37ad138d159b4b7b938ad3a159cbf6e 100644 (file)
@@ -31,9 +31,24 @@ public class TestsGraphUtils {
 
     private static final String STS_TEST_PREFIX = "STSTest";
     private static final String STS_SUITE_PREFIX = "STSSuite";
+    private static final String STS_VARIABLE_PREFIX = "STSVariable";
     
     private TestsGraphUtils() {}
     
+    public static Resource createSTSVariable(WriteGraph graph, Resource parent) throws DatabaseException {
+        String name = NameUtils.findFreshEscapedName(graph, STS_VARIABLE_PREFIX, parent);
+        Resource stsVariable = graph.newResource();
+        
+        Layer0 L0 = Layer0.getInstance(graph);
+        TestsResource TESTS = TestsResource.getInstance(graph);
+        
+        graph.claim(parent, L0.ConsistsOf, L0.PartOf, stsVariable);
+        graph.claim(stsVariable, L0.InstanceOf, TESTS.STSVariable);
+        graph.claimLiteral(stsVariable, L0.HasName, name, Bindings.STRING);
+        graph.claimLiteral(stsVariable, TESTS.STSVariable_definition, "", Bindings.STRING);
+        return stsVariable;
+    }
+    
     public static Resource createSTSTest(WriteGraph graph, Resource parent) throws DatabaseException {
         String name = NameUtils.findFreshEscapedName(graph, STS_TEST_PREFIX, parent);
         Resource stsTest = graph.newResource();
index c74f6efaea0fb0485f377177eba45ecfe26191d2..e77224b58c72caa59b1e290831e817803844368e 100644 (file)
@@ -1,7 +1,8 @@
 package org.simantics.tests.modelled.junit.v2;
 
+import java.util.ArrayList;
+import java.util.Collection;
 import java.util.List;
-import java.util.stream.Collectors;
 
 import org.eclipse.core.runtime.NullProgressMonitor;
 import org.junit.runner.Description;
@@ -12,28 +13,30 @@ import org.junit.runners.ParentRunner;
 import org.junit.runners.model.InitializationError;
 import org.simantics.Simantics;
 import org.simantics.db.testing.common.AcornTests;
-import org.simantics.scl.compiler.commands.CommandSession;
-import org.simantics.scl.osgi.SCLOsgi;
+import org.simantics.tests.modelled.utils.ModelledSTSSuite;
 import org.simantics.tests.modelled.utils.STSSuiteTestCollector;
 
 public class ModelledSTSRunner extends ParentRunner<ModelledSTSSuiteRunner> {
 
-    private CommandSession commandSession;
+    private final List<ModelledSTSSuiteRunner> children;
 
-    public ModelledSTSRunner(Class<?> testClass) throws Exception {
+    public ModelledSTSRunner(Class<?> testClass) throws InitializationError {
         super(testClass);
-        initialize0();
+        try {
+            initialize0();
+            Collection<ModelledSTSSuite> suites = STSSuiteTestCollector.collectTests();
+            children = new ArrayList<>(suites.size());
+            for (ModelledSTSSuite suite : suites) {
+                children.add(new ModelledSTSSuiteRunner(suite));
+            }
+        } catch (Exception e) {
+            throw new InitializationError(e);
+        }
     }
 
     @Override
     protected List<ModelledSTSSuiteRunner> getChildren() {
-        return STSSuiteTestCollector.collectTests().stream().map(suite -> {
-            try {
-                return new ModelledSTSSuiteRunner(suite);
-            } catch (Exception e) {
-                throw new RuntimeException(e);
-            }
-        }).collect(Collectors.toList());
+        return children;
     }
 
     @Override
@@ -55,7 +58,6 @@ public class ModelledSTSRunner extends ParentRunner<ModelledSTSSuiteRunner> {
 
     @Override
     protected void runChild(ModelledSTSSuiteRunner child, RunNotifier notifier) {
-        child.setCommandSesssion(commandSession);
         child.run(notifier);
         // TODO: Add coverage reporting to ModelledSTSRunner
 //        CombinedCoverage cover = child.getCoverage();
@@ -70,7 +72,6 @@ public class ModelledSTSRunner extends ParentRunner<ModelledSTSSuiteRunner> {
 
     private void initialize0() throws Exception {
         AcornTests.newSimanticsWorkspace(null, null);
-        this.commandSession = new CommandSession(SCLOsgi.MODULE_REPOSITORY, null);
         initialize();
     }
 
index a568976cc6da745db0beba6a818ad738c98487c7..5b7a8ef0ac3d5b352052476055bd160b7c58531b 100644 (file)
@@ -1,26 +1,32 @@
 package org.simantics.tests.modelled.junit.v2;
 
+import java.util.ArrayList;
 import java.util.List;
-import java.util.stream.Collectors;
+import java.util.Map;
 
 import org.junit.runner.Description;
 import org.junit.runner.notification.Failure;
 import org.junit.runner.notification.RunNotifier;
 import org.junit.runners.ParentRunner;
-import org.simantics.scl.compiler.commands.CommandSession;
+import org.junit.runners.model.InitializationError;
 import org.simantics.scl.compiler.module.coverage.CombinedCoverage;
-import org.simantics.scl.osgi.SCLOsgi;
 import org.simantics.tests.modelled.utils.ModelledSTSSuite;
-import org.simantics.tests.modelled.utils.STSSuiteTestCollector;
+import org.simantics.tests.modelled.utils.ModelledSTSTest;
+import org.simantics.tests.modelled.utils.ModelledSTSTest.CommandSessionVariable;
 
 public class ModelledSTSSuiteRunner extends ParentRunner<ModelledSTSTestRunner> {
 
     private final ModelledSTSSuite suite;
-    private CommandSession commandSession;
+    private final List<ModelledSTSTestRunner> children;
+    private Map<String, List<CommandSessionVariable>> storedVariables;
 
-    public ModelledSTSSuiteRunner(ModelledSTSSuite suite) throws Exception {
+    public ModelledSTSSuiteRunner(ModelledSTSSuite suite) throws InitializationError {
         super(ModelledSTSSuiteRunner.class);
         this.suite = suite;
+        this.children = new ArrayList<>(suite.getChildren().size());
+        for (ModelledSTSTest test : suite.getSortedChildren()) {
+            children.add(new ModelledSTSTestRunner(test));
+        }
     }
 
     @Override
@@ -30,7 +36,7 @@ public class ModelledSTSSuiteRunner extends ParentRunner<ModelledSTSTestRunner>
 
     @Override
     protected List<ModelledSTSTestRunner> getChildren() {
-        return suite.getChildren().stream().map(test -> new ModelledSTSTestRunner(test)).collect(Collectors.toList());
+        return children;
     }
 
     @Override
@@ -44,15 +50,21 @@ public class ModelledSTSSuiteRunner extends ParentRunner<ModelledSTSTestRunner>
         if (isIgnored(child)) {
             notifier.fireTestIgnored(description);
         } else {
-            notifier.fireTestStarted(description);
             try {
-                child.run(getCommandSession());
+                List<CommandSessionVariable> variables = new ArrayList<>();
+                for (String dep : child.getTest().getDependencies()) {
+                    List<CommandSessionVariable> storedVars = storedVariables.get(dep);
+                    if (storedVars != null) {
+                        variables.addAll(storedVars);
+                    }
+                }
+                List<CommandSessionVariable> newVars = child.runWithVars(variables);
+                storedVariables.put(child.getTest().getName(), newVars);
                 notifier.fireTestFinished(description);
-                STSSuiteTestCollector.setSuiteCoverage(child.getTest(), suite, getCommandSession());
-            } catch (Throwable e) {
+            } catch (Exception e) {
                 notifier.fireTestFailure(new Failure(description, e));
             }
-        }
+        } 
     }
 
     @Override
@@ -60,16 +72,6 @@ public class ModelledSTSSuiteRunner extends ParentRunner<ModelledSTSTestRunner>
         return child.isIgnored();
     }
 
-    public void setCommandSesssion(CommandSession commandSession) {
-        this.commandSession = commandSession;
-    }
-
-    public CommandSession getCommandSession() {
-        if (commandSession == null)
-            commandSession = new CommandSession(SCLOsgi.MODULE_REPOSITORY, null);
-        return commandSession;
-    }
-
     public CombinedCoverage getCoverage() {
         return suite.getCoverage();
     }
index a2acbfcb8d23edc93ab0a64d76c2ff6bc56a4377..5f8ea6c16117fe90f4e4bad19b7095a54200e756 100644 (file)
@@ -1,18 +1,14 @@
 package org.simantics.tests.modelled.junit.v2;
 
-import java.io.BufferedReader;
-import java.io.StringReader;
+import java.io.IOException;
+import java.util.List;
 
 import org.junit.runner.Description;
 import org.junit.runner.Runner;
+import org.junit.runner.notification.Failure;
 import org.junit.runner.notification.RunNotifier;
-import org.simantics.scl.compiler.commands.CommandSession;
-import org.simantics.scl.compiler.commands.TestScriptExecutor;
-import org.simantics.scl.osgi.SCLOsgi;
-import org.simantics.scl.runtime.SCLContext;
-import org.simantics.scl.runtime.reporting.SCLReportingHandler;
 import org.simantics.tests.modelled.utils.ModelledSTSTest;
-import org.simantics.tests.modelled.utils.STSSuiteTestCollector;
+import org.simantics.tests.modelled.utils.ModelledSTSTest.CommandSessionVariable;
 
 public class ModelledSTSTestRunner extends Runner {
 
@@ -23,41 +19,20 @@ public class ModelledSTSTestRunner extends Runner {
         this.test = test;
     }
 
-    public String getName() {
-        return test.getName();
-    }
-
     @Override
     public Description getDescription() {
         if (description == null)
-            description = Description.createTestDescription(ModelledSTSTestRunner.class, getName());
+            description = Description.createTestDescription(ModelledSTSTestRunner.class, test.getName());
         return description;
     }
 
-    /**
-     * This method is called from ModelledSTSSuite (ParentRunner) with the same
-     * CommandSession
-     * 
-     * @param session
-     */
-    public void run(CommandSession session) {
-        try (BufferedReader reader = new BufferedReader(new StringReader(test.getCode()))) {
-            SCLReportingHandler handler = (SCLReportingHandler) SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
-            new TestScriptExecutor(session, reader, handler, true).execute();
-            STSSuiteTestCollector.setTestCoverage(test, session);
-        } catch (Exception e) {
-            throw new RuntimeException(e);
-        }
+    public List<CommandSessionVariable> runWithVars(List<CommandSessionVariable> vars) throws IOException {
+        return test.run(vars);
     }
-
+    
     @Override
     public void run(RunNotifier notifier) {
-        notifier.fireTestStarted(getDescription());
-        try {
-            run(new CommandSession(SCLOsgi.MODULE_REPOSITORY, null));
-        } finally {
-            notifier.fireTestFinished(getDescription());
-        }
+        throw new UnsupportedOperationException();
     }
 
     public boolean isIgnored() {
index b856787cfc560c4c634e471909cdc86e48d49bdb..7ccb3ccd358f52b7b443476ff8ade20b8c05883b 100644 (file)
@@ -1,10 +1,15 @@
 package org.simantics.tests.modelled.utils;
 
 import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.HashSet;
+import java.util.LinkedList;
 import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
 import java.util.regex.Pattern;
 import java.util.regex.PatternSyntaxException;
-import java.util.stream.Collectors;
 
 import org.simantics.scl.compiler.module.Module;
 import org.simantics.scl.compiler.module.coverage.CombinedCoverage;
@@ -19,10 +24,12 @@ public class ModelledSTSSuite {
     private List<Pattern> moduleNameFilterPatterns = new ArrayList<>();
 
     private CoverageBuilder coverageBuilder;
+    private Map<String, String> variables;
 
-    ModelledSTSSuite(String name, List<ModelledSTSTest> children, String moduleNameFilter) {
+    ModelledSTSSuite(String name, List<ModelledSTSTest> children, String moduleNameFilter, Map<String, String> variables) {
         this.name = name;
         this.children = children;
+        this.variables = variables;
         this.moduleNameFilter = moduleNameFilter;
         for (String s : moduleNameFilter.split(",")) {
             try {
@@ -42,8 +49,46 @@ public class ModelledSTSSuite {
         return children;
     }
 
+    static Comparator<ModelledSTSTest> comparator = (test1, test2) -> compareTests(test1, test2);
+    
     public List<ModelledSTSTest> getSortedChildren() {
-        return new ArrayList<>(children).stream().sorted(ModelledSTSSuite::compareTests).collect(Collectors.toList());
+        Set<ModelledSTSTest> testsWithDeps = new HashSet<>();
+        // This TreeMap sorts the tests with the comparator 
+        TreeMap<ModelledSTSTest, String> sortedTests = new TreeMap<>(comparator);
+        for (ModelledSTSTest test : getChildren()) {
+            Set<String> testDependencies = test.getDependencies();
+            if (testDependencies.isEmpty()) {
+                // First tests that have no dependencies
+                sortedTests.put(test, test.getName());
+            } else {
+                // These are resolved later
+                testsWithDeps.add(test);
+            }
+        }
+        
+        // Construct a LinkedList that is returned as a result
+        LinkedList<ModelledSTSTest> results = new LinkedList<>(sortedTests.keySet());
+        
+//        Set<ModelledSTSTest> temp = new HashSet<>(testsWithDeps);
+        // Now resolve tests with dependencies
+        for (ModelledSTSTest testWithDep : testsWithDeps) {
+            boolean satisfied = true;
+            for (String dep : testWithDep.getDependencies()) {
+                if (!sortedTests.containsValue(dep)) {
+                    satisfied = false;
+                } else {
+                    testWithDep.resolveDependency(dep);
+                }
+            }
+            if (satisfied) {
+                results.addLast(testWithDep);
+                sortedTests.put(testWithDep, testWithDep.getName());
+            } else {
+                // Not satisfied
+                System.out.println(testWithDep.getName() + " not satisfied");
+            }
+        }
+        return results;
     }
 
     private static int compareTests(ModelledSTSTest test1, ModelledSTSTest test2) {
index 00d825d2e215017e9de0d554ece44dd6afa4eeb5..3430696b81456e5959d35b001da7319df906baac 100644 (file)
@@ -1,6 +1,26 @@
 package org.simantics.tests.modelled.utils;
 
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.StringReader;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.regex.Pattern;
+
+import org.simantics.scl.compiler.commands.CommandSession;
+import org.simantics.scl.compiler.commands.TestScriptExecutor;
 import org.simantics.scl.compiler.module.coverage.CombinedCoverage;
+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.types.Type;
+import org.simantics.scl.compiler.types.Types;
+import org.simantics.scl.osgi.SCLOsgi;
+import org.simantics.scl.runtime.SCLContext;
+import org.simantics.scl.runtime.reporting.SCLReportingHandler;
 
 public class ModelledSTSTest {
 
@@ -8,14 +28,20 @@ public class ModelledSTSTest {
     private final String code;
     private final int priority;
     private final boolean ignored;
-
+    private Set<String> dependencies;
+    private Set<String> unresolvedDependencies;
+    
     private CombinedCoverage coverage;
+    private Map<String, String> variables;
 
-    ModelledSTSTest(String name, String code, int priority, boolean ignored) {
+    ModelledSTSTest(String name, String code, int priority, boolean ignored, Set<String> dependencies, Map<String, String> variables) {
         this.name = name;
         this.code = code;
         this.priority = priority;
         this.ignored = ignored;
+        this.dependencies = dependencies;
+        this.unresolvedDependencies = new HashSet<>(dependencies);
+        this.variables = variables;
     }
 
     public String getName() {
@@ -31,7 +57,7 @@ public class ModelledSTSTest {
     }
 
     public boolean isIgnored() {
-        return ignored;
+        return (ignored || !unresolvedDependencies.isEmpty());
     }
 
     public void setCoverage(CombinedCoverage coverage) {
@@ -41,4 +67,80 @@ public class ModelledSTSTest {
     public CombinedCoverage getCoverage() {
         return coverage;
     }
+
+    public static class CommandSessionVariable {
+
+        private final String name;
+        private final Type type;
+        private final Object value;
+
+        public CommandSessionVariable(String name, Type type, Object value) {
+            this.name = name;
+            this.type = type;
+            this.value = value;
+        }
+
+        public String getName() {
+            return name;
+        }
+
+        public Type getType() {
+            return type;
+        }
+
+        public Object getValue() {
+            return value;
+        }
+    }
+    
+    public List<CommandSessionVariable> run(List<CommandSessionVariable> vars) throws IOException {
+        ModuleRepository repo = new ModuleRepository(SCLOsgi.SOURCE_REPOSITORY);
+        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);
+            }
+        });
+        
+        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<String, String> 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<CommandSessionVariable> result = new ArrayList<>();
+        for (String var : session.getVariables())
+            result.add(new CommandSessionVariable(var, session.getVariableType(var), session.getVariableValue(var)));
+        
+        return result;
+    }
+
+    public Set<String> getDependencies() {
+        return dependencies;
+    }
+    
+    public Set<String> getUnresolvedDependencies() {
+        return unresolvedDependencies;
+    }
+    
+    public boolean resolveDependency(String testDep) {
+        return unresolvedDependencies.remove(testDep);
+    }
 }
index 10530e185b6dfcc833dc77ab5a262899cba76c40..8917d16462425ddf65e93b20636d19dcaa766a78 100644 (file)
@@ -1,10 +1,12 @@
 package org.simantics.tests.modelled.utils;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
-import java.util.Collections;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
 import java.util.regex.Pattern;
 
 import org.simantics.Simantics;
@@ -32,66 +34,84 @@ public class STSSuiteTestCollector {
     /**
      * TODO: The idea of this class was to collect all the tests from shared libraries and construct
      * JUnit tests out of them programmatically and then run them with JUnit to get results
+     * @throws DatabaseException 
      * 
      */
-    private static Collection<ModelledSTSSuite> collectTestsFromGraph() {
-        try {
-            Collection<ModelledSTSSuite> suitess = Simantics.getSession().syncRequest(new UniqueRead<Collection<ModelledSTSSuite>>() {
+    public static Collection<ModelledSTSSuite> collectTests() throws DatabaseException {
+        Collection<ModelledSTSSuite> suitess = Simantics.getSession().syncRequest(new UniqueRead<Collection<ModelledSTSSuite>>() {
 
-                @Override
-                public Collection<ModelledSTSSuite> perform(ReadGraph graph) throws DatabaseException {
-                    
-                    List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
+            @Override
+            public Collection<ModelledSTSSuite> perform(ReadGraph graph) throws DatabaseException {
+                
+                List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
+                if (LOGGER.isInfoEnabled())
+                    LOGGER.info("Found {} shared ontologies from graph",  sharedOntologies.size());
+                Collection<ModelledSTSSuite> suites = new HashSet<>();
+                TestsResource TESTS = TestsResource.getInstance(graph);
+                Layer0 L0 = Layer0.getInstance(graph);
+                for (Resource sharedOntology : sharedOntologies) {
                     if (LOGGER.isInfoEnabled())
-                        LOGGER.info("Found {} shared ontologies from graph",  sharedOntologies.size());
-                    Collection<ModelledSTSSuite> suites = new HashSet<>();
-                    TestsResource TESTS = TestsResource.getInstance(graph);
-                    Layer0 L0 = Layer0.getInstance(graph);
-                    for (Resource sharedOntology : sharedOntologies) {
-                        if (LOGGER.isInfoEnabled())
-                            LOGGER.info("Searching {} for modelled tests", graph.getURI(sharedOntology));
-                        List<Resource> stsSuites = ModelingUtils.searchByType(graph, sharedOntology, TESTS.STSSuite);
-                        for (Resource stsSuite : stsSuites) {
-                            try {
-                                Collection<Resource> tests = graph.syncRequest(new ObjectsWithType(stsSuite, L0.ConsistsOf, TESTS.STSTest));
-                                if (tests.isEmpty())
-                                    continue;
-    
-                                List<ModelledSTSTest> testRunners = new ArrayList<>(tests.size());
-                                for (Resource test : tests)
-                                    testRunners.add(toModelledTest(graph, test));
+                        LOGGER.info("Searching {} for modelled tests", graph.getURI(sharedOntology));
+                    List<Resource> stsSuites = ModelingUtils.searchByType(graph, sharedOntology, TESTS.STSSuite);
+                    for (Resource stsSuite : stsSuites) {
+                        try {
+                            Collection<Resource> tests = graph.syncRequest(new ObjectsWithType(stsSuite, L0.ConsistsOf, TESTS.STSTest));
+                            if (tests.isEmpty())
+                                continue;
+
+                            List<ModelledSTSTest> testRunners = new ArrayList<>(tests.size());
+                            for (Resource test : tests)
+                                testRunners.add(toModelledTest(graph, test));
 
-                                suites.add(toModelledSuite(graph, stsSuite, testRunners));
-                            } catch (Exception e) {
-                                LOGGER.error("", e);
-                            }
+                            suites.add(toModelledSuite(graph, stsSuite, testRunners));
+                        } catch (Exception e) {
+                            LOGGER.error("", e);
                         }
                     }
-                    return suites;
                 }
-            });
-            return suitess;
-        } catch (DatabaseException e) {
-            LOGGER.error("Could not find modelled tests", e);
-            return Collections.emptyList();
-        }
+                return suites;
+            }
+        });
+        return suitess;
     }
     
 
     public static ModelledSTSTest toModelledTest(ReadGraph graph, Resource test) throws DatabaseException {
+        Layer0 L0 = Layer0.getInstance(graph);
         TestsResource TESTS = TestsResource.getInstance(graph);
-        String testName = graph.getRelatedValue(test, Layer0.getInstance(graph).HasName, Bindings.STRING);
+        String testName = graph.getRelatedValue(test, L0.HasName, Bindings.STRING);
         String code = graph.getRelatedValue(test, TESTS.STSTest_definition, Bindings.STRING);
         Integer priority = graph.getPossibleRelatedValue(test, TESTS.STSTest_executionPriority, Bindings.INTEGER);
         Boolean ignored = graph.getPossibleRelatedValue(test, TESTS.ignore, Bindings.BOOLEAN);
-        return new ModelledSTSTest(testName, code, priority != null ? priority : -1, ignored != null ? ignored : false);
+        
+        String dependencies = graph.getPossibleRelatedValue(test, TESTS.dependencies, Bindings.STRING);
+        String[] actualDeps = dependencies.isEmpty() ? new String[0] : dependencies.split(",");
+        
+        // collect variables
+        Collection<Resource> stsVariables = graph.getObjects(test, L0.ConsistsOf);
+        Map<String, String> variables = new HashMap<>(stsVariables.size());
+        for (Resource stsVariable : stsVariables) {
+            String name = graph.getRelatedValue(stsVariable, L0.HasName, Bindings.STRING);
+            String value = graph.getRelatedValue(stsVariable, TESTS.STSVariable_definition);
+            variables.put(name, value);
+        }
+        return new ModelledSTSTest(testName, code, priority != null ? priority : -1, ignored != null ? ignored : false, new HashSet<>(Arrays.asList(actualDeps)), variables);
     }
 
     public static ModelledSTSSuite toModelledSuite(ReadGraph graph, Resource suite, List<ModelledSTSTest> children) throws DatabaseException {
         TestsResource TESTS = TestsResource.getInstance(graph);
         String suiteName = graph.getURI(suite);
         String moduleNameFilter = graph.getPossibleRelatedValue2(suite, TESTS.STSSuite_moduleNameFilter, Bindings.STRING);
-        return new ModelledSTSSuite(suiteName, children, moduleNameFilter);
+        
+        Layer0 L0 = Layer0.getInstance(graph);
+        Collection<Resource> stsVariables = graph.sync(new ObjectsWithType(suite, L0.ConsistsOf, TESTS.STSVariable));
+        Map<String, String> variables = new HashMap<>(stsVariables.size());
+        for (Resource stsVariable : stsVariables) {
+            String name = graph.getRelatedValue(stsVariable, L0.HasName, Bindings.STRING);
+            String value = graph.getRelatedValue(stsVariable, TESTS.STSVariable_definition);
+            variables.put(name, value);
+        }
+        return new ModelledSTSSuite(suiteName, children, moduleNameFilter, variables);
     }
 
     public static void setTestCoverage(ModelledSTSTest test, CommandSession session) {
@@ -102,11 +122,6 @@ public class STSSuiteTestCollector {
         test.setCoverage(CoverageUtils.getCoverage(modules));
         CoverageUtils.resetCoverage(modules);
     }
-    
-    public static List<ModelledSTSSuite> collectTests() {
-        return new ArrayList<>(collectTestsFromGraph());
-    }
-
 
     public static void setSuiteCoverage(ModelledSTSTest test, ModelledSTSSuite suite, CommandSession session) {
         Collection<RuntimeModule> runtimeModules = session.getRuntimeEnvironment().getRuntimeModules();