Skip to content

Commit

Permalink
Improve tests and code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ppouchin committed Sep 20, 2024
1 parent 9b61eb5 commit e7e085b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
15 changes: 9 additions & 6 deletions src/main/java/fr/igred/ij/plugin/OMEROMacroExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.TreeMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -712,11 +711,15 @@ public void addToTable(String tableName, ResultsTable results, Long imageId, Lis
public void saveTableAsFile(String tableName, String path, CharSequence delimiter) {
TableWrapper table = tables.get(tableName);

char sep = delimiter == null || delimiter.length() != 1 ? DEFAULT_DELIMITER : delimiter.charAt(0);
try {
table.saveAs(path, sep);
} catch (FileNotFoundException | UnsupportedEncodingException e) {
IJ.error("Could not create table file: ", e.getMessage());
if (table != null) {
char sep = delimiter == null || delimiter.length() != 1 ? DEFAULT_DELIMITER : delimiter.charAt(0);
try {
table.saveAs(path, sep);
} catch (FileNotFoundException | UnsupportedEncodingException e) {
IJ.error("Could not create table file: ", e.getMessage());
}
} else {
IJ.error("Table does not exist: " + tableName);
}
}

Expand Down
35 changes: 35 additions & 0 deletions src/test/java/fr/igred/ij/plugin/OMEROExtensionErrorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package fr.igred.ij.plugin;


import ij.measure.ResultsTable;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -30,6 +31,7 @@
import java.io.InputStream;
import java.io.PrintStream;
import java.nio.file.Files;
import java.util.ArrayList;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -228,4 +230,37 @@ void testKeyNotExist() {
assertEquals(expected, outContent.toString().trim());
}


@Test
void testClearTable() throws Exception {
long imageId = 1L;
String label1 = "test";
String label2 = "test2";
double size1 = 25.023579d;
double size2 = 50.0d;

ResultsTable rt1 = new ResultsTable();
rt1.incrementCounter();
rt1.setLabel(label1, 0);
rt1.setValue("Size", 0, size1);

ResultsTable rt2 = new ResultsTable();
rt2.incrementCounter();
rt2.setLabel(label2, 0);
rt2.setValue("Size", 0, size2);

ext.addToTable("test_table", rt1, imageId, new ArrayList<>(0), null);
ext.addToTable("test_table", rt2, imageId, new ArrayList<>(0), null);

Object[] args2 = {"test_table"};
ext.handleExtension("clearTable", args2);

File textFile = new File("test.txt");
Object[] args3 = {"test_table", textFile.getCanonicalPath(), null};
ext.handleExtension("saveTableAsFile", args3);

String expected = "Table does not exist: test_table";
assertEquals(expected, outContent.toString().trim());
}

}
8 changes: 6 additions & 2 deletions src/test/java/fr/igred/ij/plugin/OMEROExtensionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -434,12 +434,15 @@ void testSaveAndGetROIs() {
roi.setImage(imp);
overlay.add(roi);
imp.setOverlay(overlay);

int savedROIs = ext.saveROIs(imp, 1L, "");
overlay.clear();
int loadedROIs = ext.getROIs(imp, 1L, true, "");
ext.removeROIs(1L);
int clearedROIs = ext.getROIs(imp, 1L, true, "");

Object[] args = {1.0d};
ext.handleExtension("removeROIs", args);

int clearedROIs = ext.getROIs(imp, 1L, true, "");

assertEquals(1, savedROIs);
assertEquals(1, loadedROIs);
Expand Down Expand Up @@ -585,6 +588,7 @@ void testTable() throws Exception {
client.connect(HOSTNAME, (int) PORT, USERNAME, PASSWORD.toCharArray());
List<TableWrapper> tables = client.getDataset(1L).getTables(client);
client.disconnect();

assertEquals(1, tables.size());
assertEquals(2, tables.get(0).getRowCount());
assertEquals(size1, tables.get(0).getData(0, 2));
Expand Down

0 comments on commit e7e085b

Please sign in to comment.