From bcc59bb0acaeed0f796e85fc91e2928174931e24 Mon Sep 17 00:00:00 2001 From: "sherlock.lin" <1193179897@qq.com> Date: Fri, 17 May 2024 15:52:44 +0800 Subject: [PATCH] migrate client module bookie tests to junit 5 --- .../client/MockBookKeeperTestCase.java | 4 + .../client/api/BKExceptionTest.java | 8 +- .../client/api/BookKeeperApiTest.java | 193 ++++++------ .../client/api/BookKeeperBuildersTest.java | 276 ++++++++++-------- .../ExplicitLACWithWriteHandleAPITest.java | 2 +- .../client/api/LedgerMetadataTest.java | 13 +- .../client/api/WriteAdvHandleTest.java | 16 +- .../bookkeeper/client/api/WriteFlagTest.java | 12 +- .../client/api/WriteHandleTest.java | 18 +- .../extension/TestContentExtension.java | 40 +++ 10 files changed, 344 insertions(+), 238 deletions(-) create mode 100644 bookkeeper-server/src/test/java/org/apache/bookkeeper/client/extension/TestContentExtension.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..3aa654d8229 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 @@ -20,17 +20,19 @@ */ package org.apache.bookkeeper.client.api; + import static java.nio.charset.StandardCharsets.UTF_8; import static org.apache.bookkeeper.common.concurrent.FutureUtils.result; +import static org.assertj.core.api.Assertions.assertThat; 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 +49,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 +130,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 +307,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).isEqualTo(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/BookKeeperBuildersTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/BookKeeperBuildersTest.java index 478a3ef208f..e920b641951 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,139 @@ 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, () -> { + result(newCreateLedgerOp() + .withPassword(password) + .execute()); + fail("shoud not be able to create a ledger, client is closed"); + }); } @Test @@ -289,58 +317,73 @@ 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, () -> { + 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 +397,15 @@ 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, () -> { + result(newDeleteLedgerOp() + .withLedgerId(ledgerId) + .execute()); + }); } protected LedgerMetadata generateLedgerMetadata(int ensembleSize, @@ -406,22 +452,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..da1f95a8e68 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,16 @@ */ 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 +94,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 +117,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..0100165d2bf 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.TestContentExtension; 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 + TestContentExtension testContentExtension = new TestContentExtension(); 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 = testContentExtension.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 = testContentExtension.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 = testContentExtension.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..abf06f2fb05 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.TestContentExtension; +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 + TestContentExtension testContentExtension = new TestContentExtension(); 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 = testContentExtension.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 = testContentExtension.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 = testContentExtension.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/TestContentExtension.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/extension/TestContentExtension.java new file mode 100644 index 00000000000..7218917d109 --- /dev/null +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/extension/TestContentExtension.java @@ -0,0 +1,40 @@ +/* + * 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 TestContentExtension implements BeforeEachCallback { + + private volatile String methodName; + + public String getMethodName() { + return methodName; + } + + @Override + public void beforeEach(ExtensionContext context) throws Exception { + methodName = context.getRequiredTestMethod().getName(); + } +}