]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.tests.modelled/src/org/simantics/tests/modelled/junit/v2/ModelledSTSTestRunner.java
Enhancements to modelled STS-tests
[simantics/platform.git] / bundles / org.simantics.tests.modelled / src / org / simantics / tests / modelled / junit / v2 / ModelledSTSTestRunner.java
1 package org.simantics.tests.modelled.junit.v2;
2
3 import java.io.BufferedReader;
4 import java.io.StringReader;
5
6 import org.junit.runner.Description;
7 import org.junit.runner.Runner;
8 import org.junit.runner.notification.RunNotifier;
9 import org.simantics.scl.compiler.commands.CommandSession;
10 import org.simantics.scl.compiler.commands.TestScriptExecutor;
11 import org.simantics.scl.osgi.SCLOsgi;
12 import org.simantics.scl.runtime.SCLContext;
13 import org.simantics.scl.runtime.reporting.SCLReportingHandler;
14 import org.simantics.tests.modelled.utils.ModelledSTSTest;
15 import org.simantics.tests.modelled.utils.STSSuiteTestCollector;
16
17 public class ModelledSTSTestRunner extends Runner {
18
19     private Description description;
20     private ModelledSTSTest test;
21
22     public ModelledSTSTestRunner(ModelledSTSTest test) {
23         this.test = test;
24     }
25
26     public String getName() {
27         return test.getName();
28     }
29
30     @Override
31     public Description getDescription() {
32         if (description == null)
33             description = Description.createTestDescription(ModelledSTSTestRunner.class, getName());
34         return description;
35     }
36
37     /**
38      * This method is called from ModelledSTSSuite (ParentRunner) with the same
39      * CommandSession
40      * 
41      * @param session
42      */
43     public void run(CommandSession session) {
44         try (BufferedReader reader = new BufferedReader(new StringReader(test.getCode()))) {
45             SCLReportingHandler handler = (SCLReportingHandler) SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
46             new TestScriptExecutor(session, reader, handler, true).execute();
47             STSSuiteTestCollector.setTestCoverage(test, session);
48         } catch (Exception e) {
49             throw new RuntimeException(e);
50         }
51     }
52
53     @Override
54     public void run(RunNotifier notifier) {
55         notifier.fireTestStarted(getDescription());
56         try {
57             run(new CommandSession(SCLOsgi.MODULE_REPOSITORY, null));
58         } finally {
59             notifier.fireTestFinished(getDescription());
60         }
61     }
62
63     public boolean isIgnored() {
64         return test.isIgnored();
65     }
66
67     public int getPriority() {
68         return test.getPriority();
69     }
70
71     public ModelledSTSTest getTest() {
72         return test;
73     }
74 }