From bcd448b2f1efecc701079c1a0f7f565a59817f22 Mon Sep 17 00:00:00 2001 From: JunRuiLee Date: Mon, 4 Dec 2023 16:28:16 +0800 Subject: [PATCH] [hotfix] Migrate BashJavaUtilsITCase and JavaBashTestBase to Junit5 and assertJ. --- .../flink/dist/BashJavaUtilsITCase.java | 58 +++++++++---------- .../apache/flink/dist/JavaBashTestBase.java | 16 ++--- 2 files changed, 34 insertions(+), 40 deletions(-) diff --git a/flink-dist/src/test/java/org/apache/flink/dist/BashJavaUtilsITCase.java b/flink-dist/src/test/java/org/apache/flink/dist/BashJavaUtilsITCase.java index a1180da0a7858..e9c0e1cf8c2b2 100644 --- a/flink-dist/src/test/java/org/apache/flink/dist/BashJavaUtilsITCase.java +++ b/flink-dist/src/test/java/org/apache/flink/dist/BashJavaUtilsITCase.java @@ -25,8 +25,7 @@ import org.apache.flink.shaded.guava32.com.google.common.collect.Sets; -import org.hamcrest.Matchers; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.ArrayList; import java.util.Arrays; @@ -35,10 +34,7 @@ import java.util.Map; import java.util.Set; -import static org.hamcrest.collection.IsArrayWithSize.arrayWithSize; -import static org.hamcrest.collection.IsIn.isIn; -import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; /** * Tests for BashJavaUtils. @@ -46,7 +42,7 @@ *

This test requires the distribution to be assembled and is hence marked as an IT case which * run after packaging. */ -public class BashJavaUtilsITCase extends JavaBashTestBase { +class BashJavaUtilsITCase extends JavaBashTestBase { private static final String RUN_BASH_JAVA_UTILS_CMD_SCRIPT = "src/test/bin/runBashJavaUtilsCmd.sh"; @@ -54,7 +50,7 @@ public class BashJavaUtilsITCase extends JavaBashTestBase { "src/test/bin/runExtractLoggingOutputs.sh"; @Test - public void testGetTmResourceParamsConfigs() throws Exception { + void testGetTmResourceParamsConfigs() throws Exception { int expectedResultLines = 2; String[] commands = { RUN_BASH_JAVA_UTILS_CMD_SCRIPT, @@ -63,13 +59,13 @@ public void testGetTmResourceParamsConfigs() throws Exception { }; List lines = Arrays.asList(executeScript(commands).split(System.lineSeparator())); - assertThat(lines.size(), is(expectedResultLines)); + assertThat(lines).hasSize(expectedResultLines); ConfigurationUtils.parseJvmArgString(lines.get(0)); ConfigurationUtils.parseTmResourceDynamicConfigs(lines.get(1)); } @Test - public void testGetTmResourceParamsConfigsWithDynamicProperties() throws Exception { + void testGetTmResourceParamsConfigsWithDynamicProperties() throws Exception { int expectedResultLines = 2; double cpuCores = 39.0; String[] commands = { @@ -80,14 +76,15 @@ public void testGetTmResourceParamsConfigsWithDynamicProperties() throws Excepti }; List lines = Arrays.asList(executeScript(commands).split(System.lineSeparator())); - assertThat(lines.size(), is(expectedResultLines)); + assertThat(lines).hasSize(expectedResultLines); Map configs = ConfigurationUtils.parseTmResourceDynamicConfigs(lines.get(1)); - assertThat(Double.valueOf(configs.get(TaskManagerOptions.CPU_CORES.key())), is(cpuCores)); + assertThat(Double.valueOf(configs.get(TaskManagerOptions.CPU_CORES.key()))) + .isEqualTo(cpuCores); } @Test - public void testGetJmResourceParams() throws Exception { + void testGetJmResourceParams() throws Exception { int expectedResultLines = 2; String[] commands = { RUN_BASH_JAVA_UTILS_CMD_SCRIPT, @@ -96,7 +93,7 @@ public void testGetJmResourceParams() throws Exception { }; List lines = Arrays.asList(executeScript(commands).split(System.lineSeparator())); - assertThat(lines.size(), is(expectedResultLines)); + assertThat(lines).hasSize(expectedResultLines); Map jvmParams = ConfigurationUtils.parseJvmArgString(lines.get(0)); Map dynamicParams = parseAndAssertDynamicParameters(lines.get(1)); @@ -104,7 +101,7 @@ public void testGetJmResourceParams() throws Exception { } @Test - public void testGetJmResourceParamsWithDynamicProperties() throws Exception { + void testGetJmResourceParamsWithDynamicProperties() throws Exception { int expectedResultLines = 2; long metaspace = 123456789L; String[] commands = { @@ -115,12 +112,12 @@ public void testGetJmResourceParamsWithDynamicProperties() throws Exception { }; List lines = Arrays.asList(executeScript(commands).split(System.lineSeparator())); - assertThat(lines.size(), is(expectedResultLines)); + assertThat(lines).hasSize(expectedResultLines); Map jvmParams = ConfigurationUtils.parseJvmArgString(lines.get(0)); Map dynamicParams = parseAndAssertDynamicParameters(lines.get(1)); assertJvmAndDynamicParametersMatch(jvmParams, dynamicParams); - assertThat(Long.valueOf(jvmParams.get("-XX:MaxMetaspaceSize=")), is(metaspace)); + assertThat(Long.valueOf(jvmParams.get("-XX:MaxMetaspaceSize="))).isEqualTo(metaspace); } private static Map parseAndAssertDynamicParameters( @@ -135,15 +132,15 @@ private static Map parseAndAssertDynamicParameters( Map actualDynamicParameters = new HashMap<>(); String[] dynamicParameterTokens = dynamicParametersStr.split(" "); - assertThat(dynamicParameterTokens.length % 2, Matchers.is(0)); + assertThat(dynamicParameterTokens.length % 2).isZero(); for (int i = 0; i < dynamicParameterTokens.length; ++i) { String parameterKeyValueStr = dynamicParameterTokens[i]; if (i % 2 == 0) { - assertThat(parameterKeyValueStr, Matchers.is("-D")); + assertThat(parameterKeyValueStr).isEqualTo("-D"); } else { String[] parameterKeyValue = parameterKeyValueStr.split("="); - assertThat(parameterKeyValue, arrayWithSize(2)); - assertThat(parameterKeyValue[0], isIn(expectedDynamicParameters)); + assertThat(parameterKeyValue).hasSize(2); + assertThat(parameterKeyValue[0]).isIn(expectedDynamicParameters); actualDynamicParameters.put(parameterKeyValue[0], parameterKeyValue[1]); } @@ -154,19 +151,16 @@ private static Map parseAndAssertDynamicParameters( private static void assertJvmAndDynamicParametersMatch( Map jvmParams, Map dynamicParams) { - assertThat( - jvmParams.get("-Xmx") + "b", - is(dynamicParams.get(JobManagerOptions.JVM_HEAP_MEMORY.key()))); - assertThat( - jvmParams.get("-Xms") + "b", - is(dynamicParams.get(JobManagerOptions.JVM_HEAP_MEMORY.key()))); - assertThat( - jvmParams.get("-XX:MaxMetaspaceSize=") + "b", - is(dynamicParams.get(JobManagerOptions.JVM_METASPACE.key()))); + assertThat(jvmParams.get("-Xmx") + "b") + .isEqualTo(dynamicParams.get(JobManagerOptions.JVM_HEAP_MEMORY.key())); + assertThat(jvmParams.get("-Xms") + "b") + .isEqualTo(dynamicParams.get(JobManagerOptions.JVM_HEAP_MEMORY.key())); + assertThat(jvmParams.get("-XX:MaxMetaspaceSize=") + "b") + .isEqualTo(dynamicParams.get(JobManagerOptions.JVM_METASPACE.key())); } @Test - public void testExtractLoggingOutputs() throws Exception { + void testExtractLoggingOutputs() throws Exception { StringBuilder input = new StringBuilder(); List expectedOutput = new ArrayList<>(); @@ -184,6 +178,6 @@ public void testExtractLoggingOutputs() throws Exception { List actualOutput = Arrays.asList(executeScript(commands).split(System.lineSeparator())); - assertThat(actualOutput, is(expectedOutput)); + assertThat(actualOutput).isEqualTo(expectedOutput); } } diff --git a/flink-dist/src/test/java/org/apache/flink/dist/JavaBashTestBase.java b/flink-dist/src/test/java/org/apache/flink/dist/JavaBashTestBase.java index 48ec28cc1b321..75674a89b41a0 100644 --- a/flink-dist/src/test/java/org/apache/flink/dist/JavaBashTestBase.java +++ b/flink-dist/src/test/java/org/apache/flink/dist/JavaBashTestBase.java @@ -19,21 +19,21 @@ package org.apache.flink.dist; import org.apache.flink.util.OperatingSystem; -import org.apache.flink.util.TestLogger; import org.apache.commons.io.IOUtils; -import org.junit.Assume; -import org.junit.BeforeClass; +import org.junit.jupiter.api.BeforeAll; import java.io.IOException; +import static org.assertj.core.api.Assertions.assertThat; + /** Abstract test class for executing bash scripts. */ -public abstract class JavaBashTestBase extends TestLogger { - @BeforeClass +abstract class JavaBashTestBase { + @BeforeAll public static void checkOperatingSystem() { - Assume.assumeTrue( - "This test checks shell scripts which are not available on Windows.", - !OperatingSystem.isWindows()); + assertThat(OperatingSystem.isWindows()) + .as("This test checks shell scripts which are not available on Windows.") + .isFalse(); } /**