Speeding up codegen tests

This commit is contained in:
Alex Tkachman
2012-02-07 19:10:16 +02:00
parent 8ef778c18e
commit bb3ca16203
4 changed files with 36 additions and 66 deletions
@@ -170,9 +170,10 @@ public class JetTestUtils {
deleteOnShutdown(answer);
return answer;
}
public static File tmpDir(String name) throws IOException {
File answer = FileUtil.createTempDirectory(name, "");
// we should use this form. otherwise directory will be deleted on each test
File answer = FileUtil.createTempDirectory(new File(System.getProperty("java.io.tmpdir")), name, "");
deleteOnShutdown(answer);
return answer;
}
@@ -107,7 +107,7 @@ public class ClassGenTest extends CodegenTestCase {
public void testAbstractClass() throws Exception {
loadText("abstract class SimpleClass() { }");
final Class aClass = loadAllClasses(generateClassesInFile()).get("SimpleClass");
final Class aClass = createClassLoader(generateClassesInFile()).loadClass("SimpleClass");
assertTrue((aClass.getModifiers() & Modifier.ABSTRACT) != 0);
}
@@ -148,7 +148,7 @@ public class ClassGenTest extends CodegenTestCase {
public void testEnumClass() throws Exception {
loadText("enum class Direction { NORTH; SOUTH; EAST; WEST }");
final Class direction = loadAllClasses(generateClassesInFile()).get("Direction");
final Class direction = createClassLoader(generateClassesInFile()).loadClass("Direction");
// System.out.println(generateToText());
final Field north = direction.getField("NORTH");
assertEquals(direction, north.getType());
@@ -157,7 +157,7 @@ public class ClassGenTest extends CodegenTestCase {
public void testEnumConstantConstructors() throws Exception {
loadText("enum class Color(val rgb: Int) { RED: Color(0xFF0000); GREEN: Color(0x00FF00); }");
final Class colorClass = loadAllClasses(generateClassesInFile()).get("Color");
final Class colorClass = createClassLoader(generateClassesInFile()).loadClass("Color");
final Field redField = colorClass.getField("RED");
final Object redValue = redField.get(null);
final Method rgbMethod = colorClass.getMethod("getRgb");
@@ -12,6 +12,7 @@ import org.jetbrains.jet.parsing.JetParsingTest;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -20,7 +21,6 @@ import java.util.Map;
* @author yole
*/
public abstract class CodegenTestCase extends JetLiteFixture {
private MyClassLoader myClassLoader;
protected static void assertThrows(Method foo, Class<? extends Throwable> exceptionClass, Object instance, Object... args) throws IllegalAccessException {
boolean caught = false;
@@ -36,13 +36,11 @@ public abstract class CodegenTestCase extends JetLiteFixture {
@Override
protected void setUp() throws Exception {
super.setUp();
myClassLoader = new MyClassLoader(NamespaceGenTest.class.getClassLoader());
}
@Override
protected void tearDown() throws Exception {
myFile = null;
myClassLoader = null;
super.tearDown();
}
@@ -89,7 +87,7 @@ public abstract class CodegenTestCase extends JetLiteFixture {
protected String blackBox() throws Exception {
ClassFileFactory codegens = generateClassesInFile();
GeneratedClassLoader loader = new GeneratedClassLoader(codegens);
GeneratedClassLoader loader = createClassLoader(codegens);
try {
String fqName = NamespaceCodegen.getJVMClassName(JetPsiUtil.getFQName(myFile), true).replace("/", ".");
@@ -101,6 +99,10 @@ public abstract class CodegenTestCase extends JetLiteFixture {
}
}
protected GeneratedClassLoader createClassLoader(ClassFileFactory codegens) throws MalformedURLException {
return new GeneratedClassLoader(codegens);
}
protected String generateToText() {
GenerationState state = new GenerationState(getProject(), ClassBuilderFactory.TEXT);
AnalyzingUtils.checkForSyntacticErrors(myFile);
@@ -121,34 +123,27 @@ public abstract class CodegenTestCase extends JetLiteFixture {
protected Class loadRootNamespaceClass(@NotNull ClassFileFactory state) {
String fqName = NamespaceCodegen.getJVMClassName(JetPsiUtil.getFQName(myFile), true).replace("/", ".");
Map<String, Class> classMap = loadAllClasses(state);
return classMap.get(fqName);
try {
return createClassLoader(state).loadClass(fqName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
}
return null;
}
protected Class loadClass(String fqName, @NotNull ClassFileFactory state) {
List<String> files = state.files();
for (String file : files) {
if (file.equals(fqName.replace('.', '/') + ".class")) {
final byte[] data = state.asBytes(file);
return myClassLoader.doDefineClass(fqName, data);
}
try {
return createClassLoader(state).loadClass(fqName);
} catch (ClassNotFoundException e) {
} catch (MalformedURLException e) {
}
fail("No classfile was generated for: " + fqName);
return null;
}
protected Map<String, Class> loadAllClasses(@NotNull ClassFileFactory state) {
Map<String, Class> result = new HashMap<String, Class>();
for (String fileName : state.files()) {
String className = StringUtil.trimEnd(fileName, ".class").replace('/', '.');
byte[] data = state.asBytes(fileName);
Class aClass = myClassLoader.doDefineClass(className, data);
result.put(className, aClass);
}
return result;
}
@NotNull
protected ClassFileFactory generateClassesInFile() {
try {
@@ -1,10 +1,14 @@
package org.jetbrains.jet.codegen;
import junit.framework.TestCase;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.compiler.CompileEnvironment;
import org.jetbrains.jet.compiler.CompileSession;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
/**
* @author alex.tkachman
@@ -14,46 +18,16 @@ public class StdlibTest extends CodegenTestCase {
protected void setUp() throws Exception {
super.setUp();
createEnvironmentWithFullJdk();
}
protected String generateToText() {
CompileSession session = new CompileSession(myEnvironment);
session.addSources(myFile.getVirtualFile());
try {
session.addStdLibSources(true);
} catch (Throwable e) {
throw new RuntimeException(e);
}
if (!session.analyze(System.out)) {
return null;
}
return session.generateText();
myEnvironment.addToClasspath(ForTestCompileStdlib.stdlibJarForTests());
CompileEnvironment.ensureRuntime(myEnvironment);
}
@NotNull
protected ClassFileFactory generateClassesInFile() {
try {
CompileSession session = new CompileSession(myEnvironment);
session.addStdLibSources(true);
session.addSources(myFile.getVirtualFile());
CompileEnvironment.ensureRuntime(myEnvironment);
if (!session.analyze(System.out)) {
throw new RuntimeException();
}
return session.generate();
} catch (RuntimeException e) {
System.out.println(generateToText());
throw e;
} catch (Throwable e) {
throw new RuntimeException(e);
}
@Override
protected GeneratedClassLoader createClassLoader(ClassFileFactory codegens) throws MalformedURLException {
return new GeneratedClassLoader(
codegens,
new URLClassLoader(new URL[]{ForTestCompileStdlib.stdlibJarForTests().toURI().toURL()},
getClass().getClassLoader()));
}
public void testKt533 () {