You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2022/07/04 04:02:43 UTC
[GitHub] [flink] yuchengxin opened a new pull request, #20145: [FLINK-25315][tests] add new extensions and utils to help the Junit5 …
yuchengxin opened a new pull request, #20145:
URL: https://github.com/apache/flink/pull/20145
…migration
<!--
*Thank you very much for contributing to Apache Flink - we are happy that you want to help us improve Flink. To help the community review your contribution in the best possible way, please go through the checklist below, which will get the contribution into a shape in which it can be best reviewed.*
*Please understand that we do not do this to make contributions to Flink a hassle. In order to uphold a high standard of quality for code contributions, while at the same time managing a large number of contributions, we need contributors to prepare the contributions well, and give reviewers enough contextual information for the review. Please also understand that contributions that do not follow this guide will take longer to review and thus typically be picked up with lower priority by the community.*
## Contribution Checklist
- Make sure that the pull request corresponds to a [JIRA issue](https://issues.apache.org/jira/projects/FLINK/issues). Exceptions are made for typos in JavaDoc or documentation files, which need no JIRA issue.
- Name the pull request in the form "[FLINK-XXXX] [component] Title of the pull request", where *FLINK-XXXX* should be replaced by the actual issue number. Skip *component* if you are unsure about which is the best component.
Typo fixes that have no associated JIRA issue should be named following this pattern: `[hotfix] [docs] Fix typo in event time introduction` or `[hotfix] [javadocs] Expand JavaDoc for PuncuatedWatermarkGenerator`.
- Fill out the template below to describe the changes contributed by the pull request. That will give reviewers the context they need to do the review.
- Make sure that the change passes the automated tests, i.e., `mvn clean verify` passes. You can set up Azure Pipelines CI to do that following [this guide](https://cwiki.apache.org/confluence/display/FLINK/Azure+Pipelines#AzurePipelines-Tutorial:SettingupAzurePipelinesforaforkoftheFlinkrepository).
- Each pull request should address only one issue, not mix up code from multiple issues.
- Each commit in the pull request has a meaningful commit message (including the JIRA id)
- Once all items of the checklist are addressed, remove the above text and this checklist, leaving only the filled out template below.
**(The sections below can be removed for hotfixes of typos)**
-->
## What is the purpose of the change
This pull request adds new extensions and utils to help the Junit5 migration.
## Brief change log
- Add `ParameterizedTestExtension` and two annotations to help to migrate the Junit4 parameterized test
- Add `TempDirUtils` to provide some common methods in `org.junit.rules.TemporaryFolder`
- Migrate `LongHashTableTest` to Junit5 as an example to show how to use the new extension
## Verifying this change
This change is already covered by existing tests.
## Does this pull request potentially affect one of the following parts:
- Dependencies (does it add or upgrade a dependency): no
- The public API, i.e., is any changed class annotated with @Public(Evolving): no
- The serializers: no
- The runtime per-record code paths (performance sensitive): no
- Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Kubernetes/Yarn, ZooKeeper: no
- The S3 file system connector: no
## Documentation
- Does this pull request introduce a new feature? : no
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] yuchengxin commented on a diff in pull request #20145: [FLINK-25315][tests] add new extensions and utils to help the Junit5 …
Posted by GitBox <gi...@apache.org>.
yuchengxin commented on code in PR #20145:
URL: https://github.com/apache/flink/pull/20145#discussion_r917228744
##########
flink-test-utils-parent/flink-test-utils-junit/src/main/java/org/apache/flink/testutils/junit/utils/TempDirUtils.java:
##########
@@ -0,0 +1,96 @@
+/*
+ * 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.testutils.junit.utils;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.attribute.FileAttribute;
+
+/** The utils contains some methods same as {@link org.junit.rules.TemporaryFolder}. */
Review Comment:
I have removed junit4's dependencies.
##########
flink-test-utils-parent/flink-test-utils-junit/src/main/java/org/apache/flink/testutils/junit/extensions/parameterized/ParameterizedTestExtension.java:
##########
@@ -0,0 +1,250 @@
+/*
+ * 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.testutils.junit.extensions.parameterized;
+
+import org.junit.jupiter.api.TestTemplate;
+import org.junit.jupiter.api.extension.BeforeEachCallback;
+import org.junit.jupiter.api.extension.Extension;
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.api.extension.ParameterContext;
+import org.junit.jupiter.api.extension.ParameterResolutionException;
+import org.junit.jupiter.api.extension.ParameterResolver;
+import org.junit.jupiter.api.extension.TestTemplateInvocationContext;
+import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider;
+import org.junit.platform.commons.support.AnnotationSupport;
+import org.junit.platform.commons.support.HierarchyTraversalMode;
+import org.junit.runners.Parameterized;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.text.MessageFormat;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.function.Function;
+import java.util.stream.Stream;
+
+/**
+ * This extension is used to implement parameterized tests for Junit 5 to replace {@link
+ * Parameterized}.
Review Comment:
I have removed junit4's dependencies.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] yuchengxin commented on a diff in pull request #20145: [FLINK-25315][tests] add new extensions and utils to help the Junit5 …
Posted by GitBox <gi...@apache.org>.
yuchengxin commented on code in PR #20145:
URL: https://github.com/apache/flink/pull/20145#discussion_r917228722
##########
flink-test-utils-parent/flink-test-utils-junit/src/main/java/org/apache/flink/testutils/junit/extensions/parameterized/Parameters.java:
##########
@@ -0,0 +1,36 @@
+/*
+ * 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.testutils.junit.extensions.parameterized;
+
+import org.junit.runners.Parameterized;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * The annotation is used to replace {@link Parameterized.Parameters} for Junit 5 parameterized
Review Comment:
I have removed junit4's dependencies.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] yuchengxin commented on pull request #20145: [FLINK-25315][tests] add new extensions and utils to help the Junit5 …
Posted by GitBox <gi...@apache.org>.
yuchengxin commented on PR #20145:
URL: https://github.com/apache/flink/pull/20145#issuecomment-1179497611
@flinkbot run azure
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] Myasuka commented on pull request #20145: [FLINK-25315][tests] add new extensions and utils to help the Junit5 …
Posted by GitBox <gi...@apache.org>.
Myasuka commented on PR #20145:
URL: https://github.com/apache/flink/pull/20145#issuecomment-1179515495
@flinkbot run azure
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] flinkbot commented on pull request #20145: [FLINK-25315][tests] add new extensions and utils to help the Junit5 …
Posted by GitBox <gi...@apache.org>.
flinkbot commented on PR #20145:
URL: https://github.com/apache/flink/pull/20145#issuecomment-1173319623
<!--
Meta data
{
"version" : 1,
"metaDataEntries" : [ {
"hash" : "db4fdb4f942b733539be83ef2eedc234a445c4ac",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "db4fdb4f942b733539be83ef2eedc234a445c4ac",
"triggerType" : "PUSH"
} ]
}-->
## CI report:
* db4fdb4f942b733539be83ef2eedc234a445c4ac UNKNOWN
<details>
<summary>Bot commands</summary>
The @flinkbot bot supports the following commands:
- `@flinkbot run azure` re-run the last Azure build
</details>
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] Myasuka closed pull request #20145: [FLINK-25315][tests] add new extensions and utils to help the Junit5 …
Posted by GitBox <gi...@apache.org>.
Myasuka closed pull request #20145: [FLINK-25315][tests] add new extensions and utils to help the Junit5 …
URL: https://github.com/apache/flink/pull/20145
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] yuchengxin commented on a diff in pull request #20145: [FLINK-25315][tests] add new extensions and utils to help the Junit5 …
Posted by GitBox <gi...@apache.org>.
yuchengxin commented on code in PR #20145:
URL: https://github.com/apache/flink/pull/20145#discussion_r917228844
##########
flink-test-utils-parent/flink-test-utils-junit/src/main/java/org/apache/flink/testutils/junit/extensions/parameterized/ParameterizedTestExtension.java:
##########
@@ -0,0 +1,250 @@
+/*
+ * 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.testutils.junit.extensions.parameterized;
+
+import org.junit.jupiter.api.TestTemplate;
+import org.junit.jupiter.api.extension.BeforeEachCallback;
+import org.junit.jupiter.api.extension.Extension;
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.api.extension.ParameterContext;
+import org.junit.jupiter.api.extension.ParameterResolutionException;
+import org.junit.jupiter.api.extension.ParameterResolver;
+import org.junit.jupiter.api.extension.TestTemplateInvocationContext;
+import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider;
+import org.junit.platform.commons.support.AnnotationSupport;
+import org.junit.platform.commons.support.HierarchyTraversalMode;
+import org.junit.runners.Parameterized;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.text.MessageFormat;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.function.Function;
+import java.util.stream.Stream;
+
+/**
+ * This extension is used to implement parameterized tests for Junit 5 to replace {@link
+ * Parameterized}.
+ *
+ * <p>When use this extension, all tests must be annotated by {@link TestTemplate}.
+ */
+public class ParameterizedTestExtension implements TestTemplateInvocationContextProvider {
+
+ private static final ExtensionContext.Namespace NAMESPACE =
+ ExtensionContext.Namespace.create("parameterized");
+ private static final String PARAMETERS_STORE_KEY = "parameters";
+ private static final String PARAMETER_FIELD_STORE_KEY_PREFIX = "parameterField_";
+
+ @Override
+ public boolean supportsTestTemplate(ExtensionContext context) {
+ return true;
+ }
+
+ @Override
+ public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(
+ ExtensionContext context) {
+
+ // Search method annotated with @Parameters
+ final List<Method> parameterProviders =
+ AnnotationSupport.findAnnotatedMethods(
+ context.getRequiredTestClass(),
+ Parameters.class,
+ HierarchyTraversalMode.TOP_DOWN);
+ if (parameterProviders.isEmpty()) {
+ throw new IllegalStateException("Cannot find any parameter provider");
+ }
+ if (parameterProviders.size() > 1) {
+ throw new IllegalStateException("Multiple parameter providers are found");
+ }
+
+ // Get potential test name
+ String testNameTemplate = parameterProviders.get(0).getAnnotation(Parameters.class).name();
+
+ // Get parameter values
+ final Object parameterValues;
+ try {
+ parameterValues = parameterProviders.get(0).invoke(null);
+ context.getStore(NAMESPACE).put(PARAMETERS_STORE_KEY, parameterValues);
+ } catch (Exception e) {
+ throw new IllegalStateException("Failed to invoke parameter provider", e);
+ }
+ assert parameterValues != null;
+
+ // Parameter values could be Object[][]
+ if (parameterValues instanceof Object[][]) {
+ Object[][] typedParameterValues = (Object[][]) parameterValues;
+ return createContextForParameters(
+ Arrays.stream(typedParameterValues), testNameTemplate, context);
+ }
+
+ // or a Collection
+ if (parameterValues instanceof Collection) {
+ final Collection<?> typedParameterValues = (Collection<?>) parameterValues;
+ final Stream<Object[]> parameterValueStream =
+ typedParameterValues.stream()
+ .map(
+ (Function<Object, Object[]>)
+ parameterValue -> {
+ if (parameterValue instanceof Object[]) {
+ return (Object[]) parameterValue;
+ } else {
+ return new Object[] {parameterValue};
+ }
+ });
+ return createContextForParameters(parameterValueStream, testNameTemplate, context);
+ }
+
+ throw new IllegalStateException(
+ String.format(
+ "Return type of @Parameters annotated method \"%s\" should be either Object[][] or Collection",
+ parameterProviders.get(0)));
+ }
+
+ private static class FieldInjectingInvocationContext implements TestTemplateInvocationContext {
+
+ private final String testNameTemplate;
+ private final Object[] parameterValues;
+
+ public FieldInjectingInvocationContext(String testNameTemplate, Object[] parameterValues) {
+ this.testNameTemplate = testNameTemplate;
+ this.parameterValues = parameterValues;
+ }
+
+ @Override
+ public String getDisplayName(int invocationIndex) {
+ if (testNameTemplate.equals("{index}")) {
Review Comment:
I have set this magic string to a private static variable
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] yuchengxin commented on a diff in pull request #20145: [FLINK-25315][tests] add new extensions and utils to help the Junit5 …
Posted by GitBox <gi...@apache.org>.
yuchengxin commented on code in PR #20145:
URL: https://github.com/apache/flink/pull/20145#discussion_r917228323
##########
flink-test-utils-parent/flink-test-utils-junit/src/main/java/org/apache/flink/testutils/junit/extensions/parameterized/ParameterizedTestExtension.java:
##########
@@ -0,0 +1,250 @@
+/*
+ * 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.testutils.junit.extensions.parameterized;
+
+import org.junit.jupiter.api.TestTemplate;
+import org.junit.jupiter.api.extension.BeforeEachCallback;
+import org.junit.jupiter.api.extension.Extension;
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.api.extension.ParameterContext;
+import org.junit.jupiter.api.extension.ParameterResolutionException;
+import org.junit.jupiter.api.extension.ParameterResolver;
+import org.junit.jupiter.api.extension.TestTemplateInvocationContext;
+import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider;
+import org.junit.platform.commons.support.AnnotationSupport;
+import org.junit.platform.commons.support.HierarchyTraversalMode;
+import org.junit.runners.Parameterized;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.text.MessageFormat;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.function.Function;
+import java.util.stream.Stream;
+
+/**
+ * This extension is used to implement parameterized tests for Junit 5 to replace {@link
+ * Parameterized}.
+ *
+ * <p>When use this extension, all tests must be annotated by {@link TestTemplate}.
+ */
+public class ParameterizedTestExtension implements TestTemplateInvocationContextProvider {
+
+ private static final ExtensionContext.Namespace NAMESPACE =
+ ExtensionContext.Namespace.create("parameterized");
+ private static final String PARAMETERS_STORE_KEY = "parameters";
+ private static final String PARAMETER_FIELD_STORE_KEY_PREFIX = "parameterField_";
+
+ @Override
+ public boolean supportsTestTemplate(ExtensionContext context) {
+ return true;
+ }
+
+ @Override
+ public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(
+ ExtensionContext context) {
+
+ // Search method annotated with @Parameters
+ final List<Method> parameterProviders =
+ AnnotationSupport.findAnnotatedMethods(
+ context.getRequiredTestClass(),
+ Parameters.class,
+ HierarchyTraversalMode.TOP_DOWN);
+ if (parameterProviders.isEmpty()) {
+ throw new IllegalStateException("Cannot find any parameter provider");
+ }
+ if (parameterProviders.size() > 1) {
+ throw new IllegalStateException("Multiple parameter providers are found");
+ }
+
+ // Get potential test name
+ String testNameTemplate = parameterProviders.get(0).getAnnotation(Parameters.class).name();
Review Comment:
Already modified as you suggested
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] Myasuka commented on pull request #20145: [FLINK-25315][tests] add new extensions and utils to help the Junit5 …
Posted by GitBox <gi...@apache.org>.
Myasuka commented on PR #20145:
URL: https://github.com/apache/flink/pull/20145#issuecomment-1179492037
@yuchengxin Thanks for the update!
I think this PR looks good. Would you please update the description as you already introduced another `PackagedProgramUtilsPipelineTest.java` test to show how to use `TempDirUtils` and `ParameterizedTestExtension`.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] Myasuka commented on pull request #20145: [FLINK-25315][tests] add new extensions and utils to help the Junit5 …
Posted by GitBox <gi...@apache.org>.
Myasuka commented on PR #20145:
URL: https://github.com/apache/flink/pull/20145#issuecomment-1180313074
Thanks for the contributions from @yuchengxin and previous work done by @ruanhang1993 !
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] yuchengxin commented on a diff in pull request #20145: [FLINK-25315][tests] add new extensions and utils to help the Junit5 …
Posted by GitBox <gi...@apache.org>.
yuchengxin commented on code in PR #20145:
URL: https://github.com/apache/flink/pull/20145#discussion_r917228473
##########
flink-test-utils-parent/flink-test-utils-junit/src/main/java/org/apache/flink/testutils/junit/utils/TempDirUtils.java:
##########
@@ -0,0 +1,96 @@
+/*
+ * 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.testutils.junit.utils;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.attribute.FileAttribute;
+
+/** The utils contains some methods same as {@link org.junit.rules.TemporaryFolder}. */
+public class TempDirUtils {
+ private static final String TMP_PREFIX = "junit";
+
+ public static File newFolder(Path path) throws IOException {
+ Path tempPath;
+ if (path != null) {
+ tempPath = Files.createTempDirectory(path, TMP_PREFIX, new FileAttribute[0]);
+ } else {
+ tempPath = Files.createTempDirectory(TMP_PREFIX, new FileAttribute[0]);
+ }
+ return tempPath.toFile();
+ }
+
+ public static File newFile(Path path) throws IOException {
+ return File.createTempFile(TMP_PREFIX, null, path.toFile());
+ }
+
+ public static File newFolder(Path base, String... paths) throws IOException {
+ if (paths.length == 0) {
+ throw new IllegalArgumentException("must pass at least one path");
+ }
+
+ /*
+ * Before checking if the paths are absolute paths, check if create() was ever called,
+ * and if it wasn't, throw IllegalStateException.
+ */
+ File root = base.toFile();
+ for (String path : paths) {
+ if (new File(path).isAbsolute()) {
+ throw new IOException("folder path \'" + path + "\' is not a relative path");
+ }
+ }
+
+ File relativePath = null;
+ File file = root;
+ boolean lastMkdirsCallSuccessful = true;
+ for (String path : paths) {
+ relativePath = new File(relativePath, path);
+ file = new File(root, relativePath.getPath());
+
+ lastMkdirsCallSuccessful = file.mkdirs();
+ if (!lastMkdirsCallSuccessful && !file.isDirectory()) {
+ if (file.exists()) {
+ throw new IOException(
+ "a file with the path \'" + relativePath.getPath() + "\' exists");
+ } else {
+ throw new IOException(
+ "could not create a folder with the path \'"
+ + relativePath.getPath()
+ + "\'");
+ }
+ }
+ }
+ if (!lastMkdirsCallSuccessful) {
+ throw new IOException(
+ "a folder with the path \'" + relativePath.getPath() + "\' already exists");
+ }
+ return file;
+ }
+
+ public static File newFile(Path folder, String fileName) throws IOException {
+ File file = new File(folder.toFile(), fileName);
+ if (!file.createNewFile()) {
+ throw new IOException(
+ "a file with the name \'" + fileName + "\' already exists in the test folder");
Review Comment:
Has updated all similar places.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] yuchengxin commented on a diff in pull request #20145: [FLINK-25315][tests] add new extensions and utils to help the Junit5 …
Posted by GitBox <gi...@apache.org>.
yuchengxin commented on code in PR #20145:
URL: https://github.com/apache/flink/pull/20145#discussion_r917228604
##########
flink-test-utils-parent/flink-test-utils-junit/src/main/java/org/apache/flink/testutils/junit/utils/TempDirUtils.java:
##########
@@ -0,0 +1,96 @@
+/*
+ * 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.testutils.junit.utils;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.attribute.FileAttribute;
+
+/** The utils contains some methods same as {@link org.junit.rules.TemporaryFolder}. */
+public class TempDirUtils {
Review Comment:
I have modified PackagedProgramUtilsPipelineTest.java as a example to use TempDirUtils and ParameterizedTestExtension
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] yuchengxin commented on a diff in pull request #20145: [FLINK-25315][tests] add new extensions and utils to help the Junit5 …
Posted by GitBox <gi...@apache.org>.
yuchengxin commented on code in PR #20145:
URL: https://github.com/apache/flink/pull/20145#discussion_r917228617
##########
flink-test-utils-parent/flink-test-utils-junit/src/main/java/org/apache/flink/testutils/junit/extensions/parameterized/Parameters.java:
##########
@@ -0,0 +1,36 @@
+/*
+ * 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.testutils.junit.extensions.parameterized;
+
+import org.junit.runners.Parameterized;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * The annotation is used to replace {@link Parameterized.Parameters} for Junit 5 parameterized
Review Comment:
Already modified as you suggested.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] Myasuka commented on a diff in pull request #20145: [FLINK-25315][tests] add new extensions and utils to help the Junit5 …
Posted by GitBox <gi...@apache.org>.
Myasuka commented on code in PR #20145:
URL: https://github.com/apache/flink/pull/20145#discussion_r913702461
##########
flink-test-utils-parent/flink-test-utils-junit/src/main/java/org/apache/flink/testutils/junit/extensions/parameterized/ParameterizedTestExtension.java:
##########
@@ -0,0 +1,250 @@
+/*
+ * 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.testutils.junit.extensions.parameterized;
+
+import org.junit.jupiter.api.TestTemplate;
+import org.junit.jupiter.api.extension.BeforeEachCallback;
+import org.junit.jupiter.api.extension.Extension;
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.api.extension.ParameterContext;
+import org.junit.jupiter.api.extension.ParameterResolutionException;
+import org.junit.jupiter.api.extension.ParameterResolver;
+import org.junit.jupiter.api.extension.TestTemplateInvocationContext;
+import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider;
+import org.junit.platform.commons.support.AnnotationSupport;
+import org.junit.platform.commons.support.HierarchyTraversalMode;
+import org.junit.runners.Parameterized;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.text.MessageFormat;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.function.Function;
+import java.util.stream.Stream;
+
+/**
+ * This extension is used to implement parameterized tests for Junit 5 to replace {@link
+ * Parameterized}.
+ *
+ * <p>When use this extension, all tests must be annotated by {@link TestTemplate}.
+ */
+public class ParameterizedTestExtension implements TestTemplateInvocationContextProvider {
+
+ private static final ExtensionContext.Namespace NAMESPACE =
+ ExtensionContext.Namespace.create("parameterized");
+ private static final String PARAMETERS_STORE_KEY = "parameters";
+ private static final String PARAMETER_FIELD_STORE_KEY_PREFIX = "parameterField_";
+
+ @Override
+ public boolean supportsTestTemplate(ExtensionContext context) {
+ return true;
+ }
+
+ @Override
+ public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(
+ ExtensionContext context) {
+
+ // Search method annotated with @Parameters
+ final List<Method> parameterProviders =
+ AnnotationSupport.findAnnotatedMethods(
+ context.getRequiredTestClass(),
+ Parameters.class,
+ HierarchyTraversalMode.TOP_DOWN);
+ if (parameterProviders.isEmpty()) {
+ throw new IllegalStateException("Cannot find any parameter provider");
+ }
+ if (parameterProviders.size() > 1) {
+ throw new IllegalStateException("Multiple parameter providers are found");
+ }
+
+ // Get potential test name
+ String testNameTemplate = parameterProviders.get(0).getAnnotation(Parameters.class).name();
Review Comment:
I can see many places have the usage of `parameterProviders.get(0)`, we can use just one variable to share it in many places.
##########
flink-test-utils-parent/flink-test-utils-junit/src/main/java/org/apache/flink/testutils/junit/extensions/parameterized/ParameterizedTestExtension.java:
##########
@@ -0,0 +1,250 @@
+/*
+ * 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.testutils.junit.extensions.parameterized;
+
+import org.junit.jupiter.api.TestTemplate;
+import org.junit.jupiter.api.extension.BeforeEachCallback;
+import org.junit.jupiter.api.extension.Extension;
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.api.extension.ParameterContext;
+import org.junit.jupiter.api.extension.ParameterResolutionException;
+import org.junit.jupiter.api.extension.ParameterResolver;
+import org.junit.jupiter.api.extension.TestTemplateInvocationContext;
+import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider;
+import org.junit.platform.commons.support.AnnotationSupport;
+import org.junit.platform.commons.support.HierarchyTraversalMode;
+import org.junit.runners.Parameterized;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.text.MessageFormat;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.function.Function;
+import java.util.stream.Stream;
+
+/**
+ * This extension is used to implement parameterized tests for Junit 5 to replace {@link
+ * Parameterized}.
+ *
+ * <p>When use this extension, all tests must be annotated by {@link TestTemplate}.
+ */
+public class ParameterizedTestExtension implements TestTemplateInvocationContextProvider {
+
+ private static final ExtensionContext.Namespace NAMESPACE =
+ ExtensionContext.Namespace.create("parameterized");
+ private static final String PARAMETERS_STORE_KEY = "parameters";
+ private static final String PARAMETER_FIELD_STORE_KEY_PREFIX = "parameterField_";
+
+ @Override
+ public boolean supportsTestTemplate(ExtensionContext context) {
+ return true;
+ }
+
+ @Override
+ public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(
+ ExtensionContext context) {
+
+ // Search method annotated with @Parameters
+ final List<Method> parameterProviders =
+ AnnotationSupport.findAnnotatedMethods(
+ context.getRequiredTestClass(),
+ Parameters.class,
+ HierarchyTraversalMode.TOP_DOWN);
+ if (parameterProviders.isEmpty()) {
+ throw new IllegalStateException("Cannot find any parameter provider");
+ }
+ if (parameterProviders.size() > 1) {
+ throw new IllegalStateException("Multiple parameter providers are found");
+ }
+
+ // Get potential test name
+ String testNameTemplate = parameterProviders.get(0).getAnnotation(Parameters.class).name();
+
+ // Get parameter values
+ final Object parameterValues;
+ try {
+ parameterValues = parameterProviders.get(0).invoke(null);
+ context.getStore(NAMESPACE).put(PARAMETERS_STORE_KEY, parameterValues);
+ } catch (Exception e) {
+ throw new IllegalStateException("Failed to invoke parameter provider", e);
+ }
+ assert parameterValues != null;
+
+ // Parameter values could be Object[][]
+ if (parameterValues instanceof Object[][]) {
+ Object[][] typedParameterValues = (Object[][]) parameterValues;
+ return createContextForParameters(
+ Arrays.stream(typedParameterValues), testNameTemplate, context);
+ }
+
+ // or a Collection
+ if (parameterValues instanceof Collection) {
+ final Collection<?> typedParameterValues = (Collection<?>) parameterValues;
+ final Stream<Object[]> parameterValueStream =
+ typedParameterValues.stream()
+ .map(
+ (Function<Object, Object[]>)
+ parameterValue -> {
+ if (parameterValue instanceof Object[]) {
+ return (Object[]) parameterValue;
+ } else {
+ return new Object[] {parameterValue};
+ }
+ });
+ return createContextForParameters(parameterValueStream, testNameTemplate, context);
+ }
+
+ throw new IllegalStateException(
+ String.format(
+ "Return type of @Parameters annotated method \"%s\" should be either Object[][] or Collection",
+ parameterProviders.get(0)));
+ }
+
+ private static class FieldInjectingInvocationContext implements TestTemplateInvocationContext {
+
+ private final String testNameTemplate;
+ private final Object[] parameterValues;
+
+ public FieldInjectingInvocationContext(String testNameTemplate, Object[] parameterValues) {
+ this.testNameTemplate = testNameTemplate;
+ this.parameterValues = parameterValues;
+ }
+
+ @Override
+ public String getDisplayName(int invocationIndex) {
+ if (testNameTemplate.equals("{index}")) {
Review Comment:
I think we should avoid to use such magic string.
##########
flink-test-utils-parent/flink-test-utils-junit/src/main/java/org/apache/flink/testutils/junit/utils/TempDirUtils.java:
##########
@@ -0,0 +1,96 @@
+/*
+ * 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.testutils.junit.utils;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.attribute.FileAttribute;
+
+/** The utils contains some methods same as {@link org.junit.rules.TemporaryFolder}. */
+public class TempDirUtils {
Review Comment:
I found we lack a test to use this class, maybe `PackagedProgramUtilsPipelineTest.java` is a good choice.
##########
flink-test-utils-parent/flink-test-utils-junit/src/main/java/org/apache/flink/testutils/junit/extensions/parameterized/Parameters.java:
##########
@@ -0,0 +1,36 @@
+/*
+ * 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.testutils.junit.extensions.parameterized;
+
+import org.junit.runners.Parameterized;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * The annotation is used to replace {@link Parameterized.Parameters} for Junit 5 parameterized
Review Comment:
We should avoid importing junit4's class here.
##########
flink-test-utils-parent/flink-test-utils-junit/src/main/java/org/apache/flink/testutils/junit/extensions/parameterized/ParameterizedTestExtension.java:
##########
@@ -0,0 +1,250 @@
+/*
+ * 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.testutils.junit.extensions.parameterized;
+
+import org.junit.jupiter.api.TestTemplate;
+import org.junit.jupiter.api.extension.BeforeEachCallback;
+import org.junit.jupiter.api.extension.Extension;
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.api.extension.ParameterContext;
+import org.junit.jupiter.api.extension.ParameterResolutionException;
+import org.junit.jupiter.api.extension.ParameterResolver;
+import org.junit.jupiter.api.extension.TestTemplateInvocationContext;
+import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider;
+import org.junit.platform.commons.support.AnnotationSupport;
+import org.junit.platform.commons.support.HierarchyTraversalMode;
+import org.junit.runners.Parameterized;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.text.MessageFormat;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.function.Function;
+import java.util.stream.Stream;
+
+/**
+ * This extension is used to implement parameterized tests for Junit 5 to replace {@link
+ * Parameterized}.
Review Comment:
We should avoid to import junit4's class here.
##########
flink-test-utils-parent/flink-test-utils-junit/src/main/java/org/apache/flink/testutils/junit/utils/TempDirUtils.java:
##########
@@ -0,0 +1,96 @@
+/*
+ * 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.testutils.junit.utils;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.attribute.FileAttribute;
+
+/** The utils contains some methods same as {@link org.junit.rules.TemporaryFolder}. */
+public class TempDirUtils {
+ private static final String TMP_PREFIX = "junit";
+
+ public static File newFolder(Path path) throws IOException {
+ Path tempPath;
+ if (path != null) {
+ tempPath = Files.createTempDirectory(path, TMP_PREFIX, new FileAttribute[0]);
+ } else {
+ tempPath = Files.createTempDirectory(TMP_PREFIX, new FileAttribute[0]);
Review Comment:
I think we can use `Files.createTempDirectory(TMP_PREFIX)` to replace this.
##########
flink-test-utils-parent/flink-test-utils-junit/src/main/java/org/apache/flink/testutils/junit/utils/TempDirUtils.java:
##########
@@ -0,0 +1,96 @@
+/*
+ * 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.testutils.junit.utils;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.attribute.FileAttribute;
+
+/** The utils contains some methods same as {@link org.junit.rules.TemporaryFolder}. */
+public class TempDirUtils {
+ private static final String TMP_PREFIX = "junit";
+
+ public static File newFolder(Path path) throws IOException {
+ Path tempPath;
+ if (path != null) {
+ tempPath = Files.createTempDirectory(path, TMP_PREFIX, new FileAttribute[0]);
+ } else {
+ tempPath = Files.createTempDirectory(TMP_PREFIX, new FileAttribute[0]);
+ }
+ return tempPath.toFile();
+ }
+
+ public static File newFile(Path path) throws IOException {
+ return File.createTempFile(TMP_PREFIX, null, path.toFile());
+ }
+
+ public static File newFolder(Path base, String... paths) throws IOException {
+ if (paths.length == 0) {
+ throw new IllegalArgumentException("must pass at least one path");
+ }
+
+ /*
+ * Before checking if the paths are absolute paths, check if create() was ever called,
+ * and if it wasn't, throw IllegalStateException.
+ */
+ File root = base.toFile();
+ for (String path : paths) {
+ if (new File(path).isAbsolute()) {
+ throw new IOException("folder path \'" + path + "\' is not a relative path");
Review Comment:
```suggestion
throw new IOException(String.format("folder path '%s' is not a relative path", path));
```
##########
flink-test-utils-parent/flink-test-utils-junit/src/main/java/org/apache/flink/testutils/junit/utils/TempDirUtils.java:
##########
@@ -0,0 +1,96 @@
+/*
+ * 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.testutils.junit.utils;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.attribute.FileAttribute;
+
+/** The utils contains some methods same as {@link org.junit.rules.TemporaryFolder}. */
Review Comment:
We should avoid referring junit4's class here.
We can change it to `The utils contains some methods same as org.junit.rules.TemporaryFolder.`.
##########
flink-test-utils-parent/flink-test-utils-junit/src/main/java/org/apache/flink/testutils/junit/utils/TempDirUtils.java:
##########
@@ -0,0 +1,96 @@
+/*
+ * 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.testutils.junit.utils;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.attribute.FileAttribute;
+
+/** The utils contains some methods same as {@link org.junit.rules.TemporaryFolder}. */
+public class TempDirUtils {
+ private static final String TMP_PREFIX = "junit";
+
+ public static File newFolder(Path path) throws IOException {
+ Path tempPath;
+ if (path != null) {
+ tempPath = Files.createTempDirectory(path, TMP_PREFIX, new FileAttribute[0]);
+ } else {
+ tempPath = Files.createTempDirectory(TMP_PREFIX, new FileAttribute[0]);
+ }
+ return tempPath.toFile();
+ }
+
+ public static File newFile(Path path) throws IOException {
+ return File.createTempFile(TMP_PREFIX, null, path.toFile());
+ }
+
+ public static File newFolder(Path base, String... paths) throws IOException {
+ if (paths.length == 0) {
+ throw new IllegalArgumentException("must pass at least one path");
+ }
+
+ /*
+ * Before checking if the paths are absolute paths, check if create() was ever called,
+ * and if it wasn't, throw IllegalStateException.
+ */
+ File root = base.toFile();
+ for (String path : paths) {
+ if (new File(path).isAbsolute()) {
+ throw new IOException("folder path \'" + path + "\' is not a relative path");
+ }
+ }
+
+ File relativePath = null;
+ File file = root;
+ boolean lastMkdirsCallSuccessful = true;
+ for (String path : paths) {
+ relativePath = new File(relativePath, path);
+ file = new File(root, relativePath.getPath());
+
+ lastMkdirsCallSuccessful = file.mkdirs();
+ if (!lastMkdirsCallSuccessful && !file.isDirectory()) {
+ if (file.exists()) {
+ throw new IOException(
+ "a file with the path \'" + relativePath.getPath() + "\' exists");
+ } else {
+ throw new IOException(
+ "could not create a folder with the path \'"
+ + relativePath.getPath()
+ + "\'");
+ }
+ }
+ }
+ if (!lastMkdirsCallSuccessful) {
+ throw new IOException(
+ "a folder with the path \'" + relativePath.getPath() + "\' already exists");
+ }
+ return file;
+ }
+
+ public static File newFile(Path folder, String fileName) throws IOException {
+ File file = new File(folder.toFile(), fileName);
+ if (!file.createNewFile()) {
+ throw new IOException(
+ "a file with the name \'" + fileName + "\' already exists in the test folder");
Review Comment:
Please update all similar places.
##########
flink-test-utils-parent/flink-test-utils-junit/src/main/java/org/apache/flink/testutils/junit/utils/TempDirUtils.java:
##########
@@ -0,0 +1,96 @@
+/*
+ * 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.testutils.junit.utils;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.attribute.FileAttribute;
+
+/** The utils contains some methods same as {@link org.junit.rules.TemporaryFolder}. */
+public class TempDirUtils {
+ private static final String TMP_PREFIX = "junit";
+
+ public static File newFolder(Path path) throws IOException {
+ Path tempPath;
+ if (path != null) {
+ tempPath = Files.createTempDirectory(path, TMP_PREFIX, new FileAttribute[0]);
+ } else {
+ tempPath = Files.createTempDirectory(TMP_PREFIX, new FileAttribute[0]);
+ }
+ return tempPath.toFile();
+ }
+
+ public static File newFile(Path path) throws IOException {
+ return File.createTempFile(TMP_PREFIX, null, path.toFile());
+ }
+
+ public static File newFolder(Path base, String... paths) throws IOException {
+ if (paths.length == 0) {
+ throw new IllegalArgumentException("must pass at least one path");
+ }
+
+ /*
+ * Before checking if the paths are absolute paths, check if create() was ever called,
+ * and if it wasn't, throw IllegalStateException.
+ */
+ File root = base.toFile();
+ for (String path : paths) {
+ if (new File(path).isAbsolute()) {
+ throw new IOException("folder path \'" + path + "\' is not a relative path");
+ }
+ }
+
+ File relativePath = null;
+ File file = root;
+ boolean lastMkdirsCallSuccessful = true;
+ for (String path : paths) {
+ relativePath = new File(relativePath, path);
+ file = new File(root, relativePath.getPath());
+
+ lastMkdirsCallSuccessful = file.mkdirs();
+ if (!lastMkdirsCallSuccessful && !file.isDirectory()) {
+ if (file.exists()) {
+ throw new IOException(
+ "a file with the path \'" + relativePath.getPath() + "\' exists");
Review Comment:
```suggestion
String.format("a file with the path '%s' exists", relativePath.getPath()));
```
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] yuchengxin commented on a diff in pull request #20145: [FLINK-25315][tests] add new extensions and utils to help the Junit5 …
Posted by GitBox <gi...@apache.org>.
yuchengxin commented on code in PR #20145:
URL: https://github.com/apache/flink/pull/20145#discussion_r917228402
##########
flink-test-utils-parent/flink-test-utils-junit/src/main/java/org/apache/flink/testutils/junit/utils/TempDirUtils.java:
##########
@@ -0,0 +1,96 @@
+/*
+ * 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.testutils.junit.utils;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.attribute.FileAttribute;
+
+/** The utils contains some methods same as {@link org.junit.rules.TemporaryFolder}. */
+public class TempDirUtils {
+ private static final String TMP_PREFIX = "junit";
+
+ public static File newFolder(Path path) throws IOException {
+ Path tempPath;
+ if (path != null) {
+ tempPath = Files.createTempDirectory(path, TMP_PREFIX, new FileAttribute[0]);
+ } else {
+ tempPath = Files.createTempDirectory(TMP_PREFIX, new FileAttribute[0]);
+ }
+ return tempPath.toFile();
+ }
+
+ public static File newFile(Path path) throws IOException {
+ return File.createTempFile(TMP_PREFIX, null, path.toFile());
+ }
+
+ public static File newFolder(Path base, String... paths) throws IOException {
+ if (paths.length == 0) {
+ throw new IllegalArgumentException("must pass at least one path");
+ }
+
+ /*
+ * Before checking if the paths are absolute paths, check if create() was ever called,
+ * and if it wasn't, throw IllegalStateException.
+ */
+ File root = base.toFile();
+ for (String path : paths) {
+ if (new File(path).isAbsolute()) {
+ throw new IOException("folder path \'" + path + "\' is not a relative path");
Review Comment:
Already modified as you suggested.
##########
flink-test-utils-parent/flink-test-utils-junit/src/main/java/org/apache/flink/testutils/junit/utils/TempDirUtils.java:
##########
@@ -0,0 +1,96 @@
+/*
+ * 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.testutils.junit.utils;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.attribute.FileAttribute;
+
+/** The utils contains some methods same as {@link org.junit.rules.TemporaryFolder}. */
+public class TempDirUtils {
+ private static final String TMP_PREFIX = "junit";
+
+ public static File newFolder(Path path) throws IOException {
+ Path tempPath;
+ if (path != null) {
+ tempPath = Files.createTempDirectory(path, TMP_PREFIX, new FileAttribute[0]);
+ } else {
+ tempPath = Files.createTempDirectory(TMP_PREFIX, new FileAttribute[0]);
+ }
+ return tempPath.toFile();
+ }
+
+ public static File newFile(Path path) throws IOException {
+ return File.createTempFile(TMP_PREFIX, null, path.toFile());
+ }
+
+ public static File newFolder(Path base, String... paths) throws IOException {
+ if (paths.length == 0) {
+ throw new IllegalArgumentException("must pass at least one path");
+ }
+
+ /*
+ * Before checking if the paths are absolute paths, check if create() was ever called,
+ * and if it wasn't, throw IllegalStateException.
+ */
+ File root = base.toFile();
+ for (String path : paths) {
+ if (new File(path).isAbsolute()) {
+ throw new IOException("folder path \'" + path + "\' is not a relative path");
+ }
+ }
+
+ File relativePath = null;
+ File file = root;
+ boolean lastMkdirsCallSuccessful = true;
+ for (String path : paths) {
+ relativePath = new File(relativePath, path);
+ file = new File(root, relativePath.getPath());
+
+ lastMkdirsCallSuccessful = file.mkdirs();
+ if (!lastMkdirsCallSuccessful && !file.isDirectory()) {
+ if (file.exists()) {
+ throw new IOException(
+ "a file with the path \'" + relativePath.getPath() + "\' exists");
Review Comment:
Already modified as you suggested.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] yuchengxin commented on a diff in pull request #20145: [FLINK-25315][tests] add new extensions and utils to help the Junit5 …
Posted by GitBox <gi...@apache.org>.
yuchengxin commented on code in PR #20145:
URL: https://github.com/apache/flink/pull/20145#discussion_r917228397
##########
flink-test-utils-parent/flink-test-utils-junit/src/main/java/org/apache/flink/testutils/junit/utils/TempDirUtils.java:
##########
@@ -0,0 +1,96 @@
+/*
+ * 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.testutils.junit.utils;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.attribute.FileAttribute;
+
+/** The utils contains some methods same as {@link org.junit.rules.TemporaryFolder}. */
+public class TempDirUtils {
+ private static final String TMP_PREFIX = "junit";
+
+ public static File newFolder(Path path) throws IOException {
+ Path tempPath;
+ if (path != null) {
+ tempPath = Files.createTempDirectory(path, TMP_PREFIX, new FileAttribute[0]);
+ } else {
+ tempPath = Files.createTempDirectory(TMP_PREFIX, new FileAttribute[0]);
Review Comment:
Already modified as you suggested
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] yuchengxin commented on pull request #20145: [FLINK-25315][tests] add new extensions and utils to help the Junit5 …
Posted by GitBox <gi...@apache.org>.
yuchengxin commented on PR #20145:
URL: https://github.com/apache/flink/pull/20145#issuecomment-1179494111
> @yuchengxin Thanks for the update! I think this PR looks good. Would you please update the description as you already introduced another `PackagedProgramUtilsPipelineTest.java` test to show how to use `TempDirUtils` and `ParameterizedTestExtension`.
I hive modified the description of this PR
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org