diff --git a/generators/test-generator/tests/org/jetbrains/kotlin/generators/tests/generator/AnnotationModel.kt b/generators/test-generator/tests/org/jetbrains/kotlin/generators/tests/generator/AnnotationModel.kt new file mode 100644 index 00000000000..dc0b941152f --- /dev/null +++ b/generators/test-generator/tests/org/jetbrains/kotlin/generators/tests/generator/AnnotationModel.kt @@ -0,0 +1,22 @@ +/* + * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.generators.tests.generator + +import org.jetbrains.kotlin.test.MuteExtraSuffix +import org.jetbrains.kotlin.utils.Printer + +class AnnotationModel( + val annotation: Class, + val arguments: List +) { + fun generate(p: Printer) { + val argumentsString = arguments.joinToString(separator = ",") { "\"$it\""} + p.print("@${annotation.simpleName}($argumentsString)") + } +} + +fun muteExtraSuffix(suffix: String) = AnnotationModel(MuteExtraSuffix::class.java, arguments = listOf(suffix)) + diff --git a/generators/test-generator/tests/org/jetbrains/kotlin/generators/tests/generator/DelegatingTestClassModel.java b/generators/test-generator/tests/org/jetbrains/kotlin/generators/tests/generator/DelegatingTestClassModel.java index f5de7c4cc67..4226f47f4eb 100644 --- a/generators/test-generator/tests/org/jetbrains/kotlin/generators/tests/generator/DelegatingTestClassModel.java +++ b/generators/test-generator/tests/org/jetbrains/kotlin/generators/tests/generator/DelegatingTestClassModel.java @@ -61,4 +61,16 @@ public class DelegatingTestClassModel implements TestClassModel { public String getDataString() { return delegate.getDataString(); } + + @NotNull + @Override + public Collection getAnnotations() { + return delegate.getAnnotations(); + } + + @NotNull + @Override + public Collection> getImports() { + return delegate.getImports(); + } } diff --git a/generators/test-generator/tests/org/jetbrains/kotlin/generators/tests/generator/SimpleTestClassModel.java b/generators/test-generator/tests/org/jetbrains/kotlin/generators/tests/generator/SimpleTestClassModel.java index fcb537ed9c8..de1efdcacbf 100644 --- a/generators/test-generator/tests/org/jetbrains/kotlin/generators/tests/generator/SimpleTestClassModel.java +++ b/generators/test-generator/tests/org/jetbrains/kotlin/generators/tests/generator/SimpleTestClassModel.java @@ -17,6 +17,7 @@ import org.jetbrains.kotlin.utils.Printer; import java.io.File; import java.util.*; import java.util.regex.Pattern; +import java.util.stream.Collectors; public class SimpleTestClassModel implements TestClassModel { private static final Comparator BY_NAME = Comparator.comparing(TestEntityModel::getName); @@ -43,6 +44,9 @@ public class SimpleTestClassModel implements TestClassModel { @Nullable private Collection testMethods; + @NotNull + private final Collection annotations; + private final boolean skipIgnored; private final String testRunnerMethodName; private final List additionalRunnerArguments; @@ -60,7 +64,8 @@ public class SimpleTestClassModel implements TestClassModel { boolean skipIgnored, String testRunnerMethodName, List additionalRunnerArguments, - Integer deep + Integer deep, + @NotNull Collection annotations ) { this.rootFile = rootFile; this.recursive = recursive; @@ -75,6 +80,7 @@ public class SimpleTestClassModel implements TestClassModel { this.testRunnerMethodName = testRunnerMethodName; this.additionalRunnerArguments = additionalRunnerArguments; this.deep = deep; + this.annotations = annotations; } @NotNull @@ -94,8 +100,9 @@ public class SimpleTestClassModel implements TestClassModel { children.add(new SimpleTestClassModel( file, true, excludeParentDirs, filenamePattern, checkFilenameStartsLowerCase, doTestMethodName, innerTestClassName, targetBackend, excludesStripOneDirectory(file.getName()), - skipIgnored, testRunnerMethodName, additionalRunnerArguments, deep != null ? deep - 1 : null) + skipIgnored, testRunnerMethodName, additionalRunnerArguments, deep != null ? deep - 1 : null, annotations) ); + } } } @@ -223,6 +230,18 @@ public class SimpleTestClassModel implements TestClassModel { return testClassName; } + @NotNull + @Override + public Collection getAnnotations() { + return annotations; + } + + @NotNull + @Override + public Collection> getImports() { + return annotations.stream().map(AnnotationModel::getAnnotation).collect(Collectors.toSet()); + } + private class TestAllFilesPresentMethodModel implements TestMethodModel { @NotNull @Override diff --git a/generators/test-generator/tests/org/jetbrains/kotlin/generators/tests/generator/SingleClassTestModel.java b/generators/test-generator/tests/org/jetbrains/kotlin/generators/tests/generator/SingleClassTestModel.java index 35b3d565042..4ed01a157a4 100644 --- a/generators/test-generator/tests/org/jetbrains/kotlin/generators/tests/generator/SingleClassTestModel.java +++ b/generators/test-generator/tests/org/jetbrains/kotlin/generators/tests/generator/SingleClassTestModel.java @@ -31,6 +31,7 @@ import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.regex.Pattern; +import java.util.stream.Collectors; public class SingleClassTestModel implements TestClassModel { @NotNull @@ -52,6 +53,9 @@ public class SingleClassTestModel implements TestClassModel { private final String testRunnerMethodName; private final List additionalRunnerArguments; + @NotNull + private final List annotations; + public SingleClassTestModel( @NotNull File rootFile, @NotNull Pattern filenamePattern, @@ -61,7 +65,8 @@ public class SingleClassTestModel implements TestClassModel { @NotNull TargetBackend targetBackend, boolean skipIgnored, String testRunnerMethodName, - List additionalRunnerArguments + List additionalRunnerArguments, + @NotNull List annotations ) { this.rootFile = rootFile; this.filenamePattern = filenamePattern; @@ -72,6 +77,7 @@ public class SingleClassTestModel implements TestClassModel { this.skipIgnored = skipIgnored; this.testRunnerMethodName = testRunnerMethodName; this.additionalRunnerArguments = additionalRunnerArguments; + this.annotations = annotations; } @NotNull @@ -134,6 +140,18 @@ public class SingleClassTestModel implements TestClassModel { return testClassName; } + @NotNull + @Override + public Collection getAnnotations() { + return annotations; + } + + @NotNull + @Override + public Collection> getImports() { + return annotations.stream().map(AnnotationModel::getAnnotation).collect(Collectors.toSet()); + } + private class TestAllFilesPresentMethodModel implements TestMethodModel { @NotNull @Override diff --git a/generators/test-generator/tests/org/jetbrains/kotlin/generators/tests/generator/TestGenerationDSL.kt b/generators/test-generator/tests/org/jetbrains/kotlin/generators/tests/generator/TestGenerationDSL.kt index 2e0e895d262..bb7454a22c8 100644 --- a/generators/test-generator/tests/org/jetbrains/kotlin/generators/tests/generator/TestGenerationDSL.kt +++ b/generators/test-generator/tests/org/jetbrains/kotlin/generators/tests/generator/TestGenerationDSL.kt @@ -15,32 +15,35 @@ class TestGroup( private val testsRoot: String, val testDataRoot: String, val testRunnerMethodName: String, - val additionalRunnerArguments: List = emptyList() + val additionalRunnerArguments: List = emptyList(), + val annotations: List = emptyList() ) { inline fun testClass( suiteTestClassName: String = getDefaultSuiteTestClassName(T::class.java.simpleName), useJunit4: Boolean = false, + annotations: List = emptyList(), noinline init: TestClass.() -> Unit ) { - testClass(T::class.java.name, suiteTestClassName, useJunit4, init) + testClass(T::class.java.name, suiteTestClassName, useJunit4, annotations, init) } fun testClass( baseTestClassName: String, suiteTestClassName: String = getDefaultSuiteTestClassName(baseTestClassName.substringAfterLast('.')), useJunit4: Boolean, + annotations: List = emptyList(), init: TestClass.() -> Unit ) { TestGenerator( testsRoot, suiteTestClassName, baseTestClassName, - TestClass().apply(init).testModels, + TestClass(annotations).apply(init).testModels, useJunit4 ).generateAndSave() } - inner class TestClass { + inner class TestClass(val annotations: List) { val testModels = ArrayList() fun model( @@ -66,13 +69,13 @@ class TestGroup( if (excludeDirs.isNotEmpty()) error("excludeDirs is unsupported for SingleClassTestModel yet") SingleClassTestModel( rootFile, compiledPattern, filenameStartsLowerCase, testMethod, className, targetBackend, - skipIgnored, testRunnerMethodName, additionalRunnerArguments + skipIgnored, testRunnerMethodName, additionalRunnerArguments, annotations ) } else { SimpleTestClassModel( rootFile, recursive, excludeParentDirs, compiledPattern, filenameStartsLowerCase, testMethod, className, - targetBackend, excludeDirs, skipIgnored, testRunnerMethodName, additionalRunnerArguments, deep + targetBackend, excludeDirs, skipIgnored, testRunnerMethodName, additionalRunnerArguments, deep, annotations ) } ) diff --git a/generators/test-generator/tests/org/jetbrains/kotlin/generators/tests/generator/TestGenerator.kt b/generators/test-generator/tests/org/jetbrains/kotlin/generators/tests/generator/TestGenerator.kt index 281d3a75037..576165104a2 100644 --- a/generators/test-generator/tests/org/jetbrains/kotlin/generators/tests/generator/TestGenerator.kt +++ b/generators/test-generator/tests/org/jetbrains/kotlin/generators/tests/generator/TestGenerator.kt @@ -25,7 +25,6 @@ class TestGenerator( testClassModels: Collection, useJunit4: Boolean ) { - private val baseTestClassPackage: String private val suiteClassPackage: String private val suiteClassName: String @@ -73,6 +72,11 @@ class TestGenerator( if (suiteClassPackage != baseTestClassPackage) { p.println("import $baseTestClassPackage.$baseTestClassName;") } + + for (clazz in testClassModels.flatMap { classModel -> classModel.imports }.toSet()) { + p.println("import ${clazz.name};") + } + p.println("import " + TestMetadata::class.java.canonicalName + ";") p.println("import " + RunWith::class.java.canonicalName + ";") if (useJunit4) { @@ -112,6 +116,12 @@ class TestGenerator( override val dataPathRoot: String? get() = null + + override val annotations: Collection + get() = emptyList() + + override val imports: Collection> + get() = emptyList() } } @@ -126,6 +136,8 @@ class TestGenerator( generateMetadata(p, testClassModel) generateTestDataPath(p, testClassModel) + generateParameterAnnotations(p, testClassModel) + p.println("@RunWith(", if (useJunit4) JUNIT4_RUNNER.simpleName else RUNNER.simpleName, ".class)") p.println("public " + staticModifier + "class ", testClassModel.name, " extends ", baseTestClassName, " {") @@ -201,6 +213,13 @@ class TestGenerator( } } + private fun generateParameterAnnotations(p: Printer, testClassModel: TestClassModel) { + for (annotationModel in testClassModel.annotations) { + annotationModel.generate(p); + p.println() + } + } + private fun generateSuppressAllWarnings(p: Printer) { p.println("@SuppressWarnings(\"all\")") } diff --git a/generators/test-generator/tests/org/jetbrains/kotlin/generators/tests/generator/TestModel.kt b/generators/test-generator/tests/org/jetbrains/kotlin/generators/tests/generator/TestModel.kt index a7826cd4c1a..20e58e1ce4a 100644 --- a/generators/test-generator/tests/org/jetbrains/kotlin/generators/tests/generator/TestModel.kt +++ b/generators/test-generator/tests/org/jetbrains/kotlin/generators/tests/generator/TestModel.kt @@ -24,10 +24,12 @@ interface TestEntityModel { } interface TestClassModel : TestEntityModel { + val imports: Collection> val innerTestClasses: Collection val methods: Collection val isEmpty: Boolean val dataPathRoot: String? + val annotations: Collection } interface MethodModel : TestEntityModel {