Search in sources :

Example 1 with MuleContextWithRegistries

use of org.mule.runtime.core.internal.context.MuleContextWithRegistries in project mule by mulesoft.

the class MVELExpressionLanguageTestCase method extensionTakesPrecedenceOverAutoResolved.

@Test
public void extensionTakesPrecedenceOverAutoResolved() throws Exception {
    PrivilegedEvent event = this.<PrivilegedEvent.Builder>getEventBuilder().message(of("")).addVariable("foo", "other").build();
    ((MuleContextWithRegistries) muleContext).getRegistry().registerObject("key", (ExpressionLanguageExtension) context -> context.addVariable("foo", "bar"));
    mvel.initialise();
    assertEquals("bar", evaluate("foo", event));
}
Also used : PropertyAccessException(org.mule.mvel2.PropertyAccessException) Arrays(java.util.Arrays) PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) AbstractDataTypeBuilderFactory(org.mule.runtime.api.metadata.AbstractDataTypeBuilderFactory) URL(java.net.URL) JSON(org.mule.runtime.api.metadata.MediaType.JSON) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) Random(java.util.Random) Collections.singletonList(java.util.Collections.singletonList) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) EXPRESSION_WITH_DELIMITER(org.mule.runtime.core.internal.el.mvel.MVELExpressionLanguageTestCase.Variant.EXPRESSION_WITH_DELIMITER) MessageContext(org.mule.runtime.core.internal.el.context.MessageContext) BigDecimal(java.math.BigDecimal) OptimizerFactory(org.mule.mvel2.optimizers.OptimizerFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Locale(java.util.Locale) Map(java.util.Map) Function(org.mule.mvel2.ast.Function) Assert.fail(org.junit.Assert.fail) BigInteger(java.math.BigInteger) InternalMessage(org.mule.runtime.core.internal.message.InternalMessage) URI(java.net.URI) SAFE_REFLECTIVE(org.mule.mvel2.optimizers.OptimizerFactory.SAFE_REFLECTIVE) Parameterized(org.junit.runners.Parameterized) RegexExpressionLanguageFuntion(org.mule.runtime.core.internal.el.mvel.function.RegexExpressionLanguageFuntion) Collection(java.util.Collection) ValidationResult(org.mule.runtime.api.el.ValidationResult) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) Message.of(org.mule.runtime.api.message.Message.of) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) AbstractMuleContextTestCase(org.mule.tck.junit4.AbstractMuleContextTestCase) CompileException(org.mule.mvel2.CompileException) STRING(org.mule.runtime.api.metadata.DataType.STRING) Matchers.is(org.hamcrest.Matchers.is) Mockito.withSettings(org.mockito.Mockito.withSettings) DefaultComponentLocation.fromSingleComponent(org.mule.runtime.dsl.api.component.config.DefaultComponentLocation.fromSingleComponent) FlowConstruct(org.mule.runtime.core.api.construct.FlowConstruct) Pattern(java.util.regex.Pattern) Matchers.containsString(org.hamcrest.Matchers.containsString) Mockito.mock(org.mockito.Mockito.mock) ParserContext(org.mule.mvel2.ParserContext) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) BindingContext(org.mule.runtime.api.el.BindingContext) DefaultComponentLocation(org.mule.runtime.dsl.api.component.config.DefaultComponentLocation) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) HashMap(java.util.HashMap) MuleManifest(org.mule.runtime.core.api.config.MuleManifest) DataHandler(javax.activation.DataHandler) EXPRESSION_STRAIGHT_UP(org.mule.runtime.core.internal.el.mvel.MVELExpressionLanguageTestCase.Variant.EXPRESSION_STRAIGHT_UP) DYNAMIC(org.mule.mvel2.optimizers.OptimizerFactory.DYNAMIC) MuleContextWithRegistries(org.mule.runtime.core.internal.context.MuleContextWithRegistries) ExpressionRuntimeException(org.mule.runtime.core.api.expression.ExpressionRuntimeException) Calendar(java.util.Calendar) DataTypeMatcher.like(org.mule.tck.junit4.matcher.DataTypeMatcher.like) MuleException(org.mule.runtime.api.exception.MuleException) Component(org.mule.runtime.api.component.Component) MimeType(javax.activation.MimeType) Collections.singletonMap(java.util.Collections.singletonMap) LOCATION_KEY(org.mule.runtime.api.component.AbstractComponent.LOCATION_KEY) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) Collections.emptyMap(java.util.Collections.emptyMap) DataType(org.mule.runtime.api.metadata.DataType) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) RegistrationException(org.mule.runtime.core.privileged.registry.RegistrationException) UTF_16(java.nio.charset.StandardCharsets.UTF_16) Test(org.junit.Test) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) OBJECT(org.mule.runtime.api.metadata.DataType.OBJECT) TypedValue(org.mule.runtime.api.metadata.TypedValue) Rule(org.junit.Rule) FileReader(java.io.FileReader) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) Test(org.junit.Test)

