Skip to content

Commit 023b1c9

Browse files
author
fanshilun
committed
HADOOP-19431. Fix Unit test & CheckStyle.
1 parent febabee commit 023b1c9

27 files changed

+940
-865
lines changed

hadoop-tools/hadoop-distcp/src/main/java/org/apache/hadoop/tools/SimpleCopyListing.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ public class SimpleCopyListing extends CopyListing {
7575
private long totalDirs = 0;
7676
private long totalBytesToCopy = 0;
7777
private int numListstatusThreads = 1;
78-
private final int fileStatusLimit;
79-
private final boolean randomizeFileListing;
78+
private int fileStatusLimit;
79+
private boolean randomizeFileListing;
8080
private final int maxRetries = 3;
8181
private CopyFilter copyFilter;
8282
private DistCpSync distCpSync;
@@ -119,10 +119,14 @@ protected SimpleCopyListing(Configuration configuration,
119119
this.randomizeFileListing = randomizeFileListing;
120120
}
121121

122-
private void initSimpleCopyListing(Configuration configuration,
123-
Credentials credentials, int numListstatusThreads, int fileStatusLimit,
124-
boolean randomizeFileListing) {
125-
122+
protected void initSimpleCopyListing(Configuration configuration,
123+
Credentials credentials, int numListstatusThreads, int fileStatusLimit,
124+
boolean randomizeFileListing) {
125+
setConf(configuration);
126+
setCredentials(credentials);
127+
this.numListstatusThreads = numListstatusThreads;
128+
this.fileStatusLimit = Math.max(1, fileStatusLimit);
129+
this.randomizeFileListing = randomizeFileListing;
126130
}
127131

128132
protected SimpleCopyListing(Configuration configuration,

hadoop-tools/hadoop-distcp/src/test/java/org/apache/hadoop/tools/TestCopyListing.java

Lines changed: 73 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,14 @@
1818

1919
package org.apache.hadoop.tools;
2020

21+
import static org.assertj.core.api.Assertions.assertThat;
22+
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
import static org.junit.jupiter.api.Assertions.assertNotNull;
2124
import static org.junit.jupiter.api.Assertions.assertThrows;
22-
import static org.mockito.Mockito.*;
25+
import static org.junit.jupiter.api.Assertions.assertTrue;
26+
import static org.junit.jupiter.api.Assertions.fail;
27+
import static org.mockito.Mockito.doThrow;
28+
import static org.mockito.Mockito.mock;
2329

2430
import org.apache.hadoop.hdfs.protocol.SnapshotDiffReport;
2531
import org.mockito.Mockito;
@@ -34,15 +40,11 @@
3440
import org.apache.hadoop.io.SequenceFile;
3541
import org.apache.hadoop.io.IOUtils;
3642
import org.apache.hadoop.io.Text;
37-
import org.junit.runner.RunWith;
38-
import org.junit.runners.Parameterized;
39-
import org.junit.runners.Parameterized.Parameters;
40-
import org.junit.jupiter.api.Test;
4143
import org.junit.jupiter.api.Timeout;
42-
import org.junit.jupiter.api.Assertions;
4344
import org.junit.jupiter.api.BeforeAll;
4445
import org.junit.jupiter.api.AfterAll;
45-
import static org.assertj.core.api.Assertions.assertThat;
46+
import org.junit.jupiter.params.ParameterizedTest;
47+
import org.junit.jupiter.params.provider.MethodSource;
4648

4749
import java.io.File;
4850
import java.io.IOException;
@@ -54,7 +56,6 @@
5456
import java.util.List;
5557
import java.util.Random;
5658

57-
@RunWith(value = Parameterized.class)
5859
public class TestCopyListing extends SimpleCopyListing {
5960
private static final Logger LOG = LoggerFactory.getLogger(TestCopyListing.class);
6061

@@ -76,18 +77,18 @@ public static void destroy() {
7677
}
7778
}
7879

79-
@Parameters
8080
public static Collection<Object[]> data() {
81-
Object[][] data = new Object[][] { { 1 }, { 2 }, { 10 }, { 20} };
81+
Object[][] data = new Object[][]{{1}, {2}, {10}, {20}};
8282
return Arrays.asList(data);
8383
}
8484

85-
public TestCopyListing(int numListstatusThreads) {
86-
super(config, CREDENTIALS, numListstatusThreads, 0, false);
85+
public TestCopyListing() {
86+
super(config, CREDENTIALS, 1, 0, false);
8787
}
8888

89-
protected TestCopyListing(Configuration configuration) {
90-
super(configuration, CREDENTIALS);
89+
public void initTestCopyListing(int numListstatusThreads) {
90+
initSimpleCopyListing(config, CREDENTIALS,
91+
numListstatusThreads, 0, false);
9192
}
9293

9394
@Override
@@ -100,9 +101,12 @@ protected long getNumberOfPaths() {
100101
return 0;
101102
}
102103

103-
@Test
104+
104105
@Timeout(value = 10)
105-
public void testMultipleSrcToFile() {
106+
@ParameterizedTest
107+
@MethodSource("data")
108+
public void testMultipleSrcToFile(int pNumListstatusThreads) {
109+
initTestCopyListing(pNumListstatusThreads);
106110
FileSystem fs = null;
107111
try {
108112
fs = FileSystem.get(getConf());
@@ -122,7 +126,7 @@ public void testMultipleSrcToFile() {
122126
fs.create(target).close();
123127
try {
124128
validatePaths(new DistCpContext(options));
125-
Assertions.fail("Invalid inputs accepted");
129+
fail("Invalid inputs accepted");
126130
} catch (InvalidInputException ignore) { }
127131
TestDistCpUtils.delete(fs, "/tmp");
128132

@@ -132,20 +136,22 @@ public void testMultipleSrcToFile() {
132136
fs.create(target).close();
133137
try {
134138
validatePaths(new DistCpContext(options));
135-
Assertions.fail("Invalid inputs accepted");
139+
fail("Invalid inputs accepted");
136140
} catch (InvalidInputException ignore) { }
137141
TestDistCpUtils.delete(fs, "/tmp");
138142
} catch (IOException e) {
139143
LOG.error("Exception encountered ", e);
140-
Assertions.fail("Test input validation failed");
144+
fail("Test input validation failed");
141145
} finally {
142146
TestDistCpUtils.delete(fs, "/tmp");
143147
}
144148
}
145149

146-
@Test
150+
@ParameterizedTest
147151
@Timeout(value = 10)
148-
public void testDuplicates() {
152+
@MethodSource("data")
153+
public void testDuplicates(int pNumListstatusThreads) {
154+
initTestCopyListing(pNumListstatusThreads);
149155
FileSystem fs = null;
150156
try {
151157
fs = FileSystem.get(getConf());
@@ -162,20 +168,23 @@ public void testDuplicates() {
162168
context);
163169
try {
164170
listing.buildListing(listingFile, context);
165-
Assertions.fail("Duplicates not detected");
171+
fail("Duplicates not detected");
166172
} catch (DuplicateFileException ignore) {
167173
}
168174
} catch (IOException e) {
169175
LOG.error("Exception encountered in test", e);
170-
Assertions.fail("Test failed " + e.getMessage());
176+
fail("Test failed " + e.getMessage());
171177
} finally {
172178
TestDistCpUtils.delete(fs, "/tmp");
173179
}
174180
}
175181

176-
@Test
182+
@ParameterizedTest
177183
@Timeout(value = 10)
178-
public void testDiffBasedSimpleCopyListing() throws IOException {
184+
@MethodSource("data")
185+
public void testDiffBasedSimpleCopyListing(int pNumListstatusThreads)
186+
throws IOException {
187+
initTestCopyListing(pNumListstatusThreads);
179188
assertThrows(DuplicateFileException.class, () -> {
180189
FileSystem fs = null;
181190
Configuration configuration = getConf();
@@ -194,10 +203,12 @@ public void testDiffBasedSimpleCopyListing() throws IOException {
194203
});
195204
}
196205

197-
@Test
206+
@ParameterizedTest
198207
@Timeout(value = 10)
199-
public void testDiffBasedSimpleCopyListingWithoutTraverseDirectory()
200-
throws IOException {
208+
@MethodSource("data")
209+
public void testDiffBasedSimpleCopyListingWithoutTraverseDirectory(
210+
int pNumListstatusThreads) throws IOException {
211+
initTestCopyListing(pNumListstatusThreads);
201212
FileSystem fs = null;
202213
Configuration configuration = getConf();
203214
DistCpSync distCpSync = Mockito.mock(DistCpSync.class);
@@ -243,8 +254,10 @@ private void buildListingUsingSnapshotDiff(FileSystem fs,
243254
listing.buildListing(listingFile, context);
244255
}
245256

246-
@Test
247-
public void testDuplicateSourcePaths() throws Exception {
257+
@ParameterizedTest
258+
@MethodSource("data")
259+
public void testDuplicateSourcePaths(int pNumListstatusThreads) throws Exception {
260+
initTestCopyListing(pNumListstatusThreads);
248261
FileSystem fs = FileSystem.get(getConf());
249262
List<Path> srcPaths = new ArrayList<Path>();
250263
try {
@@ -260,15 +273,17 @@ public void testDuplicateSourcePaths() throws Exception {
260273
CopyListing listing =
261274
CopyListing.getCopyListing(getConf(), CREDENTIALS, context);
262275
listing.buildListing(listingFile, context);
263-
Assertions.assertTrue(fs.exists(listingFile));
276+
assertTrue(fs.exists(listingFile));
264277
} finally {
265278
TestDistCpUtils.delete(fs, "/tmp");
266279
}
267280
}
268281

269-
@Test
282+
@ParameterizedTest
270283
@Timeout(value = 10)
271-
public void testBuildListing() {
284+
@MethodSource("data")
285+
public void testBuildListing(int pNumListstatusThreads) {
286+
initTestCopyListing(pNumListstatusThreads);
272287
FileSystem fs = null;
273288
try {
274289
fs = FileSystem.get(getConf());
@@ -303,7 +318,7 @@ public void testBuildListing() {
303318
CopyListing listing = new SimpleCopyListing(getConf(), CREDENTIALS);
304319
try {
305320
listing.buildListing(listingFile, new DistCpContext(options));
306-
Assertions.fail("Duplicates not detected");
321+
fail("Duplicates not detected");
307322
} catch (DuplicateFileException ignore) {
308323
}
309324
assertThat(listing.getBytesToCopy()).isEqualTo(10);
@@ -312,21 +327,24 @@ public void testBuildListing() {
312327

313328
try {
314329
listing.buildListing(listingFile, new DistCpContext(options));
315-
Assertions.fail("Invalid input not detected");
330+
fail("Invalid input not detected");
316331
} catch (InvalidInputException ignore) {
317332
}
318333
TestDistCpUtils.delete(fs, "/tmp");
319334
} catch (IOException e) {
320335
LOG.error("Exception encountered ", e);
321-
Assertions.fail("Test build listing failed");
336+
fail("Test build listing failed");
322337
} finally {
323338
TestDistCpUtils.delete(fs, "/tmp");
324339
}
325340
}
326341

327-
@Test
342+
@ParameterizedTest
328343
@Timeout(value = 60)
329-
public void testWithRandomFileListing() throws IOException {
344+
@MethodSource("data")
345+
public void testWithRandomFileListing(int pNumListstatusThreads)
346+
throws IOException {
347+
initTestCopyListing(pNumListstatusThreads);
330348
FileSystem fs = null;
331349
try {
332350
fs = FileSystem.get(getConf());
@@ -356,7 +374,7 @@ public void testWithRandomFileListing() throws IOException {
356374
SimpleCopyListing listing = new SimpleCopyListing(getConf(), CREDENTIALS);
357375
listing.buildListing(listingFile, new DistCpContext(options));
358376

359-
Assertions.assertEquals(listing.getNumberOfPaths(), pathCount);
377+
assertEquals(listing.getNumberOfPaths(), pathCount);
360378
validateFinalListing(listingFile, srcFiles);
361379
fs.delete(listingFile, true);
362380

@@ -369,7 +387,7 @@ public void testWithRandomFileListing() throws IOException {
369387
long seed = System.nanoTime();
370388
listing.setSeedForRandomListing(seed);
371389
listing.buildListing(listingFile, new DistCpContext(options));
372-
Assertions.assertEquals(listing.getNumberOfPaths(), pathCount);
390+
assertEquals(listing.getNumberOfPaths(), pathCount);
373391

374392
// validate randomness
375393
Collections.shuffle(srcFiles, new Random(seed));
@@ -391,7 +409,7 @@ private void validateFinalListing(Path pathToListFile, List<Path> srcFiles)
391409
int idx = 0;
392410
while (reader.next(currentKey)) {
393411
reader.getCurrentValue(currentVal);
394-
Assertions.assertEquals(fs.makeQualified(srcFiles.get(idx))
412+
assertEquals(fs.makeQualified(srcFiles.get(idx))
395413
, currentVal.getPath(), "srcFiles.size=" + srcFiles.size()
396414
+ ", idx=" + idx);
397415
if (LOG.isDebugEnabled()) {
@@ -403,9 +421,11 @@ private void validateFinalListing(Path pathToListFile, List<Path> srcFiles)
403421
}
404422

405423

406-
@Test
424+
@ParameterizedTest
407425
@Timeout(value = 10)
408-
public void testBuildListingForSingleFile() {
426+
@MethodSource("data")
427+
public void testBuildListingForSingleFile(int pNumListstatusThreads) {
428+
initTestCopyListing(pNumListstatusThreads);
409429
FileSystem fs = null;
410430
String testRootString = "/singleFileListing";
411431
Path testRoot = new Path(testRootString);
@@ -437,21 +457,23 @@ public void testBuildListingForSingleFile() {
437457

438458
CopyListingFileStatus fileStatus = new CopyListingFileStatus();
439459
Text relativePath = new Text();
440-
Assertions.assertTrue(reader.next(relativePath, fileStatus));
441-
Assertions.assertTrue(relativePath.toString().equals(""));
460+
assertTrue(reader.next(relativePath, fileStatus));
461+
assertTrue(relativePath.toString().equals(""));
442462
}
443463
catch (Exception e) {
444-
Assertions.fail("Unexpected exception encountered.");
464+
fail("Unexpected exception encountered.");
445465
LOG.error("Unexpected exception: ", e);
446466
}
447467
finally {
448468
TestDistCpUtils.delete(fs, testRootString);
449469
IOUtils.closeStream(reader);
450470
}
451471
}
452-
453-
@Test
454-
public void testFailOnCloseError() throws IOException {
472+
473+
@ParameterizedTest
474+
@MethodSource("data")
475+
public void testFailOnCloseError(int pNumListstatusThreads) throws IOException {
476+
initTestCopyListing(pNumListstatusThreads);
455477
File inFile = File.createTempFile("TestCopyListingIn", null);
456478
inFile.deleteOnExit();
457479
File outFile = File.createTempFile("TestCopyListingOut", null);
@@ -472,7 +494,7 @@ public void testFailOnCloseError() throws IOException {
472494
} catch (Exception e) {
473495
actualEx = e;
474496
}
475-
Assertions.assertNotNull(actualEx, "close writer didn't fail");
476-
Assertions.assertEquals(expectedEx, actualEx);
497+
assertNotNull(actualEx, "close writer didn't fail");
498+
assertEquals(expectedEx, actualEx);
477499
}
478500
}

0 commit comments

Comments
 (0)