Search in sources :

Example 1 with EndpointInject

use of org.apache.camel.EndpointInject in project camel by apache.

the class CamelPostProcessorHelperTest method testEndpointInjectPollingConsumer.

public void testEndpointInjectPollingConsumer() throws Exception {
    CamelPostProcessorHelper helper = new CamelPostProcessorHelper(context);
    MyEndpointBeanPollingConsumer bean = new MyEndpointBeanPollingConsumer();
    Method method = bean.getClass().getMethod("setConsumer", PollingConsumer.class);
    EndpointInject endpointInject = method.getAnnotation(EndpointInject.class);
    Class<?>[] parameterTypes = method.getParameterTypes();
    for (Class<?> type : parameterTypes) {
        String propertyName = ObjectHelper.getPropertyName(method);
        Object value = helper.getInjectionValue(type, endpointInject.uri(), endpointInject.ref(), endpointInject.property(), propertyName, bean, "foo");
        ObjectHelper.invokeMethod(method, bean, value);
    }
    template.sendBody("seda:foo", "Hello World");
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedBodiesReceived("Hello World");
    assertNotNull(bean.getConsumer());
    Exchange exchange = bean.consume();
    template.send("mock:result", exchange);
    assertMockEndpointsSatisfied();
}
Also used : EndpointInject(org.apache.camel.EndpointInject) Exchange(org.apache.camel.Exchange) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Method(java.lang.reflect.Method)

Example 2 with EndpointInject

use of org.apache.camel.EndpointInject in project camel by apache.

the class CamelPostProcessorHelperTest method testEndpointInjectProducer.

public void testEndpointInjectProducer() throws Exception {
    CamelPostProcessorHelper helper = new CamelPostProcessorHelper(context);
    MyEndpointBeanProducer bean = new MyEndpointBeanProducer();
    Method method = bean.getClass().getMethod("setProducer", Producer.class);
    EndpointInject endpointInject = method.getAnnotation(EndpointInject.class);
    Class<?>[] parameterTypes = method.getParameterTypes();
    for (Class<?> type : parameterTypes) {
        String propertyName = ObjectHelper.getPropertyName(method);
        Object value = helper.getInjectionValue(type, endpointInject.uri(), endpointInject.ref(), endpointInject.property(), propertyName, bean, "foo");
        ObjectHelper.invokeMethod(method, bean, value);
    }
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedBodiesReceived("Hello World");
    assertNotNull(bean.getProducer());
    Exchange exchange = new DefaultExchange(context);
    exchange.getIn().setBody("Hello World");
    bean.send(exchange);
    assertMockEndpointsSatisfied();
}
Also used : EndpointInject(org.apache.camel.EndpointInject) Exchange(org.apache.camel.Exchange) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Method(java.lang.reflect.Method)

Example 3 with EndpointInject

use of org.apache.camel.EndpointInject in project camel by apache.

the class CamelPostProcessorHelperTest method testEndpointInjectProducerTemplateField.

public void testEndpointInjectProducerTemplateField() throws Exception {
    CamelPostProcessorHelper helper = new CamelPostProcessorHelper(context);
    MyEndpointInjectProducerTemplate bean = new MyEndpointInjectProducerTemplate();
    Field field = bean.getClass().getField("producer");
    EndpointInject endpointInject = field.getAnnotation(EndpointInject.class);
    Class<?> type = field.getType();
    String propertyName = "producer";
    Object value = helper.getInjectionValue(type, endpointInject.uri(), endpointInject.ref(), endpointInject.property(), propertyName, bean, "foo");
    field.set(bean, value);
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedBodiesReceived("Hello World");
    Exchange exchange = new DefaultExchange(context);
    exchange.getIn().setBody("Hello World");
    bean.send(exchange);
    assertMockEndpointsSatisfied();
}
Also used : EndpointInject(org.apache.camel.EndpointInject) Exchange(org.apache.camel.Exchange) Field(java.lang.reflect.Field) MockEndpoint(org.apache.camel.component.mock.MockEndpoint)

Example 4 with EndpointInject

use of org.apache.camel.EndpointInject in project camel by apache.

the class CamelPostProcessorHelperTest method testEndpointInjectProducerTemplateFieldUrlUnknown.

public void testEndpointInjectProducerTemplateFieldUrlUnknown() throws Exception {
    CamelPostProcessorHelper helper = new CamelPostProcessorHelper(context);
    MyEndpointInjectProducerTemplateUrlUnknown bean = new MyEndpointInjectProducerTemplateUrlUnknown();
    Field field = bean.getClass().getField("producer");
    EndpointInject endpointInject = field.getAnnotation(EndpointInject.class);
    Class<?> type = field.getType();
    String propertyName = "producer";
    try {
        helper.getInjectionValue(type, endpointInject.uri(), endpointInject.ref(), endpointInject.property(), propertyName, bean, "foo");
        fail("Should throw exception");
    } catch (ResolveEndpointFailedException e) {
        assertEquals("Failed to resolve endpoint: xxx://foo due to: No component found with scheme: xxx", e.getMessage());
    }
}
Also used : EndpointInject(org.apache.camel.EndpointInject) Field(java.lang.reflect.Field) ResolveEndpointFailedException(org.apache.camel.ResolveEndpointFailedException)

Example 5 with EndpointInject

use of org.apache.camel.EndpointInject in project camel by apache.

the class CamelPostProcessorHelperTest method testEndpointInjectBothUriAndRef.

public void testEndpointInjectBothUriAndRef() throws Exception {
    CamelPostProcessorHelper helper = new CamelPostProcessorHelper(context);
    MyEndpointBothUriAndRef bean = new MyEndpointBothUriAndRef();
    Field field = bean.getClass().getField("producer");
    EndpointInject endpointInject = field.getAnnotation(EndpointInject.class);
    Class<?> type = field.getType();
    String propertyName = "producer";
    try {
        helper.getInjectionValue(type, endpointInject.uri(), endpointInject.ref(), endpointInject.property(), propertyName, bean, "foo");
        fail("Should throw exception");
    } catch (IllegalArgumentException e) {
        assertEquals("Both uri and name is provided, only either one is allowed: uri=seda:foo, ref=myEndpoint", e.getMessage());
    }
}
Also used : EndpointInject(org.apache.camel.EndpointInject) Field(java.lang.reflect.Field)

Aggregations

EndpointInject (org.apache.camel.EndpointInject)11 Field (java.lang.reflect.Field)6 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)6 Exchange (org.apache.camel.Exchange)5 Method (java.lang.reflect.Method)3 BeanInject (org.apache.camel.BeanInject)2 Produce (org.apache.camel.Produce)2 PropertyInject (org.apache.camel.PropertyInject)2 InjectionException (javax.enterprise.inject.InjectionException)1 UnsatisfiedResolutionException (javax.enterprise.inject.UnsatisfiedResolutionException)1 NoSuchBeanException (org.apache.camel.NoSuchBeanException)1 ResolveEndpointFailedException (org.apache.camel.ResolveEndpointFailedException)1