• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright (C) 2025 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.tools.metalava.model.junit4
18 
19 import java.text.MessageFormat
20 import org.junit.runner.Description
21 import org.junit.runner.Runner
22 import org.junit.runner.notification.RunNotifier
23 import org.junit.runners.Parameterized.Parameters
24 import org.junit.runners.ParentRunner
25 import org.junit.runners.model.TestClass
26 import org.junit.runners.parameterized.ParametersRunnerFactory
27 import org.junit.runners.parameterized.TestWithParameters
28 
29 /**
30  * A simple [ParentRunner] that will use [parametersRunnerFactory] to create a [Runner] for each of
31  * the [TestArguments.argumentSets] returned from [computeTestArguments].
32  */
33 abstract class ParameterizedRunner<A : Any>(
34     private val testClass: TestClass,
35     private val parametersRunnerFactory: ParametersRunnerFactory,
36 ) : ParentRunner<Runner>(testClass) {
37 
38     /** The set of test arguments to use. */
39     data class TestArguments<A : Any>(
40         /**
41          * The pattern describing how to construct the test name suffix for a set of arguments.
42          *
43          * See [Parameters.name] for more details.
44          */
45         val pattern: String,
46 
47         /**
48          * The sets of arguments.
49          *
50          * Each entry can be either an `Array<Any>` (for multiple arguments) or any other value (for
51          * a single argument).
52          */
53         val argumentSets: List<A>,
54     )
55 
56     /** Compute [TestArguments] for [testClass]. */
57     abstract fun computeTestArguments(testClass: TestClass): TestArguments<A>
58 
59     /** Create the runners lazily. */
60     private val runners: List<Runner> by
61         lazy(LazyThreadSafetyMode.NONE) {
62             val testArguments = computeTestArguments(testClass)
63 
64             // Add brackets around the pattern.
65             val pattern = "[${testArguments.pattern}]"
66 
67             testArguments.argumentSets.map { argumentSet ->
68                 val name = MessageFormat.format(pattern, argumentSet)
69                 val testWithParameters = TestWithParameters(name, testClass, listOf(argumentSet))
70                 parametersRunnerFactory.createRunnerForTestWithParameters(testWithParameters)
71             }
72         }
73 
74     override fun getChildren(): List<Runner> {
75         return runners
76     }
77 
78     override fun describeChild(child: Runner): Description = child.description
79 
80     override fun runChild(child: Runner, notifier: RunNotifier?) {
81         child.run(notifier)
82     }
83 }
84