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