http://git-wip-us.apache.org/repos/asf/camel/blob/641de098/components/camel-metrics/src/test/java/org/apache/camel/metrics/counter/CounterRouteTest.java ---------------------------------------------------------------------- diff --cc components/camel-metrics/src/test/java/org/apache/camel/metrics/counter/CounterRouteTest.java index 0000000,0000000..844401e new file mode 100644 --- /dev/null +++ b/components/camel-metrics/src/test/java/org/apache/camel/metrics/counter/CounterRouteTest.java @@@ -1,0 -1,0 +1,195 @@@ ++package org.apache.camel.metrics.counter; ++ ++import static org.apache.camel.metrics.MetricsComponent.HEADER_COUNTER_DECREMENT; ++import static org.apache.camel.metrics.MetricsComponent.HEADER_COUNTER_INCREMENT; ++import static org.apache.camel.metrics.MetricsComponent.HEADER_METRIC_NAME; ++import static org.apache.camel.metrics.MetricsComponent.METRIC_REGISTRY_NAME; ++import static org.mockito.Mockito.mock; ++import static org.mockito.Mockito.reset; ++import static org.mockito.Mockito.times; ++import static org.mockito.Mockito.when; ++ ++import java.util.HashMap; ++import java.util.Map; ++ ++import org.apache.camel.EndpointInject; ++import org.apache.camel.Produce; ++import org.apache.camel.ProducerTemplate; ++import org.apache.camel.builder.RouteBuilder; ++import org.apache.camel.component.mock.MockEndpoint; ++import org.apache.camel.spring.javaconfig.SingleRouteCamelConfiguration; ++import org.apache.camel.test.spring.CamelSpringDelegatingTestContextLoader; ++import org.apache.camel.test.spring.CamelSpringJUnit4ClassRunner; ++import org.apache.camel.test.spring.MockEndpoints; ++import org.junit.After; ++import org.junit.Before; ++import org.junit.Test; ++import org.junit.runner.RunWith; ++import org.mockito.InOrder; ++import org.mockito.Mockito; ++import org.springframework.context.annotation.Bean; ++import org.springframework.context.annotation.Configuration; ++import org.springframework.test.context.ContextConfiguration; ++ ++import com.codahale.metrics.Counter; ++import com.codahale.metrics.MetricRegistry; ++ ++@RunWith(CamelSpringJUnit4ClassRunner.class) ++@ContextConfiguration( ++ classes = { CounterRouteTest.TestConfig.class }, ++ loader = CamelSpringDelegatingTestContextLoader.class) ++@MockEndpoints ++public class CounterRouteTest { ++ ++ @EndpointInject(uri = "mock:out") ++ private MockEndpoint endpoint; ++ ++ @Produce(uri = "direct:in-1") ++ private ProducerTemplate producer1; ++ ++ @Produce(uri = "direct:in-2") ++ private ProducerTemplate producer2; ++ ++ @Produce(uri = "direct:in-3") ++ private ProducerTemplate producer3; ++ ++ @Produce(uri = "direct:in-4") ++ private ProducerTemplate producer4; ++ ++ private MetricRegistry mockRegistry; ++ ++ private Counter mockCounter; ++ ++ private InOrder inOrder; ++ ++ @Configuration ++ public static class TestConfig extends SingleRouteCamelConfiguration { ++ ++ @Bean ++ @Override ++ public RouteBuilder route() { ++ return new RouteBuilder() { ++ ++ @Override ++ public void configure() throws Exception { ++ from("direct:in-1") ++ .to("metrics:counter:A?increment=5") ++ .to("mock:out"); ++ ++ from("direct:in-2") ++ .to("metrics:counter:A?decrement=9") ++ .to("mock:out"); ++ ++ from("direct:in-3") ++ .setHeader(HEADER_COUNTER_INCREMENT, constant(417L)) ++ .to("metrics:counter:A") ++ .to("mock:out"); ++ ++ from("direct:in-4") ++ .setHeader(HEADER_COUNTER_INCREMENT, simple("${body.length}")) ++ .to("metrics:counter:A") ++ .to("mock:out"); ++ } ++ }; ++ } ++ ++ @Bean(name = METRIC_REGISTRY_NAME) ++ public MetricRegistry getMetricRegistry() { ++ return Mockito.mock(MetricRegistry.class); ++ } ++ } ++ ++ @Before ++ public void setup() { ++ // TODO - 12.05.2014, Lauri - is there any better way to set this up? ++ mockRegistry = endpoint.getCamelContext().getRegistry().lookupByNameAndType(METRIC_REGISTRY_NAME, MetricRegistry.class); ++ mockCounter = mock(Counter.class); ++ inOrder = Mockito.inOrder(mockRegistry, mockCounter); ++ } ++ ++ @After ++ public void tearDown() { ++ endpoint.reset(); ++ reset(mockRegistry, mockCounter); ++ } ++ ++ @Test ++ public void testOverrideMetricsName() throws Exception { ++ when(mockRegistry.counter("B")).thenReturn(mockCounter); ++ endpoint.expectedMessageCount(1); ++ producer1.sendBodyAndHeader(new Object(), HEADER_METRIC_NAME, "B"); ++ endpoint.assertIsSatisfied(); ++ inOrder.verify(mockRegistry, times(1)).counter("B"); ++ inOrder.verify(mockCounter, times(1)).inc(5L); ++ inOrder.verifyNoMoreInteractions(); ++ } ++ ++ @Test ++ public void testOverrideIncrement() throws Exception { ++ when(mockRegistry.counter("A")).thenReturn(mockCounter); ++ endpoint.expectedMessageCount(1); ++ producer1.sendBodyAndHeader(new Object(), HEADER_COUNTER_INCREMENT, 14L); ++ endpoint.assertIsSatisfied(); ++ inOrder.verify(mockRegistry, times(1)).counter("A"); ++ inOrder.verify(mockCounter, times(1)).inc(14L); ++ inOrder.verifyNoMoreInteractions(); ++ } ++ ++ @Test ++ public void testOverrideIncrementAndDecrement() throws Exception { ++ when(mockRegistry.counter("A")).thenReturn(mockCounter); ++ endpoint.expectedMessageCount(1); ++ Map<String, Object> headers = new HashMap<String, Object>(); ++ headers.put(HEADER_COUNTER_INCREMENT, 912L); ++ headers.put(HEADER_COUNTER_DECREMENT, 43219L); ++ producer1.sendBodyAndHeaders(new Object(), headers); ++ endpoint.assertIsSatisfied(); ++ inOrder.verify(mockRegistry, times(1)).counter("A"); ++ inOrder.verify(mockCounter, times(1)).inc(912L); ++ inOrder.verifyNoMoreInteractions(); ++ } ++ ++ @Test ++ public void testOverrideDecrement() throws Exception { ++ when(mockRegistry.counter("A")).thenReturn(mockCounter); ++ endpoint.expectedMessageCount(1); ++ producer2.sendBodyAndHeader(new Object(), HEADER_COUNTER_DECREMENT, 7L); ++ endpoint.assertIsSatisfied(); ++ inOrder.verify(mockRegistry, times(1)).counter("A"); ++ inOrder.verify(mockCounter, times(1)).dec(7L); ++ inOrder.verifyNoMoreInteractions(); ++ } ++ ++ @Test ++ public void testOverrideIncrementWithWrongType() throws Exception { ++ when(mockRegistry.counter("A")).thenReturn(mockCounter); ++ endpoint.expectedMessageCount(1); ++ producer1.sendBodyAndHeader(new Object(), HEADER_COUNTER_INCREMENT, "this is not a valid long value"); ++ endpoint.assertIsSatisfied(); ++ inOrder.verify(mockRegistry, times(1)).counter("A"); ++ inOrder.verifyNoMoreInteractions(); ++ } ++ ++ @Test ++ public void testOverrideUsingConstantValue() throws Exception { ++ when(mockRegistry.counter("A")).thenReturn(mockCounter); ++ endpoint.expectedMessageCount(1); ++ producer3.sendBody(new Object()); ++ endpoint.assertIsSatisfied(); ++ inOrder.verify(mockRegistry, times(1)).counter("A"); ++ inOrder.verify(mockCounter, times(1)).inc(417L); ++ inOrder.verifyNoMoreInteractions(); ++ } ++ ++ @Test ++ public void testOverrideUsingScriptEvaluation() throws Exception { ++ when(mockRegistry.counter("A")).thenReturn(mockCounter); ++ endpoint.expectedMessageCount(1); ++ String message = "Hello from Camel Metrics!"; ++ producer4.sendBody(message); ++ endpoint.assertIsSatisfied(); ++ inOrder.verify(mockRegistry, times(1)).counter("A"); ++ inOrder.verify(mockCounter, times(1)).inc(message.length()); ++ inOrder.verifyNoMoreInteractions(); ++ } ++}
http://git-wip-us.apache.org/repos/asf/camel/blob/641de098/components/camel-metrics/src/test/java/org/apache/camel/metrics/histogram/HistogramEndpointTest.java ---------------------------------------------------------------------- diff --cc components/camel-metrics/src/test/java/org/apache/camel/metrics/histogram/HistogramEndpointTest.java index 0000000,0000000..3ba9ea1 new file mode 100644 --- /dev/null +++ b/components/camel-metrics/src/test/java/org/apache/camel/metrics/histogram/HistogramEndpointTest.java @@@ -1,0 -1,0 +1,74 @@@ ++package org.apache.camel.metrics.histogram; ++ ++import static org.hamcrest.CoreMatchers.is; ++import static org.hamcrest.MatcherAssert.assertThat; ++import static org.hamcrest.Matchers.notNullValue; ++import static org.hamcrest.Matchers.nullValue; ++ ++import org.apache.camel.Producer; ++import org.junit.After; ++import org.junit.Before; ++import org.junit.Test; ++import org.junit.runner.RunWith; ++import org.mockito.InOrder; ++import org.mockito.Mock; ++import org.mockito.Mockito; ++import org.mockito.runners.MockitoJUnitRunner; ++ ++import com.codahale.metrics.MetricRegistry; ++ ++@RunWith(MockitoJUnitRunner.class) ++public class HistogramEndpointTest { ++ ++ private static final String METRICS_NAME = "metrics.name"; ++ private static final Long VALUE = System.currentTimeMillis(); ++ ++ @Mock ++ private MetricRegistry registry; ++ ++ private HistogramEndpoint endpoint; ++ ++ private InOrder inOrder; ++ ++ @Before ++ public void setUp() throws Exception { ++ endpoint = new HistogramEndpoint(registry, METRICS_NAME); ++ inOrder = Mockito.inOrder(registry); ++ } ++ ++ @After ++ public void tearDown() throws Exception { ++ inOrder.verifyNoMoreInteractions(); ++ } ++ ++ @Test ++ public void testHistogramEndpoint() throws Exception { ++ assertThat(endpoint, is(notNullValue())); ++ assertThat(endpoint.getRegistry(), is(registry)); ++ assertThat(endpoint.getMetricsName(), is(METRICS_NAME)); ++ } ++ ++ @Test ++ public void testCreateProducer() throws Exception { ++ Producer producer = endpoint.createProducer(); ++ assertThat(producer, is(notNullValue())); ++ assertThat(producer, is(HistogramProducer.class)); ++ } ++ ++ @Test ++ public void testGetValue() throws Exception { ++ assertThat(endpoint.getValue(), is(nullValue())); ++ } ++ ++ @Test ++ public void testSetValue() throws Exception { ++ assertThat(endpoint.getValue(), is(nullValue())); ++ endpoint.setValue(VALUE); ++ assertThat(endpoint.getValue(), is(VALUE)); ++ } ++ ++ @Test ++ public void testCreateEndpointUri() throws Exception { ++ assertThat(endpoint.createEndpointUri(), is(HistogramEndpoint.ENDPOINT_URI)); ++ } ++} http://git-wip-us.apache.org/repos/asf/camel/blob/641de098/components/camel-metrics/src/test/java/org/apache/camel/metrics/histogram/HistogramProducerTest.java ---------------------------------------------------------------------- diff --cc components/camel-metrics/src/test/java/org/apache/camel/metrics/histogram/HistogramProducerTest.java index 0000000,0000000..8fe1e06 new file mode 100644 --- /dev/null +++ b/components/camel-metrics/src/test/java/org/apache/camel/metrics/histogram/HistogramProducerTest.java @@@ -1,0 -1,0 +1,111 @@@ ++package org.apache.camel.metrics.histogram; ++ ++import static org.apache.camel.metrics.MetricsComponent.HEADER_HISTOGRAM_VALUE; ++import static org.hamcrest.CoreMatchers.is; ++import static org.hamcrest.MatcherAssert.assertThat; ++import static org.mockito.Mockito.times; ++import static org.mockito.Mockito.when; ++ ++import org.apache.camel.Exchange; ++import org.apache.camel.Message; ++import org.junit.Before; ++import org.junit.Test; ++import org.junit.runner.RunWith; ++import org.mockito.InOrder; ++import org.mockito.Mock; ++import org.mockito.Mockito; ++import org.mockito.runners.MockitoJUnitRunner; ++ ++import com.codahale.metrics.Histogram; ++import com.codahale.metrics.MetricRegistry; ++ ++@RunWith(MockitoJUnitRunner.class) ++public class HistogramProducerTest { ++ ++ private static final String METRICS_NAME = "metrics.name"; ++ private static final Long VALUE = System.currentTimeMillis(); ++ ++ @Mock ++ private HistogramEndpoint endpoint; ++ ++ @Mock ++ private MetricRegistry registry; ++ ++ @Mock ++ private Histogram histogram; ++ ++ @Mock ++ private Exchange exchange; ++ ++ @Mock ++ private Message in; ++ ++ private HistogramProducer producer; ++ ++ private InOrder inOrder; ++ ++ @Before ++ public void setUp() throws Exception { ++ producer = new HistogramProducer(endpoint); ++ inOrder = Mockito.inOrder(endpoint, registry, histogram, exchange, in); ++ when(endpoint.getRegistry()).thenReturn(registry); ++ when(registry.histogram(METRICS_NAME)).thenReturn(histogram); ++ when(exchange.getIn()).thenReturn(in); ++ } ++ ++ @Test ++ public void testHistogramProducer() throws Exception { ++ assertThat(producer.getEndpoint().equals(endpoint), is(true)); ++ } ++ ++ @Test ++ public void testProcessValueSet() throws Exception { ++ when(endpoint.getValue()).thenReturn(VALUE); ++ when(in.getHeader(HEADER_HISTOGRAM_VALUE, VALUE, Long.class)).thenReturn(VALUE); ++ producer.doProcess(exchange, endpoint, registry, METRICS_NAME); ++ inOrder.verify(exchange, times(1)).getIn(); ++ inOrder.verify(registry, times(1)).histogram(METRICS_NAME); ++ inOrder.verify(endpoint, times(1)).getValue(); ++ inOrder.verify(in, times(1)).getHeader(HEADER_HISTOGRAM_VALUE, VALUE, Long.class); ++ inOrder.verify(histogram, times(1)).update(VALUE); ++ inOrder.verifyNoMoreInteractions(); ++ } ++ ++ @Test ++ public void testProcessValueNotSet() throws Exception { ++ when(endpoint.getValue()).thenReturn(null); ++ when(in.getHeader(HEADER_HISTOGRAM_VALUE, null, Long.class)).thenReturn(null); ++ producer.doProcess(exchange, endpoint, registry, METRICS_NAME); ++ inOrder.verify(exchange, times(1)).getIn(); ++ inOrder.verify(registry, times(1)).histogram(METRICS_NAME); ++ inOrder.verify(endpoint, times(1)).getValue(); ++ inOrder.verify(in, times(1)).getHeader(HEADER_HISTOGRAM_VALUE, null, Long.class); ++ inOrder.verifyNoMoreInteractions(); ++ } ++ ++ @Test ++ public void testProcessOverrideValue() throws Exception { ++ when(endpoint.getValue()).thenReturn(VALUE); ++ when(in.getHeader(HEADER_HISTOGRAM_VALUE, VALUE, Long.class)).thenReturn(VALUE + 3); ++ producer.doProcess(exchange, endpoint, registry, METRICS_NAME); ++ inOrder.verify(exchange, times(1)).getIn(); ++ inOrder.verify(registry, times(1)).histogram(METRICS_NAME); ++ inOrder.verify(endpoint, times(1)).getValue(); ++ inOrder.verify(in, times(1)).getHeader(HEADER_HISTOGRAM_VALUE, VALUE, Long.class); ++ inOrder.verify(histogram, times(1)).update(VALUE + 3); ++ inOrder.verifyNoMoreInteractions(); ++ } ++ ++ @Test ++ public void testProcessOverrideUriValueNotSet() throws Exception { ++ when(endpoint.getValue()).thenReturn(null); ++ when(in.getHeader(HEADER_HISTOGRAM_VALUE, null, Long.class)).thenReturn(VALUE + 2); ++ producer.doProcess(exchange, endpoint, registry, METRICS_NAME); ++ inOrder.verify(exchange, times(1)).getIn(); ++ inOrder.verify(registry, times(1)).histogram(METRICS_NAME); ++ inOrder.verify(endpoint, times(1)).getValue(); ++ inOrder.verify(in, times(1)).getHeader(HEADER_HISTOGRAM_VALUE, null, Long.class); ++ inOrder.verify(histogram, times(1)).update(VALUE + 2); ++ inOrder.verifyNoMoreInteractions(); ++ } ++} http://git-wip-us.apache.org/repos/asf/camel/blob/641de098/components/camel-metrics/src/test/java/org/apache/camel/metrics/histogram/HistogramRouteTest.java ---------------------------------------------------------------------- diff --cc components/camel-metrics/src/test/java/org/apache/camel/metrics/histogram/HistogramRouteTest.java index 0000000,0000000..cc01a1d new file mode 100644 --- /dev/null +++ b/components/camel-metrics/src/test/java/org/apache/camel/metrics/histogram/HistogramRouteTest.java @@@ -1,0 -1,0 +1,109 @@@ ++package org.apache.camel.metrics.histogram; ++ ++import static org.apache.camel.metrics.MetricsComponent.HEADER_HISTOGRAM_VALUE; ++import static org.apache.camel.metrics.MetricsComponent.HEADER_METRIC_NAME; ++import static org.apache.camel.metrics.MetricsComponent.METRIC_REGISTRY_NAME; ++import static org.mockito.Mockito.reset; ++import static org.mockito.Mockito.times; ++import static org.mockito.Mockito.when; ++ ++import org.apache.camel.EndpointInject; ++import org.apache.camel.Produce; ++import org.apache.camel.ProducerTemplate; ++import org.apache.camel.builder.RouteBuilder; ++import org.apache.camel.component.mock.MockEndpoint; ++import org.apache.camel.spring.javaconfig.SingleRouteCamelConfiguration; ++import org.apache.camel.test.spring.CamelSpringDelegatingTestContextLoader; ++import org.apache.camel.test.spring.CamelSpringJUnit4ClassRunner; ++import org.apache.camel.test.spring.MockEndpoints; ++import org.junit.After; ++import org.junit.Before; ++import org.junit.Test; ++import org.junit.runner.RunWith; ++import org.mockito.InOrder; ++import org.mockito.Mockito; ++import org.springframework.context.annotation.Bean; ++import org.springframework.context.annotation.Configuration; ++import org.springframework.test.context.ContextConfiguration; ++ ++import com.codahale.metrics.Histogram; ++import com.codahale.metrics.MetricRegistry; ++ ++@RunWith(CamelSpringJUnit4ClassRunner.class) ++@ContextConfiguration( ++ classes = { HistogramRouteTest.TestConfig.class }, ++ loader = CamelSpringDelegatingTestContextLoader.class) ++@MockEndpoints ++public class HistogramRouteTest { ++ ++ @EndpointInject(uri = "mock:out") ++ private MockEndpoint endpoint; ++ ++ @Produce(uri = "direct:in") ++ private ProducerTemplate producer; ++ ++ private MetricRegistry mockRegistry; ++ ++ private Histogram mockHistogram; ++ ++ private InOrder inOrder; ++ ++ @Configuration ++ public static class TestConfig extends SingleRouteCamelConfiguration { ++ ++ @Bean ++ @Override ++ public RouteBuilder route() { ++ return new RouteBuilder() { ++ ++ @Override ++ public void configure() throws Exception { ++ from("direct:in") ++ .to("metrics:histogram:A?value=332491") ++ .to("mock:out"); ++ } ++ }; ++ } ++ ++ @Bean(name = METRIC_REGISTRY_NAME) ++ public MetricRegistry getMetricRegistry() { ++ return Mockito.mock(MetricRegistry.class); ++ } ++ } ++ ++ @Before ++ public void setup() { ++ // TODO - 12.05.2014, Lauri - is there any better way to set this up? ++ mockRegistry = endpoint.getCamelContext().getRegistry().lookupByNameAndType(METRIC_REGISTRY_NAME, MetricRegistry.class); ++ mockHistogram = Mockito.mock(Histogram.class); ++ inOrder = Mockito.inOrder(mockRegistry, mockHistogram); ++ } ++ ++ @After ++ public void tearDown() { ++ endpoint.reset(); ++ reset(mockRegistry); ++ } ++ ++ @Test ++ public void testOverrideMetricsName() throws Exception { ++ when(mockRegistry.histogram("B")).thenReturn(mockHistogram); ++ endpoint.expectedMessageCount(1); ++ producer.sendBodyAndHeader(new Object(), HEADER_METRIC_NAME, "B"); ++ endpoint.assertIsSatisfied(); ++ inOrder.verify(mockRegistry, times(1)).histogram("B"); ++ inOrder.verify(mockHistogram, times(1)).update(332491L); ++ inOrder.verifyNoMoreInteractions(); ++ } ++ ++ @Test ++ public void testOverrideValue() throws Exception { ++ when(mockRegistry.histogram("A")).thenReturn(mockHistogram); ++ endpoint.expectedMessageCount(1); ++ producer.sendBodyAndHeader(new Object(), HEADER_HISTOGRAM_VALUE, 181L); ++ endpoint.assertIsSatisfied(); ++ inOrder.verify(mockRegistry, times(1)).histogram("A"); ++ inOrder.verify(mockHistogram, times(1)).update(181L); ++ inOrder.verifyNoMoreInteractions(); ++ } ++} http://git-wip-us.apache.org/repos/asf/camel/blob/641de098/components/camel-metrics/src/test/java/org/apache/camel/metrics/meter/MeterEndpointTest.java ---------------------------------------------------------------------- diff --cc components/camel-metrics/src/test/java/org/apache/camel/metrics/meter/MeterEndpointTest.java index 0000000,0000000..3779da9 new file mode 100644 --- /dev/null +++ b/components/camel-metrics/src/test/java/org/apache/camel/metrics/meter/MeterEndpointTest.java @@@ -1,0 -1,0 +1,75 @@@ ++package org.apache.camel.metrics.meter; ++ ++import static org.hamcrest.CoreMatchers.is; ++import static org.hamcrest.MatcherAssert.assertThat; ++import static org.hamcrest.Matchers.instanceOf; ++import static org.hamcrest.Matchers.notNullValue; ++import static org.hamcrest.Matchers.nullValue; ++ ++import org.apache.camel.Producer; ++import org.junit.After; ++import org.junit.Before; ++import org.junit.Test; ++import org.junit.runner.RunWith; ++import org.mockito.InOrder; ++import org.mockito.Mock; ++import org.mockito.Mockito; ++import org.mockito.runners.MockitoJUnitRunner; ++ ++import com.codahale.metrics.MetricRegistry; ++ ++@RunWith(MockitoJUnitRunner.class) ++public class MeterEndpointTest { ++ ++ private static final String METRICS_NAME = "metrics.name"; ++ private static final Long VALUE = System.currentTimeMillis(); ++ ++ @Mock ++ private MetricRegistry registry; ++ ++ private MeterEndpoint endpoint; ++ ++ private InOrder inOrder; ++ ++ @Before ++ public void setUp() throws Exception { ++ endpoint = new MeterEndpoint(registry, METRICS_NAME); ++ inOrder = Mockito.inOrder(registry); ++ } ++ ++ @After ++ public void tearDown() throws Exception { ++ inOrder.verifyNoMoreInteractions(); ++ } ++ ++ @Test ++ public void testMeterEndpoint() throws Exception { ++ assertThat(endpoint, is(notNullValue())); ++ assertThat(endpoint.getRegistry(), is(registry)); ++ assertThat(endpoint.getMetricsName(), is(METRICS_NAME)); ++ } ++ ++ @Test ++ public void testCreateProducer() throws Exception { ++ Producer producer = endpoint.createProducer(); ++ assertThat(producer, is(notNullValue())); ++ assertThat(producer, is(instanceOf(MeterProducer.class))); ++ } ++ ++ @Test ++ public void testGetMark() throws Exception { ++ assertThat(endpoint.getMark(), is(nullValue())); ++ } ++ ++ @Test ++ public void testSetMark() throws Exception { ++ assertThat(endpoint.getMark(), is(nullValue())); ++ endpoint.setMark(VALUE); ++ assertThat(endpoint.getMark(), is(VALUE)); ++ } ++ ++ @Test ++ public void testCreateEndpointUri() throws Exception { ++ assertThat(endpoint.createEndpointUri(), is(MeterEndpoint.ENDPOINT_URI)); ++ } ++} http://git-wip-us.apache.org/repos/asf/camel/blob/641de098/components/camel-metrics/src/test/java/org/apache/camel/metrics/meter/MeterProducerTest.java ---------------------------------------------------------------------- diff --cc components/camel-metrics/src/test/java/org/apache/camel/metrics/meter/MeterProducerTest.java index 0000000,0000000..66dcce2 new file mode 100644 --- /dev/null +++ b/components/camel-metrics/src/test/java/org/apache/camel/metrics/meter/MeterProducerTest.java @@@ -1,0 -1,0 +1,112 @@@ ++package org.apache.camel.metrics.meter; ++ ++import static org.apache.camel.metrics.MetricsComponent.HEADER_METER_MARK; ++import static org.hamcrest.CoreMatchers.is; ++import static org.hamcrest.MatcherAssert.assertThat; ++import static org.hamcrest.Matchers.notNullValue; ++import static org.mockito.Mockito.times; ++import static org.mockito.Mockito.when; ++ ++import org.apache.camel.Exchange; ++import org.apache.camel.Message; ++import org.junit.Before; ++import org.junit.Test; ++import org.junit.runner.RunWith; ++import org.mockito.InOrder; ++import org.mockito.Mock; ++import org.mockito.Mockito; ++import org.mockito.runners.MockitoJUnitRunner; ++ ++import com.codahale.metrics.Meter; ++import com.codahale.metrics.MetricRegistry; ++ ++@RunWith(MockitoJUnitRunner.class) ++public class MeterProducerTest { ++ ++ private static final String METRICS_NAME = "metrics.name"; ++ private static final Long MARK = 9919120L; ++ ++ @Mock ++ private MeterEndpoint endpoint; ++ ++ @Mock ++ private MetricRegistry registry; ++ ++ @Mock ++ private Meter meter; ++ ++ @Mock ++ private Exchange exchange; ++ ++ @Mock ++ private Message in; ++ ++ private MeterProducer producer; ++ ++ private InOrder inOrder; ++ ++ @Before ++ public void setUp() throws Exception { ++ producer = new MeterProducer(endpoint); ++ inOrder = Mockito.inOrder(endpoint, registry, meter, exchange, in); ++ when(endpoint.getRegistry()).thenReturn(registry); ++ when(registry.meter(METRICS_NAME)).thenReturn(meter); ++ when(exchange.getIn()).thenReturn(in); ++ } ++ ++ @Test ++ public void testMeterProducer() throws Exception { ++ assertThat(producer, is(notNullValue())); ++ assertThat(producer.getEndpoint().equals(endpoint), is(true)); ++ } ++ ++ @Test ++ public void testProcessMarkSet() throws Exception { ++ when(endpoint.getMark()).thenReturn(MARK); ++ when(in.getHeader(HEADER_METER_MARK, MARK, Long.class)).thenReturn(MARK); ++ producer.doProcess(exchange, endpoint, registry, METRICS_NAME); ++ inOrder.verify(exchange, times(1)).getIn(); ++ inOrder.verify(registry, times(1)).meter(METRICS_NAME); ++ inOrder.verify(endpoint, times(1)).getMark(); ++ inOrder.verify(in, times(1)).getHeader(HEADER_METER_MARK, MARK, Long.class); ++ inOrder.verify(meter, times(1)).mark(MARK); ++ inOrder.verifyNoMoreInteractions(); ++ } ++ ++ @Test ++ public void testProcessMarkSetOverrideByHeaderValue() throws Exception { ++ when(endpoint.getMark()).thenReturn(MARK); ++ when(in.getHeader(HEADER_METER_MARK, MARK, Long.class)).thenReturn(MARK + 101); ++ producer.doProcess(exchange, endpoint, registry, METRICS_NAME); ++ inOrder.verify(exchange, times(1)).getIn(); ++ inOrder.verify(registry, times(1)).meter(METRICS_NAME); ++ inOrder.verify(endpoint, times(1)).getMark(); ++ inOrder.verify(in, times(1)).getHeader(HEADER_METER_MARK, MARK, Long.class); ++ inOrder.verify(meter, times(1)).mark(MARK + 101); ++ inOrder.verifyNoMoreInteractions(); ++ } ++ ++ @Test ++ public void testProcessMarkNotSet() throws Exception { ++ when(endpoint.getMark()).thenReturn(null); ++ when(in.getHeader(HEADER_METER_MARK, null, Long.class)).thenReturn(null); ++ producer.doProcess(exchange, endpoint, registry, METRICS_NAME); ++ inOrder.verify(registry, times(1)).meter(METRICS_NAME); ++ inOrder.verify(endpoint, times(1)).getMark(); ++ inOrder.verify(in, times(1)).getHeader(HEADER_METER_MARK, null, Long.class); ++ inOrder.verify(meter, times(1)).mark(); ++ inOrder.verifyNoMoreInteractions(); ++ } ++ ++ @Test ++ public void testProcessMarkNotSetOverrideByHeaderValue() throws Exception { ++ when(endpoint.getMark()).thenReturn(null); ++ when(in.getHeader(HEADER_METER_MARK, null, Long.class)).thenReturn(MARK); ++ producer.doProcess(exchange, endpoint, registry, METRICS_NAME); ++ inOrder.verify(registry, times(1)).meter(METRICS_NAME); ++ inOrder.verify(endpoint, times(1)).getMark(); ++ inOrder.verify(in, times(1)).getHeader(HEADER_METER_MARK, null, Long.class); ++ inOrder.verify(meter, times(1)).mark(MARK); ++ inOrder.verifyNoMoreInteractions(); ++ } ++} http://git-wip-us.apache.org/repos/asf/camel/blob/641de098/components/camel-metrics/src/test/java/org/apache/camel/metrics/meter/MeterRouteTest.java ---------------------------------------------------------------------- diff --cc components/camel-metrics/src/test/java/org/apache/camel/metrics/meter/MeterRouteTest.java index 0000000,0000000..6bd5b59 new file mode 100644 --- /dev/null +++ b/components/camel-metrics/src/test/java/org/apache/camel/metrics/meter/MeterRouteTest.java @@@ -1,0 -1,0 +1,151 @@@ ++package org.apache.camel.metrics.meter; ++ ++import static org.apache.camel.metrics.MetricsComponent.HEADER_METER_MARK; ++import static org.apache.camel.metrics.MetricsComponent.HEADER_METRIC_NAME; ++import static org.apache.camel.metrics.MetricsComponent.METRIC_REGISTRY_NAME; ++import static org.mockito.Mockito.reset; ++import static org.mockito.Mockito.times; ++import static org.mockito.Mockito.when; ++ ++import org.apache.camel.EndpointInject; ++import org.apache.camel.Produce; ++import org.apache.camel.ProducerTemplate; ++import org.apache.camel.builder.RouteBuilder; ++import org.apache.camel.component.mock.MockEndpoint; ++import org.apache.camel.spring.javaconfig.SingleRouteCamelConfiguration; ++import org.apache.camel.test.spring.CamelSpringDelegatingTestContextLoader; ++import org.apache.camel.test.spring.CamelSpringJUnit4ClassRunner; ++import org.apache.camel.test.spring.MockEndpoints; ++import org.junit.After; ++import org.junit.Before; ++import org.junit.Test; ++import org.junit.runner.RunWith; ++import org.mockito.InOrder; ++import org.mockito.Mockito; ++import org.springframework.context.annotation.Bean; ++import org.springframework.context.annotation.Configuration; ++import org.springframework.test.context.ContextConfiguration; ++ ++import com.codahale.metrics.Meter; ++import com.codahale.metrics.MetricRegistry; ++ ++@RunWith(CamelSpringJUnit4ClassRunner.class) ++@ContextConfiguration( ++ classes = { MeterRouteTest.TestConfig.class }, ++ loader = CamelSpringDelegatingTestContextLoader.class) ++@MockEndpoints ++public class MeterRouteTest { ++ ++ @EndpointInject(uri = "mock:out") ++ private MockEndpoint endpoint; ++ ++ @Produce(uri = "direct:in-1") ++ private ProducerTemplate producer1; ++ ++ @Produce(uri = "direct:in-2") ++ private ProducerTemplate producer2; ++ ++ private MetricRegistry mockRegistry; ++ ++ private Meter mockMeter; ++ ++ private InOrder inOrder; ++ ++ @Configuration ++ public static class TestConfig extends SingleRouteCamelConfiguration { ++ ++ @Bean ++ @Override ++ public RouteBuilder route() { ++ return new RouteBuilder() { ++ ++ @Override ++ public void configure() throws Exception { ++ from("direct:in-1") ++ .to("metrics:meter:A?mark=3179") ++ .to("mock:out"); ++ ++ from("direct:in-2") ++ .to("metrics:meter:A") ++ .to("mock:out"); ++ } ++ }; ++ } ++ ++ @Bean(name = METRIC_REGISTRY_NAME) ++ public MetricRegistry getMetricRegistry() { ++ return Mockito.mock(MetricRegistry.class); ++ } ++ } ++ ++ @Before ++ public void setup() { ++ // TODO - 12.05.2014, Lauri - is there any better way to set this up? ++ mockRegistry = endpoint.getCamelContext().getRegistry().lookupByNameAndType(METRIC_REGISTRY_NAME, MetricRegistry.class); ++ mockMeter = Mockito.mock(Meter.class); ++ inOrder = Mockito.inOrder(mockRegistry, mockMeter); ++ when(mockRegistry.meter("A")).thenReturn(mockMeter); ++ } ++ ++ @After ++ public void tearDown() { ++ endpoint.reset(); ++ reset(mockRegistry, mockMeter); ++ } ++ ++ @Test ++ public void testValueSetInUri() throws Exception { ++ Object body = new Object(); ++ endpoint.expectedBodiesReceived(body); ++ producer1.sendBody(body); ++ endpoint.assertIsSatisfied(); ++ inOrder.verify(mockRegistry, times(1)).meter("A"); ++ inOrder.verify(mockMeter, times(1)).mark(3179L); ++ inOrder.verifyNoMoreInteractions(); ++ } ++ ++ @Test ++ public void testValueNoSetInUri() throws Exception { ++ Object body = new Object(); ++ endpoint.expectedBodiesReceived(body); ++ producer2.sendBody(body); ++ endpoint.assertIsSatisfied(); ++ inOrder.verify(mockRegistry, times(1)).meter("A"); ++ inOrder.verify(mockMeter, times(1)).mark(); ++ inOrder.verifyNoMoreInteractions(); ++ } ++ ++ @Test ++ public void testOverrideMetricsName() throws Exception { ++ when(mockRegistry.meter("B")).thenReturn(mockMeter); ++ Object body = new Object(); ++ endpoint.expectedBodiesReceived(body); ++ producer1.sendBodyAndHeader(body, HEADER_METRIC_NAME, "B"); ++ endpoint.assertIsSatisfied(); ++ inOrder.verify(mockRegistry, times(1)).meter("B"); ++ inOrder.verify(mockMeter, times(1)).mark(3179L); ++ inOrder.verifyNoMoreInteractions(); ++ } ++ ++ @Test ++ public void testOverrideValueWithHeader() throws Exception { ++ Object body = new Object(); ++ endpoint.expectedBodiesReceived(body); ++ producer1.sendBodyAndHeader(body, HEADER_METER_MARK, 9926L); ++ endpoint.assertIsSatisfied(); ++ inOrder.verify(mockRegistry, times(1)).meter("A"); ++ inOrder.verify(mockMeter, times(1)).mark(9926L); ++ inOrder.verifyNoMoreInteractions(); ++ } ++ ++ @Test ++ public void testValueNoSetInUriOverrideWithHeader() throws Exception { ++ Object body = new Object(); ++ endpoint.expectedBodiesReceived(body); ++ producer2.sendBodyAndHeader(body, HEADER_METER_MARK, 7707370L); ++ endpoint.assertIsSatisfied(); ++ inOrder.verify(mockRegistry, times(1)).meter("A"); ++ inOrder.verify(mockMeter, times(1)).mark(7707370L); ++ inOrder.verifyNoMoreInteractions(); ++ } ++} http://git-wip-us.apache.org/repos/asf/camel/blob/641de098/components/camel-metrics/src/test/java/org/apache/camel/metrics/timer/TimerEndpointTest.java ---------------------------------------------------------------------- diff --cc components/camel-metrics/src/test/java/org/apache/camel/metrics/timer/TimerEndpointTest.java index 0000000,0000000..cb3232f new file mode 100644 --- /dev/null +++ b/components/camel-metrics/src/test/java/org/apache/camel/metrics/timer/TimerEndpointTest.java @@@ -1,0 -1,0 +1,75 @@@ ++package org.apache.camel.metrics.timer; ++ ++import static org.hamcrest.CoreMatchers.is; ++import static org.hamcrest.MatcherAssert.assertThat; ++import static org.hamcrest.Matchers.instanceOf; ++import static org.hamcrest.Matchers.notNullValue; ++import static org.hamcrest.Matchers.nullValue; ++ ++import org.apache.camel.Producer; ++import org.apache.camel.metrics.timer.TimerEndpoint.TimerAction; ++import org.junit.After; ++import org.junit.Before; ++import org.junit.Test; ++import org.junit.runner.RunWith; ++import org.mockito.InOrder; ++import org.mockito.Mock; ++import org.mockito.Mockito; ++import org.mockito.runners.MockitoJUnitRunner; ++ ++import com.codahale.metrics.MetricRegistry; ++ ++@RunWith(MockitoJUnitRunner.class) ++public class TimerEndpointTest { ++ ++ private static final String METRICS_NAME = "metrics.name"; ++ ++ @Mock ++ private MetricRegistry registry; ++ ++ private TimerEndpoint endpoint; ++ ++ private InOrder inOrder; ++ ++ @Before ++ public void setUp() throws Exception { ++ endpoint = new TimerEndpoint(registry, METRICS_NAME); ++ inOrder = Mockito.inOrder(registry); ++ } ++ ++ @After ++ public void tearDown() throws Exception { ++ inOrder.verifyNoMoreInteractions(); ++ } ++ ++ @Test ++ public void testTimerEndpoint() throws Exception { ++ assertThat(endpoint, is(notNullValue())); ++ assertThat(endpoint.getRegistry(), is(registry)); ++ assertThat(endpoint.getMetricsName(), is(METRICS_NAME)); ++ } ++ ++ @Test ++ public void testCreateProducer() throws Exception { ++ Producer producer = endpoint.createProducer(); ++ assertThat(producer, is(notNullValue())); ++ assertThat(producer, is(instanceOf(TimerProducer.class))); ++ } ++ ++ @Test ++ public void testGetAction() throws Exception { ++ assertThat(endpoint.getAction(), is(nullValue())); ++ } ++ ++ @Test ++ public void testSetAction() throws Exception { ++ assertThat(endpoint.getAction(), is(nullValue())); ++ endpoint.setAction(TimerAction.start); ++ assertThat(endpoint.getAction(), is(TimerAction.start)); ++ } ++ ++ @Test ++ public void testCreateEndpointUri() throws Exception { ++ assertThat(endpoint.createEndpointUri(), is(TimerEndpoint.ENDPOINT_URI)); ++ } ++} http://git-wip-us.apache.org/repos/asf/camel/blob/641de098/components/camel-metrics/src/test/java/org/apache/camel/metrics/timer/TimerProducerTest.java ---------------------------------------------------------------------- diff --cc components/camel-metrics/src/test/java/org/apache/camel/metrics/timer/TimerProducerTest.java index 0000000,0000000..ff7c1f6 new file mode 100644 --- /dev/null +++ b/components/camel-metrics/src/test/java/org/apache/camel/metrics/timer/TimerProducerTest.java @@@ -1,0 -1,0 +1,215 @@@ ++package org.apache.camel.metrics.timer; ++ ++import static org.apache.camel.metrics.MetricsComponent.HEADER_TIMER_ACTION; ++import static org.hamcrest.CoreMatchers.is; ++import static org.hamcrest.MatcherAssert.assertThat; ++import static org.hamcrest.Matchers.notNullValue; ++import static org.hamcrest.Matchers.nullValue; ++import static org.mockito.Mockito.times; ++import static org.mockito.Mockito.when; ++ ++import org.apache.camel.Exchange; ++import org.apache.camel.Message; ++import org.apache.camel.metrics.timer.TimerEndpoint.TimerAction; ++import org.junit.Before; ++import org.junit.Test; ++import org.junit.runner.RunWith; ++import org.mockito.InOrder; ++import org.mockito.Mock; ++import org.mockito.Mockito; ++import org.mockito.runners.MockitoJUnitRunner; ++ ++import com.codahale.metrics.MetricRegistry; ++import com.codahale.metrics.Timer; ++ ++@RunWith(MockitoJUnitRunner.class) ++public class TimerProducerTest { ++ ++ private static final String METRICS_NAME = "metrics.name"; ++ private static final String PROPERTY_NAME = TimerEndpoint.ENDPOINT_URI + ":" + METRICS_NAME; ++ ++ @Mock ++ private TimerEndpoint endpoint; ++ ++ @Mock ++ private Exchange exchange; ++ ++ @Mock ++ private MetricRegistry registry; ++ ++ @Mock ++ private Timer timer; ++ ++ @Mock ++ private Timer.Context context; ++ ++ @Mock ++ private Message in; ++ ++ private TimerProducer producer; ++ ++ @Mock ++ private InOrder inOrder; ++ ++ @Before ++ public void setUp() throws Exception { ++ producer = new TimerProducer(endpoint); ++ inOrder = Mockito.inOrder(endpoint, exchange, registry, timer, context, in); ++ when(endpoint.getRegistry()).thenReturn(registry); ++ when(registry.timer(METRICS_NAME)).thenReturn(timer); ++ when(timer.time()).thenReturn(context); ++ when(exchange.getIn()).thenReturn(in); ++ } ++ ++ @Test ++ public void testTimerProducer() throws Exception { ++ assertThat(producer, is(notNullValue())); ++ assertThat(producer.getEndpoint().equals(endpoint), is(true)); ++ } ++ ++ @Test ++ public void testProcessStart() throws Exception { ++ when(endpoint.getAction()).thenReturn(TimerAction.start); ++ when(in.getHeader(HEADER_TIMER_ACTION, TimerAction.start, TimerAction.class)).thenReturn(TimerAction.start); ++ when(exchange.getProperty(PROPERTY_NAME, Timer.Context.class)).thenReturn(null); ++ producer.doProcess(exchange, endpoint, registry, METRICS_NAME); ++ inOrder.verify(exchange, times(1)).getIn(); ++ inOrder.verify(endpoint, times(1)).getAction(); ++ inOrder.verify(in, times(1)).getHeader(HEADER_TIMER_ACTION, TimerAction.start, TimerAction.class); ++ inOrder.verify(exchange, times(1)).getProperty(PROPERTY_NAME, Timer.Context.class); ++ inOrder.verify(registry, times(1)).timer(METRICS_NAME); ++ inOrder.verify(timer, times(1)).time(); ++ inOrder.verify(exchange, times(1)).setProperty(PROPERTY_NAME, context); ++ inOrder.verifyNoMoreInteractions(); ++ } ++ ++ @Test ++ public void testProcessStartWithOverride() throws Exception { ++ when(endpoint.getAction()).thenReturn(TimerAction.start); ++ when(in.getHeader(HEADER_TIMER_ACTION, TimerAction.start, TimerAction.class)).thenReturn(TimerAction.stop); ++ when(exchange.getProperty(PROPERTY_NAME, Timer.Context.class)).thenReturn(context); ++ producer.doProcess(exchange, endpoint, registry, METRICS_NAME); ++ inOrder.verify(exchange, times(1)).getIn(); ++ inOrder.verify(endpoint, times(1)).getAction(); ++ inOrder.verify(in, times(1)).getHeader(HEADER_TIMER_ACTION, TimerAction.start, TimerAction.class); ++ inOrder.verify(exchange, times(1)).getProperty(PROPERTY_NAME, Timer.Context.class); ++ inOrder.verify(context, times(1)).stop(); ++ inOrder.verify(exchange, times(1)).removeProperty(PROPERTY_NAME); ++ inOrder.verifyNoMoreInteractions(); ++ } ++ ++ @Test ++ public void testProcessStop() throws Exception { ++ when(endpoint.getAction()).thenReturn(TimerAction.stop); ++ when(in.getHeader(HEADER_TIMER_ACTION, TimerAction.stop, TimerAction.class)).thenReturn(TimerAction.stop); ++ when(exchange.getProperty(PROPERTY_NAME, Timer.Context.class)).thenReturn(context); ++ producer.doProcess(exchange, endpoint, registry, METRICS_NAME); ++ inOrder.verify(exchange, times(1)).getIn(); ++ inOrder.verify(endpoint, times(1)).getAction(); ++ inOrder.verify(in, times(1)).getHeader(HEADER_TIMER_ACTION, TimerAction.stop, TimerAction.class); ++ inOrder.verify(exchange, times(1)).getProperty(PROPERTY_NAME, Timer.Context.class); ++ inOrder.verify(context, times(1)).stop(); ++ inOrder.verify(exchange, times(1)).removeProperty(PROPERTY_NAME); ++ inOrder.verifyNoMoreInteractions(); ++ } ++ ++ @Test ++ public void testProcessStopWithOverride() throws Exception { ++ when(endpoint.getAction()).thenReturn(TimerAction.stop); ++ when(in.getHeader(HEADER_TIMER_ACTION, TimerAction.stop, TimerAction.class)).thenReturn(TimerAction.start); ++ when(exchange.getProperty(PROPERTY_NAME, Timer.Context.class)).thenReturn(null); ++ producer.doProcess(exchange, endpoint, registry, METRICS_NAME); ++ inOrder.verify(exchange, times(1)).getIn(); ++ inOrder.verify(endpoint, times(1)).getAction(); ++ inOrder.verify(in, times(1)).getHeader(HEADER_TIMER_ACTION, TimerAction.stop, TimerAction.class); ++ inOrder.verify(exchange, times(1)).getProperty(PROPERTY_NAME, Timer.Context.class); ++ inOrder.verify(registry, times(1)).timer(METRICS_NAME); ++ inOrder.verify(timer, times(1)).time(); ++ inOrder.verify(exchange, times(1)).setProperty(PROPERTY_NAME, context); ++ inOrder.verifyNoMoreInteractions(); ++ } ++ ++ @Test ++ public void testProcessNoAction() throws Exception { ++ when(endpoint.getAction()).thenReturn(null); ++ when(in.getHeader(HEADER_TIMER_ACTION, null, TimerAction.class)).thenReturn(null); ++ producer.doProcess(exchange, endpoint, registry, METRICS_NAME); ++ inOrder.verify(exchange, times(1)).getIn(); ++ inOrder.verify(endpoint, times(1)).getAction(); ++ inOrder.verify(in, times(1)).getHeader(HEADER_TIMER_ACTION, null, TimerAction.class); ++ inOrder.verifyNoMoreInteractions(); ++ } ++ ++ @Test ++ public void testProcessNoActionOverride() throws Exception { ++ when(endpoint.getAction()).thenReturn(null); ++ when(in.getHeader(HEADER_TIMER_ACTION, null, TimerAction.class)).thenReturn(TimerAction.start); ++ producer.doProcess(exchange, endpoint, registry, METRICS_NAME); ++ inOrder.verify(exchange, times(1)).getIn(); ++ inOrder.verify(endpoint, times(1)).getAction(); ++ inOrder.verify(in, times(1)).getHeader(HEADER_TIMER_ACTION, null, TimerAction.class); ++ inOrder.verify(exchange, times(1)).getProperty(PROPERTY_NAME, Timer.Context.class); ++ inOrder.verify(registry, times(1)).timer(METRICS_NAME); ++ inOrder.verify(timer, times(1)).time(); ++ inOrder.verify(exchange, times(1)).setProperty(PROPERTY_NAME, context); ++ inOrder.verifyNoMoreInteractions(); ++ } ++ ++ @Test ++ public void testHandleStart() throws Exception { ++ when(exchange.getProperty(PROPERTY_NAME, Timer.Context.class)).thenReturn(null); ++ producer.handleStart(exchange, registry, METRICS_NAME); ++ inOrder.verify(exchange, times(1)).getProperty(PROPERTY_NAME, Timer.Context.class); ++ inOrder.verify(registry, times(1)).timer(METRICS_NAME); ++ inOrder.verify(timer, times(1)).time(); ++ inOrder.verify(exchange, times(1)).setProperty(PROPERTY_NAME, context); ++ inOrder.verifyNoMoreInteractions(); ++ } ++ ++ @Test ++ public void testHandleStartAlreadyRunning() throws Exception { ++ when(exchange.getProperty(PROPERTY_NAME, Timer.Context.class)).thenReturn(context); ++ producer.handleStart(exchange, registry, METRICS_NAME); ++ inOrder.verify(exchange, times(1)).getProperty(PROPERTY_NAME, Timer.Context.class); ++ inOrder.verifyNoMoreInteractions(); ++ } ++ ++ @Test ++ public void testHandleStop() throws Exception { ++ when(exchange.getProperty(PROPERTY_NAME, Timer.Context.class)).thenReturn(context); ++ producer.handleStop(exchange, registry, METRICS_NAME); ++ inOrder.verify(exchange, times(1)).getProperty(PROPERTY_NAME, Timer.Context.class); ++ inOrder.verify(context, times(1)).stop(); ++ inOrder.verify(exchange, times(1)).removeProperty(PROPERTY_NAME); ++ inOrder.verifyNoMoreInteractions(); ++ } ++ ++ @Test ++ public void testHandleStopContextNotFound() throws Exception { ++ when(exchange.getProperty(PROPERTY_NAME, Timer.Context.class)).thenReturn(null); ++ producer.handleStop(exchange, registry, METRICS_NAME); ++ inOrder.verify(exchange, times(1)).getProperty(PROPERTY_NAME, Timer.Context.class); ++ inOrder.verifyNoMoreInteractions(); ++ } ++ ++ @Test ++ public void testGetPropertyName() throws Exception { ++ assertThat(producer.getPropertyName(METRICS_NAME), is(TimerEndpoint.ENDPOINT_URI + ":" + METRICS_NAME)); ++ } ++ ++ @Test ++ public void testGetTimerContextFromExchange() throws Exception { ++ when(exchange.getProperty(PROPERTY_NAME, Timer.Context.class)).thenReturn(context); ++ assertThat(producer.getTimerContextFromExchange(exchange, PROPERTY_NAME), is(context)); ++ inOrder.verify(exchange, times(1)).getProperty(PROPERTY_NAME, Timer.Context.class); ++ inOrder.verifyNoMoreInteractions(); ++ } ++ ++ @Test ++ public void testGetTimerContextFromExchangeNotFound() throws Exception { ++ when(exchange.getProperty(PROPERTY_NAME, Timer.Context.class)).thenReturn(null); ++ assertThat(producer.getTimerContextFromExchange(exchange, PROPERTY_NAME), is(nullValue())); ++ inOrder.verify(exchange, times(1)).getProperty(PROPERTY_NAME, Timer.Context.class); ++ inOrder.verifyNoMoreInteractions(); ++ } ++} http://git-wip-us.apache.org/repos/asf/camel/blob/641de098/components/camel-metrics/src/test/java/org/apache/camel/metrics/timer/TimerRouteTest.java ---------------------------------------------------------------------- diff --cc components/camel-metrics/src/test/java/org/apache/camel/metrics/timer/TimerRouteTest.java index 0000000,0000000..1a6581c new file mode 100644 --- /dev/null +++ b/components/camel-metrics/src/test/java/org/apache/camel/metrics/timer/TimerRouteTest.java @@@ -1,0 -1,0 +1,130 @@@ ++package org.apache.camel.metrics.timer; ++ ++import static org.apache.camel.metrics.MetricsComponent.HEADER_METRIC_NAME; ++import static org.apache.camel.metrics.MetricsComponent.HEADER_TIMER_ACTION; ++import static org.apache.camel.metrics.MetricsComponent.METRIC_REGISTRY_NAME; ++import static org.mockito.Mockito.reset; ++import static org.mockito.Mockito.times; ++import static org.mockito.Mockito.when; ++ ++import org.apache.camel.EndpointInject; ++import org.apache.camel.Produce; ++import org.apache.camel.ProducerTemplate; ++import org.apache.camel.builder.RouteBuilder; ++import org.apache.camel.component.mock.MockEndpoint; ++import org.apache.camel.metrics.timer.TimerEndpoint.TimerAction; ++import org.apache.camel.spring.javaconfig.SingleRouteCamelConfiguration; ++import org.apache.camel.test.spring.CamelSpringDelegatingTestContextLoader; ++import org.apache.camel.test.spring.CamelSpringJUnit4ClassRunner; ++import org.apache.camel.test.spring.MockEndpoints; ++import org.junit.After; ++import org.junit.Before; ++import org.junit.Test; ++import org.junit.runner.RunWith; ++import org.mockito.InOrder; ++import org.mockito.Mockito; ++import org.springframework.context.annotation.Bean; ++import org.springframework.context.annotation.Configuration; ++import org.springframework.test.context.ContextConfiguration; ++ ++import com.codahale.metrics.MetricRegistry; ++import com.codahale.metrics.Timer; ++ ++@RunWith(CamelSpringJUnit4ClassRunner.class) ++@ContextConfiguration( ++ classes = { TimerRouteTest.TestConfig.class }, ++ loader = CamelSpringDelegatingTestContextLoader.class) ++@MockEndpoints ++public class TimerRouteTest { ++ ++ @EndpointInject(uri = "mock:out") ++ private MockEndpoint endpoint; ++ ++ @Produce(uri = "direct:in-1") ++ private ProducerTemplate producer1; ++ ++ @Produce(uri = "direct:in-2") ++ private ProducerTemplate producer2; ++ ++ private MetricRegistry mockRegistry; ++ ++ private Timer mockTimer; ++ ++ private InOrder inOrder; ++ ++ @Configuration ++ public static class TestConfig extends SingleRouteCamelConfiguration { ++ ++ @Bean ++ @Override ++ public RouteBuilder route() { ++ return new RouteBuilder() { ++ ++ @Override ++ public void configure() throws Exception { ++ from("direct:in-1") ++ .to("metrics:timer:A?action=start") ++ .to("mock:out"); ++ ++ from("direct:in-2") ++ .to("metrics:timer:A") ++ .to("mock:out"); ++ } ++ }; ++ } ++ ++ @Bean(name = METRIC_REGISTRY_NAME) ++ public MetricRegistry getMetricRegistry() { ++ return Mockito.mock(MetricRegistry.class); ++ } ++ } ++ ++ @Before ++ public void setup() { ++ // TODO - 12.05.2014, Lauri - is there any better way to set this up? ++ mockRegistry = endpoint.getCamelContext().getRegistry().lookupByNameAndType(METRIC_REGISTRY_NAME, MetricRegistry.class); ++ mockTimer = Mockito.mock(Timer.class); ++ inOrder = Mockito.inOrder(mockRegistry, mockTimer); ++ } ++ ++ @After ++ public void tearDown() { ++ endpoint.reset(); ++ reset(mockRegistry, mockTimer); ++ } ++ ++ @Test ++ public void testOverrideMetricsName() throws Exception { ++ when(mockRegistry.timer("B")).thenReturn(mockTimer); ++ ++ Object body = new Object(); ++ endpoint.expectedBodiesReceived(body); ++ producer1.sendBodyAndHeader(body, HEADER_METRIC_NAME, "B"); ++ endpoint.assertIsSatisfied(); ++ inOrder.verify(mockRegistry, times(1)).timer("B"); ++ inOrder.verify(mockTimer, times(1)).time(); ++ inOrder.verifyNoMoreInteractions(); ++ } ++ ++ @Test ++ public void testOverrideExistingAction() throws Exception { ++ when(mockRegistry.timer("A")).thenReturn(mockTimer); ++ Object body = new Object(); ++ endpoint.expectedBodiesReceived(body); ++ producer1.sendBodyAndHeader(body, HEADER_TIMER_ACTION, TimerAction.stop); ++ endpoint.assertIsSatisfied(); ++ inOrder.verifyNoMoreInteractions(); ++ } ++ ++ @Test ++ public void testOverrideNoAction() throws Exception { ++ when(mockRegistry.timer("A")).thenReturn(mockTimer); ++ Object body = new Object(); ++ endpoint.expectedBodiesReceived(body); ++ producer2.sendBodyAndHeader(body, HEADER_TIMER_ACTION, TimerAction.start); ++ endpoint.assertIsSatisfied(); ++ inOrder.verify(mockRegistry, times(1)).timer("A"); ++ inOrder.verify(mockTimer, times(1)).time(); ++ inOrder.verifyNoMoreInteractions(); ++ } ++} http://git-wip-us.apache.org/repos/asf/camel/blob/641de098/components/camel-metrics/src/test/resources/log4j.properties ---------------------------------------------------------------------- diff --cc components/camel-metrics/src/test/resources/log4j.properties index 0000000,0000000..3b1bd38 new file mode 100644 --- /dev/null +++ b/components/camel-metrics/src/test/resources/log4j.properties @@@ -1,0 -1,0 +1,14 @@@ ++# ++# The logging properties used ++# ++log4j.rootLogger=INFO, out ++ ++# uncomment the following line to turn on Camel debugging ++#log4j.logger.org.apache.camel=DEBUG ++ ++# CONSOLE appender not used by default ++log4j.appender.out=org.apache.log4j.ConsoleAppender ++log4j.appender.out.layout=org.apache.log4j.PatternLayout ++log4j.appender.out.layout.ConversionPattern=[%30.30t] %-30.30c{1} %-5p %m%n ++#log4j.appender.out.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n ++