From 134101188d6920f7594185dbcec02c6fd590c738 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Dinis=20Ferreira?= Date: Mon, 6 Apr 2026 22:22:39 +0200 Subject: [PATCH] refactor: convert log calls to parameterized logging in test utilities Replace string concatenation in `LOGGER.x(...)` calls with parameterized `{}` placeholders, using `Supplier` lambdas for lazy argument evaluation. Adds NOPMD suppressions where the call site uses SwtBot's SLF4J logger (not Log4j) so the rule doesn't fire there, and adds `org.apache.logging.log4j.util` to the OSGi manifests of `test.core` and `test.ui` for the `Supplier` import. Co-Authored-By: Claude Opus 4.7 (1M context) --- .../META-INF/MANIFEST.MF | 3 ++- .../tools/ddk/test/core/jupiter/LoggingRule.java | 16 ++++------------ .../META-INF/MANIFEST.MF | 3 ++- .../tools/ddk/test/ui/swtbot/SwtBotButton.java | 8 ++++++-- .../util/DynamicContextActionUiTestUtil.java | 14 ++++---------- .../util/WaitForDynamicContextMenuItem.java | 9 +++------ 6 files changed, 21 insertions(+), 32 deletions(-) diff --git a/com.avaloq.tools.ddk.test.core/META-INF/MANIFEST.MF b/com.avaloq.tools.ddk.test.core/META-INF/MANIFEST.MF index 698a1eb559..ed20f1c667 100644 --- a/com.avaloq.tools.ddk.test.core/META-INF/MANIFEST.MF +++ b/com.avaloq.tools.ddk.test.core/META-INF/MANIFEST.MF @@ -18,7 +18,8 @@ Require-Bundle: org.eclipse.core.runtime, junit-jupiter-api, org.opentest4j, org.eclipse.jdt.annotation -Import-Package: org.apache.logging.log4j +Import-Package: org.apache.logging.log4j, + org.apache.logging.log4j.util Export-Package: com.avaloq.tools.ddk.test.core, com.avaloq.tools.ddk.test.core.data, com.avaloq.tools.ddk.test.core.junit.runners, diff --git a/com.avaloq.tools.ddk.test.core/src/com/avaloq/tools/ddk/test/core/jupiter/LoggingRule.java b/com.avaloq.tools.ddk.test.core/src/com/avaloq/tools/ddk/test/core/jupiter/LoggingRule.java index 9152b473d6..e896c5b895 100644 --- a/com.avaloq.tools.ddk.test.core/src/com/avaloq/tools/ddk/test/core/jupiter/LoggingRule.java +++ b/com.avaloq.tools.ddk.test.core/src/com/avaloq/tools/ddk/test/core/jupiter/LoggingRule.java @@ -54,9 +54,7 @@ public static LoggingRule getInstance() { @Override public void beforeEach(final ExtensionContext context) throws Exception { - if (LOGGER.isInfoEnabled()) { - LOGGER.info("STARTING: " + getDescriptionName(context)); - } + LOGGER.info("STARTING: {}", () -> getDescriptionName(context)); } /** @@ -72,22 +70,16 @@ private String getDescriptionName(final ExtensionContext context) { @Override public void testSuccessful(final ExtensionContext context) { - if (LOGGER.isInfoEnabled()) { - LOGGER.info("SUCCEEDED: " + getDescriptionName(context)); - } + LOGGER.info("SUCCEEDED: {}", () -> getDescriptionName(context)); } @Override public void testFailed(final ExtensionContext context, final Throwable cause) { - if (LOGGER.isInfoEnabled()) { - LOGGER.info("FAILED: " + getDescriptionName(context)); - } + LOGGER.info("FAILED: {}", () -> getDescriptionName(context)); } @Override public void afterEach(final ExtensionContext context) throws Exception { - if (LOGGER.isInfoEnabled()) { - LOGGER.info("FINISHED: " + getDescriptionName(context)); - } + LOGGER.info("FINISHED: {}", () -> getDescriptionName(context)); } } diff --git a/com.avaloq.tools.ddk.test.ui/META-INF/MANIFEST.MF b/com.avaloq.tools.ddk.test.ui/META-INF/MANIFEST.MF index 9da8b65993..a767c9ff4a 100644 --- a/com.avaloq.tools.ddk.test.ui/META-INF/MANIFEST.MF +++ b/com.avaloq.tools.ddk.test.ui/META-INF/MANIFEST.MF @@ -29,6 +29,7 @@ Export-Package: com.avaloq.tools.ddk.test.ui, com.avaloq.tools.ddk.test.ui.swtbot, com.avaloq.tools.ddk.test.ui.swtbot.condition, com.avaloq.tools.ddk.test.ui.swtbot.util -Import-Package: org.slf4j, org.apache.logging.log4j +Import-Package: org.slf4j, org.apache.logging.log4j, + org.apache.logging.log4j.util Eclipse-RegisterBuddy: org.eclipse.swtbot.swt.finder Automatic-Module-Name: com.avaloq.tools.ddk.test.ui diff --git a/com.avaloq.tools.ddk.test.ui/src/com/avaloq/tools/ddk/test/ui/swtbot/SwtBotButton.java b/com.avaloq.tools.ddk.test.ui/src/com/avaloq/tools/ddk/test/ui/swtbot/SwtBotButton.java index 58c71d8f65..828d39ae02 100644 --- a/com.avaloq.tools.ddk.test.ui/src/com/avaloq/tools/ddk/test/ui/swtbot/SwtBotButton.java +++ b/com.avaloq.tools.ddk.test.ui/src/com/avaloq/tools/ddk/test/ui/swtbot/SwtBotButton.java @@ -39,7 +39,9 @@ public SwtBotButton(final Button button, final SelfDescribing description) { */ @Override public SwtBotButton click() { - log.debug("Clicking on {}", SWTUtils.getText(widget)); //$NON-NLS-1$ + if (log.isDebugEnabled()) { + log.debug("Clicking on {}", SWTUtils.getText(widget)); //$NON-NLS-1$ + } waitForEnabled(); notify(SWT.MouseEnter); notify(SWT.MouseMove); @@ -48,7 +50,9 @@ public SwtBotButton click() { notify(SWT.MouseDown); notify(SWT.MouseUp); notify(SWT.Selection); - log.debug("Clicked on {}", SWTUtils.getText(widget)); //$NON-NLS-1$ + if (log.isDebugEnabled()) { + log.debug("Clicked on {}", SWTUtils.getText(widget)); //$NON-NLS-1$ + } return this; } } diff --git a/com.avaloq.tools.ddk.test.ui/src/com/avaloq/tools/ddk/test/ui/swtbot/util/DynamicContextActionUiTestUtil.java b/com.avaloq.tools.ddk.test.ui/src/com/avaloq/tools/ddk/test/ui/swtbot/util/DynamicContextActionUiTestUtil.java index 14b299c7ac..d71242a0e7 100644 --- a/com.avaloq.tools.ddk.test.ui/src/com/avaloq/tools/ddk/test/ui/swtbot/util/DynamicContextActionUiTestUtil.java +++ b/com.avaloq.tools.ddk.test.ui/src/com/avaloq/tools/ddk/test/ui/swtbot/util/DynamicContextActionUiTestUtil.java @@ -65,9 +65,7 @@ public static void clickContextMenu(final AbstractSWTBot bot, * @throw {@link WidgetNotFoundException} if the context menu could not be found */ public static void clickContextMenu(final Runnable setSelection, final AbstractSWTBot bot, final DynamicMenuPredicate predicate, final String... labels) { - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("clickContextMenu(" + bot + " , " + Arrays.asList(labels) + ");"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ - } + LOGGER.debug("clickContextMenu({} , {});", () -> bot, () -> Arrays.asList(labels)); //$NON-NLS-1$ try { setSelection.run(); logSelection(bot); @@ -101,7 +99,7 @@ private static void logSelection(final AbstractSWTBot bot) { if (LOGGER.isDebugEnabled()) { TableCollection selection = tryGetSelection(bot); if (selection != null) { - LOGGER.debug("setSelection(" + selection + ")"); // NOPMD GuardLogStatement //$NON-NLS-1$//$NON-NLS-2$ + LOGGER.debug("setSelection({})", selection); //$NON-NLS-1$ } } } @@ -140,9 +138,7 @@ private static void tryUnselect(final AbstractSWTBot bot) { * @return {@code true} if on the given widget bot a context menu with the given text is available, else {@code false} */ public static boolean hasContextMenuItem(final AbstractSWTBot widgetBot, final DynamicMenuPredicate predicate, final String... labels) { - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("hasContextMenuItem(" + widgetBot + " , " + Arrays.asList(labels) + ");"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ - } + LOGGER.debug("hasContextMenuItem({} , {});", () -> widgetBot, () -> Arrays.asList(labels)); //$NON-NLS-1$ try { return new SwtBotDynamicContextMenu(widgetBot.contextMenu(), predicate).menu(labels) != null; } catch (WidgetNotFoundException widgetNotFound) { @@ -164,9 +160,7 @@ public static boolean hasContextMenuItem(final AbstractSWTBot * @throw {@link WidgetNotFoundException} if the context menu could not be found */ public static boolean isEnabled(final AbstractSWTBot widgetBot, final DynamicMenuPredicate predicate, final String... labels) { - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("isEnabled(" + widgetBot + " , " + Arrays.asList(labels) + ");"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ - } + LOGGER.debug("isEnabled({} , {});", () -> widgetBot, () -> Arrays.asList(labels)); //$NON-NLS-1$ try { return new SwtBotDynamicContextMenu(widgetBot.contextMenu(), predicate).menu(labels).isEnabled(); } catch (WidgetNotFoundException widgetNotFound) { diff --git a/com.avaloq.tools.ddk.test.ui/src/com/avaloq/tools/ddk/test/ui/swtbot/util/WaitForDynamicContextMenuItem.java b/com.avaloq.tools.ddk.test.ui/src/com/avaloq/tools/ddk/test/ui/swtbot/util/WaitForDynamicContextMenuItem.java index f789eca101..d448b762c0 100644 --- a/com.avaloq.tools.ddk.test.ui/src/com/avaloq/tools/ddk/test/ui/swtbot/util/WaitForDynamicContextMenuItem.java +++ b/com.avaloq.tools.ddk.test.ui/src/com/avaloq/tools/ddk/test/ui/swtbot/util/WaitForDynamicContextMenuItem.java @@ -34,6 +34,7 @@ */ public class WaitForDynamicContextMenuItem extends WaitForMenuItem { private static final Logger LOGGER = LogManager.getLogger(WaitForDynamicContextMenuItem.class); + private static final String LOG_MENU_ITEM_FINDER = "MenuItem finder matching {}"; //$NON-NLS-1$ /** * Custom wrapper around a given {@link Matcher} that collects data about waiting dynamic menu items as @@ -78,18 +79,14 @@ public WaitForDynamicContextMenuItem(final SwtBotDynamicContextMenuItem menu, fi super(menu, new DynamicContextMenuItemMatcher(matcher, menu.getPredicate()), recursive, index); itemMatcher = (DynamicContextMenuItemMatcher) this.matcher; this.widget = menu.widget; - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("MenuItem finder matching " + itemMatcher); //$NON-NLS-1$ - } + LOGGER.debug(LOG_MENU_ITEM_FINDER, itemMatcher); } public WaitForDynamicContextMenuItem(final SwtBotDynamicContextMenu menu, final Matcher matcher, final boolean recursive, final int index) { super(menu, new DynamicContextMenuItemMatcher(matcher, menu.getPredicate()), recursive, index); itemMatcher = (DynamicContextMenuItemMatcher) this.matcher; this.widget = menu.widget; - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("MenuItem finder matching " + itemMatcher); //$NON-NLS-1$ - } + LOGGER.debug(LOG_MENU_ITEM_FINDER, itemMatcher); } @Override