From cee2c5015616a70fe8d69b643b4666c454a3a2f3 Mon Sep 17 00:00:00 2001 From: elrodro83 Date: Fri, 13 Dec 2024 19:49:36 -0300 Subject: [PATCH] fix config failures tests notifications --- .../DefaultNotificationListenerRegistry.java | 24 ++++++++------ ...AbstractConfigurationFailuresTestCase.java | 33 +++---------------- ...nListenerRegistryConfigurationBuilder.java | 31 ++++++++++++++++- 3 files changed, 49 insertions(+), 39 deletions(-) diff --git a/core/src/main/java/org/mule/runtime/core/internal/context/notification/DefaultNotificationListenerRegistry.java b/core/src/main/java/org/mule/runtime/core/internal/context/notification/DefaultNotificationListenerRegistry.java index ad059064799e..5805537b683b 100644 --- a/core/src/main/java/org/mule/runtime/core/internal/context/notification/DefaultNotificationListenerRegistry.java +++ b/core/src/main/java/org/mule/runtime/core/internal/context/notification/DefaultNotificationListenerRegistry.java @@ -6,13 +6,14 @@ */ package org.mule.runtime.core.internal.context.notification; -import static java.util.Objects.requireNonNull; import static org.mule.runtime.core.api.config.i18n.CoreMessages.serverNotificationManagerNotEnabled; -import org.mule.runtime.core.api.MuleContext; +import static java.util.Objects.requireNonNull; + import org.mule.runtime.api.notification.Notification; import org.mule.runtime.api.notification.NotificationListener; import org.mule.runtime.api.notification.NotificationListenerRegistry; +import org.mule.runtime.core.api.context.notification.ServerNotificationManager; import java.util.function.Predicate; @@ -25,27 +26,30 @@ */ public class DefaultNotificationListenerRegistry implements NotificationListenerRegistry { - @Inject - private MuleContext context; + private ServerNotificationManager notificationManager; @Override public void registerListener(NotificationListener listener) { - requireNonNull(context.getNotificationManager(), serverNotificationManagerNotEnabled().getMessage()); - context.getNotificationManager().addListener(listener); + requireNonNull(notificationManager, serverNotificationManagerNotEnabled().getMessage()); + notificationManager.addListener(listener); } @Override public void registerListener(NotificationListener listener, Predicate selector) { - requireNonNull(context.getNotificationManager(), serverNotificationManagerNotEnabled().getMessage()); + requireNonNull(notificationManager, serverNotificationManagerNotEnabled().getMessage()); requireNonNull(selector); - context.getNotificationManager().addListenerSubscription(listener, selector); + notificationManager.addListenerSubscription(listener, selector); } @Override public void unregisterListener(NotificationListener listener) { - if (context.getNotificationManager() != null) { - context.getNotificationManager().removeListener(listener); + if (notificationManager != null) { + notificationManager.removeListener(listener); } } + @Inject + public void setNotificationManager(ServerNotificationManager notificationManager) { + this.notificationManager = notificationManager; + } } diff --git a/tests/functional/src/main/java/org/mule/functional/junit4/AbstractConfigurationFailuresTestCase.java b/tests/functional/src/main/java/org/mule/functional/junit4/AbstractConfigurationFailuresTestCase.java index 442149343933..6dd16de10e7a 100644 --- a/tests/functional/src/main/java/org/mule/functional/junit4/AbstractConfigurationFailuresTestCase.java +++ b/tests/functional/src/main/java/org/mule/functional/junit4/AbstractConfigurationFailuresTestCase.java @@ -6,11 +6,10 @@ */ package org.mule.functional.junit4; -import static org.mule.runtime.manifest.api.MuleManifest.getMuleManifest; import static org.mule.runtime.core.api.config.bootstrap.ArtifactType.APP; -import static org.mule.runtime.core.api.context.notification.MuleContextNotification.CONTEXT_STARTED; import static org.mule.runtime.core.api.extension.provider.MuleExtensionModelProvider.getExtensionModel; import static org.mule.runtime.core.api.lifecycle.LifecycleUtils.initialiseIfNeeded; +import static org.mule.runtime.manifest.api.MuleManifest.getMuleManifest; import static org.mule.runtime.module.extension.internal.loader.java.AbstractJavaExtensionModelLoader.TYPE_PROPERTY_NAME; import static org.mule.runtime.module.extension.internal.loader.java.AbstractJavaExtensionModelLoader.VERSION; @@ -19,16 +18,14 @@ import static java.util.Collections.singletonList; import static java.util.concurrent.TimeUnit.SECONDS; +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; import org.mule.runtime.api.dsl.DslResolvingContext; import org.mule.runtime.api.exception.MuleException; import org.mule.runtime.api.meta.model.ExtensionModel; import org.mule.runtime.api.metadata.ExpressionLanguageMetadataService; -import org.mule.runtime.api.notification.IntegerAction; -import org.mule.runtime.api.notification.NotificationListenerRegistry; import org.mule.runtime.api.util.concurrent.Latch; import org.mule.runtime.core.api.MuleContext; import org.mule.runtime.core.api.config.ConfigurationBuilder; @@ -37,8 +34,6 @@ import org.mule.runtime.core.api.context.DefaultMuleContextFactory; import org.mule.runtime.core.api.context.MuleContextBuilder; import org.mule.runtime.core.api.context.MuleContextFactory; -import org.mule.runtime.core.api.context.notification.MuleContextNotification; -import org.mule.runtime.core.api.context.notification.MuleContextNotificationListener; import org.mule.runtime.core.internal.context.MuleContextWithRegistry; import org.mule.runtime.extension.api.loader.ExtensionModelLoader; import org.mule.runtime.module.extension.internal.loader.java.CraftedExtensionModelLoader; @@ -55,7 +50,6 @@ import java.util.List; import java.util.Map; import java.util.Set; -import java.util.concurrent.atomic.AtomicReference; import org.junit.Rule; @@ -94,7 +88,8 @@ protected void doConfigure(MuleContext muleContext) throws Exception { builders.add(configurationBuilder); builders.add(testServicesConfigurationBuilder); builders.add(new TestPolicyProviderConfigurationBuilder()); - builders.add(new TestNotificationListenerRegistryConfigurationBuilder()); + final Latch contextStartedLatch = new Latch(); + builders.add(new TestNotificationListenerRegistryConfigurationBuilder(contextStartedLatch)); MuleContextBuilder contextBuilder = MuleContextBuilder.builder(APP); final DefaultMuleConfiguration muleConfiguration = new DefaultMuleConfiguration(); muleConfiguration.setId(AbstractConfigurationFailuresTestCase.class.getSimpleName()); @@ -104,27 +99,9 @@ protected void doConfigure(MuleContext muleContext) throws Exception { contextBuilder.setExecutionClassLoader(new ClassLoader(currentThread().getContextClassLoader()) {}); MuleContextWithRegistry muleContext = (MuleContextWithRegistry) muleContextFactory.createMuleContext(builders, contextBuilder); - final AtomicReference contextStartedLatch = new AtomicReference<>(); - contextStartedLatch.set(new Latch()); - NotificationListenerRegistry notificationListenerRegistry = - muleContext.getRegistry().get(NotificationListenerRegistry.REGISTRY_KEY); - notificationListenerRegistry.registerListener(new MuleContextNotificationListener() { - - @Override - public boolean isBlocking() { - return false; - } - - @Override - public void onNotification(MuleContextNotification notification) { - if (new IntegerAction(CONTEXT_STARTED).equals(notification.getAction())) { - contextStartedLatch.get().countDown(); - } - } - }); muleContext.start(); try { - assertThat(contextStartedLatch.get().await(20, SECONDS), is(true)); + assertThat(contextStartedLatch.await(20, SECONDS), is(true)); } finally { muleContext.stop(); muleContext.dispose(); diff --git a/tests/unit/src/main/java/org/mule/tck/config/TestNotificationListenerRegistryConfigurationBuilder.java b/tests/unit/src/main/java/org/mule/tck/config/TestNotificationListenerRegistryConfigurationBuilder.java index 92956a20b87f..a4343799a6d8 100644 --- a/tests/unit/src/main/java/org/mule/tck/config/TestNotificationListenerRegistryConfigurationBuilder.java +++ b/tests/unit/src/main/java/org/mule/tck/config/TestNotificationListenerRegistryConfigurationBuilder.java @@ -6,18 +6,47 @@ */ package org.mule.tck.config; +import static org.mule.runtime.core.api.context.notification.MuleContextNotification.CONTEXT_STARTED; + +import org.mule.runtime.api.notification.IntegerAction; import org.mule.runtime.api.notification.NotificationListenerRegistry; +import org.mule.runtime.api.util.concurrent.Latch; import org.mule.runtime.core.api.MuleContext; import org.mule.runtime.core.api.config.builders.AbstractConfigurationBuilder; +import org.mule.runtime.core.api.context.notification.MuleContextNotification; +import org.mule.runtime.core.api.context.notification.MuleContextNotificationListener; import org.mule.runtime.core.internal.context.MuleContextWithRegistry; import org.mule.runtime.core.internal.context.notification.DefaultNotificationListenerRegistry; import org.mule.runtime.core.privileged.registry.RegistrationException; public class TestNotificationListenerRegistryConfigurationBuilder extends AbstractConfigurationBuilder { + private Latch contextStartedLatch; + + public TestNotificationListenerRegistryConfigurationBuilder(Latch contextStartedLatch) { + this.contextStartedLatch = contextStartedLatch; + } + @Override protected void doConfigure(MuleContext muleContext) throws RegistrationException { + final DefaultNotificationListenerRegistry notificationListenerRegistry = new DefaultNotificationListenerRegistry(); + notificationListenerRegistry.setNotificationManager(muleContext.getNotificationManager()); + notificationListenerRegistry.registerListener(new MuleContextNotificationListener<>() { + + @Override + public boolean isBlocking() { + return false; + } + + @Override + public void onNotification(MuleContextNotification notification) { + if (new IntegerAction(CONTEXT_STARTED).equals(notification.getAction())) { + contextStartedLatch.countDown(); + } + } + }); + ((MuleContextWithRegistry) muleContext).getRegistry().registerObject(NotificationListenerRegistry.REGISTRY_KEY, - new DefaultNotificationListenerRegistry()); + notificationListenerRegistry); } }