Skip to content
Snippets Groups Projects
Commit 8de43d65 authored by Jakub Kolman's avatar Jakub Kolman
Browse files

[#81] test: tests generate tmp files

parent 27c5c426
No related branches found
No related tags found
No related merge requests found
package cz.fidentis.analyst.feature.services;
import cz.fidentis.analyst.feature.FeaturePoint;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
......@@ -17,7 +18,8 @@ import java.util.logging.Logger;
public class FeaturePointCsvExporter {
/**
* Exports a file to default location in csv format
* Exports a file to set location in csv format <br>
* File is located and named as {@param objectName}_landmarks.csv
*
* @param featurePointList
* @param objectName
......@@ -25,6 +27,33 @@ public class FeaturePointCsvExporter {
*/
public static void exportFeaturePointsToCSV(List<FeaturePoint> featurePointList, String objectName) throws IOException {
File csvOutputFile = new File(String.format("%s_landmarks.csv", objectName));
writeToFile(featurePointList, csvOutputFile, objectName);
}
// /**
// * This method is only for testing purpose and saves the file only TEMPORARILY. <br>
// * <p>
// * Exports a file to set location for testing purpose. To permatently store feature points {@see exportFeaturePointsToCSV}
// *
// * @param featurePointList
// * @param objectName path + name of file
// * @throws IOException
// */
// public static void testExportFeaturePointsToCSV(List<FeaturePoint> featurePointList, String objectName) throws IOException {
//// File csvOutputFile = new File(String.format("%s_test_landmarks.csv", objectName));
// File csvOutputFile = File.createTempFile(String.format("%s_test_landmarks", objectName), "csv");
// writeToFile(featurePointList, csvOutputFile, objectName);
// }
/**
* Handles logic and format of exported csv file
*
* @param featurePointList
* @param csvOutputFile
* @param objectName
* @throws IOException
*/
public static void writeToFile(List<FeaturePoint> featurePointList, File csvOutputFile, String objectName) throws IOException {
// CSV is a normal text file, need a writer
try (BufferedWriter bw = new BufferedWriter(new FileWriter(csvOutputFile))) {
bw.write("Scan name");
......@@ -53,5 +82,4 @@ public class FeaturePointCsvExporter {
});
}
}
}
}
\ No newline at end of file
......@@ -25,10 +25,14 @@ public class FeaturePointFpExporter {
* @throws FileNotFoundException
* @throws IOException
*/
public static void exportFeaturePointsToFP(List<FeaturePoint> featurePointList, String objectName) throws FileNotFoundException, IOException {
File csvOutputFile = new File(String.format("%s_landmarks.fp", objectName));
public static void exportFeaturePointsToFP(List<FeaturePoint> featurePointList, String objectName) throws IOException {
File fpOutputFile = new File(String.format("%s_landmarks.fp", objectName));
writeToFile(featurePointList, fpOutputFile, objectName);
}
public static void writeToFile(List<FeaturePoint> featurePointList, File fpOutputFile, String objectName) throws IOException {
// CSV is a normal text file, need a writer
try (BufferedWriter bw = new BufferedWriter(new FileWriter(csvOutputFile))) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(fpOutputFile))) {
bw.write(String.format("<!--Saved by software Fidentis Analyst--><facialPoints model=\"%s\">", objectName));
featurePointList.forEach(featurePoint -> {
try {
......@@ -47,7 +51,6 @@ public class FeaturePointFpExporter {
bw.newLine();
bw.write("</facialPoints>");
}
}
}
}
......@@ -8,6 +8,7 @@ package cz.fidentis.analyst.feature.services;
import cz.fidentis.analyst.feature.FeaturePoint;
import cz.fidentis.analyst.feature.services.FeaturePointCsvLoader;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
......@@ -16,6 +17,9 @@ import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.DisplayName;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotSame;
/**
* @author Jakub Kolman
*/
......@@ -27,23 +31,45 @@ public class FeaturePointExportServiceTest {
@DisplayName("Test writing a CSV file")
@Test
void exportFeaturePointsCSVTest() throws IOException {
FeaturePointExportService service = new FeaturePointExportService();
List<FeaturePoint> featurePoints = loadFeaturePoints();
service.exportFeaturePoints(featurePoints, MODEL_TEST_FILE_DIRECTORY.toString() + "test_file_csv", "CSV");
// load model of feature points from file
FeaturePointCsvExporter service = new FeaturePointCsvExporter();
List<FeaturePoint> featurePoints = loadCsvFeaturePoints(MODEL_TEST_FILE_DIRECTORY.toString(), MODEL_FILE);
// create tmp file to write in
File csvOutputFile = File.createTempFile(String.format("%s_test_file_csv_landmarks", MODEL_TEST_FILE_DIRECTORY.toString()), "csv");
service.writeToFile(featurePoints, csvOutputFile, MODEL_TEST_FILE_DIRECTORY.toString() + "/" + "test_file_csv");
// check if model file and created file generate lists with the same elements
List<FeaturePoint> loadedFeaturePoints = loadCsvFeaturePoints(MODEL_TEST_FILE_DIRECTORY.toString(), "test_file_csv_landmarks.csv");
assertEquals(featurePoints.size(), loadedFeaturePoints.size());
assertEquals(featurePoints, loadedFeaturePoints);
assertNotSame(featurePoints, loadedFeaturePoints);
}
@DisplayName("Test writing a FP file")
@Test
void exportFeaturePointsFPTest() throws IOException {
FeaturePointExportService service = new FeaturePointExportService();
List<FeaturePoint> featurePoints = loadFeaturePoints();
service.exportFeaturePoints(featurePoints, MODEL_TEST_FILE_DIRECTORY.toString() + "test_file_fp", "FP");
// load model of feature points from file
FeaturePointFpExporter service = new FeaturePointFpExporter();
List<FeaturePoint> featurePoints = loadCsvFeaturePoints(MODEL_TEST_FILE_DIRECTORY.toString(), MODEL_FILE);
// create tmp file to write in
File fpOutputFile = File.createTempFile(String.format("%s_test_file_fp_landmarks", MODEL_TEST_FILE_DIRECTORY.toString()), "fp");
service.writeToFile(featurePoints, fpOutputFile, MODEL_TEST_FILE_DIRECTORY.toString() + "/" + "test_file_fp");
// check if model file and created file generate lists with the same elements
List<FeaturePoint> loadedFeaturePoints = loadFpFeaturePoints(MODEL_TEST_FILE_DIRECTORY.toString(), "test_file_fp_landmarks.fp");
assertEquals(featurePoints.size(), loadedFeaturePoints.size());
assertEquals(featurePoints, loadedFeaturePoints);
assertNotSame(featurePoints, loadedFeaturePoints);
}
private List<FeaturePoint> loadFeaturePoints() throws IOException {
private List<FeaturePoint> loadCsvFeaturePoints(String path, String fileName) throws IOException {
FeaturePointCsvLoader service = new FeaturePointCsvLoader();
List<FeaturePoint> featurePoints = service.loadFeaturePoints(MODEL_TEST_FILE_DIRECTORY.toString(), MODEL_FILE);
return featurePoints;
}
private List<FeaturePoint> loadFpFeaturePoints(String path, String fileName) throws IOException {
FeaturePointCsvLoader service = new FeaturePointCsvLoader();
List<FeaturePoint> featurePoints = service.loadFeaturePoints(MODEL_TEST_FILE_DIRECTORY.toString(), MODEL_FILE);
return featurePoints;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment