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 d5a4f05c24a..b1def134d45 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..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 @@ -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; /** @@ -62,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() @@ -128,41 +138,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())) { + } + }); } /** @@ -172,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) @@ -198,7 +212,6 @@ public void testLedgerDigests() throws Exception { } } - @Test public void testOpenLedgerDigestUnmatchedWhenAutoDetectionEnabled() throws Exception { testOpenLedgerDigestUnmatched(true); @@ -226,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."); } @@ -280,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()); @@ -294,82 +307,87 @@ 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(); } } - @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 @@ -404,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()); + }); } } } @@ -426,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 00e14fe2c12..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; @@ -38,14 +36,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.ValueSource; /** * Tests for BookKeeper open ledger operations. */ -@RunWith(Parameterized.class) public class BookKeeperBuildersOpenLedgerTest extends MockBookKeeperTestCase { private static final int ensembleSize = 3; @@ -56,22 +52,9 @@ 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 BookKeeperBuildersOpenLedgerTest(boolean withRecovery) { - this.withRecovery = withRecovery; - } - - @Parameterized.Parameters(name = "withRecovery:({0})") - public static Collection data() { - return Arrays.asList(new Object[][]{ - {true}, - {false} - }); - } - - @Test - public void testOpenLedger() throws Exception { + @ParameterizedTest + @ValueSource(booleans = {true, false}) + public void testOpenLedger(boolean withRecovery) throws Exception { LedgerMetadata ledgerMetadata = generateLedgerMetadata(ensembleSize, writeQuorumSize, ackQuorumSize, password, customMetadata); registerMockLedgerMetadata(ledgerId, ledgerMetadata); @@ -91,8 +74,9 @@ public void testOpenLedger() throws Exception { .execute()); } - @Test - public void testOpenLedgerWithTimeoutEx() throws Exception { + @ParameterizedTest + @ValueSource(booleans = {true, false}) + 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 cc1d29c1522..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 @@ -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("should not be able to create a ledger with such specs"); + + assertThrows(BKIncorrectParameterException.class, () -> { + result(newCreateLedgerOp() + .withDigestType(null) + .withPassword(password) + .execute()); + fail("should 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("should not be able to create a ledger, client is closed"); + assertThrows(BKClientClosedException.class, () -> { + closeBookkeeper(); + result(newCreateLedgerOp() + .withPassword(password) + .execute()); + fail("should 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("should not be able to create a ledger with such specs"); + assertThrows(BKIncorrectParameterException.class, () -> { + result(newCreateLedgerOp() + .withPassword(password) + .makeAdv() + .withLedgerId(-2) + .execute()); + fail("should 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..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 @@ -36,21 +36,19 @@ import java.nio.ByteBuffer; import java.util.concurrent.LinkedBlockingQueue; 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.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInfo; /** * Unit test for {@link WriteAdvHandle}. */ public class WriteAdvHandleTest { - @Rule - public final TestName runtime = new TestName(); - 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 = runtime.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 = runtime.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 = runtime.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/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..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 @@ -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.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInfo; /** * Unit test for the default methods in {@link WriteHandle}. @@ -44,11 +44,9 @@ @Slf4j public class WriteHandleTest { - @Rule - public final TestName runtime = new TestName(); - 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 = runtime.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 = runtime.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 = runtime.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/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());