BAEL-3974 - Add unit test for streaming Base64 encoding

This commit is contained in:
mthomas 2020-05-05 22:38:52 -05:00
parent 50208fb013
commit 0774d2f238
1 changed files with 27 additions and 4 deletions

View File

@ -1,17 +1,19 @@
package com.baeldung.pdf.base64; package com.baeldung.pdf.base64;
import static org.junit.Assert.assertArrayEquals; import java.io.FileInputStream;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
public class EncodeDecodeUnitTest { public class EncodeDecodeUnitTest {
private static final String IN_FILE = "src/test/resources/input.pdf"; private static final String IN_FILE = "src/test/resources/input.pdf";
@ -39,6 +41,27 @@ public class EncodeDecodeUnitTest {
} }
@Test
public void givenJavaBase64_whenEncodedStream_thenDecodedStreamOK() throws IOException {
try (OutputStream os = java.util.Base64.getEncoder().wrap(new FileOutputStream(OUT_FILE));
FileInputStream fis = new FileInputStream(IN_FILE)) {
byte[] bytes = new byte[1024];
int read;
while ((read = fis.read(bytes)) > -1) {
os.write(bytes, 0, read);
}
}
byte[] encoded = java.util.Base64.getEncoder().encode(inFileBytes);
byte[] encodedOnDisk = Files.readAllBytes(Paths.get(OUT_FILE));
assertArrayEquals(encoded, encodedOnDisk);
byte[] decoded = java.util.Base64.getDecoder().decode(encoded);
byte[] decodedOnDisk = java.util.Base64.getDecoder().decode(encodedOnDisk);
assertArrayEquals(decoded, decodedOnDisk);
}
@Test @Test
public void givenApacheCommons_givenJavaBase64_whenEncoded_thenDecodedOK() throws IOException { public void givenApacheCommons_givenJavaBase64_whenEncoded_thenDecodedOK() throws IOException {