Skip to content

Commit 744fe66

Browse files
slfan1989anujmodi2021cnaurothsteveloughran
authored
HADOOP-19425. [JDK17] Upgrade JUnit from 4 to 5 in hadoop-azure Part1. (#7369)
Upgrade some unit tests of the hadoop-azure module to JUnit 5. Co-authored-by: Anuj Modi <[email protected]> Co-authored-by: Chris Nauroth <[email protected]> Co-authored-by: Steve Loughran <[email protected]> Reviewed-by: Anuj Modi <[email protected]> Reviewed-by: Chris Nauroth <[email protected]> Reviewed-by: Steve Loughran <[email protected]> Signed-off-by: Shilun Fan <[email protected]>
1 parent 22b8fcd commit 744fe66

File tree

52 files changed

+1088
-1065
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1088
-1065
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.hadoop.test;
19+
20+
import org.junit.jupiter.api.extension.BeforeEachCallback;
21+
import org.junit.jupiter.api.extension.ExtensionContext;
22+
23+
/**
24+
* This is a custom JUnit5 `RegisterExtension`
25+
* we created to obtain the methond name of the executing function.
26+
*/
27+
public class TestName implements BeforeEachCallback {
28+
29+
private volatile String name;
30+
31+
@Override
32+
public void beforeEach(ExtensionContext extensionContext) throws Exception {
33+
name = extensionContext.getTestMethod().get().getName();
34+
}
35+
36+
public String getMethodName() {
37+
return this.name;
38+
}
39+
}

hadoop-tools/hadoop-azure/src/test/java/org/apache/hadoop/fs/azure/AbstractWasbTestBase.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,15 @@
2121
import java.io.IOException;
2222

2323
import org.apache.hadoop.conf.Configuration;
24-
import org.junit.After;
25-
import org.junit.Before;
24+
import org.junit.jupiter.api.AfterEach;
25+
import org.junit.jupiter.api.BeforeEach;
2626
import org.slf4j.Logger;
2727
import org.slf4j.LoggerFactory;
2828

2929
import org.apache.hadoop.fs.Path;
3030
import org.apache.hadoop.fs.azure.integration.AzureTestConstants;
3131
import org.apache.hadoop.io.IOUtils;
3232

33-
import static org.junit.Assume.assumeNotNull;
3433
import static org.apache.hadoop.fs.azure.integration.AzureTestUtils.*;
3534

3635
/**
@@ -49,14 +48,14 @@ public abstract class AbstractWasbTestBase extends AbstractWasbTestWithTimeout
4948
protected NativeAzureFileSystem fs;
5049
protected AzureBlobStorageTestAccount testAccount;
5150

52-
@Before
51+
@BeforeEach
5352
public void setUp() throws Exception {
5453
AzureBlobStorageTestAccount account = createTestAccount();
55-
assumeNotNull("test account", account);
54+
assumeNotNull(account, "test account");
5655
bindToTestAccount(account);
5756
}
5857

59-
@After
58+
@AfterEach
6059
public void tearDown() throws Exception {
6160
describe("closing test account and filesystem");
6261
testAccount = cleanupTestAccount(testAccount);

hadoop-tools/hadoop-azure/src/test/java/org/apache/hadoop/fs/azure/AbstractWasbTestWithTimeout.java

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,46 +18,42 @@
1818

1919
package org.apache.hadoop.fs.azure;
2020

21-
import org.junit.Assert;
22-
import org.junit.Before;
23-
import org.junit.BeforeClass;
24-
import org.junit.Rule;
25-
import org.junit.rules.TestName;
26-
import org.junit.rules.Timeout;
27-
21+
import org.junit.jupiter.api.Assertions;
22+
import org.junit.jupiter.api.BeforeAll;
23+
import org.junit.jupiter.api.BeforeEach;
24+
import org.junit.jupiter.api.Timeout;
2825
import org.apache.hadoop.fs.azure.integration.AzureTestConstants;
26+
import org.junit.jupiter.api.extension.RegisterExtension;
27+
import org.apache.hadoop.test.TestName;
28+
29+
import static org.junit.jupiter.api.Assumptions.assumeTrue;
2930

3031
/**
3132
* Base class for any Wasb test with timeouts & named threads.
3233
* This class does not attempt to bind to Azure.
3334
*/
34-
public class AbstractWasbTestWithTimeout extends Assert {
35+
@Timeout(AzureTestConstants.AZURE_TEST_TIMEOUT)
36+
public class AbstractWasbTestWithTimeout extends Assertions {
3537

3638
/**
3739
* The name of the current method.
3840
*/
39-
@Rule
41+
@RegisterExtension
4042
public TestName methodName = new TestName();
41-
/**
42-
* Set the timeout for every test.
43-
* This is driven by the value returned by {@link #getTestTimeoutMillis()}.
44-
*/
45-
@Rule
46-
public Timeout testTimeout = new Timeout(getTestTimeoutMillis());
4743

4844
/**
4945
* Name the junit thread for the class. This will overridden
5046
* before the individual test methods are run.
5147
*/
52-
@BeforeClass
48+
@BeforeAll
5349
public static void nameTestThread() {
5450
Thread.currentThread().setName("JUnit");
5551
}
5652

5753
/**
5854
* Name the thread to the current test method.
5955
*/
60-
@Before
56+
@BeforeEach
6157
public void nameThread() {
6258
Thread.currentThread().setName("JUnit-" + methodName.getMethodName());
6359
}
@@ -70,4 +66,11 @@ protected int getTestTimeoutMillis() {
7066
return AzureTestConstants.AZURE_TEST_TIMEOUT;
7167
}
7268

69+
public static void assumeNotNull(Object objects) {
70+
assumeTrue(objects != null);
71+
}
72+
73+
public static void assumeNotNull(Object objects, String message) {
74+
assumeTrue(objects != null, message);
75+
}
7376
}

hadoop-tools/hadoop-azure/src/test/java/org/apache/hadoop/fs/azure/AzureBlobStorageTestAccount.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import com.microsoft.azure.storage.*;
2222
import com.microsoft.azure.storage.blob.*;
2323
import com.microsoft.azure.storage.core.Base64;
24-
import org.junit.Assert;
2524
import org.slf4j.Logger;
2625
import org.slf4j.LoggerFactory;
2726

@@ -50,6 +49,7 @@
5049
import static org.apache.hadoop.fs.azure.AzureNativeFileSystemStore.KEY_USE_LOCAL_SAS_KEY_MODE;
5150
import static org.apache.hadoop.fs.azure.AzureNativeFileSystemStore.KEY_USE_SECURE_MODE;
5251
import static org.apache.hadoop.fs.azure.integration.AzureTestUtils.verifyWasbAccountNameInConfig;
52+
import static org.junit.jupiter.api.Assertions.assertNotNull;
5353

5454
/**
5555
* Helper class to create WASB file systems backed by either a mock in-memory
@@ -212,9 +212,9 @@ public Number getLatestMetricValue(String metricName, Number defaultValue)
212212
* @return
213213
*/
214214
private boolean wasGeneratedByMe(MetricsRecord currentRecord) {
215-
Assert.assertNotNull("null filesystem", fs);
216-
Assert.assertNotNull("null filesystemn instance ID",
217-
fs.getInstrumentation().getFileSystemInstanceId());
215+
assertNotNull(fs, "null filesystem");
216+
assertNotNull(fs.getInstrumentation().getFileSystemInstanceId(),
217+
"null filesystemn instance ID");
218218
String myFsId = fs.getInstrumentation().getFileSystemInstanceId().toString();
219219
for (MetricsTag currentTag : currentRecord.tags()) {
220220
if (currentTag.name().equalsIgnoreCase("wasbFileSystemId")) {

hadoop-tools/hadoop-azure/src/test/java/org/apache/hadoop/fs/azure/ITestAzureConcurrentOutOfBandIo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import java.io.OutputStream;
2424
import java.util.Arrays;
2525

26-
import org.junit.Test;
26+
import org.junit.jupiter.api.Test;
2727
import org.slf4j.Logger;
2828
import org.slf4j.LoggerFactory;
2929

hadoop-tools/hadoop-azure/src/test/java/org/apache/hadoop/fs/azure/ITestAzureFileSystemErrorConditions.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import com.microsoft.azure.storage.OperationContext;
3131
import com.microsoft.azure.storage.SendingRequestEvent;
3232
import com.microsoft.azure.storage.StorageEvent;
33-
import org.junit.Test;
33+
import org.junit.jupiter.api.Test;
3434

3535
import org.apache.hadoop.conf.Configuration;
3636
import org.apache.hadoop.fs.FileStatus;
@@ -41,7 +41,6 @@
4141

4242
import static org.apache.hadoop.fs.azure.AzureNativeFileSystemStore.NO_ACCESS_TO_CONTAINER_MSG;
4343
import static org.apache.hadoop.test.LambdaTestUtils.intercept;
44-
import static org.junit.Assume.assumeNotNull;
4544

4645
/**
4746
* Error handling.
@@ -76,7 +75,7 @@ public void testAccessUnauthorizedPublicContainer() throws Exception {
7675
try {
7776
FileSystem.get(noAccessPath.toUri(), new Configuration())
7877
.open(noAccessPath);
79-
assertTrue("Should've thrown.", false);
78+
assertTrue(false, "Should've thrown.");
8079
} catch (AzureException ex) {
8180
GenericTestUtils.assertExceptionContains(
8281
String.format(NO_ACCESS_TO_CONTAINER_MSG, account, container), ex);

hadoop-tools/hadoop-azure/src/test/java/org/apache/hadoop/fs/azure/ITestBlobDataValidation.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import static org.apache.hadoop.fs.azure.AzureNativeFileSystemStore.KEY_CHECK_BLOCK_MD5;
2222
import static org.apache.hadoop.fs.azure.AzureNativeFileSystemStore.KEY_STORE_BLOB_MD5;
23-
import static org.junit.Assume.assumeNotNull;
2423

2524
import java.io.ByteArrayInputStream;
2625
import java.io.IOException;
@@ -35,8 +34,8 @@
3534
import org.apache.hadoop.fs.azure.AzureNativeFileSystemStore.TestHookOperationContext;
3635
import org.apache.hadoop.fs.azure.integration.AzureTestUtils;
3736

38-
import org.junit.After;
39-
import org.junit.Test;
37+
import org.junit.jupiter.api.AfterEach;
38+
import org.junit.jupiter.api.Test;
4039

4140
import com.microsoft.azure.storage.Constants;
4241
import com.microsoft.azure.storage.OperationContext;
@@ -56,7 +55,7 @@
5655
public class ITestBlobDataValidation extends AbstractWasbTestWithTimeout {
5756
private AzureBlobStorageTestAccount testAccount;
5857

59-
@After
58+
@AfterEach
6059
public void tearDown() throws Exception {
6160
testAccount = AzureTestUtils.cleanupTestAccount(testAccount);
6261
}
@@ -95,8 +94,7 @@ private void testStoreBlobMd5(boolean expectMd5Stored) throws Exception {
9594
assumeNotNull(testAccount);
9695
// Write a test file.
9796
NativeAzureFileSystem fs = testAccount.getFileSystem();
98-
Path testFilePath = AzureTestUtils.pathForTests(fs,
99-
methodName.getMethodName());
97+
Path testFilePath = AzureTestUtils.pathForTests(fs, methodName.getMethodName());
10098
String testFileKey = trim(testFilePath.toUri().getPath(), "/");
10199
OutputStream outStream = fs.create(testFilePath);
102100
outStream.write(new byte[] { 5, 15 });
@@ -109,7 +107,7 @@ private void testStoreBlobMd5(boolean expectMd5Stored) throws Exception {
109107
if (expectMd5Stored) {
110108
assertNotNull(obtainedMd5);
111109
} else {
112-
assertNull("Expected no MD5, found: " + obtainedMd5, obtainedMd5);
110+
assertNull(obtainedMd5, "Expected no MD5, found: " + obtainedMd5);
113111
}
114112

115113
// Mess with the content so it doesn't match the MD5.
@@ -137,8 +135,8 @@ private void testStoreBlobMd5(boolean expectMd5Stored) throws Exception {
137135
}
138136
StorageException cause = (StorageException)ex.getCause();
139137
assertNotNull(cause);
140-
assertEquals("Unexpected cause: " + cause,
141-
StorageErrorCodeStrings.INVALID_MD5, cause.getErrorCode());
138+
assertEquals(StorageErrorCodeStrings.INVALID_MD5, cause.getErrorCode(),
139+
"Unexpected cause: " + cause);
142140
}
143141
}
144142

@@ -192,7 +190,7 @@ private void checkObtainedMd5(String obtainedMd5) {
192190
if (expectMd5) {
193191
assertNotNull(obtainedMd5);
194192
} else {
195-
assertNull("Expected no MD5, found: " + obtainedMd5, obtainedMd5);
193+
assertNull(obtainedMd5, "Expected no MD5, found: " + obtainedMd5);
196194
}
197195
}
198196

hadoop-tools/hadoop-azure/src/test/java/org/apache/hadoop/fs/azure/ITestBlobTypeSpeedDifference.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import java.util.Arrays;
2424
import java.util.Date;
2525

26-
import org.junit.Test;
26+
import org.junit.jupiter.api.Test;
2727

2828
import org.apache.hadoop.conf.Configuration;
2929
import org.apache.hadoop.fs.FileSystem;

0 commit comments

Comments
 (0)