1 package org.simantics.db.testing.base;
3 import java.io.IOException;
4 import java.lang.management.ManagementFactory;
5 import java.util.ArrayList;
8 import java.util.Timer;
9 import java.util.TimerTask;
11 import org.junit.Rule;
12 import org.junit.rules.TestName;
13 import org.osgi.framework.BundleContext;
14 import org.simantics.db.testing.cases.FreshDatabaseTest;
15 import org.simantics.scl.compiler.module.repository.ModuleRepository;
16 import org.simantics.scl.compiler.testing.TestRunnable;
17 import org.simantics.scl.compiler.testing.repository.TestRepository;
18 import org.simantics.scl.osgi.SCLOsgi;
19 import org.simantics.scl.osgi.internal.Activator;
20 import org.simantics.scl.osgi.internal.ServiceBasedModuleSourceRepository;
21 import org.simantics.scl.osgi.internal.ServiceBasedTestRepository;
23 import gnu.trove.map.hash.THashMap;
26 * Utilizies {@link TestRepository} for collecting SCL tests from bundles
31 public class SCLScriptTestBase extends FreshDatabaseTest {
33 private Map<String, TestRunnable> testRunnables = new THashMap<String, TestRunnable>();
35 @Rule public TestName testName = new TestName();
38 * Constructor that initially searches for all SCL test scripts and stores
39 * them into a Map for later access
41 public SCLScriptTestBase() {
43 BundleContext context = Activator.getContext();
44 List<TestRunnable> runnables = new ArrayList<TestRunnable>();
45 context.getService(context.getServiceReference(TestRepository.class)).collectTests(runnables);
46 for (TestRunnable runnable : runnables) {
47 testRunnables.put(runnable.getName(), runnable);
52 * Simplest method for running a SCL test
54 protected void test() {
59 * Executes a test case with given timeout as seconds. When time runs out one
60 * can assume a deadlock has happened. The process is killed after the timeout
61 * in order to keep the possible multiple tests running.
63 * @param timeout allowed execution time given in seconds
65 protected void test(int timeout) {
70 * Executes a test case with given timeout as seconds
72 * @param timeout allowed execution time given in seconds
74 private void testImpl(int timeout) {
75 SCLOsgi.SOURCE_REPOSITORY = new ServiceBasedModuleSourceRepository(Activator.getContext());
76 SCLOsgi.MODULE_REPOSITORY = new ModuleRepository(SCLOsgi.SOURCE_REPOSITORY);
77 SCLOsgi.TEST_REPOSITORY = new ServiceBasedTestRepository(Activator.getContext());
79 String testName = resolveTestName();
80 TestRunnable runnable = testRunnables.get(testName);
83 Timer timer = new Timer();
84 timer.schedule(new TimerTask() {
88 String processName = ManagementFactory.getRuntimeMXBean().getName();
89 System.out.println("PID: " + processName);
90 String PID = processName.split("@")[0];
91 String command = "taskkill /F /PID " + PID;
92 System.out.println("Command: " + command);
94 Runtime.getRuntime().exec(command);
95 } catch (IOException e) {
103 } catch (Exception e) {
111 } catch (Exception e) {
118 * Resolves the full test name based on the names of classes that extends this
119 * SCLScriptTestBase class.<br><br>
121 * For example if our tests locate in <code>sclTests/Simantics/Regression/FirstTest.sts</code>
122 * the class hierarchy would be:
125 * <li><code>SCLScriptTestBase</code>
127 * <li><code>Simantics</code>
129 * <li><code>Regression</code></li>
135 * And the script file name is the same than JUnit method name.
137 * @return full testName
139 private String resolveTestName() {
140 StringBuilder sb = new StringBuilder();
141 Class<?> clazz = this.getClass();
143 if (!(clazz.getName() == SCLScriptTestBase.class.getName())) {
144 String[] classNameParts = clazz.getName().split("\\.");
146 sb.insert(0, classNameParts[classNameParts.length - 1]);
147 clazz = clazz.getSuperclass();
149 sb.append(testName.getMethodName());
153 return sb.toString();