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/WriteAdvHandleTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/WriteAdvHandleTest.java index e371bd8b381..799c28423e4 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 volatile 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..9c389369f4d 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 volatile 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(); - } -}