/** * Utility method to add a mock component location. * * @param component object to add the location. */ protected void addMockComponentLocation(Component component) { ComponentLocation componentLocation = mock(ComponentLocation.class, RETURNS_DEEP_STUBS); Mockito.when(componentLocation.getLineInFile()).thenReturn(empty()); Mockito.when(componentLocation.getFileName()).thenReturn(empty()); component.setAnnotations(Collections.singletonMap(LOCATION_KEY, componentLocation)); }
@Override public B createInstance() throws Exception { B annotatedInstance = doCreateInstance(); annotatedInstance.setAnnotations(getAnnotations()); return annotatedInstance; }
@Override public T getObject() throws Exception { if (value instanceof Component) { ((Component) value).setAnnotations(getAnnotations()); } if (!(value instanceof Initialisable)) { muleContext.getInjector().inject(value); } return value; }
@Test public void serializable() throws Exception { Component annotated = addAnnotationsToClass(IsSerializable.class).newInstance(); annotated.setAnnotations(singletonMap(LOCATION_KEY, fromSingleComponent("value"))); ((IsSerializable) annotated).setValue("Hello World!"); final ByteArrayOutputStream serializedFormStream = new ByteArrayOutputStream(); final ObjectOutputStream oos = new ObjectOutputStream(serializedFormStream); oos.writeObject(annotated); final Object deserialized = new ObjectInputStream(new ByteArrayInputStream(serializedFormStream.toByteArray())).readObject(); assertThat(deserialized, instanceOf(IsSerializable.class)); assertThat(deserialized, not(instanceOf(Component.class))); assertThat(((IsSerializable) deserialized).getValue(), is("Hello World!")); }
@Test public void extendsAbstractAnnotated() throws Exception { Component annotated = addAnnotationsToClass(ExtendsAnnotated.class).newInstance(); assertThat(annotated.getAnnotations().keySet(), empty()); annotated.setAnnotations(singletonMap(LOCATION_KEY, fromSingleComponent("value"))); assertThat(annotated.getAnnotations().keySet(), contains(LOCATION_KEY)); assertThat(annotated.getClass().getMethod("setSomething", Object.class).isAnnotationPresent(Inject.class), is(true)); }
@Test public void implementsInitialisable() throws Exception { Component annotated = addAnnotationsToClass(ImplementsInitialisable.class).newInstance(); assertThat(annotated.getAnnotations().keySet(), empty()); annotated.setAnnotations(singletonMap(LOCATION_KEY, fromSingleComponent("value"))); assertThat(annotated.getAnnotations().keySet(), contains(LOCATION_KEY)); }
@Test public void notAnnotated() throws Exception { Component annotated = addAnnotationsToClass(NotAnnotated.class).newInstance(); assertThat(annotated.getAnnotations().keySet(), empty()); annotated.setAnnotations(singletonMap(LOCATION_KEY, fromSingleComponent("value"))); assertThat(annotated.getAnnotations().keySet(), contains(LOCATION_KEY)); assertThat(annotated.getClass().getMethod("setSomething", Object.class).isAnnotationPresent(Inject.class), is(true)); assertThat(removeDynamicAnnotations(annotated), instanceOf(NotAnnotated.class)); assertThat(removeDynamicAnnotations(annotated), not(instanceOf(Component.class))); }
of(configLine.getLineNumber()), of(configLine.getStartColumn())); providerComponent.setAnnotations(ImmutableMap.<QName, Object>builder() .put(AbstractComponent.LOCATION_KEY, new DefaultComponentLocation(of(componentIdentifier.getName()),
@Test public void paramWithErrorExpression() throws Exception { Component annotatedProcessor = (Component) processor; Map<QName, Object> annotations = new HashMap<>(annotatedProcessor.getAnnotations()); Map<String, String> params = new HashMap<>((Map<String, String>) annotations.get(ANNOTATION_PARAMETERS)); params.put("errorExpr", "#[notAnExpression]"); annotations.put(ANNOTATION_PARAMETERS, params); ((Component) processor).setAnnotations(annotations); ProcessorInterceptor interceptor = prepareInterceptor(new ProcessorInterceptor() {}); startFlowWithInterceptors(interceptor); process(flow, eventBuilder(muleContext).message(Message.of("")).build()); if (useMockInterceptor) { InOrder inOrder = inOrder(processor, interceptor); inOrder.verify(interceptor) .before(any(), mapArgWithErrorEntry("errorExpr", instanceOf(ExpressionRuntimeException.class)/* "#[notAnExpression]" */), any()); inOrder.verify(interceptor) .around(any(), mapArgWithErrorEntry("errorExpr", instanceOf(ExpressionRuntimeException.class)/* "#[notAnExpression]" */), any(), any()); inOrder.verify(processor).process(any()); inOrder.verify(interceptor).after(any(), any(), any()); verifyParametersResolvedAndDisposed(times(1)); } }
referencedFlow.setAnnotations(annotations); startIfNeeded(referencedFlow);
/** * Updates the {@link Component} root container name. * * @param rootContainerName the root container name of the object. * @param component the {@link Component} to update. */ static void updateRootContainerName(String rootContainerName, Component component) { Map<QName, Object> previousAnnotations = new HashMap<>(component.getAnnotations()); previousAnnotations.put(ROOT_CONTAINER_NAME_KEY, rootContainerName); component.setAnnotations(previousAnnotations); }
/** * Utility method to add a mock component location. * * @param component object to add the location. */ protected void addMockComponentLocation(Component component) { ComponentLocation componentLocation = mock(ComponentLocation.class, RETURNS_DEEP_STUBS); Mockito.when(componentLocation.getLineInFile()).thenReturn(empty()); Mockito.when(componentLocation.getFileName()).thenReturn(empty()); component.setAnnotations(Collections.singletonMap(LOCATION_KEY, componentLocation)); }
@Override public T getObject() throws Exception { T annotatedInstance = doGetObject(); ((Component) annotatedInstance).setAnnotations(getAnnotations()); return annotatedInstance; } }
@Override public B createInstance() throws Exception { B annotatedInstance = doCreateInstance(); annotatedInstance.setAnnotations(getAnnotations()); return annotatedInstance; }
@Override public T getObject() throws Exception { if (value instanceof Component) { ((Component) value).setAnnotations(getAnnotations()); } muleContext.getInjector().inject(value); return value; }
@Test public void extendsAbstractAnnotated() throws Exception { Component annotated = addAnnotationsToClass(ExtendsAnnotated.class).newInstance(); assertThat(annotated.getAnnotations().keySet(), empty()); annotated.setAnnotations(singletonMap(LOCATION_KEY, "value")); assertThat(annotated.getAnnotations().keySet(), contains(LOCATION_KEY)); assertThat(annotated.getClass().getMethod("setSomething", Object.class).isAnnotationPresent(Inject.class), is(true)); }
@Test public void implementsInitialisable() throws Exception { Component annotated = addAnnotationsToClass(ImplementsInitialisable.class).newInstance(); assertThat(annotated.getAnnotations().keySet(), empty()); annotated.setAnnotations(singletonMap(LOCATION_KEY, "value")); assertThat(annotated.getAnnotations().keySet(), contains(LOCATION_KEY)); }
@Test public void notAnnotated() throws Exception { Component annotated = addAnnotationsToClass(NotAnnotated.class).newInstance(); assertThat(annotated.getAnnotations().keySet(), empty()); annotated.setAnnotations(singletonMap(LOCATION_KEY, "value")); assertThat(annotated.getAnnotations().keySet(), contains(LOCATION_KEY)); assertThat(annotated.getClass().getMethod("setSomething", Object.class).isAnnotationPresent(Inject.class), is(true)); assertThat(removeDynamicAnnotations(annotated), instanceOf(NotAnnotated.class)); assertThat(removeDynamicAnnotations(annotated), not(instanceOf(Component.class))); }
protected Processor getReferencedFlow(String name, FlowRefMessageProcessor flowRefMessageProcessor) throws MuleException { if (name == null) { throw new RoutePathNotFoundException(createStaticMessage("flow-ref name expression returned 'null'"), flowRefMessageProcessor); } Component referencedFlow = getReferencedProcessor(name); if (referencedFlow == null) { throw new RoutePathNotFoundException(createStaticMessage("No flow/sub-flow with name '%s' found", name), flowRefMessageProcessor); } // for subflows, we create a new one so it must be initialised manually if (!(referencedFlow instanceof Flow)) { if (referencedFlow instanceof SubflowMessageProcessorChainBuilder) { MessageProcessorChainBuilder chainBuilder = (MessageProcessorChainBuilder) referencedFlow; locator.find(flowRefMessageProcessor.getRootContainerLocation()).filter(c -> c instanceof Flow).map(c -> (Flow) c) .ifPresent(f -> chainBuilder.setProcessingStrategy(f.getProcessingStrategy())); referencedFlow = chainBuilder.build(); } initialiseIfNeeded(referencedFlow, muleContext); Map<QName, Object> annotations = new HashMap<>(referencedFlow.getAnnotations()); annotations.put(ROOT_CONTAINER_NAME_KEY, getRootContainerLocation().toString()); referencedFlow.setAnnotations(annotations); startIfNeeded(referencedFlow); } return (Processor) referencedFlow; }
@Test public void paramWithErrorExpression() throws Exception { Component annotatedProcessor = (Component) processor; Map<QName, Object> annotations = new HashMap<>(annotatedProcessor.getAnnotations()); Map<String, String> params = new HashMap<>((Map<String, String>) annotations.get(ANNOTATION_PARAMETERS)); params.put("errorExpr", "#[notAnExpression]"); annotations.put(ANNOTATION_PARAMETERS, params); ((Component) processor).setAnnotations(annotations); ProcessorInterceptor interceptor = prepareInterceptor(new ProcessorInterceptor() {}); startFlowWithInterceptors(interceptor); process(flow, eventBuilder(muleContext).message(Message.of("")).build()); if (useMockInterceptor) { InOrder inOrder = inOrder(processor, interceptor); inOrder.verify(interceptor) .before(any(), mapArgWithErrorEntry("errorExpr", instanceOf(ExpressionRuntimeException.class)/* "#[notAnExpression]" */), any()); inOrder.verify(interceptor) .around(any(), mapArgWithErrorEntry("errorExpr", instanceOf(ExpressionRuntimeException.class)/* "#[notAnExpression]" */), any(), any()); inOrder.verify(processor).process(any()); inOrder.verify(interceptor).after(any(), any(), any()); verifyParametersResolvedAndDisposed(times(1)); } }