camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [07/34] git commit: Added header to enable overriding metrics name in the URI.
Date Wed, 30 Jul 2014 13:00:50 GMT
Added header to enable overriding metrics name in the URI.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/85ca4c1f
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/85ca4c1f
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/85ca4c1f

Branch: refs/heads/master
Commit: 85ca4c1f8b67480415077f0316e4ac66f11db079
Parents: 89abb88
Author: Lauri Kimmel <lauri.kimmel@gmx.com>
Authored: Tue May 20 23:03:48 2014 +1000
Committer: Lauri Kimmel <lauri.kimmel@gmx.com>
Committed: Tue May 20 23:03:48 2014 +1000

----------------------------------------------------------------------
 README.md                                       | 16 ++++
 .../camel/metrics/AbstractMetricsEndpoint.java  | 18 +++++
 .../apache/camel/metrics/MetricsComponent.java  |  1 +
 .../camel/metrics/counter/CounterProducer.java  |  2 +-
 .../metrics/histogram/HistogramProducer.java    |  2 +-
 .../camel/metrics/meter/MeterProducer.java      |  2 +-
 .../camel/metrics/timer/TimerProducer.java      |  2 +-
 .../metrics/AbstractMetricsEndpointTest.java    | 82 +++++++++++++++++++-
 .../metrics/MetricComponentSpringTest.java      |  2 +-
 .../metrics/MetricsComponentRouteTest.java      | 22 ++++++
 .../metrics/counter/CounterProducerTest.java    | 10 +--
 .../camel/metrics/counter/CounterRouteTest.java | 78 +++++++++++++++++++
 .../histogram/HistogramProducerTest.java        |  6 +-
 .../metrics/histogram/HistogramRouteTest.java   | 78 +++++++++++++++++++
 .../camel/metrics/meter/MeterProducerTest.java  |  6 +-
 .../camel/metrics/meter/MeterRouteTest.java     | 78 +++++++++++++++++++
 .../camel/metrics/timer/TimerProducerTest.java  |  8 +-
 .../camel/metrics/timer/TimerRouteTest.java     | 78 +++++++++++++++++++
 18 files changed, 470 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/85ca4c1f/README.md
----------------------------------------------------------------------
diff --git a/README.md b/README.md
index 6a46282..34bbb23 100644
--- a/README.md
+++ b/README.md
@@ -57,6 +57,22 @@ MetricRegistry instance can be configured by adding bean with name ```metricRegi
 
 Each metric has type and name. Supported types are ```counter```, ```meter```, ```histogram```
and ```timer```. Metric name is simple string. If metric type is not provided then type ```meter```
is used by default.
 
+### Headers
+
+Metric name defined in URI can be overridden by using Header with name ```org.apache.camel.metrics.metricName```.
+
+For example
+
+```java
+from("direct:in")
+    .setHeader(MetricsComponent.HEADER_METRIC_NAME, constant("new.name"))
+    .to("metrics:counter:name.not.used")
+    .to("direct:out")
+```
+
+will update counter with name ```new.name``` instead of ```name.not.used```.
+
+
 ## Metrics type counter
 
 ```

http://git-wip-us.apache.org/repos/asf/camel/blob/85ca4c1f/src/main/java/org/apache/camel/metrics/AbstractMetricsEndpoint.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/camel/metrics/AbstractMetricsEndpoint.java b/src/main/java/org/apache/camel/metrics/AbstractMetricsEndpoint.java
index d8c8af7..9560758 100644
--- a/src/main/java/org/apache/camel/metrics/AbstractMetricsEndpoint.java
+++ b/src/main/java/org/apache/camel/metrics/AbstractMetricsEndpoint.java
@@ -1,9 +1,12 @@
 package org.apache.camel.metrics;
 
 import org.apache.camel.Consumer;
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
 import org.apache.camel.Processor;
 import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.impl.DefaultEndpoint;
+import org.apache.camel.util.ObjectHelper;
 
 import com.codahale.metrics.MetricRegistry;
 
