Introduce black box with Java codegen tests

In contrast to "black box against Java", it supports cyclic dependencies
between Java and Kotlin sources in one test case
This commit is contained in:
Alexander Udalov
2014-06-25 03:59:37 +04:00
parent 09863445bb
commit aea230d677
6 changed files with 109 additions and 10 deletions
@@ -53,12 +53,9 @@ import static org.jetbrains.jet.codegen.CodegenTestUtil.*;
import static org.jetbrains.jet.lang.resolve.java.PackageClassUtils.getPackageClassFqName;
public abstract class CodegenTestCase extends UsefulTestCase {
// for environment and classloader
protected JetCoreEnvironment myEnvironment;
protected CodegenTestFiles myFiles;
private ClassFileFactory classFileFactory;
protected ClassFileFactory classFileFactory;
protected GeneratedClassLoader initializedClassLoader;
protected void createEnvironmentWithMockJdkAndIdeaAnnotations(@NotNull ConfigurationKind configurationKind) {
@@ -19,6 +19,7 @@ package org.jetbrains.jet.codegen;
import com.google.common.base.Predicates;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.psi.PsiFile;
import kotlin.KotlinPackage;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.JetTestUtils;
@@ -39,6 +40,7 @@ import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@@ -108,13 +110,15 @@ public class CodegenTestUtil {
}
@NotNull
public static File compileJava(@NotNull String filename) {
public static File compileJava(@NotNull String filename, @NotNull String... additionalClasspath) {
try {
File javaClassesTempDirectory = JetTestUtils.tmpDir("java-classes");
String classPath = ForTestCompileRuntime.runtimeJarForTests() + File.pathSeparator +
JetTestUtils.getAnnotationsJar().getPath();
List<String> classpath = new ArrayList<String>();
classpath.add(ForTestCompileRuntime.runtimeJarForTests().getPath());
classpath.add(JetTestUtils.getAnnotationsJar().getPath());
classpath.addAll(Arrays.asList(additionalClasspath));
List<String> options = Arrays.asList(
"-classpath", classPath,
"-classpath", KotlinPackage.join(classpath, File.pathSeparator, "", "", -1, ""),
"-d", javaClassesTempDirectory.getPath()
);
@@ -17,12 +17,20 @@
package org.jetbrains.jet.codegen.generated;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.util.ArrayUtil;
import com.intellij.util.Processor;
import kotlin.KotlinPackage;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.*;
import org.jetbrains.jet.ConfigurationKind;
import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.TestJdkKind;
import org.jetbrains.jet.cli.common.output.outputUtils.OutputUtilsPackage;
import org.jetbrains.jet.cli.jvm.JVMConfigurationKeys;
import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment;
import org.jetbrains.jet.codegen.CodegenTestCase;
import org.jetbrains.jet.codegen.GenerationUtils;
import org.jetbrains.jet.codegen.InlineTestUtil;
import org.jetbrains.jet.config.CompilerConfiguration;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.utils.UtilsPackage;
@@ -45,6 +53,15 @@ public abstract class AbstractBlackBoxCodegenTest extends CodegenTestCase {
blackBoxFileAgainstJavaByFullPath(filename);
}
public void doTestWithJava(@NotNull String filename) {
try {
blackBoxFileWithJavaByFullPath(filename);
}
catch (Exception e) {
throw UtilsPackage.rethrow(e);
}
}
public void doTestWithStdlib(@NotNull String filename) {
myEnvironment = JetTestUtils.createEnvironmentWithFullJdk(getTestRootDisposable());
blackBoxFileByFullPath(filename);
@@ -66,7 +83,7 @@ public abstract class AbstractBlackBoxCodegenTest extends CodegenTestCase {
Collections.sort(files);
loadFiles(files.toArray(new String[files.size()]));
loadFiles(ArrayUtil.toStringArray(files));
blackBox();
}
@@ -86,6 +103,44 @@ public abstract class AbstractBlackBoxCodegenTest extends CodegenTestCase {
blackBox();
}
private void blackBoxFileWithJavaByFullPath(@NotNull String directory) throws Exception {
File dirFile = new File(directory);
final List<String> javaFilePaths = new ArrayList<String>();
final List<String> ktFilePaths = new ArrayList<String>();
FileUtil.processFilesRecursively(dirFile, new Processor<File>() {
@Override
public boolean process(File file) {
String path = file.getPath().substring("compiler/testData/codegen/".length());
if (path.endsWith(".kt")) {
ktFilePaths.add(path);
}
else if (path.endsWith(".java")) {
javaFilePaths.add(path);
}
return true;
}
});
CompilerConfiguration configuration = JetTestUtils.compilerConfigurationForTests(
ConfigurationKind.ALL, TestJdkKind.FULL_JDK, JetTestUtils.getAnnotationsJar()
);
configuration.add(JVMConfigurationKeys.CLASSPATH_KEY, dirFile);
myEnvironment = JetCoreEnvironment.createForTests(getTestRootDisposable(), configuration);
loadFiles(ArrayUtil.toStringArray(ktFilePaths));
classFileFactory =
GenerationUtils.compileManyFilesGetGenerationStateForTest(myEnvironment.getProject(), myFiles.getPsiFiles()).getFactory();
File kotlinOut = JetTestUtils.tmpDir(toString());
OutputUtilsPackage.writeAllTo(classFileFactory, kotlinOut);
// TODO: support several Java sources
File javaOut = compileJava(KotlinPackage.single(javaFilePaths), kotlinOut.getPath());
// Add javac output to classpath so that the created class loader can find generated Java classes
configuration.add(JVMConfigurationKeys.CLASSPATH_KEY, javaOut);
blackBox();
}
private void blackBoxFileByFullPath(@NotNull String filename) {
loadFileByFullPath(filename);
blackBox();
@@ -0,0 +1,39 @@
/*
* Copyright 2010-2014 JetBrains s.r.o.
*
* 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 org.jetbrains.jet.codegen.generated;
import junit.framework.Assert;
import junit.framework.Test;
import junit.framework.TestSuite;
import java.io.File;
import java.util.regex.Pattern;
import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.test.InnerTestClasses;
import org.jetbrains.jet.test.TestMetadata;
import org.jetbrains.jet.codegen.generated.AbstractBlackBoxCodegenTest;
/** This class is generated by {@link org.jetbrains.jet.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("compiler/testData/codegen/boxWithJava")
public class BlackBoxWithJavaCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
public void testAllFilesPresentInBoxWithJava() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("compiler/testData/codegen/boxWithJava"), Pattern.compile("^([^\\.]+)$"), false);
}
}
@@ -172,6 +172,10 @@ fun main(args: Array<String>) {
model("codegen/boxAgainstJava", testMethod = "doTestAgainstJava")
}
testClass(javaClass<AbstractBlackBoxCodegenTest>(), "BlackBoxWithJavaCodegenTestGenerated") {
model("codegen/boxWithJava", testMethod = "doTestWithJava", extension = null, recursive = false)
}
testClass(javaClass<AbstractBlackBoxCodegenTest>(), "BlackBoxWithStdlibCodegenTestGenerated") {
model("codegen/boxWithStdlib", testMethod = "doTestWithStdlib")
}