gaoyunhaii commented on code in PR #21736: URL: https://github.com/apache/flink/pull/21736#discussion_r1105396272
########## flink-test-utils-parent/flink-migration-test-utils/src/main/java/org/apache/flink/test/migration/SnapshotGeneratorExecutor.java: ########## @@ -0,0 +1,176 @@ +/* + * 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 SnapshotGeneratorExecutor { + + 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 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 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, version, argument); Review Comment: Yes, we may first remove it since users could embed the target version in the specification. I'll first remove it -- 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]
