Skip to content

Commit

Permalink
HADOOP-19415. [JDK17] Upgrade JUnit from 4 to 5 in hadoop-common Part5.
Browse files Browse the repository at this point in the history
  • Loading branch information
fanshilun committed Feb 6, 2025
1 parent 872ebda commit 0a6f4cc
Show file tree
Hide file tree
Showing 76 changed files with 1,150 additions and 1,043 deletions.
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,8 @@
*/
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.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -70,8 +70,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,9 @@
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.*;

public class TestCacheableIPList {

Expand All @@ -44,10 +44,10 @@ 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 +56,10 @@ 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 +85,21 @@ 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 +124,10 @@ 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 +136,10 @@ 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 +165,21 @@ 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,14 @@
*/
package org.apache.hadoop.util;

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

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.Assertions;
import org.junit.jupiter.api.Test;

public class TestChunkedArrayList {

Expand Down Expand Up @@ -103,41 +103,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());
Assertions.assertTrue(iter.hasNext());
Integer val = iter.next();
Assert.assertEquals(Integer.valueOf(i), val);
Assertions.assertEquals(Integer.valueOf(i), val);
}
Assert.assertFalse(iter.hasNext());
Assert.assertEquals(NUM_ELEMS, list.size());
Assertions.assertFalse(iter.hasNext());
Assertions.assertEquals(NUM_ELEMS, list.size());

// Remove even elements.
iter = list.iterator();
for (int i = 0; i < NUM_ELEMS; i++) {
Assert.assertTrue(iter.hasNext());
Assertions.assertTrue(iter.hasNext());
Integer val = iter.next();
Assert.assertEquals(Integer.valueOf(i), val);
Assertions.assertEquals(Integer.valueOf(i), val);
if (i % 2 == 0) {
iter.remove();
}
}
Assert.assertFalse(iter.hasNext());
Assert.assertEquals(NUM_ELEMS / 2, list.size());
Assertions.assertFalse(iter.hasNext());
Assertions.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());
Assertions.assertTrue(iter.hasNext());
Integer val = iter.next();
Assert.assertEquals(Integer.valueOf(1 + (2 * i)), val);
Assertions.assertEquals(Integer.valueOf(1 + (2 * i)), val);
iter.remove();
}
Assert.assertFalse(iter.hasNext());
Assertions.assertFalse(iter.hasNext());

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

@Test
Expand All @@ -148,23 +148,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));
Assertions.assertEquals(Integer.valueOf(100), list.get(100));
Assertions.assertEquals(Integer.valueOf(1000), list.get(1000));
Assertions.assertEquals(Integer.valueOf(10000), list.get(10000));
Assertions.assertEquals(Integer.valueOf(100000), list.get(100000));

Iterator<Integer> iter = list.iterator();
iter.next();
iter.remove();
Assert.assertEquals(Integer.valueOf(1), list.get(0));
Assertions.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));
Assertions.assertEquals(Integer.valueOf(502), list.get(500));
Assertions.assertEquals(Integer.valueOf(602), list.get(600));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
import org.apache.hadoop.fs.viewfs.ViewFileSystem;

import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;

public class TestClassUtil {

@Test(timeout=10000)
@Test
@Timeout(value = 10)
public void testFindContainingJar() {
String containingJar = ClassUtil.findContainingJar(Assertions.class);
Assertions
Expand All @@ -45,7 +47,8 @@ public void testFindContainingJar() {
.matches("assertj-core.*[.]jar");
}

@Test(timeout = 10000)
@Test
@Timeout(value = 10)
public void testFindContainingClass() {
String classFileLocation = ClassUtil.findClassLocation(ViewFileSystem.class);
Assertions
Expand Down
Loading

0 comments on commit 0a6f4cc

Please sign in to comment.