1 /* 2 * Copyright (C) 2024 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 com.google.common.truth.Truth.assertThat 20 import org.junit.Rule 21 import org.junit.Test 22 import org.junit.rules.TestName 23 import org.junit.runner.RunWith 24 import org.junit.runners.Parameterized.Parameter 25 import org.junit.runners.model.TestClass 26 27 /** 28 * Test to make sure a [CustomizableParameterizedRunner] can provide its own parameters and pattern. 29 */ 30 @RunWith(CustomizableParameterizedParametersProviderTest.ParameterProvider::class) 31 @CustomParameters(1, 2, pattern = "{0}") 32 class CustomizableParameterizedParametersProviderTest { 33 34 @get:Rule val testName = TestName() 35 36 @JvmField @Parameter(0) var parameter: Int = -1 37 38 @Test Test that parameters are provided by the runnernull39 fun `Test that parameters are provided by the runner`() { 40 // This method would not be run if the runner did not provide some parameters. 41 assertThat(parameter).isGreaterThan(0) 42 43 assertThat(testName.methodName) 44 .isEqualTo("Test that parameters are provided by the runner[$parameter]") 45 } 46 47 class ParameterProvider(clazz: Class<*>) : 48 CustomizableParameterizedRunner(clazz, ::mergeParameters) { 49 companion object { mergeParametersnull50 private fun mergeParameters( 51 testClass: TestClass, 52 additionalParameters: List<Array<Any>>?, 53 ): TestArguments { 54 val customParameters = testClass.getAnnotation(CustomParameters::class.java) 55 if (additionalParameters != null) 56 error("did not expect ${additionalParameters.size} additional parameters") 57 return TestArguments( 58 pattern = customParameters.pattern, 59 argumentSets = customParameters.values.toList(), 60 ) 61 } 62 } 63 } 64 } 65 66 /** Annotation used by [CustomizableParameterizedParametersProviderTest] to provide values. */ 67 annotation class CustomParameters(vararg val values: Int, val pattern: String) 68