gaoyunhaii commented on code in PR #21736: URL: https://github.com/apache/flink/pull/21736#discussion_r1178099467
########## flink-test-utils-parent/flink-migration-test-utils/src/main/java/org/apache/flink/test/migration/SnapshotGeneratorUtils.java: ########## @@ -0,0 +1,178 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.flink.test.migration; + +import org.apache.flink.FlinkVersion; +import org.apache.flink.test.util.MigrationTest; + +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.rules.ExternalResource; +import org.junit.rules.RunRules; +import org.junit.rules.TestRule; +import org.junit.runner.Description; +import org.junit.runners.Parameterized; +import org.junit.runners.model.Statement; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Array; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +/** A utility class to execute generateSnapshots method for migration tests. */ +class SnapshotGeneratorUtils { + + static void executeGenerate(Class<?> migrationTestClass, FlinkVersion flinkVersion) + throws Throwable { + for (Method method : migrationTestClass.getMethods()) { + if (method.isAnnotationPresent(MigrationTest.SnapshotsGenerator.class)) { + executeGenerateMethods(migrationTestClass, method, flinkVersion); + } else if (method.isAnnotationPresent( + MigrationTest.ParameterizedSnapshotsGenerator.class)) { + String parametersMethodName = + method.getAnnotation(MigrationTest.ParameterizedSnapshotsGenerator.class) + .value(); + Method parametersMethod = + migrationTestClass.getMethod(parametersMethodName, FlinkVersion.class); + executeParameterizedGenerateMethods( + migrationTestClass, method, parametersMethod, flinkVersion); + } + } + } + + private static void executeGenerateMethods( + Class<?> migrationTestClass, Method method, FlinkVersion version) throws Throwable { + method.setAccessible(true); + List<TestRule> classRules = getRuleFields(migrationTestClass, ClassRule.class, null); + + executeWithRules( + classRules, + new Statement() { + @Override + public void evaluate() throws Throwable { + Object migrationTest = createMigrationTest(migrationTestClass); + List<TestRule> rules = + getRuleFields(migrationTestClass, Rule.class, migrationTest); + executeWithRules( + rules, + new Statement() { + @Override + public void evaluate() throws Throwable { + method.invoke(migrationTest, version); + } + }); + } + }); + } + + private static void executeParameterizedGenerateMethods( + Class<?> migrationTestClass, + Method method, + Method parametersMethod, + FlinkVersion version) + throws Throwable { + method.setAccessible(true); + parametersMethod.setAccessible(true); + List<TestRule> classRules = getRuleFields(migrationTestClass, ClassRule.class, null); + + executeWithRules( + classRules, + new Statement() { + @Override + public void evaluate() throws Throwable { + Object migrationTest = createMigrationTest(migrationTestClass); + List<TestRule> rules = + getRuleFields(migrationTestClass, Rule.class, migrationTest); + Collection<?> arguments = + (Collection<?>) parametersMethod.invoke(migrationTest, version); + for (Object argument : arguments) { + executeWithRules( + rules, + new Statement() { + @Override + public void evaluate() throws Throwable { + method.invoke(migrationTest, argument); + } + }); + } + } + }); + } + + private static void executeWithRules(List<TestRule> rules, Statement statement) + throws Throwable { + new RunRules(statement, rules, Description.EMPTY).evaluate(); + } + + private static List<TestRule> getRuleFields( + Class<?> migrationTestClass, Class<? extends Annotation> tagClass, Object migrationTest) + throws IllegalAccessException { + List<TestRule> rules = new ArrayList<>(); + + Field[] fields = migrationTestClass.getFields(); + for (Field field : fields) { + if (ExternalResource.class.isAssignableFrom(field.getType()) + && field.isAnnotationPresent(tagClass)) { + field.setAccessible(true); + rules.add((ExternalResource) field.get(migrationTest)); + } + } + + return rules; + } + + private static Object createMigrationTest(Class<?> migrationTestClass) throws Exception { + Constructor<?>[] constructors = migrationTestClass.getDeclaredConstructors(); + for (Constructor<?> constructor : constructors) { + if (constructor.getParameterCount() == 0) { + constructor.setAccessible(true); + return constructor.newInstance(); + } + } + + // This class does not have a constructor without argument. + Constructor<?> constructor = constructors[0]; Review Comment: It seems test class could have only one constructor, if there are multiple constructors, it will throw the following exception: ``` java.lang.IllegalArgumentException: Test class can only have one constructor at org.junit.runners.model.TestClass.<init>(TestClass.java:48) at org.junit.runners.Parameterized$RunnersFactory.<init>(Parameterized.java:370) at org.junit.runners.Parameterized$RunnersFactory.<init>(Parameterized.java:360) at org.junit.runners.Parameterized.<init>(Parameterized.java:303) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104) at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86) at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:70) at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:37) at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:70) at org.junit.internal.requests.ClassRequest.createRunner(ClassRequest.java:28) at org.junit.internal.requests.MemoizingRequest.getRunner(MemoizingRequest.java:19) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:50) at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38) at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11) at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35) at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235) at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54) ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