@@ -34,4 +37,19 @@ public abstract class AbstractMetricsEndpoint extends DefaultEndpoint {
     public String getMetricsName() {
         return metricsName;
     }
+
+    public String getMetricsName(Exchange exchange) {
+        return getStringHeader(exchange, MetricsComponent.HEADER_METRIC_NAME, metricsName);
+    }
+
+    public String getStringHeader(Exchange exchange, String header, String defaultValue)
{
+        Message in = exchange.getIn();
+        String headerValue = in.getHeader(header, String.class);
+        return ObjectHelper.isNotEmpty(headerValue) ? headerValue : defaultValue;
+    }
+
+    public Long getLongHeader(Exchange exchange, String header, Long defaultValue) {
+        Message in = exchange.getIn();
+        return in.getHeader(header, defaultValue, Long.class);
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/85ca4c1f/src/main/java/org/apache/camel/metrics/MetricsComponent.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/camel/metrics/MetricsComponent.java b/src/main/java/org/apache/camel/metrics/MetricsComponent.java
index 1f467c4..bc4a2af 100644
--- a/src/main/java/org/apache/camel/metrics/MetricsComponent.java
+++ b/src/main/java/org/apache/camel/metrics/MetricsComponent.java
@@ -26,6 +26,7 @@ public class MetricsComponent extends DefaultComponent {
     public static final String METRIC_REGISTRY_NAME = "metricRegistry";
     public static final MetricsType DEFAULT_METRICS_TYPE = MetricsType.METER;
     public static final long DEFAULT_REPORTING_INTERVAL_SECONDS = 60L;
+    public static final String HEADER_METRIC_NAME = "org.apache.camel.metrics.metricName";
 
     private static final Logger LOG = LoggerFactory.getLogger(MetricsComponent.class);
 

http://git-wip-us.apache.org/repos/asf/camel/blob/85ca4c1f/src/main/java/org/apache/camel/metrics/counter/CounterProducer.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/camel/metrics/counter/CounterProducer.java b/src/main/java/org/apache/camel/metrics/counter/CounterProducer.java
index 19c470f..ec9f426 100644
--- a/src/main/java/org/apache/camel/metrics/counter/CounterProducer.java
+++ b/src/main/java/org/apache/camel/metrics/counter/CounterProducer.java
@@ -15,7 +15,7 @@ public class CounterProducer extends DefaultProducer {
     @Override
     public void process(Exchange exchange) throws Exception {
         CounterEndpoint endpoint = (CounterEndpoint) getEndpoint();
-        String metricName = endpoint.getMetricsName();
+        String metricName = endpoint.getMetricsName(exchange);
         Counter counter = endpoint.getRegistry().counter(metricName);
         Long increment = endpoint.getIncrement();
         Long decrement = endpoint.getDecrement();

http://git-wip-us.apache.org/repos/asf/camel/blob/85ca4c1f/src/main/java/org/apache/camel/metrics/histogram/HistogramProducer.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/camel/metrics/histogram/HistogramProducer.java b/src/main/java/org/apache/camel/metrics/histogram/HistogramProducer.java
index 9a15607..41b5cf7 100644
--- a/src/main/java/org/apache/camel/metrics/histogram/HistogramProducer.java
+++ b/src/main/java/org/apache/camel/metrics/histogram/HistogramProducer.java
@@ -21,7 +21,7 @@ public class HistogramProducer extends DefaultProducer {
     public void process(Exchange exchange) throws Exception {
         HistogramEndpoint endpoint = (HistogramEndpoint) getEndpoint();
         MetricRegistry registry = endpoint.getRegistry();
-        String metricsName = endpoint.getMetricsName();
+        String metricsName = endpoint.getMetricsName(exchange);
         Histogram histogram = registry.histogram(metricsName);
         Long value = endpoint.getValue();
         if (value != null) {

http://git-wip-us.apache.org/repos/asf/camel/blob/85ca4c1f/src/main/java/org/apache/camel/metrics/meter/MeterProducer.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/camel/metrics/meter/MeterProducer.java b/src/main/java/org/apache/camel/metrics/meter/MeterProducer.java
index 6cb7896..0de6ba5 100644
--- a/src/main/java/org/apache/camel/metrics/meter/MeterProducer.java
+++ b/src/main/java/org/apache/camel/metrics/meter/MeterProducer.java
@@ -16,7 +16,7 @@ public class MeterProducer extends DefaultProducer {
     @Override
     public void process(Exchange exchange) throws Exception {
         MeterEndpoint endpoint = (MeterEndpoint) getEndpoint();
-        String metricsName = endpoint.getMetricsName();
+        String metricsName = endpoint.getMetricsName(exchange);
         MetricRegistry registry = endpoint.getRegistry();
         Meter meter = registry.meter(metricsName);
         Long mark = endpoint.getMark();

http://git-wip-us.apache.org/repos/asf/camel/blob/85ca4c1f/src/main/java/org/apache/camel/metrics/timer/TimerProducer.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/camel/metrics/timer/TimerProducer.java b/src/main/java/org/apache/camel/metrics/timer/TimerProducer.java
index f655352..bd8e2b1 100644
--- a/src/main/java/org/apache/camel/metrics/timer/TimerProducer.java
+++ b/src/main/java/org/apache/camel/metrics/timer/TimerProducer.java
@@ -22,7 +22,7 @@ public class TimerProducer extends DefaultProducer {
     public void process(Exchange exchange) throws Exception {
         TimerEndpoint endpoint = (TimerEndpoint) getEndpoint();
         MetricRegistry registry = endpoint.getRegistry();
-        String metricsName = endpoint.getMetricsName();
+        String metricsName = endpoint.getMetricsName(exchange);
         TimerAction action = endpoint.getAction();
         if (action == TimerAction.start) {
             handleStart(exchange, registry, metricsName);

http://git-wip-us.apache.org/repos/asf/camel/blob/85ca4c1f/src/test/java/org/apache/camel/metrics/AbstractMetricsEndpointTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/camel/metrics/AbstractMetricsEndpointTest.java b/src/test/java/org/apache/camel/metrics/AbstractMetricsEndpointTest.java
index 0b24cf8..824581c 100644
--- a/src/test/java/org/apache/camel/metrics/AbstractMetricsEndpointTest.java
+++ b/src/test/java/org/apache/camel/metrics/AbstractMetricsEndpointTest.java
@@ -2,7 +2,11 @@ package org.apache.camel.metrics;
 
 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.apache.camel.Processor;
 import org.apache.camel.Producer;
 import org.apache.camel.RuntimeCamelException;
@@ -21,6 +25,11 @@ import com.codahale.metrics.MetricRegistry;
 public class AbstractMetricsEndpointTest {
 
     private static final String METRICS_NAME = "metrics.name";
+    private static final String HEADER = "header";
+    private static final String STRING_VALUE = "string-value";
+    private static final String DEFAULT_STRING_VALUE = "default-value";
+    private static final Long LONG_VALUE = System.currentTimeMillis();
+    private static final Long DEFAULT_LONG_VALUE = -10101L;
 
     @Mock
     private MetricRegistry registry;
@@ -28,6 +37,12 @@ public class AbstractMetricsEndpointTest {
     @Mock
     private Processor processor;
 
+    @Mock
+    private Exchange exchange;
+
+    @Mock
+    private Message in;
+
     private AbstractMetricsEndpoint endpoint;
 
     private InOrder inOrder;
@@ -45,7 +60,8 @@ public class AbstractMetricsEndpointTest {
                 return "not real endpoint";
             }
         };
-        inOrder = Mockito.inOrder(registry, processor);
+        inOrder = Mockito.inOrder(registry, processor, exchange, in);
+        when(exchange.getIn()).thenReturn(in);
     }
 
     @After
@@ -78,4 +94,68 @@ public class AbstractMetricsEndpointTest {
     public void testGetMetricsName() throws Exception {
         assertThat(endpoint.getMetricsName(), is(METRICS_NAME));
     }
+
+    @Test
+    public void testGetStringHeaderValue() {
+        when(in.getHeader(HEADER, String.class)).thenReturn(STRING_VALUE);
+        assertThat(endpoint.getStringHeader(exchange, HEADER, DEFAULT_STRING_VALUE), is(STRING_VALUE));
+        inOrder.verify(exchange, times(1)).getIn();
+        inOrder.verify(in, times(1)).getHeader(HEADER, String.class);
+    }
+
+    @Test
+    public void testGetStringHeaderValueNotFound() {
+        when(in.getHeader(HEADER, String.class)).thenReturn(null);
+        assertThat(endpoint.getStringHeader(exchange, HEADER, DEFAULT_STRING_VALUE), is(DEFAULT_STRING_VALUE));
+        inOrder.verify(exchange, times(1)).getIn();
+        inOrder.verify(in, times(1)).getHeader(HEADER, String.class);
+    }
+
+    @Test
+    public void testGetStringHeaderValueEmptyString() {
+        when(in.getHeader(HEADER, String.class)).thenReturn("");
+        assertThat(endpoint.getStringHeader(exchange, HEADER, DEFAULT_STRING_VALUE), is(DEFAULT_STRING_VALUE));
+        inOrder.verify(exchange, times(1)).getIn();
+        inOrder.verify(in, times(1)).getHeader(HEADER, String.class);
+    }
+
+    @Test
+    public void testGetStringHeaderValueWhiteSpaces() {
+        when(in.getHeader(HEADER, String.class)).thenReturn(" \n\t\r");
+        assertThat(endpoint.getStringHeader(exchange, HEADER, DEFAULT_STRING_VALUE), is(DEFAULT_STRING_VALUE));
+        inOrder.verify(exchange, times(1)).getIn();
+        inOrder.verify(in, times(1)).getHeader(HEADER, String.class);
+    }
+
+    @Test
+    public void testGetLongHeaderValue() {
+        when(in.getHeader(HEADER, DEFAULT_LONG_VALUE, Long.class)).thenReturn(LONG_VALUE);
+        assertThat(endpoint.getLongHeader(exchange, HEADER, DEFAULT_LONG_VALUE), is(LONG_VALUE));
+        inOrder.verify(exchange, times(1)).getIn();
+        inOrder.verify(in, times(1)).getHeader(HEADER, DEFAULT_LONG_VALUE, Long.class);
+    }
+
+    @Test
+    public void testGetLongHeaderValueNotFound() {
+        when(in.getHeader(HEADER, DEFAULT_LONG_VALUE, Long.class)).thenReturn(DEFAULT_LONG_VALUE);
+        assertThat(endpoint.getLongHeader(exchange, HEADER, DEFAULT_LONG_VALUE), is(DEFAULT_LONG_VALUE));
+        inOrder.verify(exchange, times(1)).getIn();
+        inOrder.verify(in, times(1)).getHeader(HEADER, DEFAULT_LONG_VALUE, Long.class);
+    }
+
+    @Test
+    public void testGetMetricsNameFromExchange() {
+        when(in.getHeader(MetricsComponent.HEADER_METRIC_NAME, String.class)).thenReturn(STRING_VALUE);
+        assertThat(endpoint.getMetricsName(exchange), is(STRING_VALUE));
+        inOrder.verify(exchange, times(1)).getIn();
+        inOrder.verify(in, times(1)).getHeader(MetricsComponent.HEADER_METRIC_NAME, String.class);
+    }
+
+    @Test
+    public void testGetMetricsNameFromExchangeNotFound() {
+        when(in.getHeader(MetricsComponent.HEADER_METRIC_NAME, String.class)).thenReturn(null);
+        assertThat(endpoint.getMetricsName(exchange), is(METRICS_NAME));
+        inOrder.verify(exchange, times(1)).getIn();
+        inOrder.verify(in, times(1)).getHeader(MetricsComponent.HEADER_METRIC_NAME, String.class);
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/85ca4c1f/src/test/java/org/apache/camel/metrics/MetricComponentSpringTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/camel/metrics/MetricComponentSpringTest.java b/src/test/java/org/apache/camel/metrics/MetricComponentSpringTest.java
index 2dd9391..a6590c5 100644
--- a/src/test/java/org/apache/camel/metrics/MetricComponentSpringTest.java
+++ b/src/test/java/org/apache/camel/metrics/MetricComponentSpringTest.java
@@ -71,7 +71,7 @@ public class MetricComponentSpringTest {
         producer.sendBody(new Object());
         endpoint.assertIsSatisfied();
         inOrder.verify(mockRegistry, times(1)).counter("A");
-        inOrder.verify(mockCounter, times(1)).inc(512);
+        inOrder.verify(mockCounter, times(1)).inc(512L);
         inOrder.verifyNoMoreInteractions();
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/85ca4c1f/src/test/java/org/apache/camel/metrics/MetricsComponentRouteTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/camel/metrics/MetricsComponentRouteTest.java b/src/test/java/org/apache/camel/metrics/MetricsComponentRouteTest.java
index 5af70b9..97301c3 100644
--- a/src/test/java/org/apache/camel/metrics/MetricsComponentRouteTest.java
+++ b/src/test/java/org/apache/camel/metrics/MetricsComponentRouteTest.java
@@ -1,5 +1,9 @@
 package org.apache.camel.metrics;
 
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
 import org.apache.camel.Produce;
 import org.apache.camel.ProducerTemplate;
 import org.apache.camel.builder.RouteBuilder;
@@ -20,6 +24,24 @@ public class MetricsComponentRouteTest extends CamelTestSupport {
         assertMockEndpointsSatisfied();
     }
 
+    @Test
+    public void testMessageContentDelivery() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        String body = "Message Body";
+        String header1 = "Header 1";
+        String header2 = "Header 2";
+        Object value1 = new Date();
+        Object value2 = System.currentTimeMillis();
+        mock.expectedBodiesReceived(body);
+        mock.expectedHeaderReceived(header1, value1);
+        mock.expectedHeaderReceived(header2, value2);
+        Map<String, Object> headers = new HashMap<String, Object>();
+        headers.put(header1, value1);
+        headers.put(header2, value2);
+        template.sendBodyAndHeaders(body, headers);
+        assertMockEndpointsSatisfied();
+    }
+
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {

http://git-wip-us.apache.org/repos/asf/camel/blob/85ca4c1f/src/test/java/org/apache/camel/metrics/counter/CounterProducerTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/camel/metrics/counter/CounterProducerTest.java b/src/test/java/org/apache/camel/metrics/counter/CounterProducerTest.java
index 3b1b89b..c6ecaa4 100644
--- a/src/test/java/org/apache/camel/metrics/counter/CounterProducerTest.java
+++ b/src/test/java/org/apache/camel/metrics/counter/CounterProducerTest.java
@@ -44,7 +44,7 @@ public class CounterProducerTest {
     public void setUp() throws Exception {
         producer = new CounterProducer(endpoint);
         inOrder = Mockito.inOrder(endpoint, exchange, registry, counter);
-        when(endpoint.getMetricsName()).thenReturn(ENDPOINT_NAME);
+        when(endpoint.getMetricsName(exchange)).thenReturn(ENDPOINT_NAME);
         when(endpoint.getRegistry()).thenReturn(registry);
         when(registry.counter(ENDPOINT_NAME)).thenReturn(counter);
     }
@@ -59,7 +59,7 @@ public class CounterProducerTest {
         when(endpoint.getIncrement()).thenReturn(INCREMENT);
         when(endpoint.getDecrement()).thenReturn(null);
         producer.process(exchange);
-        inOrder.verify(endpoint, times(1)).getMetricsName();
+        inOrder.verify(endpoint, times(1)).getMetricsName(exchange);
         inOrder.verify(endpoint, times(1)).getRegistry();
         inOrder.verify(registry, times(1)).counter(ENDPOINT_NAME);
         inOrder.verify(endpoint, times(1)).getIncrement();
@@ -73,7 +73,7 @@ public class CounterProducerTest {
         when(endpoint.getIncrement()).thenReturn(null);
         when(endpoint.getDecrement()).thenReturn(DECREMENT);
         producer.process(exchange);
-        inOrder.verify(endpoint, times(1)).getMetricsName();
+        inOrder.verify(endpoint, times(1)).getMetricsName(exchange);
         inOrder.verify(endpoint, times(1)).getRegistry();
         inOrder.verify(registry, times(1)).counter(ENDPOINT_NAME);
         inOrder.verify(endpoint, times(1)).getIncrement();
@@ -87,7 +87,7 @@ public class CounterProducerTest {
         when(endpoint.getIncrement()).thenReturn(INCREMENT);
         when(endpoint.getDecrement()).thenReturn(DECREMENT);
         producer.process(exchange);
-        inOrder.verify(endpoint, times(1)).getMetricsName();
+        inOrder.verify(endpoint, times(1)).getMetricsName(exchange);
         inOrder.verify(endpoint, times(1)).getRegistry();
         inOrder.verify(registry, times(1)).counter(ENDPOINT_NAME);
         inOrder.verify(endpoint, times(1)).getIncrement();
@@ -101,7 +101,7 @@ public class CounterProducerTest {
         when(endpoint.getIncrement()).thenReturn(null);
         when(endpoint.getDecrement()).thenReturn(null);
         producer.process(exchange);
-        inOrder.verify(endpoint, times(1)).getMetricsName();
+        inOrder.verify(endpoint, times(1)).getMetricsName(exchange);
         inOrder.verify(endpoint, times(1)).getRegistry();
         inOrder.verify(registry, times(1)).counter(ENDPOINT_NAME);
         inOrder.verify(endpoint, times(1)).getIncrement();

http://git-wip-us.apache.org/repos/asf/camel/blob/85ca4c1f/src/test/java/org/apache/camel/metrics/counter/CounterRouteTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/camel/metrics/counter/CounterRouteTest.java b/src/test/java/org/apache/camel/metrics/counter/CounterRouteTest.java
new file mode 100644
index 0000000..df9185b
--- /dev/null
+++ b/src/test/java/org/apache/camel/metrics/counter/CounterRouteTest.java
@@ -0,0 +1,78 @@
+package org.apache.camel.metrics.counter;
+
+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.MetricsComponent;
+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.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")
+    private ProducerTemplate producer;
+
+    @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:counter:A?increment=5")
+                            .to("mock:out");
+                }
+            };
+        }
+
+        @Bean(name = MetricsComponent.METRIC_REGISTRY_NAME)
+        public MetricRegistry getMetricRegistry() {
+            return Mockito.mock(MetricRegistry.class);
+        }
+    }
+
+    @Test
+    public void testOverrideMetricsName() throws Exception {
+        // TODO - 12.05.2014, Lauri - is there any better way to set this up?
+        MetricRegistry mockRegistry = endpoint.getCamelContext().getRegistry().lookupByNameAndType(MetricsComponent.METRIC_REGISTRY_NAME,
MetricRegistry.class);
+        Counter mockCounter = Mockito.mock(Counter.class);
+        InOrder inOrder = Mockito.inOrder(mockRegistry, mockCounter);
+        when(mockRegistry.counter("B")).thenReturn(mockCounter);
+
+        endpoint.expectedMessageCount(1);
+        producer.sendBodyAndHeader(new Object(), MetricsComponent.HEADER_METRIC_NAME, "B");
+        endpoint.assertIsSatisfied();
+        inOrder.verify(mockRegistry, times(1)).counter("B");
+        inOrder.verify(mockCounter, times(1)).inc(5L);
+        inOrder.verifyNoMoreInteractions();
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/85ca4c1f/src/test/java/org/apache/camel/metrics/histogram/HistogramProducerTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/camel/metrics/histogram/HistogramProducerTest.java b/src/test/java/org/apache/camel/metrics/histogram/HistogramProducerTest.java
index 296c52c..8662e9d 100644
--- a/src/test/java/org/apache/camel/metrics/histogram/HistogramProducerTest.java
+++ b/src/test/java/org/apache/camel/metrics/histogram/HistogramProducerTest.java
@@ -44,7 +44,7 @@ public class HistogramProducerTest {
         producer = new HistogramProducer(endpoint);
         inOrder = Mockito.inOrder(endpoint, registry, histogram, exchange);
         when(endpoint.getRegistry()).thenReturn(registry);
-        when(endpoint.getMetricsName()).thenReturn(METRICS_NAME);
+        when(endpoint.getMetricsName(exchange)).thenReturn(METRICS_NAME);
         when(registry.histogram(METRICS_NAME)).thenReturn(histogram);
     }
 
@@ -58,7 +58,7 @@ public class HistogramProducerTest {
         when(endpoint.getValue()).thenReturn(VALUE);
         producer.process(exchange);
         inOrder.verify(endpoint, times(1)).getRegistry();
-        inOrder.verify(endpoint, times(1)).getMetricsName();
+        inOrder.verify(endpoint, times(1)).getMetricsName(exchange);
         inOrder.verify(registry, times(1)).histogram(METRICS_NAME);
         inOrder.verify(endpoint, times(1)).getValue();
         inOrder.verify(histogram, times(1)).update(VALUE);
@@ -70,7 +70,7 @@ public class HistogramProducerTest {
         when(endpoint.getValue()).thenReturn(null);
         producer.process(exchange);
         inOrder.verify(endpoint, times(1)).getRegistry();
-        inOrder.verify(endpoint, times(1)).getMetricsName();
+        inOrder.verify(endpoint, times(1)).getMetricsName(exchange);
         inOrder.verify(registry, times(1)).histogram(METRICS_NAME);
         inOrder.verify(endpoint, times(1)).getValue();
         inOrder.verifyNoMoreInteractions();

http://git-wip-us.apache.org/repos/asf/camel/blob/85ca4c1f/src/test/java/org/apache/camel/metrics/histogram/HistogramRouteTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/camel/metrics/histogram/HistogramRouteTest.java b/src/test/java/org/apache/camel/metrics/histogram/HistogramRouteTest.java
new file mode 100644
index 0000000..e8646cb
--- /dev/null
+++ b/src/test/java/org/apache/camel/metrics/histogram/HistogramRouteTest.java
@@ -0,0 +1,78 @@
+package org.apache.camel.metrics.histogram;
+
+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.MetricsComponent;
+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.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;
+
+    @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 = MetricsComponent.METRIC_REGISTRY_NAME)
+        public MetricRegistry getMetricRegistry() {
+            return Mockito.mock(MetricRegistry.class);
+        }
+    }
+
+    @Test
+    public void testOverrideMetricsName() throws Exception {
+        // TODO - 12.05.2014, Lauri - is there any better way to set this up?
+        MetricRegistry mockRegistry = endpoint.getCamelContext().getRegistry().lookupByNameAndType(MetricsComponent.METRIC_REGISTRY_NAME,
MetricRegistry.class);
+        Histogram mockHistogram = Mockito.mock(Histogram.class);
+        InOrder inOrder = Mockito.inOrder(mockRegistry, mockHistogram);
+        when(mockRegistry.histogram("B")).thenReturn(mockHistogram);
+
+        endpoint.expectedMessageCount(1);
+        producer.sendBodyAndHeader(new Object(), MetricsComponent.HEADER_METRIC_NAME, "B");
+        endpoint.assertIsSatisfied();
+        inOrder.verify(mockRegistry, times(1)).histogram("B");
+        inOrder.verify(mockHistogram, times(1)).update(332491L);
+        inOrder.verifyNoMoreInteractions();
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/85ca4c1f/src/test/java/org/apache/camel/metrics/meter/MeterProducerTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/camel/metrics/meter/MeterProducerTest.java b/src/test/java/org/apache/camel/metrics/meter/MeterProducerTest.java
index 26c853a..ba62a18 100644
--- a/src/test/java/org/apache/camel/metrics/meter/MeterProducerTest.java
+++ b/src/test/java/org/apache/camel/metrics/meter/MeterProducerTest.java
@@ -45,7 +45,7 @@ public class MeterProducerTest {
         producer = new MeterProducer(endpoint);
         inOrder = Mockito.inOrder(endpoint, registry, meter, exchange);
         when(endpoint.getRegistry()).thenReturn(registry);
-        when(endpoint.getMetricsName()).thenReturn(METRICS_NAME);
+        when(endpoint.getMetricsName(exchange)).thenReturn(METRICS_NAME);
         when(registry.meter(METRICS_NAME)).thenReturn(meter);
     }
 
@@ -59,7 +59,7 @@ public class MeterProducerTest {
     public void testProcessMarkSet() throws Exception {
         when(endpoint.getMark()).thenReturn(MARK);
         producer.process(exchange);
-        inOrder.verify(endpoint, times(1)).getMetricsName();
+        inOrder.verify(endpoint, times(1)).getMetricsName(exchange);
         inOrder.verify(endpoint, times(1)).getRegistry();
         inOrder.verify(registry, times(1)).meter(METRICS_NAME);
         inOrder.verify(endpoint, times(1)).getMark();
@@ -71,7 +71,7 @@ public class MeterProducerTest {
     public void testProcessMarkNotSet() throws Exception {
         when(endpoint.getMark()).thenReturn(null);
         producer.process(exchange);
-        inOrder.verify(endpoint, times(1)).getMetricsName();
+        inOrder.verify(endpoint, times(1)).getMetricsName(exchange);
         inOrder.verify(endpoint, times(1)).getRegistry();
         inOrder.verify(registry, times(1)).meter(METRICS_NAME);
         inOrder.verify(endpoint, times(1)).getMark();

http://git-wip-us.apache.org/repos/asf/camel/blob/85ca4c1f/src/test/java/org/apache/camel/metrics/meter/MeterRouteTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/camel/metrics/meter/MeterRouteTest.java b/src/test/java/org/apache/camel/metrics/meter/MeterRouteTest.java
new file mode 100644
index 0000000..d6daf33
--- /dev/null
+++ b/src/test/java/org/apache/camel/metrics/meter/MeterRouteTest.java
@@ -0,0 +1,78 @@
+package org.apache.camel.metrics.meter;
+
+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.MetricsComponent;
+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.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")
+    private ProducerTemplate producer;
+
+    @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:meter:A?mark=3179")
+                            .to("mock:out");
+                }
+            };
+        }
+
+        @Bean(name = MetricsComponent.METRIC_REGISTRY_NAME)
+        public MetricRegistry getMetricRegistry() {
+            return Mockito.mock(MetricRegistry.class);
+        }
+    }
+
+    @Test
+    public void testOverrideMetricsName() throws Exception {
+        // TODO - 12.05.2014, Lauri - is there any better way to set this up?
+        MetricRegistry mockRegistry = endpoint.getCamelContext().getRegistry().lookupByNameAndType(MetricsComponent.METRIC_REGISTRY_NAME,
MetricRegistry.class);
+        Meter mockMeter = Mockito.mock(Meter.class);
+        InOrder inOrder = Mockito.inOrder(mockRegistry, mockMeter);
+        when(mockRegistry.meter("B")).thenReturn(mockMeter);
+
+        endpoint.expectedMessageCount(1);
+        producer.sendBodyAndHeader(new Object(), MetricsComponent.HEADER_METRIC_NAME, "B");
+        endpoint.assertIsSatisfied();
+        inOrder.verify(mockRegistry, times(1)).meter("B");
+        inOrder.verify(mockMeter, times(1)).mark(3179L);
+        inOrder.verifyNoMoreInteractions();
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/85ca4c1f/src/test/java/org/apache/camel/metrics/timer/TimerProducerTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/camel/metrics/timer/TimerProducerTest.java b/src/test/java/org/apache/camel/metrics/timer/TimerProducerTest.java
index 86ec26d..09e572b 100644
--- a/src/test/java/org/apache/camel/metrics/timer/TimerProducerTest.java
+++ b/src/test/java/org/apache/camel/metrics/timer/TimerProducerTest.java
@@ -51,7 +51,7 @@ public class TimerProducerTest {
         producer = new TimerProducer(endpoint);
         inOrder = Mockito.inOrder(endpoint, exchange, registry, timer, context);
         when(endpoint.getRegistry()).thenReturn(registry);
-        when(endpoint.getMetricsName()).thenReturn(METRICS_NAME);
+        when(endpoint.getMetricsName(exchange)).thenReturn(METRICS_NAME);
         when(registry.timer(METRICS_NAME)).thenReturn(timer);
         when(timer.time()).thenReturn(context);
     }
@@ -68,7 +68,7 @@ public class TimerProducerTest {
         when(exchange.getProperty(PROPERTY_NAME, Timer.Context.class)).thenReturn(null);
         producer.process(exchange);
         inOrder.verify(endpoint, times(1)).getRegistry();
-        inOrder.verify(endpoint, times(1)).getMetricsName();
+        inOrder.verify(endpoint, times(1)).getMetricsName(exchange);
         inOrder.verify(endpoint, times(1)).getAction();
         inOrder.verify(exchange, times(1)).getProperty(PROPERTY_NAME, Timer.Context.class);
         inOrder.verify(registry, times(1)).timer(METRICS_NAME);
@@ -83,7 +83,7 @@ public class TimerProducerTest {
         when(exchange.getProperty(PROPERTY_NAME, Timer.Context.class)).thenReturn(context);
         producer.process(exchange);
         inOrder.verify(endpoint, times(1)).getRegistry();
-        inOrder.verify(endpoint, times(1)).getMetricsName();
+        inOrder.verify(endpoint, times(1)).getMetricsName(exchange);
         inOrder.verify(endpoint, times(1)).getAction();
         inOrder.verify(exchange, times(1)).getProperty(PROPERTY_NAME, Timer.Context.class);
         inOrder.verify(context, times(1)).stop();
@@ -96,7 +96,7 @@ public class TimerProducerTest {
         when(endpoint.getAction()).thenReturn(null);
         producer.process(exchange);
         inOrder.verify(endpoint, times(1)).getRegistry();
-        inOrder.verify(endpoint, times(1)).getMetricsName();
+        inOrder.verify(endpoint, times(1)).getMetricsName(exchange);
         inOrder.verify(endpoint, times(1)).getAction();
         inOrder.verifyNoMoreInteractions();
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/85ca4c1f/src/test/java/org/apache/camel/metrics/timer/TimerRouteTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/camel/metrics/timer/TimerRouteTest.java b/src/test/java/org/apache/camel/metrics/timer/TimerRouteTest.java
new file mode 100644
index 0000000..279be95
--- /dev/null
+++ b/src/test/java/org/apache/camel/metrics/timer/TimerRouteTest.java
@@ -0,0 +1,78 @@
+package org.apache.camel.metrics.timer;
+
+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.MetricsComponent;
+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.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")
+    private ProducerTemplate producer;
+
+    @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:timer:A?action=start")
+                            .to("mock:out");
+                }
+            };
+        }
+
+        @Bean(name = MetricsComponent.METRIC_REGISTRY_NAME)
+        public MetricRegistry getMetricRegistry() {
+            return Mockito.mock(MetricRegistry.class);
+        }
+    }
+
+    @Test
+    public void testOverrideMetricsName() throws Exception {
+        // TODO - 12.05.2014, Lauri - is there any better way to set this up?
+        MetricRegistry mockRegistry = endpoint.getCamelContext().getRegistry().lookupByNameAndType(MetricsComponent.METRIC_REGISTRY_NAME,
MetricRegistry.class);
+        Timer mockTimer = Mockito.mock(Timer.class);
+        InOrder inOrder = Mockito.inOrder(mockRegistry, mockTimer);
+        when(mockRegistry.timer("B")).thenReturn(mockTimer);
+
+        endpoint.expectedMessageCount(1);
+        producer.sendBodyAndHeader(new Object(), MetricsComponent.HEADER_METRIC_NAME, "B");
+        endpoint.assertIsSatisfied();
+        inOrder.verify(mockRegistry, times(1)).timer("B");
+        inOrder.verify(mockTimer, times(1)).time();
+        inOrder.verifyNoMoreInteractions();
+    }
+}


Mime
View raw message