MAPREDUCE-7422. Upgrade Junit 4 to 5 in hadoop-mapreduce-examples (#5029)

Co-authored-by: Ashutosh Gupta <ashugpt@amazon.com>
Signed-off-by: Akira Ajisaka <aajisaka@apache.org>
This commit is contained in:
Ashutosh Gupta 2022-11-21 05:36:35 +00:00 committed by GitHub
parent c71a68ca80
commit dcde414570
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 145 additions and 119 deletions

View File

@ -128,6 +128,21 @@
<artifactId>assertj-core</artifactId> <artifactId>assertj-core</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -21,8 +21,9 @@
import java.io.IOException; import java.io.IOException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import org.junit.After; import org.junit.jupiter.api.AfterEach;
import org.junit.Test; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.hadoop.fs.FSDataInputStream; import org.apache.hadoop.fs.FSDataInputStream;
@ -33,14 +34,19 @@
import org.apache.hadoop.util.ExitUtil; import org.apache.hadoop.util.ExitUtil;
import org.apache.hadoop.util.ExitUtil.ExitException; import org.apache.hadoop.util.ExitUtil.ExitException;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
public class TestAggregateWordCount extends HadoopTestCase { public class TestAggregateWordCount extends HadoopTestCase {
public TestAggregateWordCount() throws IOException { public TestAggregateWordCount() throws IOException {
super(LOCAL_MR, LOCAL_FS, 1, 1); super(LOCAL_MR, LOCAL_FS, 1, 1);
} }
@After @BeforeEach
public void setUp() throws Exception {
super.setUp();
}
@AfterEach
public void tearDown() throws Exception { public void tearDown() throws Exception {
FileSystem fs = getFileSystem(); FileSystem fs = getFileSystem();
if (fs != null) { if (fs != null) {
@ -58,7 +64,7 @@ public void tearDown() throws Exception {
private static final Path OUTPUT_PATH = new Path(TEST_DIR, "outPath"); private static final Path OUTPUT_PATH = new Path(TEST_DIR, "outPath");
@Test @Test
public void testAggregateTestCount() void testAggregateTestCount()
throws IOException, ClassNotFoundException, InterruptedException { throws IOException, ClassNotFoundException, InterruptedException {
ExitUtil.disableSystemExit(); ExitUtil.disableSystemExit();

View File

@ -18,35 +18,36 @@
package org.apache.hadoop.examples; package org.apache.hadoop.examples;
import java.math.BigInteger; import java.math.BigInteger;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.Assert;
import static org.junit.jupiter.api.Assertions.assertEquals;
/** Tests for BaileyBorweinPlouffe */ /** Tests for BaileyBorweinPlouffe */
public class TestBaileyBorweinPlouffe { public class TestBaileyBorweinPlouffe {
@Test @Test
public void testMod() { void testMod() {
final BigInteger TWO = BigInteger.ONE.add(BigInteger.ONE); final BigInteger TWO = BigInteger.ONE.add(BigInteger.ONE);
for (long n = 3; n < 100; n++) { for (long n = 3; n < 100; n++) {
for (long e = 1; e < 100; e++) { for (long e = 1; e < 100; e++) {
final long r = TWO.modPow( final long r = TWO.modPow(
BigInteger.valueOf(e), BigInteger.valueOf(n)).longValue(); BigInteger.valueOf(e), BigInteger.valueOf(n)).longValue();
Assert.assertEquals("e=" + e + ", n=" + n, r, BaileyBorweinPlouffe assertEquals(r, BaileyBorweinPlouffe
.mod(e, n)); .mod(e, n), "e=" + e + ", n=" + n);
} }
} }
} }
@Test @Test
public void testHexDigit() { void testHexDigit() {
final long[] answers = {0x43F6, 0xA308, 0x29B7, 0x49F1, 0x8AC8, 0x35EA}; final long[] answers = {0x43F6, 0xA308, 0x29B7, 0x49F1, 0x8AC8, 0x35EA};
long d = 1; long d = 1;
for (int i = 0; i < answers.length; i++) { for (int i = 0; i < answers.length; i++) {
Assert.assertEquals("d=" + d, answers[i], BaileyBorweinPlouffe assertEquals(answers[i], BaileyBorweinPlouffe
.hexDigits(d)); .hexDigits(d), "d=" + d);
d *= 10; d *= 10;
} }
Assert.assertEquals(0x243FL, BaileyBorweinPlouffe.hexDigits(0)); assertEquals(0x243FL, BaileyBorweinPlouffe.hexDigits(0));
} }
} }

View File

@ -17,8 +17,6 @@
*/ */
package org.apache.hadoop.examples; package org.apache.hadoop.examples;
import static org.junit.Assert.assertEquals;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -31,9 +29,11 @@
import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path; import org.apache.hadoop.fs.Path;
import org.apache.hadoop.util.ToolRunner; import org.apache.hadoop.util.ToolRunner;
import org.junit.AfterClass; import org.junit.jupiter.api.AfterAll;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class TestWordStats { public class TestWordStats {
@ -241,13 +241,14 @@ public static boolean deleteDir(File dir) {
return dir.delete(); return dir.delete();
} }
@Before public void setup() throws Exception { @BeforeEach public void setup() throws Exception {
deleteDir(new File(MEAN_OUTPUT)); deleteDir(new File(MEAN_OUTPUT));
deleteDir(new File(MEDIAN_OUTPUT)); deleteDir(new File(MEDIAN_OUTPUT));
deleteDir(new File(STDDEV_OUTPUT)); deleteDir(new File(STDDEV_OUTPUT));
} }
@Test public void testGetTheMean() throws Exception { @Test
void testGetTheMean() throws Exception {
String args[] = new String[2]; String args[] = new String[2];
args[0] = INPUT; args[0] = INPUT;
args[1] = MEAN_OUTPUT; args[1] = MEAN_OUTPUT;
@ -261,7 +262,8 @@ public static boolean deleteDir(File dir) {
assertEquals(mean, wr.read(INPUT), 0.0); assertEquals(mean, wr.read(INPUT), 0.0);
} }
@Test public void testGetTheMedian() throws Exception { @Test
void testGetTheMedian() throws Exception {
String args[] = new String[2]; String args[] = new String[2];
args[0] = INPUT; args[0] = INPUT;
args[1] = MEDIAN_OUTPUT; args[1] = MEDIAN_OUTPUT;
@ -275,7 +277,8 @@ public static boolean deleteDir(File dir) {
assertEquals(median, wr.read(INPUT), 0.0); assertEquals(median, wr.read(INPUT), 0.0);
} }
@Test public void testGetTheStandardDeviation() throws Exception { @Test
void testGetTheStandardDeviation() throws Exception {
String args[] = new String[2]; String args[] = new String[2];
args[0] = INPUT; args[0] = INPUT;
args[1] = STDDEV_OUTPUT; args[1] = STDDEV_OUTPUT;
@ -289,7 +292,7 @@ public static boolean deleteDir(File dir) {
assertEquals(stddev, wr.read(INPUT), 0.0); assertEquals(stddev, wr.read(INPUT), 0.0);
} }
@AfterClass public static void cleanup() throws Exception { @AfterAll public static void cleanup() throws Exception {
deleteDir(new File(MEAN_OUTPUT)); deleteDir(new File(MEAN_OUTPUT));
deleteDir(new File(MEDIAN_OUTPUT)); deleteDir(new File(MEDIAN_OUTPUT));
deleteDir(new File(STDDEV_OUTPUT)); deleteDir(new File(STDDEV_OUTPUT));

View File

@ -19,8 +19,9 @@
import java.math.BigInteger; import java.math.BigInteger;
import java.util.Random; import java.util.Random;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.Assert;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class TestLongLong { public class TestLongLong {
@ -39,11 +40,11 @@ static void verifyMultiplication(long a, long b) {
"\na = %x\nb = %x\nll= " + ll + "\nbi= " + bi.toString(16) + "\n", a, "\na = %x\nb = %x\nll= " + ll + "\nbi= " + bi.toString(16) + "\n", a,
b); b);
//System.out.println(s); //System.out.println(s);
Assert.assertEquals(s, bi, ll.toBigInteger()); assertEquals(bi, ll.toBigInteger(), s);
} }
@Test @Test
public void testMultiplication() { void testMultiplication() {
for (int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++) {
final long a = nextPositiveLong(); final long a = nextPositiveLong();
final long b = nextPositiveLong(); final long b = nextPositiveLong();
@ -54,7 +55,7 @@ public void testMultiplication() {
} }
@Test @Test
public void testRightShift() { void testRightShift() {
for (int i = 0; i < 1000; i++) { for (int i = 0; i < 1000; i++) {
final long a = nextPositiveLong(); final long a = nextPositiveLong();
final long b = nextPositiveLong(); final long b = nextPositiveLong();
@ -69,12 +70,12 @@ private static void verifyRightShift(long a, long b) {
final String s = String.format( final String s = String.format(
"\na = %x\nb = %x\nll= " + ll + "\nbi= " + bi.toString(16) + "\n", a, "\na = %x\nb = %x\nll= " + ll + "\nbi= " + bi.toString(16) + "\n", a,
b); b);
Assert.assertEquals(s, bi, ll.toBigInteger()); assertEquals(bi, ll.toBigInteger(), s);
for (int i = 0; i < LongLong.SIZE >> 1; i++) { for (int i = 0; i < LongLong.SIZE >> 1; i++) {
final long result = ll.shiftRight(i) & MASK; final long result = ll.shiftRight(i) & MASK;
final long expected = bi.shiftRight(i).longValue() & MASK; final long expected = bi.shiftRight(i).longValue() & MASK;
Assert.assertEquals(s, expected, result); assertEquals(expected, result, s);
} }
} }
} }

View File

@ -21,8 +21,9 @@
import java.util.Random; import java.util.Random;
import org.apache.hadoop.examples.pi.Util.Timer; import org.apache.hadoop.examples.pi.Util.Timer;
import org.junit.Assert; import org.junit.jupiter.api.Test;
import org.junit.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class TestModular{ public class TestModular{
private static final Random RANDOM = new Random(); private static final Random RANDOM = new Random();
@ -52,13 +53,13 @@ static long div(long sum, long r, long n) {
} }
@Test @Test
public void testDiv() { void testDiv() {
for (long n = 2; n < 100; n++) for (long n = 2; n < 100; n++)
for (long r = 1; r < n; r++) { for (long r = 1; r < n; r++) {
final long a = div(0, r, n); final long a = div(0, r, n);
final long b = (long) ((r * 1.0 / n) * (1L << DIV_VALID_BIT)); final long b = (long) ((r * 1.0 / n) * (1L << DIV_VALID_BIT));
final String s = String.format("r=%d, n=%d, a=%X, b=%X", r, n, a, b); final String s = String.format("r=%d, n=%d, a=%X, b=%X", r, n, a, b);
Assert.assertEquals(s, b, a); assertEquals(b, a, s);
} }
} }
@ -151,9 +152,8 @@ static void squareBenchmarks() {
final long answer = rn[i][j][1]; final long answer = rn[i][j][1];
final long s = square_slow(r, n); final long s = square_slow(r, n);
if (s != answer) { if (s != answer) {
Assert.assertEquals( assertEquals(
"r=" + r + ", n=" + n + ", answer=" + answer + " but s=" + s, answer, s, "r=" + r + ", n=" + n + ", answer=" + answer + " but s=" + s);
answer, s);
} }
} }
} }
@ -168,9 +168,8 @@ static void squareBenchmarks() {
final long answer = rn[i][j][1]; final long answer = rn[i][j][1];
final long s = square(r, n, r2p64); final long s = square(r, n, r2p64);
if (s != answer) { if (s != answer) {
Assert.assertEquals( assertEquals(
"r=" + r + ", n=" + n + ", answer=" + answer + " but s=" + s, answer, s, "r=" + r + ", n=" + n + ", answer=" + answer + " but s=" + s);
answer, s);
} }
} }
} }
@ -185,9 +184,8 @@ static void squareBenchmarks() {
final BigInteger R = BigInteger.valueOf(r); final BigInteger R = BigInteger.valueOf(r);
final long s = R.multiply(R).mod(N).longValue(); final long s = R.multiply(R).mod(N).longValue();
if (s != answer) { if (s != answer) {
Assert.assertEquals( assertEquals(
"r=" + r + ", n=" + n + ", answer=" + answer + " but s=" + s, answer, s, "r=" + r + ", n=" + n + ", answer=" + answer + " but s=" + s);
answer, s);
} }
} }
} }
@ -202,9 +200,8 @@ static void squareBenchmarks() {
final BigInteger R = BigInteger.valueOf(r); final BigInteger R = BigInteger.valueOf(r);
final long s = R.modPow(TWO, N).longValue(); final long s = R.modPow(TWO, N).longValue();
if (s != answer) { if (s != answer) {
Assert.assertEquals( assertEquals(
"r=" + r + ", n=" + n + ", answer=" + answer + " but s=" + s, answer, s, "r=" + r + ", n=" + n + ", answer=" + answer + " but s=" + s);
answer, s);
} }
} }
} }
@ -299,9 +296,8 @@ static void modBenchmarks() {
final long answer = en[i][j][1]; final long answer = en[i][j][1];
final long s = Modular.mod(e, n); final long s = Modular.mod(e, n);
if (s != answer) { if (s != answer) {
Assert.assertEquals( assertEquals(
"e=" + e + ", n=" + n + ", answer=" + answer + " but s=" + s, answer, s, "e=" + e + ", n=" + n + ", answer=" + answer + " but s=" + s);
answer, s);
} }
} }
} }
@ -316,9 +312,8 @@ static void modBenchmarks() {
final long answer = en[i][j][1]; final long answer = en[i][j][1];
final long s = m2.mod(e); final long s = m2.mod(e);
if (s != answer) { if (s != answer) {
Assert.assertEquals( assertEquals(
"e=" + e + ", n=" + n + ", answer=" + answer + " but s=" + s, answer, s, "e=" + e + ", n=" + n + ", answer=" + answer + " but s=" + s);
answer, s);
} }
} }
} }
@ -332,9 +327,8 @@ static void modBenchmarks() {
final long answer = en[i][j][1]; final long answer = en[i][j][1];
final long s = m2.mod2(e); final long s = m2.mod2(e);
if (s != answer) { if (s != answer) {
Assert.assertEquals( assertEquals(
"e=" + e + ", n=" + n + ", answer=" + answer + " but s=" + s, answer, s, "e=" + e + ", n=" + n + ", answer=" + answer + " but s=" + s);
answer, s);
} }
} }
} }
@ -348,9 +342,8 @@ static void modBenchmarks() {
final long answer = en[i][j][1]; final long answer = en[i][j][1];
final long s = TWO.modPow(BigInteger.valueOf(e), N).longValue(); final long s = TWO.modPow(BigInteger.valueOf(e), N).longValue();
if (s != answer) { if (s != answer) {
Assert.assertEquals( assertEquals(
"e=" + e + ", n=" + n + ", answer=" + answer + " but s=" + s, answer, s, "e=" + e + ", n=" + n + ", answer=" + answer + " but s=" + s);
answer, s);
} }
} }
} }

