Skip to content

Commit

Permalink
Add getName test with invalid type (and handle it)
Browse files Browse the repository at this point in the history
  • Loading branch information
ppouchin committed Nov 15, 2024
1 parent 8dac9af commit 7d41795
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
24 changes: 13 additions & 11 deletions src/main/java/fr/igred/ij/plugin/OMEROMacroExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -1327,19 +1327,21 @@ public void unlink(String type1, long id1, String type2, long id2) {
* @return The object name.
*/
public String getName(String type, long id) {
String name = null;
String name = "";

GenericObjectWrapper<?> object = getObject(type, id);
if (object instanceof GenericRepositoryObjectWrapper<?>) {
name = ((GenericRepositoryObjectWrapper<?>) object).getName();
} else if (object instanceof TagAnnotationWrapper) {
name = ((TagAnnotationWrapper) object).getName();
} else if (object instanceof MapAnnotationWrapper) {
MapAnnotationWrapper map = (MapAnnotationWrapper) object;
name = map.getContentAsEntryList()
.stream()
.map(e -> e.getKey() + "\t" + e.getValue())
.collect(Collectors.joining(String.format("%n")));
if (object != null) {
if (object instanceof GenericRepositoryObjectWrapper<?>) {
name = ((GenericRepositoryObjectWrapper<?>) object).getName();
} else if (object instanceof TagAnnotationWrapper) {
name = ((TagAnnotationWrapper) object).getName();
} else if (object instanceof MapAnnotationWrapper) {
MapAnnotationWrapper map = (MapAnnotationWrapper) object;
name = map.getContentAsEntryList()
.stream()
.map(e -> e.getKey() + "\t" + e.getValue())
.collect(Collectors.joining(String.format("%n")));
}
}
return name;
}
Expand Down
16 changes: 14 additions & 2 deletions src/test/java/fr/igred/ij/plugin/OMEROExtensionErrorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,17 @@ void testListInvalidType(String type1, String type2, Double id) {
}


@Test
void testGetNameInvalidType() {
String type = "hello";
Object[] args = {type, 1.0};
String output = ext.handleExtension("getName", args);
String error = outContent.toString().trim();
assertTrue(output.isEmpty());
assertTrue(error.startsWith("Invalid type: hello."));
}


@ParameterizedTest
@ValueSource(strings = {"link", "unlink"})
@Disabled("Methods no longer try to link or unlink invalid types currently")
Expand All @@ -237,11 +248,12 @@ void testCannotLinkOrUnlink(String function, String type1, String type2) {
assertEquals(expected, outContent.toString().trim());
}


@Test
void testKeyNotExist() {
final String key = "notExist";
final String key = "notExist";
final double imageId = 2;
Object[] args = {"image", imageId, key, null};
Object[] args = {"image", imageId, key, null};
ext.handleExtension("getValue", args);
String expected = "Could not retrieve value: Key \"" + key + "\" not found";
assertEquals(expected, outContent.toString().trim());
Expand Down

0 comments on commit 7d41795

Please sign in to comment.