Skip to content

Commit

Permalink
Migrate to JUnit 5
Browse files Browse the repository at this point in the history
  • Loading branch information
centic9 committed Dec 15, 2024
1 parent 37c94fb commit 04b662e
Show file tree
Hide file tree
Showing 30 changed files with 461 additions and 506 deletions.
5 changes: 4 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ dependencies {
// don't include it in the jar-file
runtimeOnly files('src/runtime/resources')

testImplementation 'org.junit.vintage:junit-vintage-engine:5.11.3'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.11.3'
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.11.3'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.11.3'
testImplementation 'org.dstadler:commons-test:1.0.0.21'
testImplementation 'org.apache.commons:commons-rng-simple:1.6'
}
Expand All @@ -93,6 +95,7 @@ test {

// avoid failures because Gradle provides it's own version of JNA in the system properties
systemProperties['jna.boot.library.path'] = null
useJUnitPlatform()
}

jacoco {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,19 +249,19 @@ private double getPerSecond(AtomicLong bytesWrittenOverall) {
public String toString() {
//long time = System.currentTimeMillis() - start;
/*", time: " + time + "/" + (double)time / 1000 +*/
return String.format("Start: %d, " +
"CPS: %.2f, " +
"CPSRead: %.2f, " +
"CPSWritten: %.2f, " +
"Written: %d bytes/%d chunks, %.2f bytes/s, %.2f chunks/s, " +
"Read: %d bytes/%d chunks, %.2f bytes/s, %.2f chunks/s, " +
"%s",
start,
getChunksPerSecond(),
getChunksReadPerSecond(),
getChunksWrittenPerSecond(),
bytesWrittenOverall.get(), chunksWrittenOverall.get(), getPerSecond(bytesWrittenOverall), getPerSecond(chunksWrittenOverall),
bytesReadOverall.get(), chunksReadOverall.get(), getPerSecond(bytesReadOverall), getPerSecond(chunksReadOverall),
delegate.toString());
return ("Start: %d, " +
"CPS: %.2f, " +
"CPSRead: %.2f, " +
"CPSWritten: %.2f, " +
"Written: %d bytes/%d chunks, %.2f bytes/s, %.2f chunks/s, " +
"Read: %d bytes/%d chunks, %.2f bytes/s, %.2f chunks/s, " +
"%s").formatted(
start,
getChunksPerSecond(),
getChunksReadPerSecond(),
getChunksWrittenPerSecond(),
bytesWrittenOverall.get(), chunksWrittenOverall.get(), getPerSecond(bytesWrittenOverall), getPerSecond(chunksWrittenOverall),
bytesReadOverall.get(), chunksReadOverall.get(), getPerSecond(bytesReadOverall), getPerSecond(chunksReadOverall),
delegate.toString());
}
}
20 changes: 10 additions & 10 deletions src/main/java/org/dstadler/audio/buffer/RangeDownloadingBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ public int fillupBuffer(int min, int max) throws IOException {
throw e;
}

log.warning(String.format("Retry %,d: Failed to download, buffer: %,d bytes, chunkSize: %,d, bufferedChunks: %,d, " +
"min: %,d, max: %,d from position %,d: length: %,d: %s",
log.warning(("Retry %,d: Failed to download, buffer: %,d bytes, chunkSize: %,d, bufferedChunks: %,d, " +
"min: %,d, max: %,d from position %,d: length: %,d: %s").formatted(
retries, buffer.size(), chunkSize, bufferedChunks, min, max, nextDownloadPos,
download.getLength(), e));

Expand Down Expand Up @@ -173,7 +173,7 @@ private int downloadChunksSync(int min, int max) throws IOException {
toDownload, bufferedChunks, buffer.size());

if (log.isLoggable(Level.FINE)) {
log.fine(String.format("Downloading %,d chunks at download-position %,d from %s",
log.fine("Downloading %,d chunks at download-position %,d from %s".formatted(
toDownload, nextDownloadPos, download));
}

Expand Down Expand Up @@ -232,10 +232,10 @@ public Chunk next() {
// empty() indicates that we cannot fetch more data any more
if(buffer.empty() && !empty()) {
try {
log.info(String.format("Filling buffer for next() with download-position at %,d, length %,d, buffer: %s",
log.info("Filling buffer for next() with download-position at %,d, length %,d, buffer: %s".formatted(
nextDownloadPos, download.getLength(), buffer));
int chunks = fillupBuffer(-1, 10);
log.info(String.format("Downloaded %,d chunks, now at download-position %,d, length %,d, buffer: %s",
log.info("Downloaded %,d chunks, now at download-position %,d, length %,d, buffer: %s".formatted(
chunks, nextDownloadPos, download.getLength(), buffer));
} catch (IOException e) {
throw new IllegalStateException("Failed to fill-up buffer", e);
Expand All @@ -256,15 +256,15 @@ public Chunk peek() {
// empty() indicates that we cannot fetch more data any more
if(buffer.empty() && !empty()) {
try {
log.info(String.format("Filling buffer for peek() with download-position at %,d, length %,d, buffer: %s",
log.info("Filling buffer for peek() with download-position at %,d, length %,d, buffer: %s".formatted(
nextDownloadPos, download.getLength(), buffer));
int chunks = fillupBuffer(-1, 10);
log.info(String.format("Downloaded %,d chunks, now at download-position %,d, length %,d, buffer: %s",
log.info("Downloaded %,d chunks, now at download-position %,d, length %,d, buffer: %s".formatted(
chunks, nextDownloadPos, download.getLength(), buffer));
} catch (IOException e) {
log.log(Level.WARNING,
String.format("Failed to fill-up buffer for peek, now at download-position %,d, length %,d, buffer: %s",
nextDownloadPos, download.getLength(), buffer), e);
"Failed to fill-up buffer for peek, now at download-position %,d, length %,d, buffer: %s".formatted(
nextDownloadPos, download.getLength(), buffer), e);
return null;
}
}
Expand Down Expand Up @@ -408,7 +408,7 @@ public synchronized BufferPersistenceDTO toPersistence(Stream stream, boolean pl
long startPosition = nextDownloadPos - (long) buffer.size() * chunkSize;

if(startPosition < 0) {
log.warning(String.format("Found invalid startPosition: %,d with next download-position at %,d and buffer-size of %,d, resetting to 0",
log.warning("Found invalid startPosition: %,d with next download-position at %,d and buffer-size of %,d, resetting to 0".formatted(
startPosition, nextDownloadPos, buffer.size() * chunkSize));
startPosition = 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public RangeDownloadHTTP(String url, String user, String pwd) throws IOException
httpClient.getHttpClient().getConnectionManager().closeIdleConnections(0, TimeUnit.SECONDS);
}

log.info(String.format("Prepared download of %s, length: %,d", url, length));
log.info("Prepared download of %s, length: %,d".formatted(url, length));
}

@Override
Expand All @@ -105,7 +105,7 @@ public byte[] readRange(long start, int size) throws IOException {
length, start, size);

if(start + size > length) {
log.info(String.format("Reducing number of bytes to read for %s at position %,d from %,d bytes to %,d bytes because of length of stream %,d",
log.info("Reducing number of bytes to read for %s at position %,d from %,d bytes to %,d bytes because of length of stream %,d".formatted(
url, start, size, length - start, length));
size = (int)(length - start);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/dstadler/audio/example/SimplePlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -40,7 +40,7 @@ public static void main(String[] args) throws Exception {
private static void run(String file) {
log.info("Playing file " + file);

try (final InputStream input = new BufferedInputStream(Files.newInputStream(Paths.get(file)), CHUNK_SIZE)) {
try (final InputStream input = new BufferedInputStream(Files.newInputStream(Path.of(file)), CHUNK_SIZE)) {
AudioPlayer player = createPlayer(input);

//player.setOptions("");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@
import org.dstadler.commons.testing.MemoryLeakVerifier;
import org.dstadler.commons.testing.TestHelpers;
import org.dstadler.commons.testing.ThreadTestHelper;
import org.junit.After;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

public abstract class AbstractBlockingSeekableRingBufferTester {
private static final Chunk CHUNK_1 = new Chunk(new byte[] { 1, 2, 3 }, "", 0);
Expand All @@ -26,7 +21,7 @@ public abstract class AbstractBlockingSeekableRingBufferTester {

abstract SeekableRingBuffer<Chunk> getBlockingSeekableRingBuffer();

@After
@AfterEach
public void tearDown() {
verifier.assertGarbageCollected();
}
Expand Down Expand Up @@ -212,20 +207,16 @@ public void testSizes() {
public void addAndGetMany() {
for(byte i = 0;i < 20;i++) {
buffer.add(new Chunk(new byte[] { i }, "", 0));
assertEquals("Failed for i: " + i,
Math.min(i+1, 9), buffer.size());
assertEquals("Failed for i: " + i,
Math.min(i+1, 9), buffer.fill());
assertEquals(Math.min(i+1, 9), buffer.size(), "Failed for i: " + i);
assertEquals(Math.min(i+1, 9), buffer.fill(), "Failed for i: " + i);
}

assertEquals(9, buffer.size());
assertEquals(9, buffer.fill());

for(byte i = 11;i < 20;i++) {
assertEquals("Failed for i: " + i,
new Chunk(new byte[] { i }, "", 0), buffer.next());
assertEquals("Failed for i: " + i,
9, buffer.fill());
assertEquals(new Chunk(new byte[] { i }, "", 0), buffer.next(), "Failed for i: " + i);
assertEquals(9, buffer.fill(), "Failed for i: " + i);
}

assertEquals(0, buffer.size());
Expand Down Expand Up @@ -279,13 +270,13 @@ protected ThreadTestHelper createThreadTestHelper() {

@Test
public void testSeekInEmptyBuffer() {
assertEquals("Cannot seek 0 buffer", 0, buffer.seek(0));
assertEquals("Cannot seek forward in empty buffer", 0, buffer.seek(1));
assertEquals(0, buffer.seek(0), "Cannot seek 0 buffer");
assertEquals(0, buffer.seek(1), "Cannot seek forward in empty buffer");

assertEquals(0, buffer.size());
assertEquals("Can seek backwards", -1, buffer.seek(-1));
assertEquals(-1, buffer.seek(-1), "Can seek backwards");
assertEquals(1, buffer.size());
assertEquals("Can seek forward now", 1, buffer.seek(1));
assertEquals(1, buffer.seek(1), "Can seek forward now");
assertEquals(0, buffer.size());
}

Expand All @@ -294,19 +285,19 @@ public void testSeekInFilledBuffer() {
buffer.add(CHUNK_1);
buffer.add(CHUNK_2);

assertEquals("Cannot seek 0 buffer even when not empty", 0, buffer.seek(0));
assertEquals(0, buffer.seek(0), "Cannot seek 0 buffer even when not empty");

assertEquals(CHUNK_1, buffer.peek());
assertEquals(2, buffer.size());
assertEquals("Can seek forward in filled buffer", 1, buffer.seek(1));
assertEquals(1, buffer.seek(1), "Can seek forward in filled buffer");
assertEquals(CHUNK_2, buffer.peek());
assertEquals(1, buffer.size());

assertEquals("Can seek backwards in filled buffer", -1, buffer.seek(-1));
assertEquals(-1, buffer.seek(-1), "Can seek backwards in filled buffer");
assertEquals(CHUNK_1, buffer.peek());
assertEquals(2, buffer.size());

assertEquals("Can seek forward again", 1, buffer.seek(1));
assertEquals(1, buffer.seek(1), "Can seek forward again");
assertEquals(CHUNK_2, buffer.peek());
assertEquals(1, buffer.size());
assertEquals(2, buffer.fill());
Expand All @@ -317,43 +308,43 @@ public void testSeekInFullBuffer() {
for(byte i = 0;i < 15;i++) {
buffer.add(new Chunk(new byte[] { i }, "", 0));
}
assertTrue("buffer should be full now", buffer.full());
assertTrue(buffer.full(), "buffer should be full now");
assertFalse(buffer.empty());
assertArrayEquals(new byte[] { 6 }, buffer.peek().getData());
assertEquals(new Chunk(new byte[] { 6 }, "", 0), buffer.peek());
assertEquals(9, buffer.size());
assertEquals(9, buffer.fill());

assertEquals("Cannot seek backwards in full buffer", 0, buffer.seek(-1));
assertEquals(0, buffer.seek(-1), "Cannot seek backwards in full buffer");
assertArrayEquals(new byte[] { 6 }, buffer.peek().getData());
assertEquals(new Chunk(new byte[] { 6 }, "", 0), buffer.peek());
assertEquals(9, buffer.size());
assertEquals(9, buffer.fill());

assertEquals("Can seek forward in full buffer, making it non-full", 1, buffer.seek(1));
assertEquals(1, buffer.seek(1), "Can seek forward in full buffer, making it non-full");
assertArrayEquals(new byte[] { 7 }, buffer.peek().getData());
assertEquals(new Chunk(new byte[] { 7 }, "", 0), buffer.peek());
assertFalse(buffer.full());
assertFalse(buffer.empty());
assertEquals(8, buffer.size());
assertEquals(9, buffer.fill());

assertEquals("Can seek backwards now, making the buffer full again", -1, buffer.seek(-1));
assertEquals(-1, buffer.seek(-1), "Can seek backwards now, making the buffer full again");
assertArrayEquals(new byte[] { 6 }, buffer.peek().getData());
assertEquals(new Chunk(new byte[] { 6 }, "", 0), buffer.peek());
assertTrue(buffer.full());
assertFalse(buffer.empty());
assertEquals(9, buffer.size());
assertEquals(9, buffer.fill());

assertEquals("Can seek forward up to empty() in full buffer", 9, buffer.seek(20));
assertNull("Buffer is empty now as we forward up to head", buffer.peek());
assertEquals(9, buffer.seek(20), "Can seek forward up to empty() in full buffer");
assertNull(buffer.peek(), "Buffer is empty now as we forward up to head");
assertFalse(buffer.full());
assertTrue(buffer.empty());
assertEquals(0, buffer.size());
assertEquals(9, buffer.fill());

assertEquals("Can seek backwards up to full() in full buffer", -9, buffer.seek(-20));
assertEquals(-9, buffer.seek(-20), "Can seek backwards up to full() in full buffer");
assertArrayEquals(new byte[] { 6 }, buffer.peek().getData());
assertEquals(new Chunk(new byte[] { 6 }, "", 0), buffer.peek());
assertTrue(buffer.full());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
package org.dstadler.audio.buffer;

import org.dstadler.audio.stream.Stream;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.io.IOException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

public class BlockingSeekableRingBufferTest extends AbstractBlockingSeekableRingBufferTester {
@Override
Expand Down Expand Up @@ -71,21 +66,21 @@ public void testToStringBuffer() {
try (BlockingSeekableRingBuffer localBuffer = new BlockingSeekableRingBuffer(10)) {
assertTrue(localBuffer.empty());
assertFalse(localBuffer.full());
assertTrue("Had: " + localBuffer,
localBuffer.toString().contains("empty=true"));
assertTrue("Had: " + localBuffer,
localBuffer.toString().contains("full=false"));
assertTrue(localBuffer.toString().contains("empty=true"),
"Had: " + localBuffer);
assertTrue(localBuffer.toString().contains("full=false"),
"Had: " + localBuffer);

for (byte i = 0; i < 15; i++) {
localBuffer.add(new Chunk(new byte[]{i}, "", 0));
}

assertFalse(localBuffer.empty());
assertTrue(localBuffer.full());
assertTrue("Had: " + localBuffer,
localBuffer.toString().contains("empty=false"));
assertTrue("Had: " + localBuffer,
localBuffer.toString().contains("full=true"));
assertTrue(localBuffer.toString().contains("empty=false"),
"Had: " + localBuffer);
assertTrue(localBuffer.toString().contains("full=true"),
"Had: " + localBuffer);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@

import org.dstadler.audio.stream.Stream;
import org.dstadler.commons.testing.TestHelpers;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.io.File;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

public class BufferPersistenceDTOTest {
@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@

import org.dstadler.audio.stream.Stream;
import org.dstadler.commons.logging.jdk.LoggerFactory;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.util.logging.Logger;

import static org.dstadler.audio.buffer.Chunk.CHUNK_SIZE;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

public class BufferPersistenceTest {
private final static Logger log = LoggerFactory.make();
Expand Down Expand Up @@ -190,8 +187,7 @@ public void testInvalidStartPosition() throws IOException {
BufferPersistenceDTO dtoBack = BufferPersistence.readBufferFromDisk(tempPersist);

try (RangeDownloadingBuffer buffer = RangeDownloadingBuffer.fromPersistence(dtoBack, 100, Chunk.CHUNK_SIZE)) {
assertEquals("Zero expected here because we restored the buffer",
0, buffer.bufferedBackward());
assertEquals(0, buffer.bufferedBackward(), "Zero expected here because we restored the buffer");

buffer.next();

Expand Down
Loading

0 comments on commit 04b662e

Please sign in to comment.