TestGenerator: J2K
This commit is contained in:
+129
-166
@@ -1,223 +1,186 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2010-2015 JetBrains s.r.o.
|
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||||
*
|
* that can be found in the license/LICENSE.txt file.
|
||||||
* 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.kotlin.generators.tests.generator;
|
package org.jetbrains.kotlin.generators.tests.generator
|
||||||
|
|
||||||
import kotlin.io.FilesKt;
|
import org.jetbrains.kotlin.generators.util.GeneratorsFileUtil
|
||||||
import kotlin.text.Charsets;
|
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners
|
||||||
import kotlin.text.StringsKt;
|
import org.jetbrains.kotlin.test.KotlinTestUtils
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.kotlin.test.TargetBackend
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.kotlin.test.TestMetadata
|
||||||
import org.jetbrains.kotlin.generators.util.GeneratorsFileUtil;
|
import org.jetbrains.kotlin.utils.Printer
|
||||||
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners;
|
import org.junit.runner.RunWith
|
||||||
import org.jetbrains.kotlin.test.KotlinTestUtils;
|
import java.io.File
|
||||||
import org.jetbrains.kotlin.test.TargetBackend;
|
import java.io.IOException
|
||||||
import org.jetbrains.kotlin.test.TestMetadata;
|
import java.util.*
|
||||||
import org.jetbrains.kotlin.utils.Printer;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
|
|
||||||
import java.io.File;
|
class TestGenerator(
|
||||||
import java.io.IOException;
|
baseDir: String,
|
||||||
import java.util.*;
|
suiteTestClassFqName: String,
|
||||||
|
baseTestClassFqName: String,
|
||||||
|
testClassModels: Collection<TestClassModel>
|
||||||
|
) {
|
||||||
|
|
||||||
import static kotlin.collections.CollectionsKt.single;
|
private val baseTestClassPackage: String
|
||||||
|
private val suiteClassPackage: String
|
||||||
|
private val suiteClassName: String
|
||||||
|
private val baseTestClassName: String
|
||||||
|
private val testClassModels: Collection<TestClassModel>
|
||||||
|
private val testSourceFilePath: String
|
||||||
|
|
||||||
public class TestGenerator {
|
init {
|
||||||
private static final Set<String> GENERATED_FILES = new HashSet<>();
|
this.baseTestClassPackage = baseTestClassFqName.substringBeforeLast('.', "")
|
||||||
private static final Class RUNNER = JUnit3RunnerWithInners.class;
|
this.baseTestClassName = baseTestClassFqName.substringAfterLast('.', baseTestClassFqName)
|
||||||
|
this.suiteClassPackage = suiteTestClassFqName.substringBeforeLast('.', baseTestClassPackage)
|
||||||
|
this.suiteClassName = suiteTestClassFqName.substringAfterLast('.', suiteTestClassFqName)
|
||||||
|
this.testClassModels = ArrayList(testClassModels)
|
||||||
|
|
||||||
private final String baseTestClassPackage;
|
this.testSourceFilePath = baseDir + "/" + this.suiteClassPackage.replace(".", "/") + "/" + this.suiteClassName + ".java"
|
||||||
private final String suiteClassPackage;
|
|
||||||
private final String suiteClassName;
|
|
||||||
private final String baseTestClassName;
|
|
||||||
private final Collection<TestClassModel> testClassModels;
|
|
||||||
private final String testSourceFilePath;
|
|
||||||
|
|
||||||
public TestGenerator(
|
|
||||||
@NotNull String baseDir,
|
|
||||||
@NotNull String suiteTestClassFqName,
|
|
||||||
@NotNull String baseTestClassFqName,
|
|
||||||
@NotNull Collection<? extends TestClassModel> testClassModels
|
|
||||||
) {
|
|
||||||
this.baseTestClassPackage = StringsKt.substringBeforeLast(baseTestClassFqName, '.', "");
|
|
||||||
this.baseTestClassName = StringsKt.substringAfterLast(baseTestClassFqName, '.', baseTestClassFqName);
|
|
||||||
this.suiteClassPackage = StringsKt.substringBeforeLast(suiteTestClassFqName, '.', baseTestClassPackage);
|
|
||||||
this.suiteClassName = StringsKt.substringAfterLast(suiteTestClassFqName, '.', suiteTestClassFqName);
|
|
||||||
this.testClassModels = new ArrayList<>(testClassModels);
|
|
||||||
|
|
||||||
this.testSourceFilePath = baseDir + "/" + this.suiteClassPackage.replace(".", "/") + "/" + this.suiteClassName + ".java";
|
|
||||||
|
|
||||||
if (!GENERATED_FILES.add(testSourceFilePath)) {
|
if (!GENERATED_FILES.add(testSourceFilePath)) {
|
||||||
throw new IllegalArgumentException("Same test file already generated in current session: " + testSourceFilePath);
|
throw IllegalArgumentException("Same test file already generated in current session: " + testSourceFilePath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void generateAndSave() throws IOException {
|
@Throws(IOException::class)
|
||||||
StringBuilder out = new StringBuilder();
|
fun generateAndSave() {
|
||||||
Printer p = new Printer(out);
|
val out = StringBuilder()
|
||||||
|
val p = Printer(out)
|
||||||
|
|
||||||
p.println(FilesKt.readText(new File("license/LICENSE.txt"), Charsets.UTF_8));
|
p.println(File("license/LICENSE.txt").readText(Charsets.UTF_8))
|
||||||
p.println("package ", suiteClassPackage, ";");
|
p.println("package ", suiteClassPackage, ";")
|
||||||
p.println();
|
p.println()
|
||||||
p.println("import com.intellij.testFramework.TestDataPath;");
|
p.println("import com.intellij.testFramework.TestDataPath;")
|
||||||
p.println("import ", RUNNER.getCanonicalName(), ";");
|
p.println("import ", RUNNER.canonicalName, ";")
|
||||||
p.println("import " + KotlinTestUtils.class.getCanonicalName() + ";");
|
p.println("import " + KotlinTestUtils::class.java.canonicalName + ";")
|
||||||
p.println("import " + TargetBackend.class.getCanonicalName() + ";");
|
p.println("import " + TargetBackend::class.java.canonicalName + ";")
|
||||||
if (!suiteClassPackage.equals(baseTestClassPackage)) {
|
if (suiteClassPackage != baseTestClassPackage) {
|
||||||
p.println("import " + baseTestClassPackage + "." + baseTestClassName + ";");
|
p.println("import $baseTestClassPackage.$baseTestClassName;")
|
||||||
}
|
}
|
||||||
p.println("import " + TestMetadata.class.getCanonicalName() + ";");
|
p.println("import " + TestMetadata::class.java.canonicalName + ";")
|
||||||
p.println("import " + RunWith.class.getCanonicalName() + ";");
|
p.println("import " + RunWith::class.java.canonicalName + ";")
|
||||||
p.println();
|
p.println()
|
||||||
p.println("import java.io.File;");
|
p.println("import java.io.File;")
|
||||||
p.println("import java.util.regex.Pattern;");
|
p.println("import java.util.regex.Pattern;")
|
||||||
p.println();
|
p.println()
|
||||||
p.println("/** This class is generated by {@link ", KotlinTestUtils.TEST_GENERATOR_NAME, "}. DO NOT MODIFY MANUALLY */");
|
p.println("/** This class is generated by {@link ", KotlinTestUtils.TEST_GENERATOR_NAME, "}. DO NOT MODIFY MANUALLY */")
|
||||||
|
|
||||||
generateSuppressAllWarnings(p);
|
generateSuppressAllWarnings(p)
|
||||||
|
|
||||||
TestClassModel model;
|
val model: TestClassModel
|
||||||
if (testClassModels.size() == 1) {
|
if (testClassModels.size == 1) {
|
||||||
model = new DelegatingTestClassModel(single(testClassModels)) {
|
model = object : DelegatingTestClassModel(testClassModels.single()) {
|
||||||
@NotNull
|
override val name: String
|
||||||
@Override
|
get() = suiteClassName
|
||||||
public String getName() {
|
}
|
||||||
return suiteClassName;
|
} else {
|
||||||
}
|
model = object : TestClassModel {
|
||||||
};
|
override val innerTestClasses: Collection<TestClassModel>
|
||||||
}
|
get() = testClassModels
|
||||||
else {
|
|
||||||
model = new TestClassModel() {
|
|
||||||
@NotNull
|
|
||||||
@Override
|
|
||||||
public Collection<TestClassModel> getInnerTestClasses() {
|
|
||||||
return testClassModels;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
override val methods: Collection<MethodModel>
|
||||||
@Override
|
get() = emptyList()
|
||||||
public Collection<MethodModel> getMethods() {
|
|
||||||
return Collections.emptyList();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
override val isEmpty: Boolean
|
||||||
public boolean isEmpty() {
|
get() = false
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
override val name: String
|
||||||
@Override
|
get() = suiteClassName
|
||||||
public String getName() {
|
|
||||||
return suiteClassName;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
override val dataString: String?
|
||||||
public String getDataString() {
|
get() = null
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
override val dataPathRoot: String?
|
||||||
@Override
|
get() = null
|
||||||
public String getDataPathRoot() {
|
}
|
||||||
return null;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
generateTestClass(p, model, false);
|
generateTestClass(p, model, false)
|
||||||
|
|
||||||
File testSourceFile = new File(testSourceFilePath);
|
val testSourceFile = File(testSourceFilePath)
|
||||||
GeneratorsFileUtil.writeFileIfContentChanged(testSourceFile, out.toString(), false);
|
GeneratorsFileUtil.writeFileIfContentChanged(testSourceFile, out.toString(), false)
|
||||||
}
|
}
|
||||||
|
|
||||||
private void generateTestClass(Printer p, TestClassModel testClassModel, boolean isStatic) {
|
private fun generateTestClass(p: Printer, testClassModel: TestClassModel, isStatic: Boolean) {
|
||||||
String staticModifier = isStatic ? "static " : "";
|
val staticModifier = if (isStatic) "static " else ""
|
||||||
|
|
||||||
generateMetadata(p, testClassModel);
|
generateMetadata(p, testClassModel)
|
||||||
generateTestDataPath(p, testClassModel);
|
generateTestDataPath(p, testClassModel)
|
||||||
p.println("@RunWith(", RUNNER.getSimpleName(), ".class)");
|
p.println("@RunWith(", RUNNER.simpleName, ".class)")
|
||||||
|
|
||||||
p.println("public " + staticModifier + "class ", testClassModel.getName(), " extends ", baseTestClassName, " {");
|
p.println("public " + staticModifier + "class ", testClassModel.name, " extends ", baseTestClassName, " {")
|
||||||
p.pushIndent();
|
p.pushIndent()
|
||||||
|
|
||||||
Collection<MethodModel> testMethods = testClassModel.getMethods();
|
val testMethods = testClassModel.methods
|
||||||
Collection<TestClassModel> innerTestClasses = testClassModel.getInnerTestClasses();
|
val innerTestClasses = testClassModel.innerTestClasses
|
||||||
|
|
||||||
boolean first = true;
|
var first = true
|
||||||
|
|
||||||
for (MethodModel methodModel : testMethods) {
|
for (methodModel in testMethods) {
|
||||||
if (!methodModel.shouldBeGenerated()) continue;
|
if (!methodModel.shouldBeGenerated()) continue
|
||||||
|
|
||||||
if (first) {
|
if (first) {
|
||||||
first = false;
|
first = false
|
||||||
}
|
} else {
|
||||||
else {
|
p.println()
|
||||||
p.println();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
generateTestMethod(p, methodModel);
|
generateTestMethod(p, methodModel)
|
||||||
}
|
}
|
||||||
|
|
||||||
for (TestClassModel innerTestClass : innerTestClasses) {
|
for (innerTestClass in innerTestClasses) {
|
||||||
if (!innerTestClass.isEmpty()) {
|
if (!innerTestClass.isEmpty) {
|
||||||
if (first) {
|
if (first) {
|
||||||
first = false;
|
first = false
|
||||||
}
|
} else {
|
||||||
else {
|
p.println()
|
||||||
p.println();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
generateTestClass(p, innerTestClass, true);
|
generateTestClass(p, innerTestClass, true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
p.popIndent();
|
p.popIndent()
|
||||||
p.println("}");
|
p.println("}")
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void generateTestMethod(Printer p, MethodModel methodModel) {
|
companion object {
|
||||||
generateMetadata(p, methodModel);
|
private val GENERATED_FILES = HashSet<String>()
|
||||||
|
private val RUNNER = JUnit3RunnerWithInners::class.java
|
||||||
|
|
||||||
methodModel.generateSignature(p);
|
private fun generateTestMethod(p: Printer, methodModel: MethodModel) {
|
||||||
p.printWithNoIndent(" {");
|
generateMetadata(p, methodModel)
|
||||||
p.println();
|
|
||||||
|
|
||||||
p.pushIndent();
|
methodModel.generateSignature(p)
|
||||||
|
p.printWithNoIndent(" {")
|
||||||
|
p.println()
|
||||||
|
|
||||||
methodModel.generateBody(p);
|
p.pushIndent()
|
||||||
|
|
||||||
p.popIndent();
|
methodModel.generateBody(p)
|
||||||
p.println("}");
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void generateMetadata(Printer p, TestEntityModel testDataSource) {
|
p.popIndent()
|
||||||
String dataString = testDataSource.getDataString();
|
p.println("}")
|
||||||
if (dataString != null) {
|
|
||||||
p.println("@TestMetadata(\"", dataString, "\")");
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private static void generateTestDataPath(Printer p, TestClassModel testClassModel) {
|
private fun generateMetadata(p: Printer, testDataSource: TestEntityModel) {
|
||||||
String dataPathRoot = testClassModel.getDataPathRoot();
|
val dataString = testDataSource.dataString
|
||||||
if (dataPathRoot != null) {
|
if (dataString != null) {
|
||||||
p.println("@TestDataPath(\"", dataPathRoot, "\")");
|
p.println("@TestMetadata(\"", dataString, "\")")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private static void generateSuppressAllWarnings(Printer p) {
|
private fun generateTestDataPath(p: Printer, testClassModel: TestClassModel) {
|
||||||
p.println("@SuppressWarnings(\"all\")");
|
val dataPathRoot = testClassModel.dataPathRoot
|
||||||
|
if (dataPathRoot != null) {
|
||||||
|
p.println("@TestDataPath(\"", dataPathRoot, "\")")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun generateSuppressAllWarnings(p: Printer) {
|
||||||
|
p.println("@SuppressWarnings(\"all\")")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user