Skip to content

Commit d3778e6

Browse files
sherlock-linStevenLuMT
authored andcommitted
test: migrate client module api tests to junit 5 (#4377)
(cherry picked from commit 4936d39)
1 parent 6ba6ad2 commit d3778e6

12 files changed

Lines changed: 350 additions & 300 deletions

bookkeeper-server/src/test/java/org/apache/bookkeeper/client/MockBookKeeperTestCase.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@
7777
import org.apache.bookkeeper.versioning.Versioned;
7878
import org.junit.After;
7979
import org.junit.Before;
80+
import org.junit.jupiter.api.AfterEach;
81+
import org.junit.jupiter.api.BeforeEach;
8082
import org.mockito.invocation.InvocationOnMock;
8183
import org.mockito.stubbing.Answer;
8284
import org.mockito.stubbing.Stubber;
@@ -141,6 +143,7 @@ public MockEntry(byte[] payload, long lastAddConfirmed) {
141143

142144
}
143145

146+
@BeforeEach
144147
@Before
145148
public void setup() throws Exception {
146149
maxNumberOfAvailableBookies = Integer.MAX_VALUE;
@@ -264,6 +267,7 @@ private DigestManager getDigestType(long ledgerId) throws GeneralSecurityExcepti
264267
UnpooledByteBufAllocator.DEFAULT, false);
265268
}
266269

