@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)); }
public Processor createMockProcessor(String processorPath, boolean useLocationSettings) { ComponentLocation componentLocation = mock(ComponentLocation.class); when(componentLocation.getLocation()).thenReturn(processorPath); when(componentLocation.getFileName()).thenReturn(useLocationSettings ? of(CONFIG_FILE_NAME) : empty()); when(componentLocation.getLineInFile()).thenReturn(useLocationSettings ? of(LINE_NUMBER) : empty()); Component annotatedMessageProcessor = (Component) mock(Processor.class, withSettings().extraInterfaces(Component.class).defaultAnswer(RETURNS_DEEP_STUBS)); when(annotatedMessageProcessor.getAnnotation(any())).thenReturn(null); when(annotatedMessageProcessor.getLocation()).thenReturn(componentLocation); return (Processor) annotatedMessageProcessor; }
/** * @return the first {@code test:processor} from a flow with the provided name. */ public static FunctionalTestProcessor getFromFlow(ConfigurationComponentLocator locator, String flowName) throws Exception { return locator.find(ComponentIdentifier.builder().namespace("test").name("processor").build()) .stream() .filter(c -> flowName.equals(c.getRootContainerLocation().toString())) .map(c -> (FunctionalTestProcessor) c) .findAny().get(); }
/** * {@inheritDoc} */ @Override public ComponentLocation getComponentLocation() { return component.getLocation(); }
/** * 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)); }
private void withXmlElement(Component component, String value) { when(component.getAnnotation(SOURCE_ELEMENT_ANNOTATION_KEY)).thenReturn(value); } }
@Test public void implementsAnnotated() throws Exception { Component annotated = addAnnotationsToClass(ImplementsAnnotated.class).newInstance(); assertThat(annotated.getAnnotations(), is(nullValue())); }
@Override public Supplier<ComponentLocation> resolve(ExecutionContext executionContext) { return () -> ((ExecutionContextAdapter) executionContext).getComponent().getLocation(); } }
@Override public B createInstance() throws Exception { B annotatedInstance = doCreateInstance(); annotatedInstance.setAnnotations(getAnnotations()); return annotatedInstance; }
private void setCacheIdGenerator() { DslResolvingContext context = DslResolvingContext.getDefault(extensionManager.getExtensions()); MetadataCacheIdGeneratorFactory.ComponentLocator<ComponentConfiguration> configLocator = location -> componentLocator .find(location) .map(component -> (ComponentConfiguration) component.getAnnotation(ANNOTATION_COMPONENT_CONFIG)); this.cacheIdGenerator = cacheIdGeneratorFactory.create(context, configLocator); }
@Test public void implementsAnnotated() throws Exception { Component annotated = addAnnotationsToClass(ImplementsAnnotated.class).newInstance(); assertThat(annotated.getAnnotations(), is(nullValue())); }
/** * Adds a new component to the locator. * * @param component the component to be added */ public void addComponent(Component component) { this.componentsMap.put(component.getLocation().getLocation(), component); }
@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)); }
@Before public void setupMVEL() throws InitialisationException { mvel = new MVELExpressionLanguage(muleContext); mvel.initialise(); flowConstruct = mock(FlowConstruct.class, withSettings().extraInterfaces(Component.class)); when(flowConstruct.getName()).thenReturn("myFlow"); final DefaultComponentLocation location = fromSingleComponent("myFlow"); when(((Component) flowConstruct).getAnnotation(LOCATION_KEY)).thenReturn(location); when(((Component) flowConstruct).getLocation()).thenReturn(location); }
@Override public T getObject() throws Exception { if (value instanceof Component) { ((Component) value).setAnnotations(getAnnotations()); } if (!(value instanceof Initialisable)) { muleContext.getInjector().inject(value); } return value; }
@Override public void onStart(SourceCallback<InputStream, Void> sourceCallback) throws MuleException { if (nextOperationReference != null) { Component proc = locator.find(builderFromStringRepresentation(location.getParts().get(0).getPartPath()).addProcessorsPart() .addIndexPart(0).build()) .orElseThrow(() -> new IllegalArgumentException("Missing processor after this source")); Map<String, Object> parameters = (Map<String, Object>) proc.getAnnotation(ANNOTATION_PARAMETERS); if (!parameters.get("name").equals(nextOperationReference)) { throw new IllegalArgumentException(String.format("Next processor %s does not match the expected operation %s", parameters.get("name"), nextOperationReference)); } } scheduler = schedulerService.cpuLightScheduler(); sourceCallbackHandleTask = scheduler.scheduleAtFixedRate(() -> sourceCallback.handle(Result.<InputStream, Void>builder() .output(new ByteArrayInputStream(getSpellBytes(spell))) .build()), 0, castFrequencyInMillis, MILLISECONDS); }
/** * Returns the {@link FlowConstruct} of the root container element if there is one. Otherwise return {@link Optional#empty()} * * @param componentLocator the locator for the {@link FlowConstruct} * @param component the component that may be configured within a {@link FlowConstruct} * @return the {@link FlowConstruct} of the root container element if there is one. Otherwise return {@link Optional#empty()} */ static Optional<FlowConstruct> getFromAnnotatedObject(ConfigurationComponentLocator componentLocator, Component component) { Optional<Component> objectFoundOptional = componentLocator.find(component.getRootContainerLocation()); Optional<FlowConstruct> flowConstruct = objectFoundOptional.flatMap(objectFound -> objectFound instanceof FlowConstruct ? of((FlowConstruct) objectFound) : empty()).filter(object -> object != null); return flowConstruct; }
@Override public Object postProcessAfterInitialization(Object o, String s) throws BeansException { // We check for instanceof Component because this method may be called for spring objects if ((!(o instanceof FactoryBean) || o instanceof ComponentFactory) && o instanceof Component && ((Component) o).getLocation() != null) { componentLocator.addComponent((Component) o); } return o; } }
@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))); }
@Override public ReactiveProcessor apply(Processor component, ReactiveProcessor next) { if (!isInterceptable(component)) { return next; } final ComponentLocation componentLocation = ((Component) component).getLocation(); if (!interceptorFactory.intercept(componentLocation)) { return next; } final ProcessorInterceptor interceptor = interceptorFactory.get(); Map<String, String> dslParameters = (Map<String, String>) ((Component) component).getAnnotation(ANNOTATION_PARAMETERS); ReactiveProcessor interceptedProcessor = doApply(component, next, componentLocation, interceptor, dslParameters); LOGGER.debug("Interceptor '{}' for processor '{}' configured.", interceptor, componentLocation.getLocation()); return interceptedProcessor; }