View File

@ -28,8 +28,9 @@
import org.apache.hadoop.examples.pi.Util; import org.apache.hadoop.examples.pi.Util;
import org.apache.hadoop.examples.pi.Util.Timer; import org.apache.hadoop.examples.pi.Util.Timer;
import org.apache.hadoop.examples.pi.math.TestModular.Montgomery2; import org.apache.hadoop.examples.pi.math.TestModular.Montgomery2;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.Assert;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class TestSummation { public class TestSummation {
static final Random RANDOM = new Random(); static final Random RANDOM = new Random();
@ -58,12 +59,12 @@ private static void runTestSubtract(Summation sigma, List<Summation> diff) {
final List<Summation> combined = Util.combine(a); final List<Summation> combined = Util.combine(a);
// Util.out.println("combined=" + combined); // Util.out.println("combined=" + combined);
Assert.assertEquals(1, combined.size()); assertEquals(1, combined.size());
Assert.assertEquals(sigma, combined.get(0)); assertEquals(sigma, combined.get(0));
} }
@Test @Test
public void testSubtract() { void testSubtract() {
final Summation sigma = newSummation(3, 10000, 20); final Summation sigma = newSummation(3, 10000, 20);
final int size = 10; final int size = 10;
final List<Summation> parts = Arrays.asList(sigma.partition(size)); final List<Summation> parts = Arrays.asList(sigma.partition(size));
@ -132,16 +133,16 @@ private static void computeBenchmarks(final Summation2 sigma) {
t.tick("sigma=" + sigma); t.tick("sigma=" + sigma);
final double value = sigma.compute(); final double value = sigma.compute();
t.tick("compute=" + value); t.tick("compute=" + value);
Assert.assertEquals(value, sigma.compute_modular(), DOUBLE_DELTA); assertEquals(value, sigma.compute_modular(), DOUBLE_DELTA);
t.tick("compute_modular"); t.tick("compute_modular");
Assert.assertEquals(value, sigma.compute_montgomery(), DOUBLE_DELTA); assertEquals(value, sigma.compute_montgomery(), DOUBLE_DELTA);
t.tick("compute_montgomery"); t.tick("compute_montgomery");
Assert.assertEquals(value, sigma.compute_montgomery2(), DOUBLE_DELTA); assertEquals(value, sigma.compute_montgomery2(), DOUBLE_DELTA);
t.tick("compute_montgomery2"); t.tick("compute_montgomery2");
Assert.assertEquals(value, sigma.compute_modBigInteger(), DOUBLE_DELTA); assertEquals(value, sigma.compute_modBigInteger(), DOUBLE_DELTA);
t.tick("compute_modBigInteger"); t.tick("compute_modBigInteger");
Assert.assertEquals(value, sigma.compute_modPow(), DOUBLE_DELTA); assertEquals(value, sigma.compute_modPow(), DOUBLE_DELTA);
t.tick("compute_modPow"); t.tick("compute_modPow");
} }

View File

@ -25,14 +25,15 @@
import org.apache.hadoop.mapred.FileAlreadyExistsException; import org.apache.hadoop.mapred.FileAlreadyExistsException;
import org.apache.hadoop.mapred.HadoopTestCase; import org.apache.hadoop.mapred.HadoopTestCase;
import org.apache.hadoop.util.ToolRunner; import org.apache.hadoop.util.ToolRunner;
import org.junit.After; import org.junit.jupiter.api.AfterEach;
import org.junit.Test; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.fail; import static org.junit.jupiter.api.Assertions.fail;
public class TestTeraSort extends HadoopTestCase { public class TestTeraSort extends HadoopTestCase {
private static final Logger LOG = LoggerFactory.getLogger(TestTeraSort.class); private static final Logger LOG = LoggerFactory.getLogger(TestTeraSort.class);
@ -42,7 +43,12 @@ public TestTeraSort()
super(LOCAL_MR, LOCAL_FS, 1, 1); super(LOCAL_MR, LOCAL_FS, 1, 1);
} }
@After @BeforeEach
public void setUp() throws Exception {
super.setUp();
}
@AfterEach
public void tearDown() throws Exception { public void tearDown() throws Exception {
getFileSystem().delete(TEST_DIR, true); getFileSystem().delete(TEST_DIR, true);
super.tearDown(); super.tearDown();
@ -85,7 +91,7 @@ private void runTeraValidator(Configuration job,
} }
@Test @Test
public void testTeraSort() throws Exception { void testTeraSort() throws Exception {
// Run TeraGen to generate input for 'terasort' // Run TeraGen to generate input for 'terasort'
runTeraGen(createJobConf(), SORT_INPUT_PATH); runTeraGen(createJobConf(), SORT_INPUT_PATH);
@ -114,7 +120,7 @@ public void testTeraSort() throws Exception {
} }
@Test @Test
public void testTeraSortWithLessThanTwoArgs() throws Exception { void testTeraSortWithLessThanTwoArgs() throws Exception {
String[] args = new String[1]; String[] args = new String[1];
assertThat(new TeraSort().run(args)).isEqualTo(2); assertThat(new TeraSort().run(args)).isEqualTo(2);
} }