Example 2 with MuleContextWithRegistries

use of org.mule.runtime.core.internal.context.MuleContextWithRegistries in project mule by mulesoft.

the class DefaultExpressionManagerTestCase method doNotRegistersMelWhenCompatibilityPluginIsNotInstalled.

@Test
public void doNotRegistersMelWhenCompatibilityPluginIsNotInstalled() throws Exception {
    Registry registry = mock(Registry.class);
    when(registry.lookupByType(DefaultExpressionLanguageFactoryService.class)).thenReturn(of(mock(DefaultExpressionLanguageFactoryService.class, RETURNS_DEEP_STUBS)));
    when(registry.lookupByName(COMPATIBILITY_PLUGIN_INSTALLED)).thenReturn(empty());
    final MuleContextWithRegistries mockMuleContext = MuleContextUtils.mockMuleContext();
    MuleConfiguration config = mockMuleContext.getConfiguration();
    doReturn(true).when(config).isValidateExpressions();
    expressionManager = new DefaultExpressionManager();
    ((DefaultExpressionManager) expressionManager).setRegistry(registry);
    ((DefaultExpressionManager) expressionManager).setMuleContext(mockMuleContext);
    initialiseIfNeeded(expressionManager, false, mockMuleContext);
    expectedException.expect(IllegalStateException.class);
    expectedException.expectMessage("There is no expression language registered for 'mel'");
    expressionManager.isValid("mel:'Hello'");
}
Also used : MuleConfiguration(org.mule.runtime.core.api.config.MuleConfiguration) MuleContextWithRegistries(org.mule.runtime.core.internal.context.MuleContextWithRegistries) Registry(org.mule.runtime.api.artifact.Registry) Test(org.junit.Test)

Example 3 with MuleContextWithRegistries

use of org.mule.runtime.core.internal.context.MuleContextWithRegistries in project mule by mulesoft.

the class TransformDiscoveryTestCase method testSimpleDiscovery.

@Test
public void testSimpleDiscovery() throws Exception {
    MuleRegistry registry = ((MuleContextWithRegistries) muleContext).getRegistry();
    Transformer t = registry.lookupTransformer(DataType.STRING, DataType.fromType(Apple.class));
    assertNotNull(t);
    assertEquals(StringToApple.class, t.getClass());
    t = registry.lookupTransformer(DataType.STRING, DataType.fromType(Orange.class));
    assertNotNull(t);
    assertEquals(StringToOrange.class, t.getClass());
    try {
        registry.lookupTransformer(DataType.STRING, DataType.fromType(Banana.class));
        fail("There is no transformer to go from String to Banana");
    } catch (TransformerException e) {
    // expected
    }
    registry.registerTransformer(new StringToRedApple());
    t = registry.lookupTransformer(DataType.STRING, DataType.fromType(RedApple.class));
    assertNotNull(t);
    assertEquals(StringToRedApple.class, t.getClass());
}
Also used : Apple(org.mule.tck.testmodels.fruit.Apple) RedApple(org.mule.tck.testmodels.fruit.RedApple) Transformer(org.mule.runtime.core.api.transformer.Transformer) MuleContextWithRegistries(org.mule.runtime.core.internal.context.MuleContextWithRegistries) MuleRegistry(org.mule.runtime.core.internal.registry.MuleRegistry) TransformerException(org.mule.runtime.core.api.transformer.TransformerException) Banana(org.mule.tck.testmodels.fruit.Banana) Test(org.junit.Test)

Example 4 with MuleContextWithRegistries

use of org.mule.runtime.core.internal.context.MuleContextWithRegistries in project mule by mulesoft.

the class DefaultPolicyManager method initialise.

