From aee1cd31cb8bbfc90aff4e2fbc95cf978c13371d Mon Sep 17 00:00:00 2001 From: "sherlock.lin" <1193179897@qq.com> Date: Sat, 11 May 2024 15:55:51 +0800 Subject: [PATCH] Upgrading Unit Tests to Junit 5 --- .../bookkeeper/client/BKExceptionTest.java | 40 +++++++++---------- .../BookieAddressResolverDisabledTest.java | 19 +++++---- .../client/BookieDecommissionTest.java | 14 +++---- .../BookieNetworkAddressChangeTest.java | 10 ++--- .../bookkeeper/client/BookieRecoveryTest.java | 36 ++++++++--------- .../client/BookieRecoveryUseIOThreadTest.java | 13 ++---- 6 files changed, 65 insertions(+), 67 deletions(-) diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/BKExceptionTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/BKExceptionTest.java index fb293924d3f..0d74fb62809 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/BKExceptionTest.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/BKExceptionTest.java @@ -20,47 +20,47 @@ */ package org.apache.bookkeeper.client; +import static org.junit.jupiter.api.Assertions.assertEquals; + import java.util.concurrent.CompletionException; import java.util.concurrent.ExecutionException; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * Test for extracting codes from BKException. */ public class BKExceptionTest { + @Test public void testBKExceptionCode() { - Assert.assertEquals(BKException.Code.WriteException, - BKException.getExceptionCode(new BKException.BKWriteException(), - BKException.Code.ReadException)); + assertEquals(BKException.Code.WriteException, + BKException.getExceptionCode(new BKException.BKWriteException(), + BKException.Code.ReadException)); } @Test public void testNonBKExceptionCode() { - Assert.assertEquals(BKException.Code.ReadException, - BKException.getExceptionCode(new Exception(), - BKException.Code.ReadException)); - + assertEquals(BKException.Code.ReadException, + BKException.getExceptionCode(new Exception(), + BKException.Code.ReadException)); } @Test public void testNestedBKExceptionCode() { - Assert.assertEquals(BKException.Code.WriteException, - BKException.getExceptionCode( - new ExecutionException("test", new BKException.BKWriteException()), - BKException.Code.ReadException)); + assertEquals(BKException.Code.WriteException, + BKException.getExceptionCode( + new ExecutionException("test", new BKException.BKWriteException()), + BKException.Code.ReadException)); } @Test public void testDoubleNestedBKExceptionCode() { - Assert.assertEquals(BKException.Code.WriteException, - BKException.getExceptionCode( - new ExecutionException("test", - new CompletionException("blah", - new BKException.BKWriteException())), - BKException.Code.ReadException)); - + assertEquals(BKException.Code.WriteException, + BKException.getExceptionCode( + new ExecutionException("test", + new CompletionException("blah", + new BKException.BKWriteException())), + BKException.Code.ReadException)); } } diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/BookieAddressResolverDisabledTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/BookieAddressResolverDisabledTest.java index 611e3e9674f..fdf608b9a14 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/BookieAddressResolverDisabledTest.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/BookieAddressResolverDisabledTest.java @@ -20,11 +20,14 @@ */ package org.apache.bookkeeper.client; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + import org.apache.bookkeeper.net.BookieId; import org.apache.bookkeeper.net.BookieSocketAddress; import org.apache.bookkeeper.proto.BookieAddressResolver; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * Unit test of {@link BookieAddressResolverDisabled}. @@ -36,18 +39,18 @@ public void testResolve() { BookieAddressResolver resolver = new BookieAddressResolverDisabled(); BookieSocketAddress addr1 = resolver.resolve(BookieId.parse("127.0.0.1:3181")); - Assert.assertEquals("127.0.0.1", addr1.getHostName()); - Assert.assertEquals(3181, addr1.getPort()); + assertEquals("127.0.0.1", addr1.getHostName()); + assertEquals(3181, addr1.getPort()); BookieSocketAddress addr2 = resolver.resolve(BookieId.parse("localhost:3182")); - Assert.assertEquals("localhost", addr2.getHostName()); - Assert.assertEquals(3182, addr2.getPort()); + assertEquals("localhost", addr2.getHostName()); + assertEquals(3182, addr2.getPort()); try { resolver.resolve(BookieId.parse("foobar")); - Assert.fail("Non-legacy style bookie id should fail to resolve address"); + fail("Non-legacy style bookie id should fail to resolve address"); } catch (Exception e) { - Assert.assertTrue(e instanceof BookieAddressResolver.BookieIdNotResolvedException); + assertTrue(e instanceof BookieAddressResolver.BookieIdNotResolvedException); } } diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/BookieDecommissionTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/BookieDecommissionTest.java index ffd96ed155b..8b87b5913bf 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/BookieDecommissionTest.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/BookieDecommissionTest.java @@ -18,9 +18,9 @@ */ package org.apache.bookkeeper.client; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.fail; import java.util.Iterator; import java.util.LinkedList; @@ -35,7 +35,7 @@ import org.apache.bookkeeper.meta.ZkLedgerUnderreplicationManager; import org.apache.bookkeeper.net.BookieId; import org.apache.bookkeeper.test.BookKeeperClusterTestCase; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * Unit test of bookie decommission operations. @@ -149,7 +149,7 @@ public void testDecommissionForLedgersWithMultipleSegmentsAndNotWriteClosed() th startNewBookie(); - assertEquals("Number of Available Bookies", NUM_OF_BOOKIES + 1, bkAdmin.getAvailableBookies().size()); + assertEquals(NUM_OF_BOOKIES + 1, bkAdmin.getAvailableBookies().size(), "Number of Available Bookies"); BookieId killedBookieId = getBookie(0); log.warn("Killing bookie {}", killedBookieId); @@ -224,12 +224,12 @@ public void testDecommissionForEmptyLedgers() throws Exception { startNewBookie(); - assertEquals("Number of Available Bookies", NUM_OF_BOOKIES + 1, bkAdmin.getAvailableBookies().size()); + assertEquals(NUM_OF_BOOKIES + 1, bkAdmin.getAvailableBookies().size(), "Number of Available Bookies"); BookieId killedBookieId = getBookie(0); log.warn("Killing bookie {}", killedBookieId); killBookie(0); - assertEquals("Number of Available Bookies", NUM_OF_BOOKIES, bkAdmin.getAvailableBookies().size()); + assertEquals(NUM_OF_BOOKIES, bkAdmin.getAvailableBookies().size(), "Number of Available Bookies"); bkAdmin.decommissionBookie(killedBookieId); bkAdmin.triggerAudit(); diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/BookieNetworkAddressChangeTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/BookieNetworkAddressChangeTest.java index 4bf8287c677..0a6a9869201 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/BookieNetworkAddressChangeTest.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/BookieNetworkAddressChangeTest.java @@ -20,8 +20,8 @@ */ package org.apache.bookkeeper.client; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; import lombok.extern.slf4j.Slf4j; import org.apache.bookkeeper.client.BKException.BKBookieHandleNotAvailableException; @@ -32,8 +32,8 @@ import org.apache.bookkeeper.conf.ClientConfiguration; import org.apache.bookkeeper.discover.ZKRegistrationClient; import org.apache.bookkeeper.test.BookKeeperClusterTestCase; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; /** * Tests of the main BookKeeper client and the BP-41 bookieAddressTracking feature. @@ -85,7 +85,7 @@ public void testFollowBookieAddressChange() throws Exception { } @Test - @Ignore("PLSR-1850 Seems like restart of the bookie always comes up on same port hence failing this test") + @Disabled("PLSR-1850 Seems like restart of the bookie always comes up on same port hence failing this test") public void testFollowBookieAddressChangeTrckingDisabled() throws Exception { ClientConfiguration conf = new ClientConfiguration(); conf.setMetadataServiceUri(zkUtil.getMetadataServiceUri()); diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/BookieRecoveryTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/BookieRecoveryTest.java index 2b0c40b23a0..8b0ca9b529a 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/BookieRecoveryTest.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/BookieRecoveryTest.java @@ -20,9 +20,9 @@ */ package org.apache.bookkeeper.client; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; import io.netty.buffer.ByteBuf; import java.io.IOException; @@ -46,9 +46,9 @@ import org.apache.bookkeeper.proto.BookieProtocol; import org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.ReadEntryCallback; import org.apache.bookkeeper.test.BookKeeperClusterTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -104,7 +104,7 @@ public BookieRecoveryTest() { baseClientConf.setLedgerManagerFactoryClassName(ledgerManagerFactory); } - @Before + @BeforeEach @Override public void setUp() throws Exception { // Set up the configuration properties needed. @@ -119,7 +119,7 @@ public void setUp() throws Exception { bkAdmin = new BookKeeperAdmin(adminConf); } - @After + @AfterEach @Override public void tearDown() throws Exception { // Release any resources used by the BookieRecoveryTest instance. @@ -279,7 +279,7 @@ void metadataConflictWithRecovery(BookKeeper bkc) throws Exception { startAndAddBookie(confOfKilledBookie); // all ensembles should be fully replicated since it is recovered - assertTrue("Not fully replicated", verifyFullyReplicated(lh, 3 * numEntries)); + assertTrue(verifyFullyReplicated(lh, 3 * numEntries), "Not fully replicated"); lh.close(); } @@ -607,7 +607,7 @@ public void testBookieRecoveryOnClosedLedgers() throws Exception { bkAdmin.recoverBookieData(bookieToKill); for (LedgerHandle lh : lhs) { - assertTrue("Not fully replicated", verifyFullyReplicated(lh, numMsgs)); + assertTrue(verifyFullyReplicated(lh, numMsgs), "Not fully replicated"); lh.close(); } } @@ -637,7 +637,7 @@ public void testBookieRecoveryOnOpenedLedgers() throws Exception { bkAdmin.recoverBookieData(bookieToKill); for (LedgerHandle lh : lhs) { - assertTrue("Not fully replicated", verifyFullyReplicated(lh, numMsgs)); + assertTrue(verifyFullyReplicated(lh, numMsgs), "Not fully replicated"); } try { @@ -694,7 +694,7 @@ public void testBookieRecoveryOnInRecoveryLedger() throws Exception { bkAdmin.recoverBookieData(bookieToKill); for (LedgerHandle lh : lhs) { - assertTrue("Not fully replicated", verifyFullyReplicated(lh, numMsgs)); + assertTrue(verifyFullyReplicated(lh, numMsgs), "Not fully replicated"); } // open ledgers to read metadata @@ -770,13 +770,13 @@ public void testSyncBookieRecoveryToRandomBookiesCheckForDupes() throws Exceptio sync.value = false; bkAdmin.recoverBookieData(bookieSrc); - assertFalse("Dupes exist in ensembles", findDupesInEnsembles(lhs)); + assertFalse(findDupesInEnsembles(lhs), "Dupes exist in ensembles"); // Write some more entries to ensure fencing hasn't broken stuff writeEntriestoLedgers(numMsgs, numMsgs * 2, lhs); for (LedgerHandle lh : lhs) { - assertTrue("Not fully replicated", verifyFullyReplicated(lh, numMsgs * 3)); + assertTrue(verifyFullyReplicated(lh, numMsgs * 3), "Not fully replicated"); lh.close(); } } @@ -801,7 +801,7 @@ public void recoverWithoutPasswordInConf() throws Exception { // Check that entries are missing lh = bkc.openLedgerNoRecovery(ledgerId, digestCorrect, passwdCorrect); - assertFalse("Should be entries missing", verifyFullyReplicated(lh, 100)); + assertFalse(verifyFullyReplicated(lh, 100), "Should be entries missing"); lh.close(); // Try to recover with bad password in conf @@ -818,7 +818,7 @@ public void recoverWithoutPasswordInConf() throws Exception { bka.close(); lh = bkc.openLedgerNoRecovery(ledgerId, digestCorrect, passwdCorrect); - assertTrue("Should be back to fully replication", verifyFullyReplicated(lh, 100)); + assertTrue(verifyFullyReplicated(lh, 100), "Should be back to fully replication"); lh.close(); bookieSrc = addressByIndex(0); @@ -827,7 +827,7 @@ public void recoverWithoutPasswordInConf() throws Exception { // Check that entries are missing lh = bkc.openLedgerNoRecovery(ledgerId, digestCorrect, passwdCorrect); - assertFalse("Should be entries missing", verifyFullyReplicated(lh, 100)); + assertFalse(verifyFullyReplicated(lh, 100), "Should be entries missing"); lh.close(); // Try to recover with no password in conf @@ -840,7 +840,7 @@ public void recoverWithoutPasswordInConf() throws Exception { bka.close(); lh = bkc.openLedgerNoRecovery(ledgerId, digestCorrect, passwdCorrect); - assertTrue("Should be back to fully replication", verifyFullyReplicated(lh, 100)); + assertTrue(verifyFullyReplicated(lh, 100), "Should be back to fully replication"); lh.close(); } diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/BookieRecoveryUseIOThreadTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/BookieRecoveryUseIOThreadTest.java index 3bf87f4cf17..3515c7699f9 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/BookieRecoveryUseIOThreadTest.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/BookieRecoveryUseIOThreadTest.java @@ -18,14 +18,14 @@ */ package org.apache.bookkeeper.client; +import static org.junit.jupiter.api.Assertions.assertEquals; + import java.io.IOException; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicInteger; import org.apache.bookkeeper.conf.ClientConfiguration; import org.apache.bookkeeper.test.BookKeeperClusterTestCase; -import org.junit.Assert; -import org.junit.Test; - +import org.junit.jupiter.api.Test; /** * Tests for Bookie recovery use IO threads. */ @@ -33,14 +33,9 @@ public class BookieRecoveryUseIOThreadTest extends BookKeeperClusterTestCase { public BookieRecoveryUseIOThreadTest() { super(1); - } - - @Override - public void setUp() throws Exception { baseConf.setNumAddWorkerThreads(0); baseConf.setNumReadWorkerThreads(0); baseConf.setNumHighPriorityWorkerThreads(0); - super.setUp(); } @Test @@ -75,6 +70,6 @@ public void openComplete(int rc, LedgerHandle lh, Object ctx) { }, null); latch.await(); } - Assert.assertEquals(finalRc.get(), org.apache.bookkeeper.client.api.BKException.Code.OK); + assertEquals(finalRc.get(), org.apache.bookkeeper.client.api.BKException.Code.OK); } }