generated from digitalservicebund/java-application-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for input stream helper
- Loading branch information
1 parent
c65b3f1
commit 4bed5b1
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
src/test/java/de/bund/digitalservice/a2j/security/CachedBodyServletInputStreamTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} | ||
})); | ||
} | ||
} |