BAEL-3909: Moved code to another module (#8931)
This commit is contained in:
@@ -64,6 +64,11 @@
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${jmh-generator.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
<version>${commons-codec.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
@@ -113,6 +118,7 @@
|
||||
<hibernate-validator.version>6.0.2.Final</hibernate-validator.version>
|
||||
<javax.el-api.version>3.0.0</javax.el-api.version>
|
||||
<javax.el.version>2.2.6</javax.el.version>
|
||||
<commons-codec.version>1.14</commons-codec.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.baeldung.pdf.base64;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class EncodeDecodeUnitTest {
|
||||
|
||||
private static final String IN_FILE = "src/test/resources/input.pdf";
|
||||
private static final String OUT_FILE = "src/test/resources/output.pdf";
|
||||
private static byte[] inFileBytes;
|
||||
|
||||
@BeforeClass
|
||||
public static void fileToByteArray() throws IOException {
|
||||
inFileBytes = Files.readAllBytes(Paths.get(IN_FILE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJavaBase64_whenEncoded_thenDecodedOK() throws IOException {
|
||||
|
||||
byte[] encoded = java.util.Base64.getEncoder().encode(inFileBytes);
|
||||
|
||||
byte[] decoded = java.util.Base64.getDecoder().decode(encoded);
|
||||
|
||||
writeToFile(OUT_FILE, decoded);
|
||||
|
||||
assertNotEquals(encoded.length, decoded.length);
|
||||
assertEquals(inFileBytes.length, decoded.length);
|
||||
|
||||
assertArrayEquals(decoded, inFileBytes);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenApacheCommons_givenJavaBase64_whenEncoded_thenDecodedOK() throws IOException {
|
||||
|
||||
byte[] encoded = org.apache.commons.codec.binary.Base64.encodeBase64(inFileBytes);
|
||||
|
||||
byte[] decoded = org.apache.commons.codec.binary.Base64.decodeBase64(encoded);
|
||||
|
||||
writeToFile(OUT_FILE, decoded);
|
||||
|
||||
assertNotEquals(encoded.length, decoded.length);
|
||||
assertEquals(inFileBytes.length, decoded.length);
|
||||
|
||||
assertArrayEquals(decoded, inFileBytes);
|
||||
}
|
||||
|
||||
private void writeToFile(String fileName, byte[] bytes) throws IOException {
|
||||
FileOutputStream fos = new FileOutputStream(fileName);
|
||||
fos.write(bytes);
|
||||
fos.flush();
|
||||
fos.close();
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Reference in New Issue
Block a user