From 859af11924daf6deecf516f9fc3361dfbf5f5e77 Mon Sep 17 00:00:00 2001 From: "sherlock.lin" <1193179897@qq.com> Date: Fri, 17 May 2024 15:52:44 +0800 Subject: [PATCH 1/2] migrate client module bookie tests to junit 5 --- .../client/MockBookKeeperTestCase.java | 4 + .../client/api/BKExceptionTest.java | 8 +- .../client/api/BookKeeperApiTest.java | 190 ++++++------ .../api/BookKeeperBuildersOpenLedgerTest.java | 19 +- .../client/api/BookKeeperBuildersTest.java | 279 ++++++++++-------- .../ExplicitLACWithWriteHandleAPITest.java | 2 +- .../client/api/LedgerMetadataTest.java | 12 +- .../client/api/WriteAdvHandleTest.java | 16 +- .../bookkeeper/client/api/WriteFlagTest.java | 12 +- .../client/api/WriteHandleTest.java | 18 +- .../extension/TestContextExtension.java | 39 +++ .../client/impl/LedgerEntriesImplTest.java | 12 +- .../client/impl/LedgerEntryImplTest.java | 10 +- 13 files changed, 360 insertions(+), 261 deletions(-) create mode 100644 bookkeeper-server/src/test/java/org/apache/bookkeeper/client/extension/TestContextExtension.java diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/MockBookKeeperTestCase.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/MockBookKeeperTestCase.java index e1881d74771..6004f6fcb91 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/MockBookKeeperTestCase.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/MockBookKeeperTestCase.java @@ -77,6 +77,8 @@ import org.apache.bookkeeper.versioning.Versioned; import org.junit.After; import org.junit.Before; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; import org.mockito.stubbing.Stubber; @@ -141,6 +143,7 @@ public MockEntry(byte[] payload, long lastAddConfirmed) { } + @BeforeEach @Before public void setup() throws Exception { maxNumberOfAvailableBookies = Integer.MAX_VALUE; @@ -264,6 +267,7 @@ private DigestManager getDigestType(long ledgerId) throws GeneralSecurityExcepti UnpooledByteBufAllocator.DEFAULT, false); } + @AfterEach @After public void tearDown() { scheduler.shutdown(); diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/BKExceptionTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/BKExceptionTest.java index 25f1d8f65c8..ba869e43fa8 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/BKExceptionTest.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/BKExceptionTest.java @@ -16,12 +16,12 @@ package org.apache.bookkeeper.client.api; import static org.apache.bookkeeper.client.api.BKException.Code.UnexpectedConditionException; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.lang.reflect.Field; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * Tests for BKException methods. diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/BookKeeperApiTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/BookKeeperApiTest.java index 5cd71247a9d..2e597c4cb24 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/BookKeeperApiTest.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/BookKeeperApiTest.java @@ -25,12 +25,13 @@ import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.hasProperty; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; import io.netty.buffer.Unpooled; import java.nio.ByteBuffer; @@ -47,7 +48,7 @@ import org.apache.bookkeeper.conf.ClientConfiguration; import org.apache.bookkeeper.util.LoggerOutput; import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.slf4j.event.LoggingEvent; /** @@ -128,41 +129,45 @@ public void testWriteAdvHandleWithFixedLedgerId() throws Exception { } } - @Test(expected = BKDuplicateEntryIdException.class) + @Test public void testWriteAdvHandleBKDuplicateEntryId() throws Exception { - try (WriteAdvHandle writer = result(newCreateLedgerOp() - .withAckQuorumSize(1) - .withWriteQuorumSize(2) - .withEnsembleSize(3) - .withPassword(password) - .makeAdv() - .withLedgerId(1234) - .execute())) { - assertEquals(1234, writer.getId()); - long entryId = 0; - writer.write(entryId++, ByteBuffer.wrap(data)); - assertEquals(data.length, writer.getLength()); - writer.write(entryId - 1, ByteBuffer.wrap(data)); - } + assertThrows(BKDuplicateEntryIdException.class, () -> { + try (WriteAdvHandle writer = result(newCreateLedgerOp() + .withAckQuorumSize(1) + .withWriteQuorumSize(2) + .withEnsembleSize(3) + .withPassword(password) + .makeAdv() + .withLedgerId(1234) + .execute())) { + assertEquals(1234, writer.getId()); + long entryId = 0; + writer.write(entryId++, ByteBuffer.wrap(data)); + assertEquals(data.length, writer.getLength()); + writer.write(entryId - 1, ByteBuffer.wrap(data)); + } + }); } - @Test(expected = BKUnauthorizedAccessException.class) + @Test public void testOpenLedgerUnauthorized() throws Exception { - long lId; - try (WriteHandle writer = result(newCreateLedgerOp() - .withAckQuorumSize(1) - .withWriteQuorumSize(2) - .withEnsembleSize(3) - .withPassword(password) - .execute())) { - lId = writer.getId(); - assertEquals(-1L, writer.getLastAddPushed()); - } - try (ReadHandle ignored = result(newOpenLedgerOp() - .withPassword("bad-password".getBytes(UTF_8)) - .withLedgerId(lId) - .execute())) { - } + assertThrows(BKUnauthorizedAccessException.class, () -> { + long lId; + try (WriteHandle writer = result(newCreateLedgerOp() + .withAckQuorumSize(1) + .withWriteQuorumSize(2) + .withEnsembleSize(3) + .withPassword(password) + .execute())) { + lId = writer.getId(); + assertEquals(-1L, writer.getLastAddPushed()); + } + try (ReadHandle ignored = result(newOpenLedgerOp() + .withPassword("bad-password".getBytes(UTF_8)) + .withLedgerId(lId) + .execute())) { + } + }); } /** @@ -301,75 +306,80 @@ public void testOpenLedgerRead() throws Exception { } } - @Test(expected = BKLedgerFencedException.class) + @Test public void testOpenLedgerWithRecovery() throws Exception { + assertThrows(BKLedgerFencedException.class, () -> { + loggerOutput.expect((List logEvents) -> { + assertThat(logEvents, hasItem(hasProperty("message", + containsString("due to LedgerFencedException: " + + "Ledger has been fenced off. Some other client must have opened it to read") + ))); + }); - loggerOutput.expect((List logEvents) -> { - assertThat(logEvents, hasItem(hasProperty("message", - containsString("due to LedgerFencedException: " - + "Ledger has been fenced off. Some other client must have opened it to read") - ))); - }); - - long lId; - try (WriteHandle writer = result(newCreateLedgerOp() - .withAckQuorumSize(1) - .withWriteQuorumSize(2) - .withEnsembleSize(3) - .withPassword(password) - .execute())) { - lId = writer.getId(); - - writer.append(ByteBuffer.wrap(data)); - writer.append(ByteBuffer.wrap(data)); - assertEquals(1L, writer.getLastAddPushed()); - - // open with fencing - try (ReadHandle reader = result(newOpenLedgerOp() + long lId; + try (WriteHandle writer = result(newCreateLedgerOp() + .withAckQuorumSize(1) + .withWriteQuorumSize(2) + .withEnsembleSize(3) .withPassword(password) - .withRecovery(true) - .withLedgerId(lId) .execute())) { - assertTrue(reader.isClosed()); - assertEquals(1L, reader.getLastAddConfirmed()); - } + lId = writer.getId(); - writer.append(ByteBuffer.wrap(data)); + writer.append(ByteBuffer.wrap(data)); + writer.append(ByteBuffer.wrap(data)); + assertEquals(1L, writer.getLastAddPushed()); + + // open with fencing + try (ReadHandle reader = result(newOpenLedgerOp() + .withPassword(password) + .withRecovery(true) + .withLedgerId(lId) + .execute())) { + assertTrue(reader.isClosed()); + assertEquals(1L, reader.getLastAddConfirmed()); + } - } + writer.append(ByteBuffer.wrap(data)); + + } + }); } - @Test(expected = BKNoSuchLedgerExistsOnMetadataServerException.class) + @Test public void testDeleteLedger() throws Exception { - long lId; + assertThrows(BKNoSuchLedgerExistsOnMetadataServerException.class, () -> { + long lId; - try (WriteHandle writer = result(newCreateLedgerOp() - .withPassword(password) - .execute())) { - lId = writer.getId(); - assertEquals(-1L, writer.getLastAddPushed()); - } + try (WriteHandle writer = result(newCreateLedgerOp() + .withPassword(password) + .execute())) { + lId = writer.getId(); + assertEquals(-1L, writer.getLastAddPushed()); + } - result(newDeleteLedgerOp().withLedgerId(lId).execute()); + result(newDeleteLedgerOp().withLedgerId(lId).execute()); - result(newOpenLedgerOp() - .withPassword(password) - .withLedgerId(lId) - .execute()); + result(newOpenLedgerOp() + .withPassword(password) + .withLedgerId(lId) + .execute()); + }); } - @Test(expected = BKNoSuchLedgerExistsOnMetadataServerException.class) + @Test public void testCannotDeleteLedgerTwice() throws Exception { - long lId; + assertThrows(BKNoSuchLedgerExistsOnMetadataServerException.class, () -> { + long lId; - try (WriteHandle writer = result(newCreateLedgerOp() - .withPassword(password) - .execute())) { - lId = writer.getId(); - assertEquals(-1L, writer.getLastAddPushed()); - } - result(newDeleteLedgerOp().withLedgerId(lId).execute()); - result(newDeleteLedgerOp().withLedgerId(lId).execute()); + try (WriteHandle writer = result(newCreateLedgerOp() + .withPassword(password) + .execute())) { + lId = writer.getId(); + assertEquals(-1L, writer.getLastAddPushed()); + } + result(newDeleteLedgerOp().withLedgerId(lId).execute()); + result(newDeleteLedgerOp().withLedgerId(lId).execute()); + }); } @Test diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/BookKeeperBuildersOpenLedgerTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/BookKeeperBuildersOpenLedgerTest.java index 00e14fe2c12..b2928250751 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/BookKeeperBuildersOpenLedgerTest.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/BookKeeperBuildersOpenLedgerTest.java @@ -38,14 +38,12 @@ import org.apache.bookkeeper.net.BookieId; import org.apache.bookkeeper.proto.BookieProtocol; import org.apache.bookkeeper.proto.BookkeeperInternalCallbacks; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; /** * Tests for BookKeeper open ledger operations. */ -@RunWith(Parameterized.class) public class BookKeeperBuildersOpenLedgerTest extends MockBookKeeperTestCase { private static final int ensembleSize = 3; @@ -58,11 +56,10 @@ public class BookKeeperBuildersOpenLedgerTest extends MockBookKeeperTestCase { private boolean withRecovery; - public BookKeeperBuildersOpenLedgerTest(boolean withRecovery) { + public void initBookKeeperBuildersOpenLedgerTest(boolean withRecovery) { this.withRecovery = withRecovery; } - @Parameterized.Parameters(name = "withRecovery:({0})") public static Collection data() { return Arrays.asList(new Object[][]{ {true}, @@ -70,8 +67,9 @@ public static Collection data() { }); } - @Test - public void testOpenLedger() throws Exception { + @MethodSource("data") + @ParameterizedTest(name = "withRecovery:({0})") + public void testOpenLedger(boolean withRecovery) throws Exception { LedgerMetadata ledgerMetadata = generateLedgerMetadata(ensembleSize, writeQuorumSize, ackQuorumSize, password, customMetadata); registerMockLedgerMetadata(ledgerId, ledgerMetadata); @@ -91,8 +89,9 @@ public void testOpenLedger() throws Exception { .execute()); } - @Test - public void testOpenLedgerWithTimeoutEx() throws Exception { + @MethodSource("data") + @ParameterizedTest(name = "withRecovery:({0})") + public void testOpenLedgerWithTimeoutEx(boolean withRecovery) throws Exception { mockReadEntryTimeout(); LedgerMetadata ledgerMetadata = generateLedgerMetadata(ensembleSize, writeQuorumSize, ackQuorumSize, password, customMetadata); diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/BookKeeperBuildersTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/BookKeeperBuildersTest.java index 478a3ef208f..d50c964ce69 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/BookKeeperBuildersTest.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/BookKeeperBuildersTest.java @@ -22,9 +22,10 @@ import static org.apache.bookkeeper.client.api.WriteFlag.DEFERRED_SYNC; import static org.apache.bookkeeper.common.concurrent.FutureUtils.result; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.fail; import java.util.EnumSet; import java.util.HashMap; @@ -38,7 +39,7 @@ import org.apache.bookkeeper.client.LedgerMetadataBuilder; import org.apache.bookkeeper.client.MockBookKeeperTestCase; import org.apache.bookkeeper.conf.ClientConfiguration; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * Unit tests of builders. @@ -73,112 +74,138 @@ public void testCreateLedger() throws Exception { assertArrayEquals(password, metadata.getPassword()); } - @Test(expected = BKIncorrectParameterException.class) + @Test public void testFailEnsembleSize0() throws Exception { - result(newCreateLedgerOp() - .withEnsembleSize(0) - .withPassword(password) - .execute()); + assertThrows(BKIncorrectParameterException.class, () -> { + result(newCreateLedgerOp() + .withEnsembleSize(0) + .withPassword(password) + .execute()); + }); } - @Test(expected = BKIncorrectParameterException.class) + @Test public void testFailWriteQuorumSize0() throws Exception { - result(newCreateLedgerOp() - .withEnsembleSize(2) - .withWriteQuorumSize(0) - .withPassword(password) - .execute()); + assertThrows(BKIncorrectParameterException.class, () -> { + result(newCreateLedgerOp() + .withEnsembleSize(2) + .withWriteQuorumSize(0) + .withPassword(password) + .execute()); + }); } - @Test(expected = BKIncorrectParameterException.class) + @Test public void testFailNullWriteFlags() throws Exception { - result(newCreateLedgerOp() - .withWriteFlags((EnumSet) null) - .withPassword(password) - .execute()); + assertThrows(BKIncorrectParameterException.class, () -> { + result(newCreateLedgerOp() + .withWriteFlags((EnumSet) null) + .withPassword(password) + .execute()); + }); } - @Test(expected = BKIncorrectParameterException.class) + @Test public void testFailAckQuorumSize0() throws Exception { - result(newCreateLedgerOp() - .withEnsembleSize(2) - .withWriteQuorumSize(1) - .withAckQuorumSize(0) - .withPassword(password) - .execute()); + assertThrows(BKIncorrectParameterException.class, () -> { + result(newCreateLedgerOp() + .withEnsembleSize(2) + .withWriteQuorumSize(1) + .withAckQuorumSize(0) + .withPassword(password) + .execute()); + }); } - @Test(expected = BKIncorrectParameterException.class) + @Test public void testFailWriteQuorumSizeGreaterThanEnsembleSize() throws Exception { - result(newCreateLedgerOp() - .withEnsembleSize(1) - .withWriteQuorumSize(2) - .withAckQuorumSize(1) - .withPassword(password) - .execute()); + assertThrows(BKIncorrectParameterException.class, () -> { + result(newCreateLedgerOp() + .withEnsembleSize(1) + .withWriteQuorumSize(2) + .withAckQuorumSize(1) + .withPassword(password) + .execute()); + }); } - @Test(expected = BKIncorrectParameterException.class) + @Test public void testFailAckQuorumSizeGreaterThanWriteQuorumSize() throws Exception { - result(newCreateLedgerOp() - .withEnsembleSize(1) - .withWriteQuorumSize(1) - .withAckQuorumSize(2) - .withPassword(password) - .execute()); + assertThrows(BKIncorrectParameterException.class, () -> { + result(newCreateLedgerOp() + .withEnsembleSize(1) + .withWriteQuorumSize(1) + .withAckQuorumSize(2) + .withPassword(password) + .execute()); + }); } - @Test(expected = BKIncorrectParameterException.class) + @Test public void testFailNoPassword() throws Exception { - result(newCreateLedgerOp() - .execute()); + assertThrows(BKIncorrectParameterException.class, () -> { + result(newCreateLedgerOp() + .execute()); + }); } - @Test(expected = BKIncorrectParameterException.class) + @Test public void testFailPasswordNull() throws Exception { - result(newCreateLedgerOp() - .withPassword(null) - .execute()); + assertThrows(BKIncorrectParameterException.class, () -> { + result(newCreateLedgerOp() + .withPassword(null) + .execute()); + }); } - @Test(expected = BKIncorrectParameterException.class) + @Test public void testFailCustomMetadataNull() throws Exception { - result(newCreateLedgerOp() - .withCustomMetadata(null) - .withPassword(password) - .execute()); + assertThrows(BKIncorrectParameterException.class, () -> { + result(newCreateLedgerOp() + .withCustomMetadata(null) + .withPassword(password) + .execute()); + }); } - @Test(expected = BKIncorrectParameterException.class) + @Test public void testFailDigestTypeNullAndAutodetectionTrue() throws Exception { ClientConfiguration config = new ClientConfiguration(); config.setEnableDigestTypeAutodetection(true); setBookKeeperConfig(config); - result(newCreateLedgerOp() - .withDigestType(null) - .withPassword(password) - .execute()); + + assertThrows(BKIncorrectParameterException.class, () -> { + result(newCreateLedgerOp() + .withDigestType(null) + .withPassword(password) + .execute()); + }); } - @Test(expected = BKIncorrectParameterException.class) + @Test public void testFailDigestTypeNullAndAutodetectionFalse() throws Exception { ClientConfiguration config = new ClientConfiguration(); config.setEnableDigestTypeAutodetection(false); setBookKeeperConfig(config); - result(newCreateLedgerOp() - .withDigestType(null) - .withPassword(password) - .execute()); - fail("shoud not be able to create a ledger with such specs"); + + assertThrows(BKIncorrectParameterException.class, () -> { + result(newCreateLedgerOp() + .withDigestType(null) + .withPassword(password) + .execute()); + fail("shoud not be able to create a ledger with such specs"); + }); } - @Test(expected = BKClientClosedException.class) + @Test public void testFailDigestTypeNullAndBookkKeeperClosed() throws Exception { - closeBookkeeper(); - result(newCreateLedgerOp() - .withPassword(password) - .execute()); - fail("shoud not be able to create a ledger, client is closed"); + assertThrows(BKClientClosedException.class, () -> { + closeBookkeeper(); + result(newCreateLedgerOp() + .withPassword(password) + .execute()); + fail("shoud not be able to create a ledger, client is closed"); + }); } @Test @@ -289,58 +316,72 @@ public void testCreateLedgerWriteFlagsVarargs() throws Exception { assertEquals(writeFlagsDeferredSync, lh.getWriteFlags()); } - @Test(expected = BKIncorrectParameterException.class) + @Test public void testFailCreateAdvLedgerBadFixedLedgerIdMinus1() throws Exception { - result(newCreateLedgerOp() - .withPassword(password) - .makeAdv() - .withLedgerId(-1) - .execute()); + assertThrows(BKIncorrectParameterException.class, () -> { + result(newCreateLedgerOp() + .withPassword(password) + .makeAdv() + .withLedgerId(-1) + .execute()); + }); } - @Test(expected = BKIncorrectParameterException.class) + @Test public void testFailCreateAdvLedgerBadFixedLedgerIdNegative() throws Exception { - result(newCreateLedgerOp() - .withPassword(password) - .makeAdv() - .withLedgerId(-2) - .execute()); - fail("shoud not be able to create a ledger with such specs"); + assertThrows(BKIncorrectParameterException.class, () -> { + result(newCreateLedgerOp() + .withPassword(password) + .makeAdv() + .withLedgerId(-2) + .execute()); + fail("shoud not be able to create a ledger with such specs"); + }); } - @Test(expected = BKNoSuchLedgerExistsOnMetadataServerException.class) + @Test public void testOpenLedgerNoId() throws Exception { - result(newOpenLedgerOp().execute()); + assertThrows(BKNoSuchLedgerExistsOnMetadataServerException.class, () -> { + result(newOpenLedgerOp().execute()); + }); } - @Test(expected = BKNoSuchLedgerExistsOnMetadataServerException.class) + @Test public void testOpenLedgerBadId() throws Exception { - result(newOpenLedgerOp() - .withPassword(password) - .withLedgerId(ledgerId) - .execute()); + assertThrows(BKNoSuchLedgerExistsOnMetadataServerException.class, () -> { + result(newOpenLedgerOp() + .withPassword(password) + .withLedgerId(ledgerId) + .execute()); + }); } - @Test(expected = BKClientClosedException.class) + @Test public void testOpenLedgerClientClosed() throws Exception { - closeBookkeeper(); - result(newOpenLedgerOp() - .withPassword(password) - .withLedgerId(ledgerId) - .execute()); + assertThrows(BKClientClosedException.class, () -> { + closeBookkeeper(); + result(newOpenLedgerOp() + .withPassword(password) + .withLedgerId(ledgerId) + .execute()); + }); } - @Test(expected = BKIncorrectParameterException.class) + @Test public void testDeleteLedgerNoLedgerId() throws Exception { - result(newDeleteLedgerOp() - .execute()); + assertThrows(BKIncorrectParameterException.class, () -> { + result(newDeleteLedgerOp() + .execute()); + }); } - @Test(expected = BKIncorrectParameterException.class) + @Test public void testDeleteLedgerBadLedgerId() throws Exception { - result(newDeleteLedgerOp() - .withLedgerId(-1) - .execute()); + assertThrows(BKIncorrectParameterException.class, () -> { + result(newDeleteLedgerOp() + .withLedgerId(-1) + .execute()); + }); } @Test @@ -354,12 +395,14 @@ public void testDeleteLedger() throws Exception { .execute()); } - @Test(expected = BKClientClosedException.class) + @Test public void testDeleteLedgerBookKeeperClosed() throws Exception { - closeBookkeeper(); - result(newDeleteLedgerOp() - .withLedgerId(ledgerId) - .execute()); + assertThrows(BKClientClosedException.class, () -> { + closeBookkeeper(); + result(newDeleteLedgerOp() + .withLedgerId(ledgerId) + .execute()); + }); } protected LedgerMetadata generateLedgerMetadata(int ensembleSize, @@ -406,22 +449,22 @@ public void testCreateLedgerWithOpportunisticStriping() throws Exception { } - @Test(expected = BKException.BKNotEnoughBookiesException.class) + @Test public void testNotEnoughBookies() throws Exception { - maxNumberOfAvailableBookies = 1; ClientConfiguration config = new ClientConfiguration(); config.setOpportunisticStriping(false); setBookKeeperConfig(config); - setNewGeneratedLedgerId(ledgerId); - result(newCreateLedgerOp() - .withAckQuorumSize(ackQuorumSize) - .withEnsembleSize(ensembleSize) - .withWriteQuorumSize(writeQuorumSize) - .withCustomMetadata(customMetadata) - .withPassword(password) - .execute()); + assertThrows(BKException.BKNotEnoughBookiesException.class, () -> { + result(newCreateLedgerOp() + .withAckQuorumSize(ackQuorumSize) + .withEnsembleSize(ensembleSize) + .withWriteQuorumSize(writeQuorumSize) + .withCustomMetadata(customMetadata) + .withPassword(password) + .execute()); + }); } } diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/ExplicitLACWithWriteHandleAPITest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/ExplicitLACWithWriteHandleAPITest.java index 9917149ce64..4a4d43f6489 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/ExplicitLACWithWriteHandleAPITest.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/ExplicitLACWithWriteHandleAPITest.java @@ -24,7 +24,7 @@ import org.apache.bookkeeper.conf.ClientConfiguration; import org.apache.bookkeeper.test.BookKeeperClusterTestCase; import org.apache.bookkeeper.util.TestUtils; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/LedgerMetadataTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/LedgerMetadataTest.java index 7d7f6c19afc..d7c4c9ba8f3 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/LedgerMetadataTest.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/LedgerMetadataTest.java @@ -18,15 +18,15 @@ */ package org.apache.bookkeeper.client.api; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; import java.util.Iterator; import org.apache.bookkeeper.common.concurrent.FutureUtils; import org.apache.bookkeeper.conf.ClientConfiguration; import org.apache.bookkeeper.test.BookKeeperClusterTestCase; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * Bookkeeper Client API ledger metadata and ledgers listing test. @@ -93,7 +93,7 @@ public void testListLedgers() assertEquals(ledgerIds[count++], ledgerId); } - assertEquals("Unexpected ledgers count", numOfLedgers, count); + assertEquals(numOfLedgers, count, "Unexpected ledgers count"); try { result.iterator(); fail("Should thrown error"); @@ -116,7 +116,7 @@ public void testListLedgers() assertEquals(ledgerIds[count++], ledgerId); } - assertEquals("Unexpected ledgers count", numOfLedgers, count); + assertEquals(numOfLedgers, count, "Unexpected ledgers count"); try { result.iterator(); fail("Should thrown error"); diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/WriteAdvHandleTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/WriteAdvHandleTest.java index 6c469c44e41..e371bd8b381 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/WriteAdvHandleTest.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/WriteAdvHandleTest.java @@ -35,18 +35,18 @@ import io.netty.buffer.ByteBufUtil; import java.nio.ByteBuffer; import java.util.concurrent.LinkedBlockingQueue; +import org.apache.bookkeeper.client.extension.TestContextExtension; import org.apache.bookkeeper.common.concurrent.FutureUtils; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TestName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; /** * Unit test for {@link WriteAdvHandle}. */ public class WriteAdvHandleTest { - @Rule - public final TestName runtime = new TestName(); + @RegisterExtension + TestContextExtension testContextExtension = new TestContextExtension(); private final long entryId; private final WriteAdvHandle handle = mock(WriteAdvHandle.class); @@ -67,7 +67,7 @@ public WriteAdvHandleTest() { @Test public void testAppendBytes() throws Exception { - byte[] testData = runtime.getMethodName().getBytes(UTF_8); + byte[] testData = testContextExtension.getMethodName().getBytes(UTF_8); handle.writeAsync(entryId, testData); ByteBuf buffer = entryQueue.take(); @@ -78,7 +78,7 @@ public void testAppendBytes() throws Exception { @Test public void testAppendBytes2() throws Exception { - byte[] testData = runtime.getMethodName().getBytes(UTF_8); + byte[] testData = testContextExtension.getMethodName().getBytes(UTF_8); handle.writeAsync(entryId, testData, 1, testData.length / 2); byte[] expectedData = new byte[testData.length / 2]; System.arraycopy(testData, 1, expectedData, 0, testData.length / 2); @@ -91,7 +91,7 @@ public void testAppendBytes2() throws Exception { @Test public void testAppendByteBuffer() throws Exception { - byte[] testData = runtime.getMethodName().getBytes(UTF_8); + byte[] testData = testContextExtension.getMethodName().getBytes(UTF_8); handle.writeAsync(entryId, ByteBuffer.wrap(testData, 1, testData.length / 2)); byte[] expectedData = new byte[testData.length / 2]; System.arraycopy(testData, 1, expectedData, 0, testData.length / 2); diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/WriteFlagTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/WriteFlagTest.java index 902bb50a7de..93ffb08d786 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/WriteFlagTest.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/WriteFlagTest.java @@ -21,10 +21,12 @@ package org.apache.bookkeeper.client.api; import static org.apache.bookkeeper.client.api.WriteFlag.DEFERRED_SYNC; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.util.EnumSet; -import org.junit.Test; +import org.junit.jupiter.api.Test; + /** * Unit tests for WriteFlag. @@ -45,9 +47,11 @@ public void testGetWriteFlagsNone() { WriteFlag.getWriteFlags(NONE)); } - @Test(expected = NullPointerException.class) + @Test public void testGetWriteFlagsValueNull() { - WriteFlag.getWriteFlagsValue(null); + assertThrows(NullPointerException.class, () -> { + WriteFlag.getWriteFlagsValue(null); + }); } @Test diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/WriteHandleTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/WriteHandleTest.java index 576d5537715..7fbde4660ba 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/WriteHandleTest.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/WriteHandleTest.java @@ -20,7 +20,7 @@ package org.apache.bookkeeper.client.api; import static java.nio.charset.StandardCharsets.UTF_8; -import static org.junit.Assert.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.Mockito.doAnswer; @@ -34,9 +34,9 @@ import java.nio.ByteBuffer; import java.util.concurrent.LinkedBlockingQueue; import lombok.extern.slf4j.Slf4j; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TestName; +import org.apache.bookkeeper.client.extension.TestContextExtension; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; /** * Unit test for the default methods in {@link WriteHandle}. @@ -44,8 +44,8 @@ @Slf4j public class WriteHandleTest { - @Rule - public final TestName runtime = new TestName(); + @RegisterExtension + TestContextExtension testContextExtension = new TestContextExtension(); private final WriteHandle handle = mock(WriteHandle.class); private final LinkedBlockingQueue entryQueue; @@ -64,7 +64,7 @@ public WriteHandleTest() throws Exception { @Test public void testAppendBytes() throws Exception { - byte[] testData = runtime.getMethodName().getBytes(UTF_8); + byte[] testData = testContextExtension.getMethodName().getBytes(UTF_8); handle.append(testData); ByteBuf buffer = entryQueue.take(); @@ -75,7 +75,7 @@ public void testAppendBytes() throws Exception { @Test public void testAppendBytes2() throws Exception { - byte[] testData = runtime.getMethodName().getBytes(UTF_8); + byte[] testData = testContextExtension.getMethodName().getBytes(UTF_8); handle.append(testData, 1, testData.length / 2); byte[] expectedData = new byte[testData.length / 2]; System.arraycopy(testData, 1, expectedData, 0, testData.length / 2); @@ -88,7 +88,7 @@ public void testAppendBytes2() throws Exception { @Test public void testAppendByteBuffer() throws Exception { - byte[] testData = runtime.getMethodName().getBytes(UTF_8); + byte[] testData = testContextExtension.getMethodName().getBytes(UTF_8); handle.append(ByteBuffer.wrap(testData, 1, testData.length / 2)); byte[] expectedData = new byte[testData.length / 2]; System.arraycopy(testData, 1, expectedData, 0, testData.length / 2); diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/extension/TestContextExtension.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/extension/TestContextExtension.java new file mode 100644 index 00000000000..8569390906f --- /dev/null +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/extension/TestContextExtension.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.bookkeeper.client.extension; + +import org.junit.jupiter.api.extension.BeforeEachCallback; +import org.junit.jupiter.api.extension.ExtensionContext; + +/** + * Unit Test Context Information. + */ +public class TestContextExtension implements BeforeEachCallback { + + private volatile String methodName; + + public String getMethodName() { + return methodName; + } + + @Override + public void beforeEach(ExtensionContext context) throws Exception { + methodName = context.getRequiredTestMethod().getName(); + } +} diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/impl/LedgerEntriesImplTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/impl/LedgerEntriesImplTest.java index ff7117c8e2e..ebf442b7e0a 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/impl/LedgerEntriesImplTest.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/impl/LedgerEntriesImplTest.java @@ -20,9 +20,9 @@ package org.apache.bookkeeper.client.impl; import static java.nio.charset.StandardCharsets.UTF_8; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; import com.google.common.collect.Lists; import io.netty.buffer.ByteBuf; @@ -31,8 +31,8 @@ import java.util.Iterator; import java.util.List; import org.apache.bookkeeper.client.api.LedgerEntry; -import org.junit.After; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; /** * Unit test for {@link LedgerEntriesImpl}. @@ -63,7 +63,7 @@ public LedgerEntriesImplTest () { ledgerEntriesImpl = LedgerEntriesImpl.create(entryList); } - @After + @AfterEach public void tearDown() { ledgerEntriesImpl.close(); diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/impl/LedgerEntryImplTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/impl/LedgerEntryImplTest.java index 2b7cc0f6737..eb10867991a 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/impl/LedgerEntryImplTest.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/impl/LedgerEntryImplTest.java @@ -20,14 +20,14 @@ package org.apache.bookkeeper.client.impl; import static java.nio.charset.StandardCharsets.UTF_8; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import java.nio.ByteBuffer; -import org.junit.After; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; /** * Unit test for {@link LedgerEntryImpl}. @@ -54,7 +54,7 @@ public LedgerEntryImplTest() { dataBuf); } - @After + @AfterEach public void teardown() { this.entryImpl.close(); assertEquals(0, dataBuf.refCnt()); From 84335b9a8ee99fda53c15398d0d41e9deb07b4ea Mon Sep 17 00:00:00 2001 From: "sherlock.lin" <1193179897@qq.com> Date: Thu, 30 May 2024 18:23:45 +0800 Subject: [PATCH 2/2] migrate client module bookie tests to junit 5 --- .../client/api/BookKeeperApiTest.java | 45 +++++++++---------- .../api/BookKeeperBuildersOpenLedgerTest.java | 25 +++-------- .../client/api/BookKeeperBuildersTest.java | 6 +-- .../client/api/WriteAdvHandleTest.java | 19 ++++---- .../client/api/WriteHandleTest.java | 19 ++++---- .../extension/TestContextExtension.java | 39 ---------------- 6 files changed, 52 insertions(+), 101 deletions(-) delete mode 100644 bookkeeper-server/src/test/java/org/apache/bookkeeper/client/extension/TestContextExtension.java diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/BookKeeperApiTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/BookKeeperApiTest.java index 2e597c4cb24..75ba8e30b9b 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/BookKeeperApiTest.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/BookKeeperApiTest.java @@ -63,6 +63,15 @@ public class BookKeeperApiTest extends MockBookKeeperTestCase { @Rule public LoggerOutput loggerOutput = new LoggerOutput(); + private static void checkEntries(LedgerEntries entries, byte[] data) + throws InterruptedException, BKException { + Iterator iterator = entries.iterator(); + while (iterator.hasNext()) { + LedgerEntry entry = iterator.next(); + assertArrayEquals(data, entry.getEntryBytes()); + } + } + @Test public void testWriteHandle() throws Exception { try (WriteHandle writer = result(newCreateLedgerOp() @@ -177,7 +186,7 @@ public void testOpenLedgerUnauthorized() throws Exception { */ @Test public void testLedgerDigests() throws Exception { - for (DigestType type: DigestType.values()) { + for (DigestType type : DigestType.values()) { long lId; try (WriteHandle writer = result(newCreateLedgerOp() .withAckQuorumSize(1) @@ -203,7 +212,6 @@ public void testLedgerDigests() throws Exception { } } - @Test public void testOpenLedgerDigestUnmatchedWhenAutoDetectionEnabled() throws Exception { testOpenLedgerDigestUnmatched(true); @@ -231,10 +239,10 @@ private void testOpenLedgerDigestUnmatched(boolean autodetection) throws Excepti assertEquals(-1L, writer.getLastAddPushed()); } try (ReadHandle ignored = result(newOpenLedgerOp() - .withDigestType(DigestType.CRC32) - .withPassword(password) - .withLedgerId(lId) - .execute())) { + .withDigestType(DigestType.CRC32) + .withPassword(password) + .withLedgerId(lId) + .execute())) { if (!autodetection) { fail("Should fail to open read handle if digest type auto detection is disabled."); } @@ -285,10 +293,10 @@ public void testOpenLedgerRead() throws Exception { } try (ReadHandle reader = result(newOpenLedgerOp() - .withPassword(password) - .withRecovery(false) - .withLedgerId(lId) - .execute())) { + .withPassword(password) + .withRecovery(false) + .withLedgerId(lId) + .execute())) { assertTrue(reader.isClosed()); assertEquals(2, reader.getLastAddConfirmed()); assertEquals(3 * data.length, reader.getLength()); @@ -299,7 +307,7 @@ public void testOpenLedgerRead() throws Exception { // test readLastAddConfirmedAndEntry LastConfirmedAndEntry lastConfirmedAndEntry = - reader.readLastAddConfirmedAndEntry(0, 999, false); + reader.readLastAddConfirmedAndEntry(0, 999, false); assertEquals(2L, lastConfirmedAndEntry.getLastAddConfirmed()); assertArrayEquals(data, lastConfirmedAndEntry.getEntry().getEntryBytes()); lastConfirmedAndEntry.close(); @@ -414,9 +422,9 @@ public void testLedgerEntriesIterable() throws Exception { } i.set(0); entries.forEach((e) -> { - assertEquals(i.getAndIncrement(), e.getEntryId()); - assertArrayEquals(data, e.getEntryBytes()); - }); + assertEquals(i.getAndIncrement(), e.getEntryId()); + assertArrayEquals(data, e.getEntryBytes()); + }); } } } @@ -436,13 +444,4 @@ public void testBKExceptionCodeLogger() { assertEquals("123: Unexpected condition", BKException.codeLogger(123).toString()); assertEquals("-201: Unexpected condition", BKException.codeLogger(-201).toString()); } - - private static void checkEntries(LedgerEntries entries, byte[] data) - throws InterruptedException, BKException { - Iterator iterator = entries.iterator(); - while (iterator.hasNext()) { - LedgerEntry entry = iterator.next(); - assertArrayEquals(data, entry.getEntryBytes()); - } - } } diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/BookKeeperBuildersOpenLedgerTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/BookKeeperBuildersOpenLedgerTest.java index b2928250751..177da38e921 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/BookKeeperBuildersOpenLedgerTest.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/BookKeeperBuildersOpenLedgerTest.java @@ -27,8 +27,6 @@ import static org.mockito.Mockito.anyLong; import static org.mockito.Mockito.doAnswer; -import java.util.Arrays; -import java.util.Collection; import java.util.HashMap; import java.util.Map; import org.apache.bookkeeper.client.BKException; @@ -39,7 +37,7 @@ import org.apache.bookkeeper.proto.BookieProtocol; import org.apache.bookkeeper.proto.BookkeeperInternalCallbacks; import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; /** * Tests for BookKeeper open ledger operations. @@ -54,21 +52,8 @@ public class BookKeeperBuildersOpenLedgerTest extends MockBookKeeperTestCase { private static final byte[] password = new byte[3]; private static final byte[] entryData = new byte[32]; - private boolean withRecovery; - - public void initBookKeeperBuildersOpenLedgerTest(boolean withRecovery) { - this.withRecovery = withRecovery; - } - - public static Collection data() { - return Arrays.asList(new Object[][]{ - {true}, - {false} - }); - } - - @MethodSource("data") - @ParameterizedTest(name = "withRecovery:({0})") + @ParameterizedTest + @ValueSource(booleans = {true, false}) public void testOpenLedger(boolean withRecovery) throws Exception { LedgerMetadata ledgerMetadata = generateLedgerMetadata(ensembleSize, writeQuorumSize, ackQuorumSize, password, customMetadata); @@ -89,8 +74,8 @@ public void testOpenLedger(boolean withRecovery) throws Exception { .execute()); } - @MethodSource("data") - @ParameterizedTest(name = "withRecovery:({0})") + @ParameterizedTest + @ValueSource(booleans = {true, false}) public void testOpenLedgerWithTimeoutEx(boolean withRecovery) throws Exception { mockReadEntryTimeout(); LedgerMetadata ledgerMetadata = generateLedgerMetadata(ensembleSize, diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/BookKeeperBuildersTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/BookKeeperBuildersTest.java index d50c964ce69..c039c1f77db 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/BookKeeperBuildersTest.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/BookKeeperBuildersTest.java @@ -193,7 +193,7 @@ public void testFailDigestTypeNullAndAutodetectionFalse() throws Exception { .withDigestType(null) .withPassword(password) .execute()); - fail("shoud not be able to create a ledger with such specs"); + fail("should not be able to create a ledger with such specs"); }); } @@ -204,7 +204,7 @@ public void testFailDigestTypeNullAndBookkKeeperClosed() throws Exception { result(newCreateLedgerOp() .withPassword(password) .execute()); - fail("shoud not be able to create a ledger, client is closed"); + fail("should not be able to create a ledger, client is closed"); }); } @@ -335,7 +335,7 @@ public void testFailCreateAdvLedgerBadFixedLedgerIdNegative() throws Exception { .makeAdv() .withLedgerId(-2) .execute()); - fail("shoud not be able to create a ledger with such specs"); + fail("should not be able to create a ledger with such specs"); }); } diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/WriteAdvHandleTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/WriteAdvHandleTest.java index e371bd8b381..545cfc5059d 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/WriteAdvHandleTest.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/WriteAdvHandleTest.java @@ -35,22 +35,20 @@ import io.netty.buffer.ByteBufUtil; import java.nio.ByteBuffer; import java.util.concurrent.LinkedBlockingQueue; -import org.apache.bookkeeper.client.extension.TestContextExtension; import org.apache.bookkeeper.common.concurrent.FutureUtils; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.TestInfo; /** * Unit test for {@link WriteAdvHandle}. */ public class WriteAdvHandleTest { - @RegisterExtension - TestContextExtension testContextExtension = new TestContextExtension(); - private final long entryId; private final WriteAdvHandle handle = mock(WriteAdvHandle.class); private final LinkedBlockingQueue entryQueue; + private String testName; public WriteAdvHandleTest() { this.entryId = System.currentTimeMillis(); @@ -65,9 +63,14 @@ public WriteAdvHandleTest() { when(handle.writeAsync(anyLong(), any(ByteBuffer.class))).thenCallRealMethod(); } + @BeforeEach + public void setUp(TestInfo testInfo) throws Exception { + testName = testInfo.getDisplayName(); + } + @Test public void testAppendBytes() throws Exception { - byte[] testData = testContextExtension.getMethodName().getBytes(UTF_8); + byte[] testData = testName.getBytes(UTF_8); handle.writeAsync(entryId, testData); ByteBuf buffer = entryQueue.take(); @@ -78,7 +81,7 @@ public void testAppendBytes() throws Exception { @Test public void testAppendBytes2() throws Exception { - byte[] testData = testContextExtension.getMethodName().getBytes(UTF_8); + byte[] testData = testName.getBytes(UTF_8); handle.writeAsync(entryId, testData, 1, testData.length / 2); byte[] expectedData = new byte[testData.length / 2]; System.arraycopy(testData, 1, expectedData, 0, testData.length / 2); @@ -91,7 +94,7 @@ public void testAppendBytes2() throws Exception { @Test public void testAppendByteBuffer() throws Exception { - byte[] testData = testContextExtension.getMethodName().getBytes(UTF_8); + byte[] testData = testName.getBytes(UTF_8); handle.writeAsync(entryId, ByteBuffer.wrap(testData, 1, testData.length / 2)); byte[] expectedData = new byte[testData.length / 2]; System.arraycopy(testData, 1, expectedData, 0, testData.length / 2); diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/WriteHandleTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/WriteHandleTest.java index 7fbde4660ba..3829d624e6c 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/WriteHandleTest.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/WriteHandleTest.java @@ -34,9 +34,9 @@ import java.nio.ByteBuffer; import java.util.concurrent.LinkedBlockingQueue; import lombok.extern.slf4j.Slf4j; -import org.apache.bookkeeper.client.extension.TestContextExtension; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.TestInfo; /** * Unit test for the default methods in {@link WriteHandle}. @@ -44,11 +44,9 @@ @Slf4j public class WriteHandleTest { - @RegisterExtension - TestContextExtension testContextExtension = new TestContextExtension(); - private final WriteHandle handle = mock(WriteHandle.class); private final LinkedBlockingQueue entryQueue; + private String testName; public WriteHandleTest() throws Exception { this.entryQueue = new LinkedBlockingQueue<>(); @@ -62,9 +60,14 @@ public WriteHandleTest() throws Exception { when(handle.append(any(ByteBuffer.class))).thenCallRealMethod(); } + @BeforeEach + public void setUp(TestInfo testInfo) throws Exception { + testName = testInfo.getDisplayName(); + } + @Test public void testAppendBytes() throws Exception { - byte[] testData = testContextExtension.getMethodName().getBytes(UTF_8); + byte[] testData = testName.getBytes(UTF_8); handle.append(testData); ByteBuf buffer = entryQueue.take(); @@ -75,7 +78,7 @@ public void testAppendBytes() throws Exception { @Test public void testAppendBytes2() throws Exception { - byte[] testData = testContextExtension.getMethodName().getBytes(UTF_8); + byte[] testData = testName.getBytes(UTF_8); handle.append(testData, 1, testData.length / 2); byte[] expectedData = new byte[testData.length / 2]; System.arraycopy(testData, 1, expectedData, 0, testData.length / 2); @@ -88,7 +91,7 @@ public void testAppendBytes2() throws Exception { @Test public void testAppendByteBuffer() throws Exception { - byte[] testData = testContextExtension.getMethodName().getBytes(UTF_8); + byte[] testData = testName.getBytes(UTF_8); handle.append(ByteBuffer.wrap(testData, 1, testData.length / 2)); byte[] expectedData = new byte[testData.length / 2]; System.arraycopy(testData, 1, expectedData, 0, testData.length / 2); diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/extension/TestContextExtension.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/extension/TestContextExtension.java deleted file mode 100644 index 8569390906f..00000000000 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/extension/TestContextExtension.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.bookkeeper.client.extension; - -import org.junit.jupiter.api.extension.BeforeEachCallback; -import org.junit.jupiter.api.extension.ExtensionContext; - -/** - * Unit Test Context Information. - */ -public class TestContextExtension implements BeforeEachCallback { - - private volatile String methodName; - - public String getMethodName() { - return methodName; - } - - @Override - public void beforeEach(ExtensionContext context) throws Exception { - methodName = context.getRequiredTestMethod().getName(); - } -}