]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.testing/src/org/simantics/db/testing/base/ScriptTestBase.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / bundles / org.simantics.db.testing / src / org / simantics / db / testing / base / ScriptTestBase.java
1 package org.simantics.db.testing.base;
2
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;
13
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;
22
23 public class ScriptTestBase  extends FreshWorkspaceTest {
24     
25
26     String path;
27
28     public ScriptTestBase(String path) {
29         this.path = path;
30     }    
31
32     /**
33      * Runs a test case without a timeout
34      */
35     protected void test() {
36         
37         String testPath = getTestPath();
38         test(0, testPath);
39     }
40     
41     /**
42      * Runs a test case with given timeout as seconds.
43      * 
44      * @param allowedExecutionTime allowed execution time given in seconds.
45      */
46     protected void test(int allowedExecutionTime) {
47         
48         String testPath = getTestPath();
49         test(allowedExecutionTime, testPath);
50     }
51
52     /**
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.
55      * 
56      * @param allowedExecutionTime allowed execution time given in seconds.
57      */
58     protected void test(final int allowedExecutionTime, final String testPath) {
59         
60         if(allowedExecutionTime > 0) {
61                 Timer timer = new Timer();
62                 try {
63                 timer.schedule(new TimerTask() {
64
65                                 @Override
66                                 public void run() {
67                                         
68                                         try {
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("/");
74                                                 
75 //                                              String hdumpFileName = workspacePath + testName[testName.length - 1] + ".hprof";
76 //                                              GetHeapDump.dumpHeap(hdumpFileName, false);
77                                                 
78 //                                              String tdumpFileName = workspacePath + testName[testName.length - 1] + ".tdump";
79 //                                              GetThreadDump.dumpThread(tdumpFileName);
80                                                 
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);
85                                                 
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);
92
93                                         } catch (IOException ioe) {
94                                                 ioe.printStackTrace();
95                                                 System.err.println(ioe);
96                                         }
97                                         
98                                 }
99                 }, 1000*allowedExecutionTime);
100                 testImpl(testPath);
101                 } finally {
102                         timer.cancel(); 
103                 }
104         } else {
105                 testImpl(testPath);
106         }
107         
108     }
109
110     protected void testImpl(String testPath) {
111         
112         System.out.println("Testing script: " + testPath);
113
114         InputStream stream = getClass().getResourceAsStream(testPath);
115         if(stream == null)
116             Assert.fail("Didn't find " + testPath + " at " + getClass().getResource(""));
117         try {
118             CommandSession session = new CommandSession(SCLOsgi.MODULE_REPOSITORY, null);
119             TestScriptExecutor executor = new TestScriptExecutor(session,
120                     new BufferedReader(new InputStreamReader(stream)), null);
121             executor.execute();
122             //isDBLogClean();
123         } catch(IOException e) {
124             throw new RuntimeException(e);
125         } finally {
126             try {
127                 stream.close();
128             } catch (IOException e) {
129             }
130         }
131     }
132     
133     public String getTestPath() {
134         
135         String methodName = new Exception().getStackTrace()[2].getMethodName();
136         String testPath = path + "/" + methodName + ".sts";
137         
138         return testPath;
139     }
140     
141     public void isDBLogClean() throws IOException {
142         
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);
146         }
147         
148     }
149     
150 }