Skip to content

Commit

Permalink
HADOOP-19415. Fix JunitTest & CheckStyle.
Browse files Browse the repository at this point in the history
  • Loading branch information
fanshilun committed Feb 7, 2025
1 parent fba9cb4 commit 50f43c6
Show file tree
Hide file tree
Showing 28 changed files with 302 additions and 317 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,16 @@
*/
package org.apache.hadoop.test;

import org.junit.Rule;
import org.junit.rules.TestRule;
import org.junit.rules.Timeout;

import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Timeout;

/**
* Class for test units to extend in order that their individual tests will
* be timed out and fail automatically should they run more than 10 seconds.
* This provides an automatic regression check for tests that begin running
* longer than expected.
*/
@Timeout(10)
public class UnitTestcaseTimeLimit {
public final int timeOutSecs = 10;

@Rule public TestRule globalTimeout =
new Timeout(timeOutSecs, TimeUnit.SECONDS);
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void accept(Key k, Integer v) {
Key equalToKey1 = new Key("key1");

// IdentityHashStore compares by object equality, not equals()
assertNull(store.get(equalToKey1));
assertNull(store.get(equalToKey1));

assertTrue(!store.isEmpty());
assertEquals(value1, store.get(key1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

import org.apache.hadoop.util.ExitUtil.ExitException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,19 @@
package org.apache.hadoop.util;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class TestOptions {

@Test
public void testAppend() throws Exception {
assertArrayEquals(
new String[]{"Dr.", "Who", "hi", "there"}
, Options.prependOptions(new String[]{"hi", "there"},
"Dr.", "Who"), "first append");
assertArrayEquals(
new String[]{"aa","bb","cc","dd","ee","ff"}
, Options.prependOptions(new String[]{"dd", "ee", "ff"},
"aa", "bb", "cc"), "second append");
assertArrayEquals(new String[]{"Dr.", "Who", "hi", "there"},
Options.prependOptions(new String[]{"hi", "there"},
"Dr.", "Who"), "first append");
assertArrayEquals(new String[]{"aa","bb","cc","dd","ee","ff"},
Options.prependOptions(new String[]{"dd", "ee", "ff"},
"aa", "bb", "cc"), "second append");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,28 @@

package org.apache.hadoop.util;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class TestProgress {

@Test
public void testSet(){
Progress progress = new Progress();
progress.set(Float.NaN);
Assertions.assertEquals(0, progress.getProgress(), 0.0);
assertEquals(0, progress.getProgress(), 0.0);

progress.set(Float.NEGATIVE_INFINITY);
Assertions.assertEquals(0,progress.getProgress(),0.0);
assertEquals(0,progress.getProgress(),0.0);

progress.set(-1);
Assertions.assertEquals(0,progress.getProgress(),0.0);
assertEquals(0,progress.getProgress(),0.0);

progress.set((float) 1.1);
Assertions.assertEquals(1,progress.getProgress(),0.0);
assertEquals(1,progress.getProgress(),0.0);

progress.set(Float.POSITIVE_INFINITY);
Assertions.assertEquals(1,progress.getProgress(),0.0);
assertEquals(1,progress.getProgress(),0.0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
import java.util.zip.CRC32;
import java.util.zip.Checksum;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* Unit test to verify that the pure-Java CRC32 algorithm gives
* the same results as the built-in implementation.
Expand Down Expand Up @@ -96,7 +97,7 @@ private void checkOnBytes(byte[] bytes, boolean print) {
}

private void checkSame() {
Assertions.assertEquals(theirs.getValue(), ours.getValue());
assertEquals(theirs.getValue(), ours.getValue());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.apache.hadoop.util.DiskChecker.DiskErrorException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;

import java.io.File;
import java.nio.file.Files;
Expand Down Expand Up @@ -64,13 +63,12 @@ public void testReadWriteDiskValidator()

ReadWriteDiskValidatorMetrics metric =
ReadWriteDiskValidatorMetrics.getMetric(testDir.toString());
Assertions.assertEquals(
metric.getFileReadQuantiles()[0].getEstimator().getCount(), count, "The count number of estimator in MutableQuantiles"
assertEquals(metric.getFileReadQuantiles()[0].getEstimator().getCount(), count,
"The count number of estimator in MutableQuantiles"
+ "metrics of file read is not right");

Assertions.assertEquals(
metric.getFileWriteQuantiles()[0].getEstimator().getCount()
, count, "The count number of estimator in MutableQuantiles"
assertEquals(metric.getFileWriteQuantiles()[0].getEstimator().getCount(),
count, "The count number of estimator in MutableQuantiles"
+ "metrics of file write is not right");

MetricsSource source = ms.getSource(
Expand Down Expand Up @@ -154,8 +152,8 @@ public void testCheckFailures() throws Throwable {
"FailureCount", 2);
Long lastFailureTime2 = (Long) MetricsRecords.getMetricValueByName(
collector.getRecords().get(1), "LastFailureTime");
assertTrue(
lastFailureTime1 < lastFailureTime2, "The first failure time should be less than the second one");
assertTrue(lastFailureTime1 < lastFailureTime2,
"The first failure time should be less than the second one");

testDir.delete();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
import java.util.HashMap;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import org.apache.hadoop.test.GenericTestUtils;
import org.apache.hadoop.test.GenericTestUtils.LogCapturer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.test.GenericTestUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -106,10 +105,10 @@ public void testUnJar() throws Exception {
// Unjar everything
RunJar.unJar(new File(TEST_ROOT_DIR, TEST_JAR_NAME),
unjarDir, MATCH_ANY);
assertTrue(
new File(unjarDir, TestRunJar.FOOBAR_TXT).exists(), "foobar unpacked");
assertTrue(
new File(unjarDir, FOOBAZ_TXT).exists(), "foobaz unpacked");
assertTrue(new File(unjarDir, TestRunJar.FOOBAR_TXT).exists(),
"foobar unpacked");
assertTrue(new File(unjarDir, FOOBAZ_TXT).exists(),
"foobaz unpacked");
}

/**
Expand All @@ -123,10 +122,9 @@ public void testUnJarWithPattern() throws Exception {
RunJar.unJar(new File(TEST_ROOT_DIR, TEST_JAR_NAME),
unjarDir,
Pattern.compile(".*baz.*"));
assertFalse(
new File(unjarDir, TestRunJar.FOOBAR_TXT).exists(), "foobar not unpacked");
assertTrue(
new File(unjarDir, FOOBAZ_TXT).exists(), "foobaz unpacked");
assertFalse(new File(unjarDir, TestRunJar.FOOBAR_TXT).exists(),
"foobar not unpacked");
assertTrue(new File(unjarDir, FOOBAZ_TXT).exists(), "foobaz unpacked");
}

private File generateBigJar(File dir) throws Exception {
Expand Down Expand Up @@ -157,18 +155,18 @@ private File generateBigJar(File dir) throws Exception {
public void testBigJar() throws Exception {
Random r = new Random(System.currentTimeMillis());
File dir = new File(TEST_ROOT_DIR, Long.toHexString(r.nextLong()));
Assertions.assertTrue(dir.mkdirs());
assertTrue(dir.mkdirs());
File input = generateBigJar(dir);
File output = new File(dir, "job2.jar");
try {
try (InputStream is = new FileInputStream(input)) {
RunJar.unJarAndSave(is, dir, "job2.jar", Pattern.compile(".*"));
}
Assertions.assertEquals(input.length(), output.length());
assertEquals(input.length(), output.length());
for (int i = 0; i < 10; ++i) {
File subdir = new File(dir, ((i % 2 == 0) ? "dir/" : ""));
File f = new File(subdir, "f" + Integer.toString(i));
Assertions.assertEquals(756, f.length());
assertEquals(756, f.length());
}
} finally {
// Clean up
Expand All @@ -192,8 +190,8 @@ public void testUnJarDoesNotLooseLastModify() throws Exception {

private File getUnjarDir(String dirName) {
File unjarDir = new File(TEST_ROOT_DIR, dirName);
assertFalse(
new File(unjarDir, TestRunJar.FOOBAR_TXT).exists(), "unjar dir shouldn't exist at test start");
assertFalse(new File(unjarDir, TestRunJar.FOOBAR_TXT).exists(),
"unjar dir shouldn't exist at test start");
return unjarDir;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
*/
package org.apache.hadoop.util;

import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import org.apache.commons.io.FileUtils;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.Assertions;

import java.io.BufferedReader;
Expand All @@ -40,21 +43,12 @@
import org.apache.hadoop.test.GenericTestUtils;

import static org.apache.hadoop.util.Shell.*;
import static org.junit.Assume.assumeTrue;

import org.junit.Assume;
import org.junit.jupiter.api.BeforeEach;
import org.junit.Rule;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.rules.TestName;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import static org.junit.jupiter.api.Assumptions.assumeFalse;

@Timeout(value = 30)
public class TestShell extends Assertions {

@Rule
public TestName methodName = new TestName();

private File rootTestDir = GenericTestUtils.getTestDir();

/**
Expand Down Expand Up @@ -91,10 +85,10 @@ public int getRunCount() {
}

@BeforeEach
public void setup() {
public void setup(TestInfo testInfo) {
rootTestDir.mkdirs();
assertTrue(rootTestDir.isDirectory(), "Not a directory " + rootTestDir);
methodDir = new File(rootTestDir, methodName.getMethodName());
methodDir = new File(rootTestDir, testInfo.getDisplayName());
}

@Test
Expand Down Expand Up @@ -129,7 +123,7 @@ public void testShellCommandExecutorToString() throws Throwable {

@Test
public void testShellCommandTimeout() throws Throwable {
Assume.assumeFalse(WINDOWS);
assumeFalse(WINDOWS);
String rootDir = rootTestDir.getAbsolutePath();
File shellFile = new File(rootDir, "timeout.sh");
String timeoutCommand = "sleep 4; echo \"hello\"";
Expand All @@ -152,13 +146,13 @@ public void testShellCommandTimeout() throws Throwable {

@Test
public void testEnvVarsWithInheritance() throws Exception {
Assume.assumeFalse(WINDOWS);
assumeFalse(WINDOWS);
testEnvHelper(true);
}

@Test
public void testEnvVarsWithoutInheritance() throws Exception {
Assume.assumeFalse(WINDOWS);
assumeFalse(WINDOWS);
testEnvHelper(false);
}

Expand Down Expand Up @@ -242,7 +236,7 @@ public void testGetCheckProcessIsAliveCommand() throws Exception {
expectedCommand = new String[] {"bash", "-c", "kill -0 '" + anyPid +
"'" };
}
Assertions.assertArrayEquals(expectedCommand, checkProcessAliveCommand);
assertArrayEquals(expectedCommand, checkProcessAliveCommand);
}

@Test
Expand All @@ -264,7 +258,7 @@ public void testGetSignalKillCommand() throws Exception {
expectedCommand = new String[]{ "bash", "-c", "kill -9 '" + anyPid +
"'"};
}
Assertions.assertArrayEquals(expectedCommand, checkProcessAliveCommand);
assertArrayEquals(expectedCommand, checkProcessAliveCommand);
}

private void testInterval(long interval) throws IOException {
Expand Down Expand Up @@ -384,7 +378,7 @@ public void testBinWinUtilsNotAFile() throws Throwable {
*/
@Test
public void testNoWinutilsOnUnix() throws Throwable {
Assume.assumeFalse(WINDOWS);
assumeFalse(WINDOWS);
try {
getWinUtilsFile();
} catch (FileNotFoundException ex) {
Expand Down Expand Up @@ -476,7 +470,7 @@ public void testBashQuote() {
@Test
@Timeout(value = 120)
public void testDestroyAllShellProcesses() throws Throwable {
Assume.assumeFalse(WINDOWS);
assumeFalse(WINDOWS);
StringBuilder sleepCommand = new StringBuilder();
sleepCommand.append("sleep 200");
String[] shellCmd = {"bash", "-c", sleepCommand.toString()};
Expand Down Expand Up @@ -532,6 +526,6 @@ public void testIsJavaVersionAtLeast() {

@Test
public void testIsBashSupported() throws InterruptedIOException {
assumeTrue("Bash is not supported", Shell.checkIsBashSupported());
assumeTrue(Shell.checkIsBashSupported(), "Bash is not supported");
}
}
Loading

0 comments on commit 50f43c6

Please sign in to comment.