1 package org.simantics.db.testing.base;
3 import java.io.BufferedReader;
4 import java.io.FileReader;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.InputStreamReader;
8 import java.lang.management.ManagementFactory;
9 import java.text.SimpleDateFormat;
10 import java.util.Date;
11 import java.util.Timer;
12 import java.util.TimerTask;
14 import org.eclipse.core.runtime.Platform;
15 import org.eclipse.osgi.service.datalocation.Location;
16 import org.junit.Assert;
17 import org.simantics.db.testing.cases.FreshWorkspaceTest;
18 import org.simantics.scl.compiler.commands.CommandSession;
19 import org.simantics.scl.compiler.commands.TestScriptExecutor;
20 import org.simantics.scl.osgi.SCLOsgi;
21 import org.simantics.utils.FileUtils;
23 public class ScriptTestBase extends FreshWorkspaceTest {
28 public ScriptTestBase(String path) {
33 * Runs a test case without a timeout
35 protected void test() {
37 String testPath = getTestPath();
42 * Runs a test case with given timeout as seconds.
44 * @param allowedExecutionTime allowed execution time given in seconds.
46 protected void test(int allowedExecutionTime) {
48 String testPath = getTestPath();
49 test(allowedExecutionTime, testPath);
53 * Runs a test case with given timeout as seconds. When the time runs out, heapdump and threaddump of the java process is taken and stored to the current workspace.
54 * The workspace is being compressed to a Zip-file and then copied and stored to the current working directory. In the end the whole java process gets killed to prevent deadlocks.
56 * @param allowedExecutionTime allowed execution time given in seconds.
58 protected void test(final int allowedExecutionTime, final String testPath) {
60 if(allowedExecutionTime > 0) {
61 Timer timer = new Timer();
63 timer.schedule(new TimerTask() {
69 String workingDir = System.getProperty("user.dir");
70 Location location = Platform.getInstanceLocation();
71 String workspacePath = location.getURL().getPath().substring(1);
72 String[] pathToWorkspace = workspacePath.split("/");
73 // String[] testName = testPath.split("/");
75 // String hdumpFileName = workspacePath + testName[testName.length - 1] + ".hprof";
76 // GetHeapDump.dumpHeap(hdumpFileName, false);
78 // String tdumpFileName = workspacePath + testName[testName.length - 1] + ".tdump";
79 // GetThreadDump.dumpThread(tdumpFileName);
81 SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyy_HH.mm.ss");
82 String date = sdf.format(new Date());
83 String zipDir = workingDir + "/" + pathToWorkspace[pathToWorkspace.length - 1] + "_" + date + ".zip";
84 FileUtils.compressZip(workspacePath, zipDir);
86 String processName = ManagementFactory.getRuntimeMXBean().getName();
87 System.out.println("PID: " + processName);
88 String PID = processName.split("@")[0];
89 String command = "taskkill /F /PID " + PID;
90 System.out.println("Command: " + command);
91 Runtime.getRuntime().exec(command);
93 } catch (IOException ioe) {
94 ioe.printStackTrace();
95 System.err.println(ioe);
99 }, 1000*allowedExecutionTime);
110 protected void testImpl(String testPath) {
112 System.out.println("Testing script: " + testPath);
114 InputStream stream = getClass().getResourceAsStream(testPath);
116 Assert.fail("Didn't find " + testPath + " at " + getClass().getResource(""));
118 CommandSession session = new CommandSession(SCLOsgi.MODULE_REPOSITORY, null);
119 TestScriptExecutor executor = new TestScriptExecutor(session,
120 new BufferedReader(new InputStreamReader(stream)), null);
123 } catch(IOException e) {
124 throw new RuntimeException(e);
128 } catch (IOException e) {
133 public String getTestPath() {
135 String methodName = new Exception().getStackTrace()[2].getMethodName();
136 String testPath = path + "/" + methodName + ".sts";
141 public void isDBLogClean() throws IOException {
143 String workingDir = System.getProperty("user.dir");
144 try (BufferedReader br = new BufferedReader(new FileReader(workingDir + "/db-client.log"))) {
145 Assert.assertEquals(br.readLine(), null);