Skip to content

Commit 7f6fca4

Browse files
slfan1989cnaurothzhtttylz
authored
HADOOP-19415. [JDK17] Upgrade JUnit from 4 to 5 in hadoop-common Part12. (#7672)
* HADOOP-19415. [JDK17] Upgrade JUnit from 4 to 5 in hadoop-common Part12. Co-authored-by: Chris Nauroth <[email protected]> Co-authored-by: Hualong Zhang <[email protected]> Reviewed-by: Chris Nauroth <[email protected]> Reviewed-by: Hualong Zhang <[email protected]> Signed-off-by: Shilun Fan <[email protected]>
1 parent 00cffd1 commit 7f6fca4

31 files changed

+525
-486
lines changed

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/AvroTestUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import org.apache.avro.reflect.ReflectDatumReader;
2929
import org.apache.avro.io.DecoderFactory;
3030

31-
import static org.junit.Assert.assertEquals;
31+
import static org.junit.jupiter.api.Assertions.assertEquals;
3232

3333
public class AvroTestUtil {
3434

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestArrayFile.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@
2626
import org.apache.hadoop.test.GenericTestUtils;
2727
import org.apache.hadoop.util.Progressable;
2828
import org.apache.hadoop.conf.*;
29-
import org.junit.Test;
29+
import org.junit.jupiter.api.Test;
3030
import org.slf4j.Logger;
3131
import org.slf4j.LoggerFactory;
3232

3333
import static org.assertj.core.api.Assertions.assertThat;
34-
import static org.junit.Assert.assertNull;
35-
import static org.junit.Assert.assertNotNull;
36-
import static org.junit.Assert.assertTrue;
37-
import static org.junit.Assert.assertFalse;
38-
import static org.junit.Assert.fail;
34+
import static org.junit.jupiter.api.Assertions.assertNull;
35+
import static org.junit.jupiter.api.Assertions.assertNotNull;
36+
import static org.junit.jupiter.api.Assertions.assertTrue;
37+
import static org.junit.jupiter.api.Assertions.assertFalse;
38+
import static org.junit.jupiter.api.Assertions.fail;
3939

4040
/** Support for flat files of binary key/value pairs. */
4141
public class TestArrayFile {
@@ -134,7 +134,7 @@ public void testArrayFileIteration() {
134134
FileSystem fs = FileSystem.get(conf);
135135
ArrayFile.Writer writer = new ArrayFile.Writer(conf, fs, TEST_FILE,
136136
LongWritable.class, CompressionType.RECORD, defaultProgressable);
137-
assertNotNull("testArrayFileIteration error !!!", writer);
137+
assertNotNull(writer, "testArrayFileIteration error !!!");
138138

139139
for (int i = 0; i < SIZE; i++)
140140
writer.append(new LongWritable(i));
@@ -149,15 +149,15 @@ public void testArrayFileIteration() {
149149
assertThat(nextWritable.get()).isEqualTo(i);
150150
}
151151

152-
assertTrue("testArrayFileIteration seek error !!!",
153-
reader.seek(new LongWritable(6)));
152+
assertTrue(reader.seek(new LongWritable(6)),
153+
"testArrayFileIteration seek error !!!");
154154
nextWritable = (LongWritable) reader.next(nextWritable);
155155
assertThat(reader.key()).withFailMessage(
156156
"testArrayFileIteration error !!!").isEqualTo(7);
157157
assertThat(nextWritable).withFailMessage(
158158
"testArrayFileIteration error !!!").isEqualTo(new LongWritable(7));
159-
assertFalse("testArrayFileIteration error !!!",
160-
reader.seek(new LongWritable(SIZE + 5)));
159+
assertFalse(reader.seek(new LongWritable(SIZE + 5)),
160+
"testArrayFileIteration error !!!");
161161
reader.close();
162162
} catch (Exception ex) {
163163
fail("testArrayFileWriterConstruction error !!!");

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestArrayPrimitiveWritable.java

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
import java.util.Arrays;
2323

2424
import org.apache.hadoop.util.StringUtils;
25-
import org.junit.Test;
26-
import org.junit.Before;
27-
import static org.junit.Assert.assertEquals;
28-
import static org.junit.Assert.assertTrue;
29-
import static org.junit.Assert.fail;
25+
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.api.BeforeEach;
27+
import static org.junit.jupiter.api.Assertions.assertEquals;
28+
import static org.junit.jupiter.api.Assertions.assertTrue;
29+
import static org.junit.jupiter.api.Assertions.fail;
3030

3131

3232
/** Unit tests for {@link ArrayPrimitiveWritable} */
@@ -48,7 +48,7 @@ public class TestArrayPrimitiveWritable {
4848
final DataOutputBuffer out = new DataOutputBuffer();
4949
final DataInputBuffer in = new DataInputBuffer();
5050

51-
@Before
51+
@BeforeEach
5252
public void resetBuffers() throws IOException {
5353
out.reset();
5454
in.reset();
@@ -79,12 +79,11 @@ public void testMany() throws IOException {
7979
//validate data structures and values
8080
assertEquals(expectedResultSet.length, resultSet.length);
8181
for (int x = 0; x < resultSet.length; x++) {
82-
assertEquals("ComponentType of array " + x,
83-
expectedResultSet[x].getClass().getComponentType(),
84-
resultSet[x].getClass().getComponentType());
82+
assertEquals(expectedResultSet[x].getClass().getComponentType(),
83+
resultSet[x].getClass().getComponentType(), "ComponentType of array " + x);
8584
}
86-
assertTrue("In and Out arrays didn't match values",
87-
Arrays.deepEquals(expectedResultSet, resultSet));
85+
assertTrue(Arrays.deepEquals(expectedResultSet, resultSet),
86+
"In and Out arrays didn't match values");
8887
}
8988

9089
@Test
@@ -107,36 +106,35 @@ public void testObjectLabeling() throws IOException {
107106
//Read the int[] object as written by ObjectWritable, but
108107
//"going around" ObjectWritable
109108
String className = UTF8.readString(in);
110-
assertEquals("The int[] written by ObjectWritable was not labelled as "
111-
+ "an ArrayPrimitiveWritable.Internal",
112-
ArrayPrimitiveWritable.Internal.class.getName(), className);
109+
assertEquals(ArrayPrimitiveWritable.Internal.class.getName(), className,
110+
"The int[] written by ObjectWritable was not labelled as "
111+
+ "an ArrayPrimitiveWritable.Internal");
113112
ArrayPrimitiveWritable.Internal apwi =
114113
new ArrayPrimitiveWritable.Internal();
115114
apwi.readFields(in);
116-
assertEquals("The ArrayPrimitiveWritable.Internal component type was corrupted",
117-
int.class, apw.getComponentType());
118-
assertTrue("The int[] written by ObjectWritable as "
119-
+ "ArrayPrimitiveWritable.Internal was corrupted",
120-
Arrays.equals(i, (int[])(apwi.get())));
115+
assertEquals(int.class, apw.getComponentType(),
116+
"The ArrayPrimitiveWritable.Internal component type was corrupted");
117+
assertTrue(Arrays.equals(i, (int[])(apwi.get())), "The int[] written by ObjectWritable as "
118+
+ "ArrayPrimitiveWritable.Internal was corrupted");
121119

122120
//Read the APW object as written by ObjectWritable, but
123121
//"going around" ObjectWritable
124122
String declaredClassName = UTF8.readString(in);
125-
assertEquals("The APW written by ObjectWritable was not labelled as "
126-
+ "declaredClass ArrayPrimitiveWritable",
127-
ArrayPrimitiveWritable.class.getName(), declaredClassName);
123+
assertEquals(ArrayPrimitiveWritable.class.getName(), declaredClassName,
124+
"The APW written by ObjectWritable was not labelled as "
125+
+ "declaredClass ArrayPrimitiveWritable");
128126
className = UTF8.readString(in);
129-
assertEquals("The APW written by ObjectWritable was not labelled as "
130-
+ "class ArrayPrimitiveWritable",
131-
ArrayPrimitiveWritable.class.getName(), className);
127+
assertEquals(ArrayPrimitiveWritable.class.getName(), className,
128+
"The APW written by ObjectWritable was not labelled as "
129+
+ "class ArrayPrimitiveWritable");
132130
ArrayPrimitiveWritable apw2 =
133131
new ArrayPrimitiveWritable();
134132
apw2.readFields(in);
135-
assertEquals("The ArrayPrimitiveWritable component type was corrupted",
136-
int.class, apw2.getComponentType());
137-
assertTrue("The int[] written by ObjectWritable as "
138-
+ "ArrayPrimitiveWritable was corrupted",
139-
Arrays.equals(i, (int[])(apw2.get())));
133+
assertEquals(int.class, apw2.getComponentType(),
134+
"The ArrayPrimitiveWritable component type was corrupted");
135+
assertTrue(Arrays.equals(i, (int[])(apw2.get())),
136+
"The int[] written by ObjectWritable as "
137+
+ "ArrayPrimitiveWritable was corrupted");
140138
}
141139

142140
@Test
@@ -154,13 +152,14 @@ public void testOldFormat() throws IOException {
154152
//"going around" ObjectWritable
155153
@SuppressWarnings("deprecation")
156154
String className = UTF8.readString(in);
157-
assertEquals("The int[] written by ObjectWritable as a non-compact array "
158-
+ "was not labelled as an array of int",
159-
i.getClass().getName(), className);
155+
assertEquals(i.getClass().getName(), className,
156+
"The int[] written by ObjectWritable as a non-compact array "
157+
+ "was not labelled as an array of int");
160158

161159
int length = in.readInt();
162-
assertEquals("The int[] written by ObjectWritable as a non-compact array "
163-
+ "was not expected length", i.length, length);
160+
assertEquals(i.length, length,
161+
"The int[] written by ObjectWritable as a non-compact array "
162+
+ "was not expected length");
164163

165164
int[] readValue = new int[length];
166165
try {
@@ -173,8 +172,9 @@ public void testOldFormat() throws IOException {
173172
+ length + ". Got exception:\n"
174173
+ StringUtils.stringifyException(e));
175174
}
176-
assertTrue("The int[] written by ObjectWritable as a non-compact array "
177-
+ "was corrupted.", Arrays.equals(i, readValue));
175+
assertTrue(Arrays.equals(i, readValue),
176+
"The int[] written by ObjectWritable as a non-compact array "
177+
+ "was corrupted.");
178178

179179
}
180180
}

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestArrayWritable.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@
1818

1919
package org.apache.hadoop.io;
2020

21-
import static org.junit.Assert.assertArrayEquals;
22-
import static org.junit.Assert.assertEquals;
23-
import static org.junit.Assert.assertTrue;
21+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
22+
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
import static org.junit.jupiter.api.Assertions.assertThrows;
24+
import static org.junit.jupiter.api.Assertions.assertTrue;
2425

2526
import java.io.IOException;
2627

27-
import org.junit.Test;
28+
import org.junit.jupiter.api.Test;
2829

2930

3031
/** Unit tests for ArrayWritable */
@@ -73,7 +74,8 @@ public void testArrayWritableToArray() {
7374
arrayWritable.set(elements);
7475
Object array = arrayWritable.toArray();
7576

76-
assertTrue("TestArrayWritable testArrayWritableToArray error!!! ", array instanceof Text[]);
77+
assertTrue(array instanceof Text[],
78+
"TestArrayWritable testArrayWritableToArray error!!! ");
7779
Text[] destElements = (Text[]) array;
7880

7981
for (int i = 0; i < elements.length; i++) {
@@ -84,9 +86,11 @@ public void testArrayWritableToArray() {
8486
/**
8587
* test {@link ArrayWritable} constructor with null
8688
*/
87-
@Test(expected = IllegalArgumentException.class)
89+
@Test
8890
public void testNullArgument() {
89-
new ArrayWritable((Class<? extends Writable>) null);
91+
assertThrows(IllegalArgumentException.class, () -> {
92+
new ArrayWritable((Class<? extends Writable>) null);
93+
});
9094
}
9195

9296
/**
@@ -96,10 +100,10 @@ public void testNullArgument() {
96100
public void testArrayWritableStringConstructor() {
97101
String[] original = { "test1", "test2", "test3" };
98102
ArrayWritable arrayWritable = new ArrayWritable(original);
99-
assertEquals("testArrayWritableStringConstructor class error!!!",
100-
Text.class, arrayWritable.getValueClass());
101-
assertArrayEquals("testArrayWritableStringConstructor toString error!!!",
102-
original, arrayWritable.toStrings());
103+
assertEquals(Text.class, arrayWritable.getValueClass(),
104+
"testArrayWritableStringConstructor class error!!!");
105+
assertArrayEquals(original, arrayWritable.toStrings(),
106+
"testArrayWritableStringConstructor toString error!!!");
103107
}
104108

105109
}

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestBloomMapFile.java

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
package org.apache.hadoop.io;
2020

21-
import static org.mockito.Mockito.*;
21+
import static org.mockito.Mockito.mock;
22+
import static org.mockito.Mockito.spy;
23+
import static org.mockito.Mockito.when;
2224

2325
import java.io.IOException;
2426
import java.io.InputStream;
@@ -42,13 +44,13 @@
4244
import org.apache.hadoop.io.compress.Decompressor;
4345
import org.apache.hadoop.test.GenericTestUtils;
4446
import org.apache.hadoop.util.Progressable;
45-
import static org.junit.Assert.assertEquals;
46-
import static org.junit.Assert.assertTrue;
47-
import static org.junit.Assert.fail;
48-
import static org.junit.Assert.assertNotNull;
49-
import static org.junit.Assert.assertNull;
50-
import org.junit.Before;
51-
import org.junit.Test;
47+
import static org.junit.jupiter.api.Assertions.assertEquals;
48+
import static org.junit.jupiter.api.Assertions.assertTrue;
49+
import static org.junit.jupiter.api.Assertions.fail;
50+
import static org.junit.jupiter.api.Assertions.assertNotNull;
51+
import static org.junit.jupiter.api.Assertions.assertNull;
52+
import org.junit.jupiter.api.BeforeEach;
53+
import org.junit.jupiter.api.Test;
5254

5355
public class TestBloomMapFile {
5456
private static final Logger LOG =
@@ -59,7 +61,7 @@ public class TestBloomMapFile {
5961
private static final Path TEST_DIR = new Path(TEST_ROOT, "testfile");
6062
private static final Path TEST_FILE = new Path(TEST_ROOT, "testfile");
6163

62-
@Before
64+
@BeforeEach
6365
public void setUp() throws Exception {
6466
LocalFileSystem fs = FileSystem.getLocal(conf);
6567
if (fs.exists(TEST_ROOT) && !fs.delete(TEST_ROOT, true)) {
@@ -134,8 +136,8 @@ private void checkMembershipVaryingSizedKeys(List<Text> keys)
134136
reader = new BloomMapFile.Reader(fs, qualifiedDirName.toString(), conf);
135137
Collections.reverse(keys);
136138
for (Text key : keys) {
137-
assertTrue("False negative for existing key " + key,
138-
reader.probablyHasKey(key));
139+
assertTrue(reader.probablyHasKey(key),
140+
"False negative for existing key " + key);
139141
}
140142
reader.close();
141143
fs.delete(qualifiedDirName, true);
@@ -171,7 +173,7 @@ public void testDeleteFile() {
171173
writer = new BloomMapFile.Writer(conf, TEST_FILE,
172174
MapFile.Writer.keyClass(IntWritable.class),
173175
MapFile.Writer.valueClass(Text.class));
174-
assertNotNull("testDeleteFile error !!!", writer);
176+
assertNotNull(writer, "testDeleteFile error !!!");
175177
writer.close();
176178
BloomMapFile.delete(fs, TEST_FILE.toString());
177179
} catch (Exception ex) {
@@ -201,8 +203,8 @@ public void testIOExceptionInWriterConstructor() {
201203
reader = new BloomMapFile.Reader(dirNameSpy, conf,
202204
MapFile.Reader.comparator(new WritableComparator(IntWritable.class)));
203205

204-
assertNull("testIOExceptionInWriterConstructor error !!!",
205-
reader.getBloomFilter());
206+
assertNull(reader.getBloomFilter(),
207+
"testIOExceptionInWriterConstructor error !!!");
206208
} catch (Exception ex) {
207209
fail("unexpect ex in testIOExceptionInWriterConstructor !!!");
208210
} finally {
@@ -232,12 +234,12 @@ public void testGetBloomMapFile() {
232234
MapFile.Reader.comparator(new WritableComparator(IntWritable.class)));
233235

234236
for (int i = 0; i < SIZE; i++) {
235-
assertNotNull("testGetBloomMapFile error !!!",
236-
reader.get(new IntWritable(i), new Text()));
237+
assertNotNull(reader.get(new IntWritable(i), new Text()),
238+
"testGetBloomMapFile error !!!");
237239
}
238240

239-
assertNull("testGetBloomMapFile error !!!",
240-
reader.get(new IntWritable(SIZE + 5), new Text()));
241+
assertNull(reader.get(new IntWritable(SIZE + 5), new Text()),
242+
"testGetBloomMapFile error !!!");
241243
} catch (Exception ex) {
242244
fail("unexpect ex in testGetBloomMapFile !!!");
243245
} finally {
@@ -258,34 +260,34 @@ public void testBloomMapFileConstructors() {
258260
writer = new BloomMapFile.Writer(conf, ts,
259261
testFileName, IntWritable.class, Text.class, CompressionType.BLOCK,
260262
defaultCodec, defaultProgress);
261-
assertNotNull("testBloomMapFileConstructors error !!!", writer);
263+
assertNotNull(writer, "testBloomMapFileConstructors error !!!");
262264
writer.close();
263265
writer = new BloomMapFile.Writer(conf, ts,
264266
testFileName, IntWritable.class, Text.class, CompressionType.BLOCK,
265267
defaultProgress);
266-
assertNotNull("testBloomMapFileConstructors error !!!", writer);
268+
assertNotNull(writer, "testBloomMapFileConstructors error !!!");
267269
writer.close();
268270
writer = new BloomMapFile.Writer(conf, ts,
269271
testFileName, IntWritable.class, Text.class, CompressionType.BLOCK);
270-
assertNotNull("testBloomMapFileConstructors error !!!", writer);
272+
assertNotNull(writer, "testBloomMapFileConstructors error !!!");
271273
writer.close();
272274
writer = new BloomMapFile.Writer(conf, ts,
273275
testFileName, IntWritable.class, Text.class, CompressionType.RECORD,
274276
defaultCodec, defaultProgress);
275-
assertNotNull("testBloomMapFileConstructors error !!!", writer);
277+
assertNotNull(writer, "testBloomMapFileConstructors error !!!");
276278
writer.close();
277279
writer = new BloomMapFile.Writer(conf, ts,
278280
testFileName, IntWritable.class, Text.class, CompressionType.RECORD,
279281
defaultProgress);
280-
assertNotNull("testBloomMapFileConstructors error !!!", writer);
282+
assertNotNull(writer, "testBloomMapFileConstructors error !!!");
281283
writer.close();
282284
writer = new BloomMapFile.Writer(conf, ts,
283285
testFileName, IntWritable.class, Text.class, CompressionType.RECORD);
284-
assertNotNull("testBloomMapFileConstructors error !!!", writer);
286+
assertNotNull(writer, "testBloomMapFileConstructors error !!!");
285287
writer.close();
286288
writer = new BloomMapFile.Writer(conf, ts,
287289
testFileName, WritableComparator.get(Text.class), Text.class);
288-
assertNotNull("testBloomMapFileConstructors error !!!", writer);
290+
assertNotNull(writer, "testBloomMapFileConstructors error !!!");
289291
writer.close();
290292
} catch (Exception ex) {
291293
fail("testBloomMapFileConstructors error !!!");

0 commit comments

Comments
 (0)