+package org.simantics.tests.modelled.junit;\r
+\r
+import java.io.BufferedReader;\r
+import java.io.IOException;\r
+import java.io.StringReader;\r
+\r
+import org.junit.runner.Description;\r
+import org.junit.runner.Runner;\r
+import org.junit.runner.notification.RunNotifier;\r
+import org.simantics.scl.compiler.commands.CommandSession;\r
+import org.simantics.scl.compiler.commands.TestScriptExecutor;\r
+import org.simantics.scl.osgi.SCLOsgi;\r
+import org.simantics.scl.runtime.SCLContext;\r
+import org.simantics.scl.runtime.reporting.SCLReportingHandler;\r
+\r
+public class RuntimeSTSTestRunner extends Runner {\r
+ \r
+ private final String name;\r
+ private final String code;\r
+ private final Integer priority;\r
+ private CommandSession session;\r
+\r
+ public RuntimeSTSTestRunner(String name, String code, Integer priority) {\r
+ this.name = name;\r
+ this.code = code;\r
+ this.priority = priority;\r
+ }\r
+ \r
+ public void setCommandSession(CommandSession session) {\r
+ this.session = session;\r
+ }\r
+\r
+ @Override\r
+ public Description getDescription() {\r
+ return Description.createTestDescription(RuntimeSTSTestRunner.class, name);\r
+ }\r
+\r
+ @Override\r
+ public void run(RunNotifier notifier) {\r
+ StringReader reade = new StringReader(code);\r
+ BufferedReader reader = new BufferedReader(reade);\r
+ \r
+ SCLContext context = SCLContext.getCurrent();\r
+ SCLReportingHandler printer = (SCLReportingHandler)context.get(SCLReportingHandler.REPORTING_HANDLER);\r
+ SCLReportingHandler handler;\r
+ if(printer instanceof SCLReportingHandler)\r
+ handler = (SCLReportingHandler)printer;\r
+ else\r
+ handler = SCLReportingHandler.DEFAULT;\r
+ \r
+ try {\r
+ if (session == null)\r
+ session = new CommandSession(SCLOsgi.MODULE_REPOSITORY, handler);\r
+ new TestScriptExecutor(session, reader, handler).execute();\r
+ } catch (IOException e) {\r
+ throw new RuntimeException(e);\r
+ } finally {\r
+ try {\r
+ reader.close();\r
+ } catch (IOException e) {\r
+ // Ignore\r
+ }\r
+ }\r
+ }\r
+\r
+ public Integer getPriority() {\r
+ return priority;\r
+ }\r
+}\r