From 7d41795723a3d0c8ce269863695f8d94b80b3761 Mon Sep 17 00:00:00 2001 From: Pierre Pouchin Date: Fri, 15 Nov 2024 14:58:19 +0100 Subject: [PATCH] Add getName test with invalid type (and handle it) --- .../igred/ij/plugin/OMEROMacroExtension.java | 24 ++++++++++--------- .../ij/plugin/OMEROExtensionErrorTest.java | 16 +++++++++++-- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/main/java/fr/igred/ij/plugin/OMEROMacroExtension.java b/src/main/java/fr/igred/ij/plugin/OMEROMacroExtension.java index d7b47c2..d6c7770 100644 --- a/src/main/java/fr/igred/ij/plugin/OMEROMacroExtension.java +++ b/src/main/java/fr/igred/ij/plugin/OMEROMacroExtension.java @@ -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; } diff --git a/src/test/java/fr/igred/ij/plugin/OMEROExtensionErrorTest.java b/src/test/java/fr/igred/ij/plugin/OMEROExtensionErrorTest.java index 402de8e..0ea5174 100644 --- a/src/test/java/fr/igred/ij/plugin/OMEROExtensionErrorTest.java +++ b/src/test/java/fr/igred/ij/plugin/OMEROExtensionErrorTest.java @@ -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") @@ -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());