tests are standalone now
This commit is contained in:
Binary file not shown.
@@ -24,7 +24,6 @@ import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.PsiFileFactory;
|
||||
import com.intellij.psi.PsiJavaFile;
|
||||
import com.intellij.util.Function;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.j2k.visitors.ClassVisitor;
|
||||
@@ -43,21 +42,34 @@ public class JavaToKotlinTranslator {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
protected static PsiFile createFile(@NonNls String name, String text) {
|
||||
private static PsiFile createFile(@NotNull String text) {
|
||||
JavaCoreEnvironment javaCoreEnvironment = setUpJavaCoreEnvironment();
|
||||
return PsiFileFactory.getInstance(javaCoreEnvironment.getProject()).createFileFromText(
|
||||
"test.java", JavaLanguage.INSTANCE, text
|
||||
);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
static PsiFile createFile(@NotNull JavaCoreEnvironment javaCoreEnvironment, @NotNull String text) {
|
||||
return PsiFileFactory.getInstance(javaCoreEnvironment.getProject()).createFileFromText(
|
||||
"test.java", JavaLanguage.INSTANCE, text
|
||||
);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
static JavaCoreEnvironment setUpJavaCoreEnvironment() {
|
||||
JavaCoreEnvironment javaCoreEnvironment = new JavaCoreEnvironment(new Disposable() {
|
||||
@Override
|
||||
public void dispose() {
|
||||
}
|
||||
});
|
||||
|
||||
javaCoreEnvironment.addToClasspath(findRtJar(true));
|
||||
javaCoreEnvironment.addToClasspath(findRtJar());
|
||||
File annotations = findAnnotations();
|
||||
if (annotations != null && annotations.exists()) {
|
||||
javaCoreEnvironment.addToClasspath(annotations);
|
||||
}
|
||||
return PsiFileFactory.getInstance(javaCoreEnvironment.getProject()).createFileFromText(
|
||||
name, JavaLanguage.INSTANCE, text
|
||||
);
|
||||
return javaCoreEnvironment;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -75,21 +87,22 @@ public class JavaToKotlinTranslator {
|
||||
;
|
||||
}
|
||||
|
||||
public static File findRtJar(boolean failOnError) {
|
||||
@Nullable
|
||||
private static File findRtJar() {
|
||||
String javaHome = System.getenv("JAVA_HOME");
|
||||
File rtJar;
|
||||
if (javaHome == null) {
|
||||
rtJar = findActiveRtJar(failOnError);
|
||||
rtJar = findActiveRtJar(true);
|
||||
|
||||
if (rtJar == null && failOnError) {
|
||||
if (rtJar == null) {
|
||||
throw new SetupJavaCoreEnvironmentException("JAVA_HOME environment variable needs to be defined");
|
||||
}
|
||||
} else {
|
||||
rtJar = findRtJar(javaHome);
|
||||
}
|
||||
|
||||
if ((rtJar == null || !rtJar.exists()) && failOnError) {
|
||||
rtJar = findActiveRtJar(failOnError);
|
||||
if (rtJar == null || !rtJar.exists()) {
|
||||
rtJar = findActiveRtJar(true);
|
||||
|
||||
if ((rtJar == null || !rtJar.exists())) {
|
||||
throw new SetupJavaCoreEnvironmentException("No rt.jar found under JAVA_HOME=" + javaHome);
|
||||
@@ -98,6 +111,7 @@ public class JavaToKotlinTranslator {
|
||||
return rtJar;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static File findRtJar(String javaHome) {
|
||||
File rtJar = new File(javaHome, "jre/lib/rt.jar");
|
||||
if (rtJar.exists()) {
|
||||
@@ -123,7 +137,8 @@ public class JavaToKotlinTranslator {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static File findActiveRtJar(boolean failOnError) {
|
||||
@Nullable
|
||||
private static File findActiveRtJar(boolean failOnError) {
|
||||
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
|
||||
if (systemClassLoader instanceof URLClassLoader) {
|
||||
URLClassLoader loader = (URLClassLoader) systemClassLoader;
|
||||
@@ -139,8 +154,9 @@ public class JavaToKotlinTranslator {
|
||||
}
|
||||
if (failOnError) {
|
||||
throw new SetupJavaCoreEnvironmentException("Could not find rt.jar in system class loader: " + StringUtil.join(loader.getURLs(), new Function<URL, String>() {
|
||||
@NotNull
|
||||
@Override
|
||||
public String fun(URL url) {
|
||||
public String fun(@NotNull URL url) {
|
||||
return url.toString() + "\n";
|
||||
}
|
||||
}, ", "));
|
||||
@@ -151,15 +167,16 @@ public class JavaToKotlinTranslator {
|
||||
return null;
|
||||
}
|
||||
|
||||
private static void setClassIdentifiers(PsiElement psiFile) {
|
||||
static void setClassIdentifiers(@NotNull PsiElement psiFile) {
|
||||
ClassVisitor c = new ClassVisitor();
|
||||
psiFile.accept(c);
|
||||
Converter.clearClassIdentifiers();
|
||||
Converter.setClassIdentifiers(c.getClassIdentifiers());
|
||||
}
|
||||
|
||||
static String generateKotlinCode(String arg) {
|
||||
PsiFile file = createFile("test.java", arg);
|
||||
@NotNull
|
||||
static String generateKotlinCode(@NotNull String arg) {
|
||||
PsiFile file = createFile(arg);
|
||||
if (file != null && file instanceof PsiJavaFile) {
|
||||
setClassIdentifiers(file);
|
||||
return prettify(Converter.fileToFile((PsiJavaFile) file).toKotlin());
|
||||
@@ -167,7 +184,17 @@ public class JavaToKotlinTranslator {
|
||||
return "";
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
@NotNull
|
||||
static String generateKotlinCodeWithCompatibilityImport(@NotNull String arg) {
|
||||
PsiFile file = createFile(arg);
|
||||
if (file != null && file instanceof PsiJavaFile) {
|
||||
setClassIdentifiers(file);
|
||||
return prettify(Converter.fileToFileWithCompatibilityImport((PsiJavaFile) file).toKotlin());
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public static void main(@NotNull String[] args) throws IOException {
|
||||
//noinspection UseOfSystemOutOrSystemErr
|
||||
final PrintStream out = System.out;
|
||||
if (args.length == 1) {
|
||||
|
||||
+24
-41
@@ -1,15 +1,14 @@
|
||||
package org.jetbrains.jet.j2k;
|
||||
|
||||
import com.intellij.codeInsight.daemon.LightDaemonAnalyzerTestCase;
|
||||
import com.intellij.openapi.projectRoots.Sdk;
|
||||
import com.intellij.openapi.projectRoots.impl.JavaSdkImpl;
|
||||
import com.intellij.core.JavaCoreEnvironment;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.PsiJavaFile;
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.j2k.visitors.ClassVisitor;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@@ -21,18 +20,21 @@ import static org.jetbrains.jet.j2k.TestCaseBuilder.getTestDataPathBase;
|
||||
/**
|
||||
* @author ignatov
|
||||
*/
|
||||
public class JavaToKotlinConverterTest extends LightDaemonAnalyzerTestCase {
|
||||
public class StandaloneJavaToKotlinConverterTest extends TestCase {
|
||||
private final String myDataPath;
|
||||
private final String myName;
|
||||
@NotNull
|
||||
private final JavaCoreEnvironment myJavaCoreEnvironment;
|
||||
|
||||
public JavaToKotlinConverterTest(String dataPath, String name) {
|
||||
public StandaloneJavaToKotlinConverterTest(String dataPath, String name) {
|
||||
myDataPath = dataPath;
|
||||
myName = name;
|
||||
myJavaCoreEnvironment = JavaToKotlinTranslator.setUpJavaCoreEnvironment();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void runTest() throws Throwable {
|
||||
String javaPath = getTestDataPath() + File.separator + getTestFilePath();
|
||||
String javaPath = "testData" + File.separator + getTestFilePath();
|
||||
String kotlinPath = javaPath.replace(".jav", ".kt");
|
||||
|
||||
final File kotlinFile = new File(kotlinPath);
|
||||
@@ -65,19 +67,6 @@ public class JavaToKotlinConverterTest extends LightDaemonAnalyzerTestCase {
|
||||
return myDataPath + File.separator + myName + ".jav";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected String getTestDataPath() {
|
||||
return "testData";
|
||||
}
|
||||
|
||||
private static Sdk jdkFromIdeaHome() {
|
||||
return new JavaSdkImpl().createJdk("JDK", "jre", true);
|
||||
}
|
||||
|
||||
protected Sdk getProjectJDK() {
|
||||
return jdkFromIdeaHome();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
@@ -92,39 +81,33 @@ public class JavaToKotlinConverterTest extends LightDaemonAnalyzerTestCase {
|
||||
@NotNull
|
||||
@Override
|
||||
public Test createTest(@NotNull String dataPath, @NotNull String name) {
|
||||
return new JavaToKotlinConverterTest(dataPath, name);
|
||||
return new StandaloneJavaToKotlinConverterTest(dataPath, name);
|
||||
}
|
||||
}));
|
||||
return suite;
|
||||
}
|
||||
|
||||
private static void configureFromText(String text) throws IOException {
|
||||
configureFromFileText("test.java", text);
|
||||
}
|
||||
|
||||
private static void setClassIdentifiers() {
|
||||
ClassVisitor c = new ClassVisitor();
|
||||
myFile.accept(c);
|
||||
Converter.clearClassIdentifiers();
|
||||
Converter.setClassIdentifiers(c.getClassIdentifiers());
|
||||
@NotNull
|
||||
private static String fileToFileWithCompatibilityImport(@NotNull String text) {
|
||||
return JavaToKotlinTranslator.generateKotlinCodeWithCompatibilityImport(text);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String fileToFileWithCompatibilityImport(String text) throws IOException {
|
||||
configureFromText(text);
|
||||
setClassIdentifiers();
|
||||
return prettify(Converter.fileToFileWithCompatibilityImport((PsiJavaFile) myFile).toKotlin());
|
||||
private String fileToKotlin(@NotNull String text) {
|
||||
return generateKotlinCode(JavaToKotlinTranslator.createFile(myJavaCoreEnvironment, text));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String fileToKotlin(String text) throws IOException {
|
||||
configureFromText(text);
|
||||
setClassIdentifiers();
|
||||
return prettify(Converter.fileToFile((PsiJavaFile) myFile).toKotlin());
|
||||
private static String generateKotlinCode(@Nullable PsiFile file) {
|
||||
if (file != null && file instanceof PsiJavaFile) {
|
||||
JavaToKotlinTranslator.setClassIdentifiers(file);
|
||||
return prettify(Converter.fileToFile((PsiJavaFile) file).toKotlin());
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String methodToKotlin(String text) throws IOException {
|
||||
private String methodToKotlin(String text) throws IOException {
|
||||
String result = fileToKotlin("final class C {" + text + "}")
|
||||
.replaceAll("class C\\(\\) \\{", "");
|
||||
result = result.substring(0, result.lastIndexOf("}"));
|
||||
@@ -132,7 +115,7 @@ public class JavaToKotlinConverterTest extends LightDaemonAnalyzerTestCase {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String statementToKotlin(String text) throws Exception {
|
||||
private String statementToKotlin(String text) throws Exception {
|
||||
String result = methodToKotlin("void main() {" + text + "}");
|
||||
int pos = result.lastIndexOf("}");
|
||||
result = result.substring(0, pos).replaceFirst("fun main\\(\\) : Unit \\{", "");
|
||||
@@ -140,7 +123,7 @@ public class JavaToKotlinConverterTest extends LightDaemonAnalyzerTestCase {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String expressionToKotlin(String code) throws Exception {
|
||||
private String expressionToKotlin(String code) throws Exception {
|
||||
String result = statementToKotlin("Object o =" + code + "}");
|
||||
result = result.replaceFirst("var o : Any\\? =", "");
|
||||
return prettify(result);
|
||||
Reference in New Issue
Block a user