Permalink
Browse files

implemented generation/execution of single test classes for compiled …

…beam/triq tests
  • Loading branch information...
1 parent 0264154 commit 96c2d8bbc45abb70a3f5be6dc43828418c8d2489 @pavlobaron committed Dec 30, 2010
@@ -24,11 +24,91 @@ public AbstractErjangTestCase(File file) {
public AbstractErjangTestCase(String name) {
super(name);
+ file = new File(name);
}
public void setFile(File file) {
this.file = file;
+ this.setName(file.getName());
}
- //abstract public void
+ protected static String getClassContent(Class<? extends AbstractErjangTestCase> clazz, File file) {
+ StringBuffer s = new StringBuffer();
+ String name = getClassName(file);
+ String path = getPath(file);
+
+ s.append(getPackage());
+ s.append(getImports());
+ s.append(getClassStart(clazz, name));
+ s.append(getConstructor(name, path));
+ s.append(getSuite(name));
+ s.append(getClassEnd());
+
+ return s.toString();
+ }
+
+ protected static String getPackage() {
+ return "package erjang;\n\n";
+ }
+
+ protected static String getImports() {
+ StringBuffer s = new StringBuffer();
+
+ s.append("import junit.framework.TestSuite;\n");
+ s.append("import junit.framework.Test;\n\n");
+
+ return s.toString();
+ }
+
+ public static String getClassName(File file) {
+ String name = file.getName();
+ name = name.replace('.', '_');
+ name = name.replace('-', '_');
+
+ return name + "_TEST";
+ }
+
+ protected static String getPath(File file) {
+ String path = file.getAbsolutePath();
+
+ //Windows backslash hack...
+ path = path.replace("\\", "\\\\");
+
+ return path;
+ }
+
+ protected static String getClassStart(Class<? extends AbstractErjangTestCase> clazz, String name) {
+ return "public class " + name + " extends " + clazz.getName() + " {\n\n";
+ }
+
+ protected static String getConstructor(String name, String path) {
+ StringBuffer s = new StringBuffer();
+
+ s.append("\tpublic " + name + "() {\n");
+ s.append("\t\tsuper(\"" + path + "\");\n");
+ s.append("\t}\n\n");
+
+ return s.toString();
+ }
+
+ protected static String getSuite(String name) {
+ StringBuffer s = new StringBuffer();
+
+ //backup: generate as suite: will we need to group the tests somehow?
+ /*s.append("\tpublic static Test suite() {\n");
+ s.append("\t\tTestSuite ts = new TestSuite();\n");
+ s.append("\\ttts.addTest(new " + name + "());\n");
+ s.append("\t\treturn ts;\n");
+ s.append("\t}\n");*/
+
+ s.append("\tpublic static Test suite() {\n");
+ s.append("\t\treturn new " + name + "();\n");
+ s.append("\t}\n");
+
+ return s.toString();
+ }
+
+ protected static String getClassEnd() {
+ return "}";
+ }
}
@@ -20,6 +20,7 @@
package erjang;
import java.io.File;
+import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -29,7 +30,10 @@
import junit.framework.TestSuite;
/**
+ * when called using "main", this class generates test class files. Otherwise, it is a test suite
*
+ * @author <unknown> (who wrote the original version?)
+ * @author Pavlo Baron <pb@pbit.org>
*/
public class AllTests {
@@ -41,27 +45,28 @@ public static Test suite() {
TestSuite otpCompileSuite = new TestSuite("Compiling OTP");
//$JUnit-BEGIN$
Map<File, List<File>> testsOTP = new HashMap<File, List<File>>();
- find_beam_files(testsOTP, new File(OTP_HOME));
+ find_files(testsOTP, new File(OTP_HOME), ".beam");
buildTestHiearchie(testsOTP, otpCompileSuite, TestCompileFile.class);
//$JUnit-END$
suite.addTest(otpCompileSuite);
TestSuite coverageRunSuite = new TestSuite("Coverage run tests");
//$JUnit-BEGIN$
Map<File, List<File>> testsErl = new HashMap<File, List<File>>();
- find_erl_files(testsErl, new File("src/test/erl"));
+ find_files(testsErl, new File("src/test/erl"), ".erl");
buildTestHiearchie(testsErl, coverageRunSuite, TestRunFile.class);
//$JUnit-END$
-
suite.addTest(coverageRunSuite);
return suite;
}
- protected static void buildTestHiearchie(Map<File, List<File>> tests, TestSuite suite, Class<? extends AbstractErjangTestCase> clazz) {
+ protected static void buildTestHiearchie(Map<File, List<File>> tests, TestSuite suite,
+ Class<? extends AbstractErjangTestCase> clazz) {
TestSuite ts = null;
for (File key : tests.keySet()) {
ts = new TestSuite(key.getPath());
+ suite.addTest(ts);
for (File sub : tests.get(key)) {
try {
AbstractErjangTestCase tc = clazz.newInstance();
@@ -75,13 +80,13 @@ protected static void buildTestHiearchie(Map<File, List<File>> tests, TestSuite
}
}
- static void find_beam_files(Map<File, List<File>> tests, File dir) {
+ static void find_files(Map<File, List<File>> tests, File dir, String ext) {
List<File> ts = null;
if (! dir.isDirectory()) throw new IllegalArgumentException("not a directory: "+dir);
for (File file : dir.listFiles()) {
if (file.isDirectory()) {
- find_beam_files(tests, file);
- } else if (file.getName().endsWith(".beam")) {
+ find_files(tests, file, ext);
+ } else if (file.getName().endsWith(ext)) {
if (ts == null) {
System.err.println("added.. " + dir);
@@ -94,22 +99,41 @@ static void find_beam_files(Map<File, List<File>> tests, File dir) {
}
}
- static void find_erl_files(Map<File, List<File>> tests, File dir) {
- if (! dir.isDirectory()) throw new IllegalArgumentException("not a directory: "+dir);
- List<File> ts = null;
+ public static void main(String[] args) {
+ if (args.length == 0) {
+ throw new IllegalArgumentException("need at least one command line argument (path)");
+ }
- for (File file : dir.listFiles()) {
- if (file.isDirectory()) {
- find_erl_files(tests, file);
- } else if (file.getName().endsWith(".erl")) {
- if (ts == null) {
- System.err.println("added.. " + dir);
- ts = new ArrayList<File>();
- tests.put(dir, ts);
- }
+ Map<File, List<File>> testsOTP = new HashMap<File, List<File>>();
+ find_files(testsOTP, new File(OTP_HOME), ".beam");
+ generateTestClasses(args[0], testsOTP, TestCompileFile.class);
- ts.add(file);
+ Map<File, List<File>> testsErl = new HashMap<File, List<File>>();
+ find_files(testsErl, new File("src/test/erl"), ".erl");
+ generateTestClasses(args[0], testsErl, TestRunFile.class);
+ }
+
+ protected static void generateTestClasses(String path, Map<File, List<File>> tests,
+ Class<? extends AbstractErjangTestCase> clazz) {
+ for (File key : tests.keySet()) {
+ for (File sub : tests.get(key)) {
+ String name = AbstractErjangTestCase.getClassName(sub);
+ String content = AbstractErjangTestCase.getClassContent(clazz, sub);
+ try {
+ File file = new File(path + "/" + name + ".java");
+
+ System.out.println("generating test class: " + file.getName());
+
+ file.createNewFile();
+ FileOutputStream fop = new FileOutputStream(file);
+ fop.write(content.getBytes());
+ fop.flush();
+ fop.close();
+ }
+ catch (Exception e) {
+ throw new IllegalStateException(e);
+ }
}
}
- }
+ }
}
@@ -29,6 +29,10 @@
*/
public class TestCompileFile extends AbstractErjangTestCase {
+ public TestCompileFile(String name) {
+ super(name);
+ }
+
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@@ -64,6 +64,14 @@
final static BeamLoader beamParser = new ErjangBeamDisLoader();
+ public TestRunFile() {
+ super();
+ }
+
+ public TestRunFile(String name) {
+ super(name);
+ }
+
/**
* @param file
*/
@@ -157,6 +165,7 @@ private EObject erl_run(File file) throws Exception {
"-sasl", "sasl_error_logger", "false", // Prevent SASL from polluting stdout
"-s", "run_wrapper", "run_wrapper", "erlang", moduleName,
"-s", "erlang", "halt"};
+
byte[] bin = execGetBinaryOutput(cmd);
return ErlConvert.binary_to_term(new EBinary(bin));
}
@@ -174,6 +183,7 @@ private String execGetOutput(String[] cmd) throws Exception {
private byte[] execGetBinaryOutput(String[] cmd) throws Exception {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(cmd);
+
OutputCollectorThread outThread = new OutputCollectorThread(p.getInputStream());
OutputCollectorThread errThread = new OutputCollectorThread(p.getErrorStream());
outThread.start();

0 comments on commit 96c2d8b

Please sign in to comment.