270+
@AfterEach
267271
@After
268272
public void tearDown() {
269273
scheduler.shutdown();

bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/BKExceptionTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
package org.apache.bookkeeper.client.api;
1717

1818
import static org.apache.bookkeeper.client.api.BKException.Code.UnexpectedConditionException;
19-
import static org.junit.Assert.assertEquals;
20-
import static org.junit.Assert.assertNotEquals;
21-
import static org.junit.Assert.assertTrue;
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
21+
import static org.junit.jupiter.api.Assertions.assertTrue;
2222

2323
import java.lang.reflect.Field;
24-
import org.junit.Test;
24+
import org.junit.jupiter.api.Test;
2525

2626
/**
2727
* Tests for BKException methods.

bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/BookKeeperApiTest.java

Lines changed: 122 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@
2525
import static org.hamcrest.Matchers.containsString;
2626
import static org.hamcrest.Matchers.hasItem;
2727
import static org.hamcrest.Matchers.hasProperty;
28-
import static org.junit.Assert.assertArrayEquals;
29-
import static org.junit.Assert.assertEquals;
30-
import static org.junit.Assert.assertFalse;
3128
import static org.junit.Assert.assertThat;
32-
import static org.junit.Assert.assertTrue;
33-
import static org.junit.Assert.fail;
29+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
30+
import static org.junit.jupiter.api.Assertions.assertEquals;
31+
import static org.junit.jupiter.api.Assertions.assertFalse;
32+
import static org.junit.jupiter.api.Assertions.assertThrows;
33+
import static org.junit.jupiter.api.Assertions.assertTrue;
34+
import static org.junit.jupiter.api.Assertions.fail;
3435

3536
import io.netty.buffer.Unpooled;
3637
import java.nio.ByteBuffer;
@@ -47,7 +48,7 @@
4748
import org.apache.bookkeeper.conf.ClientConfiguration;
4849
import org.apache.bookkeeper.util.LoggerOutput;
4950
import org.junit.Rule;
50-
import org.junit.Test;
51+
import org.junit.jupiter.api.Test;
5152
import org.slf4j.event.LoggingEvent;
5253

5354
/**
@@ -62,6 +63,15 @@ public class BookKeeperApiTest extends MockBookKeeperTestCase {
6263
@Rule
6364
public LoggerOutput loggerOutput = new LoggerOutput();
6465

66+
private static void checkEntries(LedgerEntries entries, byte[] data)
67+
throws InterruptedException, BKException {
68+
Iterator<LedgerEntry> iterator = entries.iterator();
69+
while (iterator.hasNext()) {
70+
LedgerEntry entry = iterator.next();
71+
assertArrayEquals(data, entry.getEntryBytes());
72+
}
73+
}
74+
6575
@Test
6676
public void testWriteHandle() throws Exception {
6777
try (WriteHandle writer = result(newCreateLedgerOp()
@@ -128,41 +138,45 @@ public void testWriteAdvHandleWithFixedLedgerId() throws Exception {
128138
}
129139
}
130140

131-
@Test(expected = BKDuplicateEntryIdException.class)
141+
@Test
132142
public void testWriteAdvHandleBKDuplicateEntryId() throws Exception {
133-
try (WriteAdvHandle writer = result(newCreateLedgerOp()
134-
.withAckQuorumSize(1)
135-
.withWriteQuorumSize(2)
136-
.withEnsembleSize(3)
137-
.withPassword(password)
138-
.makeAdv()
139-
.withLedgerId(1234)
140-
.execute())) {
141-
assertEquals(1234, writer.getId());
142-
long entryId = 0;
143-
writer.write(entryId++, ByteBuffer.wrap(data));
144-
assertEquals(data.length, writer.getLength());
145-
writer.write(entryId - 1, ByteBuffer.wrap(data));
146-
}
143+
assertThrows(BKDuplicateEntryIdException.class, () -> {
144+
try (WriteAdvHandle writer = result(newCreateLedgerOp()
145+
.withAckQuorumSize(1)
146+
.withWriteQuorumSize(2)
147+
.withEnsembleSize(3)
148+
.withPassword(password)
149+
.makeAdv()
150+
.withLedgerId(1234)
151+
.execute())) {
152+
assertEquals(1234, writer.getId());
153+
long entryId = 0;
154+
writer.write(entryId++, ByteBuffer.wrap(data));
155+
assertEquals(data.length, writer.getLength());
156+
writer.write(entryId - 1, ByteBuffer.wrap(data));
157+
}
158+
});
147159
}
148160

149-
@Test(expected = BKUnauthorizedAccessException.class)
161+
@Test
150162
public void testOpenLedgerUnauthorized() throws Exception {
151-
long lId;
152-
try (WriteHandle writer = result(newCreateLedgerOp()
153-
.withAckQuorumSize(1)
154-
.withWriteQuorumSize(2)
155-
.withEnsembleSize(3)
156-
.withPassword(password)
157-
.execute())) {
158-
lId = writer.getId();
159-
assertEquals(-1L, writer.getLastAddPushed());
160-
}
161-
try (ReadHandle ignored = result(newOpenLedgerOp()
162-
.withPassword("bad-password".getBytes(UTF_8))
163-
.withLedgerId(lId)
164-
.execute())) {
165-
}
163+
assertThrows(BKUnauthorizedAccessException.class, () -> {
164+
long lId;
165+
try (WriteHandle writer = result(newCreateLedgerOp()
166+
.withAckQuorumSize(1)
167+
.withWriteQuorumSize(2)
168+
.withEnsembleSize(3)
169+
.withPassword(password)
170+
.execute())) {
171+
lId = writer.getId();
172+
assertEquals(-1L, writer.getLastAddPushed());
173+
}
174+
try (ReadHandle ignored = result(newOpenLedgerOp()
175+
.withPassword("bad-password".getBytes(UTF_8))
176+
.withLedgerId(lId)
177+
.execute())) {
178+
}
179+
});
166180
}
167181

168182
/**
@@ -172,7 +186,7 @@ public void testOpenLedgerUnauthorized() throws Exception {
172186
*/
173187
@Test
174188
public void testLedgerDigests() throws Exception {
175-
for (DigestType type: DigestType.values()) {
189+
for (DigestType type : DigestType.values()) {
176190
long lId;
177191
try (WriteHandle writer = result(newCreateLedgerOp()
178192
.withAckQuorumSize(1)
@@ -198,7 +212,6 @@ public void testLedgerDigests() throws Exception {
198212
}
199213
}
200214

201-
202215
@Test
203216
public void testOpenLedgerDigestUnmatchedWhenAutoDetectionEnabled() throws Exception {
204217
testOpenLedgerDigestUnmatched(true);
@@ -226,10 +239,10 @@ private void testOpenLedgerDigestUnmatched(boolean autodetection) throws Excepti
226239
assertEquals(-1L, writer.getLastAddPushed());
227240
}
228241
try (ReadHandle ignored = result(newOpenLedgerOp()
229-
.withDigestType(DigestType.CRC32)
230-
.withPassword(password)
231-
.withLedgerId(lId)
232-
.execute())) {
242+
.withDigestType(DigestType.CRC32)
243+
.withPassword(password)
244+
.withLedgerId(lId)
245+
.execute())) {
233246
if (!autodetection) {
234247
fail("Should fail to open read handle if digest type auto detection is disabled.");
235248
}
@@ -280,10 +293,10 @@ public void testOpenLedgerRead() throws Exception {
280293
}
281294

282295
try (ReadHandle reader = result(newOpenLedgerOp()
283-
.withPassword(password)
284-
.withRecovery(false)
285-
.withLedgerId(lId)
286-
.execute())) {
296+
.withPassword(password)
297+
.withRecovery(false)
298+
.withLedgerId(lId)
299+
.execute())) {
287300
assertTrue(reader.isClosed());
288301
assertEquals(2, reader.getLastAddConfirmed());
289302
assertEquals(3 * data.length, reader.getLength());
@@ -294,82 +307,87 @@ public void testOpenLedgerRead() throws Exception {
294307

295308
// test readLastAddConfirmedAndEntry
296309
LastConfirmedAndEntry lastConfirmedAndEntry =
297-
reader.readLastAddConfirmedAndEntry(0, 999, false);
310+
reader.readLastAddConfirmedAndEntry(0, 999, false);
298311
assertEquals(2L, lastConfirmedAndEntry.getLastAddConfirmed());
299312
assertArrayEquals(data, lastConfirmedAndEntry.getEntry().getEntryBytes());
300313
lastConfirmedAndEntry.close();
301314
}
302315
}
303316

304-
@Test(expected = BKLedgerFencedException.class)
317+
@Test
305318
public void testOpenLedgerWithRecovery() throws Exception {
319+
assertThrows(BKLedgerFencedException.class, () -> {
320+
loggerOutput.expect((List<LoggingEvent> logEvents) -> {
321+
assertThat(logEvents, hasItem(hasProperty("message",
322+
containsString("due to LedgerFencedException: "
323+
+ "Ledger has been fenced off. Some other client must have opened it to read")
324+
)));
325+
});
306326

307-
loggerOutput.expect((List<LoggingEvent> logEvents) -> {
308-
assertThat(logEvents, hasItem(hasProperty("message",
309-
containsString("due to LedgerFencedException: "
310-
+ "Ledger has been fenced off. Some other client must have opened it to read")
311-
)));
312-
});
313-
314-
long lId;
315-
try (WriteHandle writer = result(newCreateLedgerOp()
316-
.withAckQuorumSize(1)
317-
.withWriteQuorumSize(2)
318-
.withEnsembleSize(3)
319-
.withPassword(password)
320-
.execute())) {
321-
lId = writer.getId();
322-
323-
writer.append(ByteBuffer.wrap(data));
324-
writer.append(ByteBuffer.wrap(data));
325-
assertEquals(1L, writer.getLastAddPushed());
326-
327-
// open with fencing
328-
try (ReadHandle reader = result(newOpenLedgerOp()
327+
long lId;
328+
try (WriteHandle writer = result(newCreateLedgerOp()
329+
.withAckQuorumSize(1)
330+
.withWriteQuorumSize(2)
331+
.withEnsembleSize(3)
329332
.withPassword(password)
330-
.withRecovery(true)
331-
.withLedgerId(lId)
332333
.execute())) {
333-
assertTrue(reader.isClosed());
334-
assertEquals(1L, reader.getLastAddConfirmed());
335-
}
334+
lId = writer.getId();
336335

337-
writer.append(ByteBuffer.wrap(data));
336+
writer.append(ByteBuffer.wrap(data));
337+
writer.append(ByteBuffer.wrap(data));
338+
assertEquals(1L, writer.getLastAddPushed());
339+
340+
// open with fencing
341+
try (ReadHandle reader = result(newOpenLedgerOp()
342+
.withPassword(password)
343+
.withRecovery(true)
344+
.withLedgerId(lId)
345+
.execute())) {
346+
assertTrue(reader.isClosed());
347+
assertEquals(1L, reader.getLastAddConfirmed());
348+
}
338349

339-
}
350+
writer.append(ByteBuffer.wrap(data));
351+
352+
}
353+
});
340354
}
341355

342-
@Test(expected = BKNoSuchLedgerExistsOnMetadataServerException.class)
356+
@Test
343357
public void testDeleteLedger() throws Exception {
344-
long lId;
358+
assertThrows(BKNoSuchLedgerExistsOnMetadataServerException.class, () -> {
359+
long lId;
345360

346-
try (WriteHandle writer = result(newCreateLedgerOp()
347-
.withPassword(password)
348-
.execute())) {
349-
lId = writer.getId();
350-
assertEquals(-1L, writer.getLastAddPushed());
351-
}
361+
try (WriteHandle writer = result(newCreateLedgerOp()
362+
.withPassword(password)
363+
.execute())) {
364+
lId = writer.getId();
365+
assertEquals(-1L, writer.getLastAddPushed());
366+
}
352367

353-
result(newDeleteLedgerOp().withLedgerId(lId).execute());
368+
result(newDeleteLedgerOp().withLedgerId(lId).execute());
354369

355-
result(newOpenLedgerOp()
356-
.withPassword(password)
357-
.withLedgerId(lId)
358-
.execute());
370+
result(newOpenLedgerOp()
371+
.withPassword(password)
372+
.withLedgerId(lId)
373+
.execute());
374+
});
359375
}
360376

361-
@Test(expected = BKNoSuchLedgerExistsOnMetadataServerException.class)
377+
@Test
362378
public void testCannotDeleteLedgerTwice() throws Exception {
363-
long lId;
379+
assertThrows(BKNoSuchLedgerExistsOnMetadataServerException.class, () -> {
380+
long lId;
364381

365-
try (WriteHandle writer = result(newCreateLedgerOp()
366-
.withPassword(password)
367-
.execute())) {
368-
lId = writer.getId();
369-
assertEquals(-1L, writer.getLastAddPushed());
370-
}
371-
result(newDeleteLedgerOp().withLedgerId(lId).execute());
372-
result(newDeleteLedgerOp().withLedgerId(lId).execute());
382+
try (WriteHandle writer = result(newCreateLedgerOp()
383+
.withPassword(password)
384+
.execute())) {
385+
lId = writer.getId();
386+
assertEquals(-1L, writer.getLastAddPushed());
387+
}
388+
result(newDeleteLedgerOp().withLedgerId(lId).execute());
389+
result(newDeleteLedgerOp().withLedgerId(lId).execute());
390+
});
373391
}
374392

375393
@Test
@@ -404,9 +422,9 @@ public void testLedgerEntriesIterable() throws Exception {
404422
}
405423
i.set(0);
406424
entries.forEach((e) -> {
407-
assertEquals(i.getAndIncrement(), e.getEntryId());
408-
assertArrayEquals(data, e.getEntryBytes());
409-
});
425+
assertEquals(i.getAndIncrement(), e.getEntryId());
426+
assertArrayEquals(data, e.getEntryBytes());
427+
});
410428
}
411429
}
412430
}
@@ -426,13 +444,4 @@ public void testBKExceptionCodeLogger() {
426444
assertEquals("123: Unexpected condition", BKException.codeLogger(123).toString());
427445
assertEquals("-201: Unexpected condition", BKException.codeLogger(-201).toString());
428446
}
429-
430-
private static void checkEntries(LedgerEntries entries, byte[] data)
431-
throws InterruptedException, BKException {
432-
Iterator<LedgerEntry> iterator = entries.iterator();
433-
while (iterator.hasNext()) {
434-
LedgerEntry entry = iterator.next();
435-
assertArrayEquals(data, entry.getEntryBytes());
436-
}
437-
}
438447
}

0 commit comments

Comments
 (0)