Skip to content

Commit

Permalink
Add unit tests for input stream helper
Browse files Browse the repository at this point in the history
  • Loading branch information
zechmeister committed Nov 6, 2024
1 parent c65b3f1 commit 4bed5b1
Showing 1 changed file with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package de.bund.digitalservice.a2j.security;

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 jakarta.servlet.ReadListener;
import java.io.IOException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class CachedBodyServletInputStreamTest {

private CachedBodyServletInputStream inputStream;
private byte[] data;

@BeforeEach
void setup() {
data = "test".getBytes();
inputStream = new CachedBodyServletInputStream(data);
}

@Test
void testRead() throws IOException {
for (byte b : data) {
assertEquals(b, inputStream.read());
}
assertEquals(-1, inputStream.read());
}

@Test
void testIsFinished() throws IOException {
assertFalse(inputStream.isFinished());

while (inputStream.read() != -1) {}

assertTrue(inputStream.isFinished());
}

@Test
void testIsReady() {
assertTrue(inputStream.isReady());
}

@Test
void testSetReadListener() {
assertThrows(
UnsupportedOperationException.class,
() ->
inputStream.setReadListener(
new ReadListener() {
@Override
public void onDataAvailable() throws IOException {}

@Override
public void onAllDataRead() throws IOException {}

@Override
public void onError(Throwable throwable) {}
}));
}
}

0 comments on commit 4bed5b1

Please sign in to comment.