]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.testing/src/org/simantics/db/testing/base/SCLScriptTestBase.java
Fixed typo from string returned from Rename.renameNode
[simantics/platform.git] / bundles / org.simantics.db.testing / src / org / simantics / db / testing / base / SCLScriptTestBase.java
1 package org.simantics.db.testing.base;
2
3 import java.io.IOException;
4 import java.lang.management.ManagementFactory;
5 import java.util.ArrayList;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.Timer;
9 import java.util.TimerTask;
10
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;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import gnu.trove.map.hash.THashMap;
26
27 /**
28  * Utilizies {@link TestRepository} for collecting SCL tests from bundles
29  * 
30  * @author Jani Simomaa
31  */
32 public class SCLScriptTestBase extends FreshDatabaseTest {
33
34     private static final Logger LOGGER = LoggerFactory.getLogger(SCLScriptTestBase.class);
35
36     private Map<String, TestRunnable> testRunnables = new THashMap<String, TestRunnable>();
37
38     @Rule public TestName testName = new TestName();
39
40     /**
41      * Constructor that initially searches for all SCL test scripts and stores
42      * them into a Map for later access
43      */
44     public SCLScriptTestBase() {
45         super();
46         BundleContext context = Activator.getContext();
47         List<TestRunnable> runnables = new ArrayList<TestRunnable>();
48         context.getService(context.getServiceReference(TestRepository.class)).collectTests(runnables);
49         for (TestRunnable runnable : runnables) {
50             testRunnables.put(runnable.getName(), runnable);
51         }
52     }
53
54     /**
55      * Simplest method for running a SCL test
56      */
57     protected void test() {
58         test(-1);
59     }
60
61     /**
62      * Executes a test case with given timeout as seconds. When time runs out one
63      * can assume a deadlock has happened. The process is killed after the timeout
64      * in order to keep the possible multiple tests running.
65      * 
66      * @param timeout allowed execution time given in seconds
67      */
68     protected void test(int timeout) {
69         testImpl(timeout);
70     }
71
72     /**
73      * Executes a test case with given timeout as seconds
74      * 
75      * @param timeout allowed execution time given in seconds
76      */
77     private void testImpl(int timeout) {
78         SCLOsgi.SOURCE_REPOSITORY = new ServiceBasedModuleSourceRepository(Activator.getContext());
79         SCLOsgi.MODULE_REPOSITORY = new ModuleRepository(SCLOsgi.SOURCE_REPOSITORY);
80         SCLOsgi.TEST_REPOSITORY = new ServiceBasedTestRepository(Activator.getContext());
81
82         String testName = resolveTestName();
83         TestRunnable runnable = testRunnables.get(testName);
84
85         if (timeout > -1) {
86             Timer timer = new Timer();
87             timer.schedule(new TimerTask() {
88
89                 @Override
90                 public void run() {
91                     LOGGER.info("Watchdog will kill this test process because it has been executing for over {} seconds", timeout); //$NON-NLS-1$
92                     String processName = ManagementFactory.getRuntimeMXBean().getName();
93                     LOGGER.info("Test Process Name: {}", processName); //$NON-NLS-1$
94                     String PID = processName.split("@")[0];
95                     String command = "taskkill /F /PID " + PID;
96                     LOGGER.info("Running command to kill test process: {}", command); //$NON-NLS-1$
97                     try {
98                         Runtime.getRuntime().exec(command);
99                     } catch (IOException e) {
100                         LOGGER.error("Failed to kill process that ran over its execution time limit of {} seconds", timeout, e);
101                     }
102                 }
103                 
104             }, timeout*1000);
105             try {
106                 runnable.run();
107             } catch (Exception e) {
108                 LOGGER.error("Failed to run test {} runnable {}", testName, runnable, e);
109             } finally {
110                 timer.cancel();
111             }
112         } else {
113             try {
114                 runnable.run();
115             } catch (Exception e) {
116                 LOGGER.error("Failed to run test {} runnable {}", testName, runnable, e);
117             }
118         }
119     }
120
121     /**
122      * Resolves the full test name based on the names of classes that extends this
123      * SCLScriptTestBase class.<br><br>
124      * 
125      * For example if our tests locate in <code>sclTests/Simantics/Regression/FirstTest.sts</code>
126      * the class hierarchy would be:
127      * 
128      * <ul>
129      *   <li><code>SCLScriptTestBase</code>
130      *     <ul>
131      *       <li><code>Simantics</code>
132      *         <ul>
133      *           <li><code>Regression</code></li>
134      *         </ul>
135      *       </li>
136      *     </ul>
137      *   </li>
138      * </ul>
139      * And the script file name is the same than JUnit method name.
140      * 
141      * @return full testName
142      */
143     private String resolveTestName() {
144         StringBuilder sb = new StringBuilder();
145         Class<?> clazz = this.getClass();
146         while (true) {
147             if (!(clazz.getName() == SCLScriptTestBase.class.getName())) {
148                 String[] classNameParts = clazz.getName().split("\\.");
149                 sb.insert(0, "/");
150                 sb.insert(0, classNameParts[classNameParts.length - 1]);
151                 clazz = clazz.getSuperclass();
152             } else {
153                 sb.append(testName.getMethodName());
154                 break;
155             }
156         }
157         return sb.toString();
158     }
159
160 }