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; package cz.fidentis.analyst.feature.services;
import cz.fidentis.analyst.feature.FeaturePoint; import cz.fidentis.analyst.feature.FeaturePoint;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
...@@ -17,7 +18,8 @@ import java.util.logging.Logger; ...@@ -17,7 +18,8 @@ import java.util.logging.Logger;
public class FeaturePointCsvExporter { 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 featurePointList
* @param objectName * @param objectName
...@@ -25,6 +27,33 @@ public class FeaturePointCsvExporter { ...@@ -25,6 +27,33 @@ public class FeaturePointCsvExporter {
*/ */
public static void exportFeaturePointsToCSV(List<FeaturePoint> featurePointList, String objectName) throws IOException { public static void exportFeaturePointsToCSV(List<FeaturePoint> featurePointList, String objectName) throws IOException {
File csvOutputFile = new File(String.format("%s_landmarks.csv", objectName)); 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 // CSV is a normal text file, need a writer
try (BufferedWriter bw = new BufferedWriter(new FileWriter(csvOutputFile))) { try (BufferedWriter bw = new BufferedWriter(new FileWriter(csvOutputFile))) {
bw.write("Scan name"); bw.write("Scan name");
...@@ -53,5 +82,4 @@ public class FeaturePointCsvExporter { ...@@ -53,5 +82,4 @@ public class FeaturePointCsvExporter {
}); });
} }
} }
}
} \ No newline at end of file
...@@ -25,10 +25,14 @@ public class FeaturePointFpExporter { ...@@ -25,10 +25,14 @@ public class FeaturePointFpExporter {
* @throws FileNotFoundException * @throws FileNotFoundException
* @throws IOException * @throws IOException
*/ */
public static void exportFeaturePointsToFP(List<FeaturePoint> featurePointList, String objectName) throws FileNotFoundException, IOException { public static void exportFeaturePointsToFP(List<FeaturePoint> featurePointList, String objectName) throws IOException {
File csvOutputFile = new File(String.format("%s_landmarks.fp", objectName)); 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 // 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)); bw.write(String.format("<!--Saved by software Fidentis Analyst--><facialPoints model=\"%s\">", objectName));
featurePointList.forEach(featurePoint -> { featurePointList.forEach(featurePoint -> {
try { try {
...@@ -47,7 +51,6 @@ public class FeaturePointFpExporter { ...@@ -47,7 +51,6 @@ public class FeaturePointFpExporter {
bw.newLine(); bw.newLine();
bw.write("</facialPoints>"); bw.write("</facialPoints>");
} }
}
}
} }
...@@ -8,6 +8,7 @@ package cz.fidentis.analyst.feature.services; ...@@ -8,6 +8,7 @@ package cz.fidentis.analyst.feature.services;
import cz.fidentis.analyst.feature.FeaturePoint; import cz.fidentis.analyst.feature.FeaturePoint;
import cz.fidentis.analyst.feature.services.FeaturePointCsvLoader; import cz.fidentis.analyst.feature.services.FeaturePointCsvLoader;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
...@@ -16,6 +17,9 @@ import java.util.List; ...@@ -16,6 +17,9 @@ import java.util.List;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.DisplayName; 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 * @author Jakub Kolman
*/ */
...@@ -27,23 +31,45 @@ public class FeaturePointExportServiceTest { ...@@ -27,23 +31,45 @@ public class FeaturePointExportServiceTest {
@DisplayName("Test writing a CSV file") @DisplayName("Test writing a CSV file")
@Test @Test
void exportFeaturePointsCSVTest() throws IOException { void exportFeaturePointsCSVTest() throws IOException {
FeaturePointExportService service = new FeaturePointExportService(); // load model of feature points from file
List<FeaturePoint> featurePoints = loadFeaturePoints(); FeaturePointCsvExporter service = new FeaturePointCsvExporter();
service.exportFeaturePoints(featurePoints, MODEL_TEST_FILE_DIRECTORY.toString() + "test_file_csv", "CSV"); 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") @DisplayName("Test writing a FP file")
@Test @Test
void exportFeaturePointsFPTest() throws IOException { void exportFeaturePointsFPTest() throws IOException {
FeaturePointExportService service = new FeaturePointExportService(); // load model of feature points from file
List<FeaturePoint> featurePoints = loadFeaturePoints(); FeaturePointFpExporter service = new FeaturePointFpExporter();
service.exportFeaturePoints(featurePoints, MODEL_TEST_FILE_DIRECTORY.toString() + "test_file_fp", "FP"); 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(); FeaturePointCsvLoader service = new FeaturePointCsvLoader();
List<FeaturePoint> featurePoints = service.loadFeaturePoints(MODEL_TEST_FILE_DIRECTORY.toString(), MODEL_FILE); List<FeaturePoint> featurePoints = service.loadFeaturePoints(MODEL_TEST_FILE_DIRECTORY.toString(), MODEL_FILE);
return featurePoints; 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