Skip to content

Commit 3e22d08

Browse files
committed
[FLINK-38809][Tests] Migrate flink-end-to-end-tests-common from JUnit4 to JUnit5
Testcontainers 2.0.2 removed JUnit4 support, requiring migration to JUnit5. Created local ExternalResource interface to be fully JUnit5-compatible with minimum code changes. This migration allows the module to work with Testcontainers 2.0.2 while maintaining all existing test functionality.
1 parent 883e33b commit 3e22d08

File tree

9 files changed

+105
-47
lines changed

9 files changed

+105
-47
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.apache.flink.tests.util;
20+
21+
/**
22+
* Resource lifecycle interface for end-to-end tests.
23+
*
24+
* <p>This interface provides hooks for resource setup and cleanup, allowing resources to
25+
* differentiate between successful and failed tests in their cleanup methods.
26+
*
27+
* <p>This is a JUnit5-compatible version that does not extend TestRule.
28+
*/
29+
public interface ExternalResource {
30+
31+
/** Called before the test execution. */
32+
void before() throws Exception;
33+
34+
/** Called after successful test execution. */
35+
void afterTestSuccess();
36+
37+
/** Called after failed test execution. Defaults to calling {@link #afterTestSuccess()}. */
38+
default void afterTestFailure() {
39+
afterTestSuccess();
40+
}
41+
}

flink-end-to-end-tests/flink-end-to-end-tests-common/src/main/java/org/apache/flink/tests/util/activation/OperatingSystemRestriction.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020

2121
import org.apache.flink.util.OperatingSystem;
2222

23-
import org.junit.Assume;
24-
import org.junit.AssumptionViolatedException;
23+
import org.junit.jupiter.api.Assumptions;
24+
import org.opentest4j.TestAbortedException;
2525

2626
import java.util.Arrays;
2727
import java.util.EnumSet;
@@ -39,26 +39,27 @@ public enum OperatingSystemRestriction {
3939
*
4040
* @param reason reason for the restriction
4141
* @param operatingSystems allowed operating systems
42-
* @throws AssumptionViolatedException if this method is called on a forbidden operating system
42+
* @throws TestAbortedException if this method is called on a forbidden operating system
4343
*/
4444
public static void restrictTo(final String reason, final OperatingSystem... operatingSystems)
45-
throws AssumptionViolatedException {
45+
throws TestAbortedException {
4646
final EnumSet<OperatingSystem> allowed = EnumSet.copyOf(Arrays.asList(operatingSystems));
47-
Assume.assumeTrue(reason, allowed.contains(OperatingSystem.getCurrentOperatingSystem()));
47+
Assumptions.assumeTrue(
48+
allowed.contains(OperatingSystem.getCurrentOperatingSystem()), reason);
4849
}
4950

5051
/**
5152
* Forbids the execution on the given set of operating systems.
5253
*
5354
* @param reason reason for the restriction
5455
* @param forbiddenSystems forbidden operating systems
55-
* @throws AssumptionViolatedException if this method is called on a forbidden operating system
56+
* @throws TestAbortedException if this method is called on a forbidden operating system
5657
*/
5758
public static void forbid(final String reason, final OperatingSystem... forbiddenSystems)
58-
throws AssumptionViolatedException {
59+
throws TestAbortedException {
5960
final OperatingSystem os = OperatingSystem.getCurrentOperatingSystem();
6061
for (final OperatingSystem forbiddenSystem : forbiddenSystems) {
61-
Assume.assumeTrue(reason, os != forbiddenSystem);
62+
Assumptions.assumeTrue(os != forbiddenSystem, reason);
6263
}
6364
}
6465
}

flink-end-to-end-tests/flink-end-to-end-tests-common/src/main/java/org/apache/flink/tests/util/cache/DownloadCache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
package org.apache.flink.tests.util.cache;
2020

21+
import org.apache.flink.tests.util.ExternalResource;
2122
import org.apache.flink.tests.util.util.FactoryUtils;
22-
import org.apache.flink.util.ExternalResource;
2323

2424
import java.io.IOException;
2525
import java.nio.file.Path;

flink-end-to-end-tests/flink-end-to-end-tests-common/src/main/java/org/apache/flink/tests/util/cache/LolCache.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818

1919
package org.apache.flink.tests.util.cache;
2020

21-
import org.junit.rules.TemporaryFolder;
21+
import org.apache.flink.util.FileUtils;
2222

23+
import java.io.IOException;
24+
import java.nio.file.Path;
2325
import java.util.regex.Matcher;
2426
import java.util.regex.Pattern;
2527

@@ -31,16 +33,20 @@
3133
public final class LolCache extends AbstractDownloadCache {
3234

3335
private static final Pattern CACHE_FILE_NAME_PATTERN = Pattern.compile(".*");
34-
private final TemporaryFolder folder;
36+
private final Path tempDirectory;
3537

36-
public LolCache(TemporaryFolder folder) {
37-
super(folder.getRoot().toPath());
38-
this.folder = folder;
38+
public LolCache(Path tempDirectory) {
39+
super(tempDirectory);
40+
this.tempDirectory = tempDirectory;
3941
}
4042

4143
@Override
4244
public void afterTestSuccess() {
43-
folder.delete();
45+
try {
46+
FileUtils.deleteDirectory(tempDirectory.toFile());
47+
} catch (IOException e) {
48+
// Ignore cleanup failures
49+
}
4450
}
4551

4652
@Override

flink-end-to-end-tests/flink-end-to-end-tests-common/src/main/java/org/apache/flink/tests/util/cache/LolCacheFactory.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,16 @@
1818

1919
package org.apache.flink.tests.util.cache;
2020

21-
import org.junit.rules.TemporaryFolder;
22-
2321
import java.io.IOException;
22+
import java.nio.file.Files;
23+
import java.nio.file.Path;
2424

2525
/** A {@link DownloadCacheFactory} for the {@link LolCache}. */
2626
public final class LolCacheFactory implements DownloadCacheFactory {
2727

2828
@Override
2929
public DownloadCache create() throws IOException {
30-
final TemporaryFolder folder = new TemporaryFolder();
31-
folder.create();
32-
return new LolCache(folder);
30+
final Path tempDirectory = Files.createTempDirectory("flink-lol-cache-");
31+
return new LolCache(tempDirectory);
3332
}
3433
}

flink-end-to-end-tests/flink-end-to-end-tests-common/src/main/java/org/apache/flink/tests/util/flink/FlinkResource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
package org.apache.flink.tests.util.flink;
2020

2121
import org.apache.flink.test.util.JobSubmission;
22+
import org.apache.flink.tests.util.ExternalResource;
2223
import org.apache.flink.tests.util.util.FactoryUtils;
23-
import org.apache.flink.util.ExternalResource;
2424

2525
import java.io.IOException;
2626
import java.time.Duration;

flink-end-to-end-tests/flink-end-to-end-tests-common/src/main/java/org/apache/flink/tests/util/flink/LocalStandaloneFlinkResource.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,18 @@
2929
import org.apache.flink.test.util.SQLJobSubmission;
3030
import org.apache.flink.tests.util.TestUtils;
3131
import org.apache.flink.util.ConfigurationException;
32+
import org.apache.flink.util.FileUtils;
3233
import org.apache.flink.util.concurrent.Executors;
3334
import org.apache.flink.util.concurrent.FutureUtils;
3435

35-
import org.junit.rules.TemporaryFolder;
3636
import org.slf4j.Logger;
3737
import org.slf4j.LoggerFactory;
3838
import org.slf4j.event.Level;
3939

4040
import javax.annotation.Nullable;
4141

4242
import java.io.IOException;
43+
import java.nio.file.Files;
4344
import java.nio.file.Path;
4445
import java.time.Duration;
4546
import java.util.Collections;
@@ -58,12 +59,12 @@ public class LocalStandaloneFlinkResource implements FlinkResource {
5859

5960
private static final Logger LOG = LoggerFactory.getLogger(LocalStandaloneFlinkResource.class);
6061

61-
private final TemporaryFolder temporaryFolder = new TemporaryFolder();
6262
private final Path distributionDirectory;
6363
@Nullable private final Path logBackupDirectory;
6464
private final FlinkResourceSetup setup;
6565

6666
private FlinkDistribution distribution;
67+
private Path tempDirectory;
6768

6869
LocalStandaloneFlinkResource(
6970
Path distributionDirectory,
@@ -77,8 +78,8 @@ public class LocalStandaloneFlinkResource implements FlinkResource {
7778

7879
@Override
7980
public void before() throws Exception {
80-
temporaryFolder.create();
81-
Path tmp = temporaryFolder.newFolder().toPath();
81+
tempDirectory = Files.createTempDirectory("flink-local-standalone-");
82+
Path tmp = Files.createDirectory(tempDirectory.resolve("dist"));
8283
LOG.info("Copying distribution to {}.", tmp);
8384
TestUtils.copyDirectory(distributionDirectory, tmp);
8485

@@ -98,7 +99,7 @@ public void before() throws Exception {
9899
@Override
99100
public void afterTestSuccess() {
100101
shutdownCluster();
101-
temporaryFolder.delete();
102+
cleanupTempDirectory();
102103
}
103104

104105
@Override
@@ -107,7 +108,17 @@ public void afterTestFailure() {
107108
shutdownCluster();
108109
backupLogs();
109110
}
110-
temporaryFolder.delete();
111+
cleanupTempDirectory();
112+
}
113+
114+
private void cleanupTempDirectory() {
115+
if (tempDirectory != null) {
116+
try {
117+
FileUtils.deleteDirectory(tempDirectory.toFile());
118+
} catch (IOException e) {
119+
LOG.warn("Failed to delete temporary directory: {}", tempDirectory, e);
120+
}
121+
}
111122
}
112123

113124
private void shutdownCluster() {

flink-end-to-end-tests/flink-end-to-end-tests-common/src/test/java/org/apache/flink/tests/util/TestUtilsTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,23 @@
2121
import org.apache.flink.util.OperatingSystem;
2222
import org.apache.flink.util.TestLogger;
2323

24-
import org.junit.Assert;
25-
import org.junit.BeforeClass;
26-
import org.junit.Rule;
27-
import org.junit.Test;
28-
import org.junit.rules.TemporaryFolder;
24+
import org.junit.jupiter.api.BeforeAll;
25+
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.api.io.TempDir;
2927

3028
import java.io.IOException;
3129
import java.nio.file.Files;
3230
import java.nio.file.Path;
3331
import java.nio.file.Paths;
3432

33+
import static org.junit.jupiter.api.Assertions.assertTrue;
34+
3535
/** Tests for {@link TestUtils}. */
3636
public class TestUtilsTest extends TestLogger {
3737

38-
@Rule public final TemporaryFolder temporaryFolder = new TemporaryFolder();
38+
@TempDir Path temporaryFolder;
3939

40-
@BeforeClass
40+
@BeforeAll
4141
public static void setupClass() {
4242
OperatingSystemRestriction.forbid(
4343
"Symbolic links usually require special permissions on Windows.",
@@ -50,7 +50,7 @@ public void copyDirectory() throws IOException {
5050
Paths.get("file1"), Paths.get("dir1", "file2"),
5151
};
5252

53-
Path source = temporaryFolder.newFolder("source").toPath();
53+
Path source = Files.createDirectory(temporaryFolder.resolve("source"));
5454
for (Path file : files) {
5555
Files.createDirectories(source.resolve(file).getParent());
5656
Files.createFile(source.resolve(file));
@@ -63,7 +63,7 @@ public void copyDirectory() throws IOException {
6363
TestUtils.copyDirectory(symbolicLink, target);
6464

6565
for (Path file : files) {
66-
Assert.assertTrue(Files.exists(target.resolve(file)));
66+
assertTrue(Files.exists(target.resolve(file)));
6767
}
6868
}
6969
}

flink-end-to-end-tests/flink-end-to-end-tests-common/src/test/java/org/apache/flink/tests/util/util/FileUtilsTest.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@
2020
import org.apache.flink.test.util.FileUtils;
2121
import org.apache.flink.util.TestLogger;
2222

23-
import org.junit.Assert;
24-
import org.junit.Before;
25-
import org.junit.ClassRule;
26-
import org.junit.Test;
27-
import org.junit.rules.TemporaryFolder;
23+
import org.junit.jupiter.api.BeforeEach;
24+
import org.junit.jupiter.api.Test;
25+
import org.junit.jupiter.api.io.TempDir;
2826

2927
import java.io.IOException;
3028
import java.nio.file.Files;
@@ -34,18 +32,20 @@
3432
import java.util.List;
3533
import java.util.regex.Pattern;
3634

35+
import static org.junit.jupiter.api.Assertions.assertEquals;
36+
3737
/** Tests for {@link FileUtils}. */
3838
public class FileUtilsTest extends TestLogger {
3939

40-
@ClassRule public static final TemporaryFolder TMP = new TemporaryFolder();
40+
@TempDir static Path tempDir;
4141

4242
private static final List<String> ORIGINAL_LINES =
4343
Collections.unmodifiableList(Arrays.asList("line1", "line2", "line3"));
4444
private Path testFile;
4545

46-
@Before
46+
@BeforeEach
4747
public void setupFile() throws IOException {
48-
Path path = TMP.newFile().toPath();
48+
Path path = Files.createTempFile(tempDir, null, null);
4949

5050
Files.write(path, ORIGINAL_LINES);
5151

@@ -56,7 +56,7 @@ public void setupFile() throws IOException {
5656
public void replaceSingleMatch() throws IOException {
5757
FileUtils.replace(testFile, Pattern.compile("line1"), matcher -> "removed");
5858

59-
Assert.assertEquals(
59+
assertEquals(
6060
Arrays.asList("removed", ORIGINAL_LINES.get(1), ORIGINAL_LINES.get(2)),
6161
Files.readAllLines(testFile));
6262
}
@@ -65,14 +65,14 @@ public void replaceSingleMatch() throws IOException {
6565
public void replaceMultipleMatch() throws IOException {
6666
FileUtils.replace(testFile, Pattern.compile("line(.*)"), matcher -> matcher.group(1));
6767

68-
Assert.assertEquals(Arrays.asList("1", "2", "3"), Files.readAllLines(testFile));
68+
assertEquals(Arrays.asList("1", "2", "3"), Files.readAllLines(testFile));
6969
}
7070

7171
@Test
7272
public void replaceWithEmptyLine() throws IOException {
7373
FileUtils.replace(testFile, Pattern.compile("line2"), matcher -> "");
7474

75-
Assert.assertEquals(
75+
assertEquals(
7676
Arrays.asList(ORIGINAL_LINES.get(0), "", ORIGINAL_LINES.get(2)),
7777
Files.readAllLines(testFile));
7878
}

0 commit comments

Comments
 (0)