Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HADOOP-19415. [JDK17] Upgrade JUnit from 4 to 5 in hadoop-common Part5. #7357

Merged
merged 5 commits into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -20,11 +20,11 @@

import static org.apache.hadoop.util.ApplicationClassLoader.constructUrlsFromClasspath;
import static org.apache.hadoop.util.ApplicationClassLoader.isSystemClass;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.io.FileOutputStream;
Expand All @@ -39,16 +39,16 @@
import org.apache.commons.io.IOUtils;
import org.apache.hadoop.fs.FileUtil;
import org.apache.hadoop.test.GenericTestUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import org.apache.hadoop.thirdparty.com.google.common.base.Splitter;

public class TestApplicationClassLoader {

private static File testDir = GenericTestUtils.getTestDir("appclassloader");

@Before
@BeforeEach
public void setUp() {
FileUtil.fullyDelete(testDir);
testDir.mkdirs();
Expand All @@ -57,17 +57,17 @@ public void setUp() {
@Test
public void testConstructUrlsFromClasspath() throws Exception {
File file = new File(testDir, "file");
assertTrue("Create file", file.createNewFile());
assertTrue(file.createNewFile(), "Create file");

File dir = new File(testDir, "dir");
assertTrue("Make dir", dir.mkdir());
assertTrue(dir.mkdir(), "Make dir");

File jarsDir = new File(testDir, "jarsdir");
assertTrue("Make jarsDir", jarsDir.mkdir());
assertTrue(jarsDir.mkdir(), "Make jarsDir");
File nonJarFile = new File(jarsDir, "nonjar");
assertTrue("Create non-jar file", nonJarFile.createNewFile());
assertTrue(nonJarFile.createNewFile(), "Create non-jar file");
File jarFile = new File(jarsDir, "a.jar");
assertTrue("Create jar file", jarFile.createNewFile());
assertTrue(jarFile.createNewFile(), "Create jar file");

File nofile = new File(testDir, "nofile");
// don't create nofile
Expand Down Expand Up @@ -130,11 +130,11 @@ public void testGetResource() throws IOException {
ClassLoader appClassloader = new ApplicationClassLoader(
new URL[] { testJar }, currentClassLoader, null);

assertNull("Resource should be null for current classloader",
currentClassLoader.getResourceAsStream("resource.txt"));
assertNull(currentClassLoader.getResourceAsStream("resource.txt"),
"Resource should be null for current classloader");

InputStream in = appClassloader.getResourceAsStream("resource.txt");
assertNotNull("Resource should not be null for app classloader", in);
assertNotNull(in, "Resource should not be null for app classloader");
assertEquals("hello", IOUtils.toString(in, StandardCharsets.UTF_8));
}

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

import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -70,8 +72,8 @@ public void testAsyncDiskService() throws Throwable {
} catch (RuntimeException ex) {
e = ex;
}
assertNotNull("Executing a task on a non-existing volume should throw an "
+ "Exception.", e);
assertNotNull(e, "Executing a task on a non-existing volume should throw an "
+ "Exception.");

service.shutdown();
if (!service.awaitTermination(5000)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
*/
package org.apache.hadoop.util;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.Test;
import org.junit.jupiter.api.Test;
/**
* A test class for AutoCloseableLock.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
package org.apache.hadoop.util;

import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.apache.hadoop.util.DiskChecker.DiskErrorException;

Expand All @@ -37,7 +37,7 @@ protected void checkDirs(boolean isDir, String perm, boolean success)

DiskValidatorFactory.getInstance(BasicDiskValidator.NAME).
checkStatus(localDir);
assertTrue("call to checkDir() succeeded.", success);
assertTrue(success, "call to checkDir() succeeded.");
} catch (DiskErrorException e) {
// call to checkDir() succeeded even though it was expected to fail
// if success is false, otherwise throw the exception
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
package org.apache.hadoop.util;

import java.io.IOException;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class TestCacheableIPList {

Expand All @@ -44,10 +45,8 @@ public void testAddWithSleepForCacheTimeout() throws IOException, InterruptedExc
CacheableIPList cipl = new CacheableIPList(
new FileBasedIPList("ips.txt"),100);

assertFalse("10.113.221.222 is in the list",
cipl.isIn("10.113.221.222"));
assertFalse ("10.222.103.121 is in the list",
cipl.isIn("10.222.103.121"));
assertFalse(cipl.isIn("10.113.221.222"), "10.113.221.222 is in the list");
assertFalse(cipl.isIn("10.222.103.121"), "10.222.103.121 is in the list");

TestFileBasedIPList.removeFile("ips.txt");
String[]ips2 = {"10.119.103.112", "10.221.102.0/23",
Expand All @@ -56,10 +55,8 @@ public void testAddWithSleepForCacheTimeout() throws IOException, InterruptedExc
TestFileBasedIPList.createFileWithEntries ("ips.txt", ips2);
Thread.sleep(101);

assertTrue("10.113.221.222 is not in the list",
cipl.isIn("10.113.221.222"));
assertTrue ("10.222.103.121 is not in the list",
cipl.isIn("10.222.103.121"));
assertTrue(cipl.isIn("10.113.221.222"), "10.113.221.222 is not in the list");
assertTrue(cipl.isIn("10.222.103.121"), "10.222.103.121 is not in the list");

TestFileBasedIPList.removeFile("ips.txt");
}
Expand All @@ -85,21 +82,17 @@ public void testRemovalWithSleepForCacheTimeout() throws IOException, Interrupte
CacheableIPList cipl = new CacheableIPList(
new FileBasedIPList("ips.txt"),100);

assertTrue("10.113.221.222 is not in the list",
cipl.isIn("10.113.221.222"));
assertTrue ("10.222.103.121 is not in the list",
cipl.isIn("10.222.103.121"));
assertTrue(cipl.isIn("10.113.221.222"), "10.113.221.222 is not in the list");
assertTrue(cipl.isIn("10.222.103.121"), "10.222.103.121 is not in the list");

TestFileBasedIPList.removeFile("ips.txt");
String[]ips2 = {"10.119.103.112", "10.221.102.0/23", "10.113.221.221"};

TestFileBasedIPList.createFileWithEntries ("ips.txt", ips2);
Thread.sleep(1005);

assertFalse("10.113.221.222 is in the list",
cipl.isIn("10.113.221.222"));
assertFalse ("10.222.103.121 is in the list",
cipl.isIn("10.222.103.121"));
assertFalse(cipl.isIn("10.113.221.222"), "10.113.221.222 is in the list");
assertFalse(cipl.isIn("10.222.103.121"), "10.222.103.121 is in the list");

TestFileBasedIPList.removeFile("ips.txt");
}
Expand All @@ -124,10 +117,8 @@ public void testAddWithRefresh() throws IOException, InterruptedException {
CacheableIPList cipl = new CacheableIPList(
new FileBasedIPList("ips.txt"),100);

assertFalse("10.113.221.222 is in the list",
cipl.isIn("10.113.221.222"));
assertFalse ("10.222.103.121 is in the list",
cipl.isIn("10.222.103.121"));
assertFalse(cipl.isIn("10.113.221.222"), "10.113.221.222 is in the list");
assertFalse(cipl.isIn("10.222.103.121"), "10.222.103.121 is in the list");

TestFileBasedIPList.removeFile("ips.txt");
String[]ips2 = {"10.119.103.112", "10.221.102.0/23",
Expand All @@ -136,10 +127,8 @@ public void testAddWithRefresh() throws IOException, InterruptedException {
TestFileBasedIPList.createFileWithEntries ("ips.txt", ips2);
cipl.refresh();

assertTrue("10.113.221.222 is not in the list",
cipl.isIn("10.113.221.222"));
assertTrue ("10.222.103.121 is not in the list",
cipl.isIn("10.222.103.121"));
assertTrue(cipl.isIn("10.113.221.222"), "10.113.221.222 is not in the list");
assertTrue(cipl.isIn("10.222.103.121"), "10.222.103.121 is not in the list");

TestFileBasedIPList.removeFile("ips.txt");
}
Expand All @@ -165,21 +154,17 @@ public void testRemovalWithRefresh() throws IOException, InterruptedException {
CacheableIPList cipl = new CacheableIPList(
new FileBasedIPList("ips.txt"),100);

assertTrue("10.113.221.222 is not in the list",
cipl.isIn("10.113.221.222"));
assertTrue ("10.222.103.121 is not in the list",
cipl.isIn("10.222.103.121"));
assertTrue(cipl.isIn("10.113.221.222"), "10.113.221.222 is not in the list");
assertTrue(cipl.isIn("10.222.103.121"), "10.222.103.121 is not in the list");

TestFileBasedIPList.removeFile("ips.txt");
String[]ips2 = {"10.119.103.112", "10.221.102.0/23", "10.113.221.221"};

TestFileBasedIPList.createFileWithEntries ("ips.txt", ips2);
cipl.refresh();

assertFalse("10.113.221.222 is in the list",
cipl.isIn("10.113.221.222"));
assertFalse ("10.222.103.121 is in the list",
cipl.isIn("10.222.103.121"));
assertFalse(cipl.isIn("10.113.221.222"), "10.113.221.222 is in the list");
assertFalse(cipl.isIn("10.222.103.121"), "10.222.103.121 is in the list");

TestFileBasedIPList.removeFile("ips.txt");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
*/
package org.apache.hadoop.util;

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

import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;

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

public class TestChunkedArrayList {

Expand Down Expand Up @@ -103,41 +104,41 @@ public void testRemovals() throws Exception {
// Iterate through all list elements.
Iterator<Integer> iter = list.iterator();
for (int i = 0; i < NUM_ELEMS; i++) {
Assert.assertTrue(iter.hasNext());
assertTrue(iter.hasNext());
Integer val = iter.next();
Assert.assertEquals(Integer.valueOf(i), val);
assertEquals(Integer.valueOf(i), val);
}
Assert.assertFalse(iter.hasNext());
Assert.assertEquals(NUM_ELEMS, list.size());
assertFalse(iter.hasNext());
assertEquals(NUM_ELEMS, list.size());

// Remove even elements.
iter = list.iterator();
for (int i = 0; i < NUM_ELEMS; i++) {
Assert.assertTrue(iter.hasNext());
assertTrue(iter.hasNext());
Integer val = iter.next();
Assert.assertEquals(Integer.valueOf(i), val);
assertEquals(Integer.valueOf(i), val);
if (i % 2 == 0) {
iter.remove();
}
}
Assert.assertFalse(iter.hasNext());
Assert.assertEquals(NUM_ELEMS / 2, list.size());
assertFalse(iter.hasNext());
assertEquals(NUM_ELEMS / 2, list.size());

// Iterate through all odd list elements.
iter = list.iterator();
for (int i = 0; i < NUM_ELEMS / 2; i++) {
Assert.assertTrue(iter.hasNext());
assertTrue(iter.hasNext());
Integer val = iter.next();
Assert.assertEquals(Integer.valueOf(1 + (2 * i)), val);
assertEquals(Integer.valueOf(1 + (2 * i)), val);
iter.remove();
}
Assert.assertFalse(iter.hasNext());
assertFalse(iter.hasNext());

// Check that list is now empty.
Assert.assertEquals(0, list.size());
Assert.assertTrue(list.isEmpty());
assertEquals(0, list.size());
assertTrue(list.isEmpty());
iter = list.iterator();
Assert.assertFalse(iter.hasNext());
assertFalse(iter.hasNext());
}

@Test
Expand All @@ -148,23 +149,23 @@ public void testGet() throws Exception {
list.add(i);
}

Assert.assertEquals(Integer.valueOf(100), list.get(100));
Assert.assertEquals(Integer.valueOf(1000), list.get(1000));
Assert.assertEquals(Integer.valueOf(10000), list.get(10000));
Assert.assertEquals(Integer.valueOf(100000), list.get(100000));
assertEquals(Integer.valueOf(100), list.get(100));
assertEquals(Integer.valueOf(1000), list.get(1000));
assertEquals(Integer.valueOf(10000), list.get(10000));
assertEquals(Integer.valueOf(100000), list.get(100000));

Iterator<Integer> iter = list.iterator();
iter.next();
iter.remove();
Assert.assertEquals(Integer.valueOf(1), list.get(0));
assertEquals(Integer.valueOf(1), list.get(0));

iter = list.iterator();
for (int i = 0; i < 500; i++) {
iter.next();
}
iter.remove();

Assert.assertEquals(Integer.valueOf(502), list.get(500));
Assert.assertEquals(Integer.valueOf(602), list.get(600));
assertEquals(Integer.valueOf(502), list.get(500));
assertEquals(Integer.valueOf(602), list.get(600));
}
}
Loading