forked from trifork/erjang
/
AllTests.java
137 lines (117 loc) · 4.4 KB
/
AllTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* This file is part of Erjang - A JVM-based Erlang VM
*
* Copyright (c) 2009 by Trifork
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package erjang;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import junit.framework.Test;
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 {
static final String OTP_HOME = ErjangConfig.getString("erjang.otp.root");
public static Test suite() {
TestSuite suite = new TestSuite("Test for erjang");
TestSuite otpCompileSuite = new TestSuite("Compiling OTP");
//$JUnit-BEGIN$
//find_beam_files(otpCompileSuite, new File(OTP_HOME));
//$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_files(testsErl, new File("src/test/erl/deterministic"), ".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) {
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();
tc.setFile(sub);
ts.addTest(tc);
}
catch(Exception e) {
throw new IllegalArgumentException("cannot create object of: " + clazz);
}
}
}
}
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_files(tests, file, ext);
} else if (file.getName().endsWith(ext)) {
if (ts == null) {
System.err.println("added.. " + dir);
ts = new ArrayList<File>();
tests.put(dir, ts);
}
ts.add(file);
}
}
}
public static void main(String[] args) {
if (args.length == 0) {
throw new IllegalArgumentException("need at least one command line argument (path)");
}
Map<File, List<File>> testsOTP = new HashMap<File, List<File>>();
find_files(testsOTP, new File(OTP_HOME), ".beam");
generateTestClasses(args[0], testsOTP, TestCompileFile.class);
Map<File, List<File>> testsErl = new HashMap<File, List<File>>();
find_files(testsErl, new File("src/test/erl/deterministic"), ".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 = TestClassGenerator.classNameFor(sub);
String content = TestClassGenerator.generateClassSource(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);
}
}
}
}
}