@Override
public void initialise() throws InitialisationException {
    operationPolicyProcessorFactory = new DefaultOperationPolicyProcessorFactory(policyStateHandler);
    sourcePolicyProcessorFactory = new DefaultSourcePolicyProcessorFactory(policyStateHandler);
    MuleRegistry registry = ((MuleContextWithRegistries) muleContext).getRegistry();
    policyProvider = registry.lookupLocalObjects(PolicyProvider.class).stream().findFirst().orElse(new NullPolicyProvider());
    sourcePolicyParametersTransformerCollection = registry.lookupObjects(SourcePolicyParametersTransformer.class);
    operationPolicyParametersTransformerCollection = registry.lookupObjects(OperationPolicyParametersTransformer.class);
    policyPointcutParametersManager = new PolicyPointcutParametersManager(registry.lookupObjects(SourcePolicyPointcutParametersFactory.class), registry.lookupObjects(OperationPolicyPointcutParametersFactory.class));
}
Also used : OperationPolicyParametersTransformer(org.mule.runtime.core.api.policy.OperationPolicyParametersTransformer) MuleContextWithRegistries(org.mule.runtime.core.internal.context.MuleContextWithRegistries) MuleRegistry(org.mule.runtime.core.internal.registry.MuleRegistry) SourcePolicyParametersTransformer(org.mule.runtime.core.api.policy.SourcePolicyParametersTransformer)

Example 5 with MuleContextWithRegistries

use of org.mule.runtime.core.internal.context.MuleContextWithRegistries in project mule by mulesoft.

the class TransactionNotificationsTestCase method testTransactionNotifications.

@Test
public void testTransactionNotifications() throws Exception {
    final CountDownLatch latch = new CountDownLatch(3);
    // the code is simple and deceptive :) The trick is this dummy transaction is handled by
    // a global TransactionCoordination instance, which binds it to the current thread.
    Transaction transaction = new DummyTransaction(muleContext);
    ((MuleContextWithRegistries) muleContext).getRegistry().lookupObject(NotificationListenerRegistry.class).registerListener(new TransactionNotificationListener<TransactionNotification>() {

        @Override
        public boolean isBlocking() {
            return false;
        }

        @Override
        public void onNotification(TransactionNotification notification) {
            if (new IntegerAction(TRANSACTION_BEGAN).equals(notification.getAction())) {
                assertEquals("begin", notification.getActionName());
                latch.countDown();
            } else if (new IntegerAction(TRANSACTION_COMMITTED).equals(notification.getAction())) {
                assertEquals("commit", notification.getActionName());
                latch.countDown();
            } else if (new IntegerAction(TRANSACTION_ROLLEDBACK).equals(notification.getAction())) {
                assertEquals("rollback", notification.getActionName());
                latch.countDown();
            }
        }
    }, notification -> transaction.getId().equals(notification.getResourceIdentifier()));
    transaction.begin();
    transaction.commit();
    transaction.rollback();
    // Wait for the notifcation event to be fired as they are queued
    latch.await(2000, MILLISECONDS);
    assertEquals("There are still some notifications left unfired.", 0, latch.getCount());
}
Also used : TransactionNotification(org.mule.runtime.api.notification.TransactionNotification) IntegerAction(org.mule.runtime.api.notification.IntegerAction) AbstractSingleResourceTransaction(org.mule.runtime.core.privileged.transaction.AbstractSingleResourceTransaction) MuleContextWithRegistries(org.mule.runtime.core.internal.context.MuleContextWithRegistries) NotificationListenerRegistry(org.mule.runtime.api.notification.NotificationListenerRegistry) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

MuleContextWithRegistries (org.mule.runtime.core.internal.context.MuleContextWithRegistries)25 Test (org.junit.Test)12 MuleRegistry (org.mule.runtime.core.internal.registry.MuleRegistry)7 Before (org.junit.Before)6 NotificationDispatcher (org.mule.runtime.api.notification.NotificationDispatcher)6 RegistrationException (org.mule.runtime.core.privileged.registry.RegistrationException)6 Collections.singletonMap (java.util.Collections.singletonMap)5 HashMap (java.util.HashMap)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 Map (java.util.Map)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Assert.assertEquals (org.junit.Assert.assertEquals)4 DataType (org.mule.runtime.api.metadata.DataType)4 Transformer (org.mule.runtime.core.api.transformer.Transformer)4 FileReader (java.io.FileReader)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 BigDecimal (java.math.BigDecimal)3 BigInteger (java.math.BigInteger)3 URI (java.net.URI)3