reformat and add test file
This commit is contained in:
parent
06f9a5f445
commit
f325351275
@ -3,7 +3,6 @@ package com.baeldung.fileToBase64StringConversion;
|
|||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
|
|
||||||
@ -12,38 +11,33 @@ import org.junit.Test;
|
|||||||
|
|
||||||
public class FileToBase64StringConversionUnitTest {
|
public class FileToBase64StringConversionUnitTest {
|
||||||
|
|
||||||
private String inputFilePath = "";
|
private String inputFilePath = "test_image.jpg";
|
||||||
private String outputFilePath = "";
|
private String outputFilePath = "test_image_copy.jpg";
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
// file paths are from environment arguments:
|
public void fileToBase64StringConversion() throws IOException {
|
||||||
// mvn test
|
//load file from /src/test/resources
|
||||||
// -Dtest=com.baeldung.fileToBase64StringConversion.FileToBase64StringConversionUnitTest
|
ClassLoader classLoader = getClass().getClassLoader();
|
||||||
// -DinputFilePath="abc.png" -DoutFilePath="abc.png"
|
File inputFile = new File(classLoader
|
||||||
|
.getResource(inputFilePath)
|
||||||
|
.getFile());
|
||||||
|
|
||||||
public void fileToBase64StringConversion() throws FileNotFoundException, IOException {
|
byte[] fileContent = FileUtils.readFileToByteArray(inputFile);
|
||||||
inputFilePath = System.getProperty("inputFilePath");
|
String encodedString = Base64
|
||||||
outputFilePath = System.getProperty("outputFilePath");
|
.getEncoder()
|
||||||
|
.encodeToString(fileContent);
|
||||||
|
|
||||||
if (inputFilePath == null || outputFilePath == null)
|
//create output file
|
||||||
return;
|
File outputFile = new File(inputFile
|
||||||
|
.getParentFile()
|
||||||
|
.getAbsolutePath() + File.pathSeparator + outputFilePath);
|
||||||
|
|
||||||
File outputFile = new File(outputFilePath);
|
// decode the string and write to file
|
||||||
File inputFile = new File(inputFilePath);
|
byte[] decodedBytes = Base64
|
||||||
|
.getDecoder()
|
||||||
|
.decode(encodedString);
|
||||||
|
FileUtils.writeByteArrayToFile(outputFile, decodedBytes);
|
||||||
|
|
||||||
if (!inputFile.exists())
|
assertTrue(FileUtils.contentEquals(inputFile, outputFile));
|
||||||
return;
|
}
|
||||||
|
|
||||||
byte[] fileContent = FileUtils.readFileToByteArray(inputFile);
|
|
||||||
String encodedString = Base64.getEncoder().encodeToString(fileContent);
|
|
||||||
|
|
||||||
// print encoded base64 String
|
|
||||||
System.out.println(encodedString);
|
|
||||||
|
|
||||||
// decode the string and write to file
|
|
||||||
byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
|
|
||||||
FileUtils.writeByteArrayToFile(outputFile, decodedBytes);
|
|
||||||
|
|
||||||
assertTrue(outputFile.length() == fileContent.length);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
BIN
core-java-8/src/test/resources/test_image.jpg
Normal file
BIN
core-java-8/src/test/resources/test_image.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.8 KiB |
Loading…
x
Reference in New Issue
Block a user