You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2020/03/29 09:15:43 UTC
[camel] 02/02: CAMEL-14805: Use doInit for iniitalizing. Fixed tests
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git
commit dcf209178819d236125b24d4134e991d2c8bcdf8
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sun Mar 29 11:15:27 2020 +0200
CAMEL-14805: Use doInit for iniitalizing. Fixed tests
---
.../camel/component/metrics/MetricsComponent.java | 39 +++++++++++++---------
.../component/metrics/MetricsComponentTest.java | 15 ++++++---
2 files changed, 34 insertions(+), 20 deletions(-)
diff --git a/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/MetricsComponent.java b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/MetricsComponent.java
index 614ddec..4adbd6a 100644
--- a/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/MetricsComponent.java
+++ b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/MetricsComponent.java
@@ -28,6 +28,7 @@ import org.apache.camel.spi.Metadata;
import org.apache.camel.spi.Registry;
import org.apache.camel.spi.annotations.Component;
import org.apache.camel.support.DefaultComponent;
+import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.StringHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -52,10 +53,6 @@ public class MetricsComponent extends DefaultComponent {
@Override
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
- if (metricRegistry == null) {
- Registry camelRegistry = getCamelContext().getRegistry();
- metricRegistry = getOrCreateMetricRegistry(camelRegistry, METRIC_REGISTRY_NAME);
- }
String metricsName = getMetricsName(remaining);
MetricsType metricsType = getMetricsType(remaining);
@@ -65,6 +62,17 @@ public class MetricsComponent extends DefaultComponent {
return endpoint;
}
+ public MetricRegistry getMetricRegistry() {
+ return metricRegistry;
+ }
+
+ /**
+ * To use a custom configured MetricRegistry.
+ */
+ public void setMetricRegistry(MetricRegistry metricRegistry) {
+ this.metricRegistry = metricRegistry;
+ }
+
String getMetricsName(String remaining) {
String name = StringHelper.after(remaining, ":");
return name == null ? remaining : name;
@@ -84,7 +92,7 @@ public class MetricsComponent extends DefaultComponent {
return type;
}
- MetricRegistry getOrCreateMetricRegistry(Registry camelRegistry, String registryName) {
+ static MetricRegistry getOrCreateMetricRegistry(Registry camelRegistry, String registryName) {
LOG.debug("Looking up MetricRegistry from Camel Registry for name \"{}\"", registryName);
MetricRegistry result = getMetricRegistryFromCamelRegistry(camelRegistry, registryName);
if (result == null) {
@@ -95,7 +103,7 @@ public class MetricsComponent extends DefaultComponent {
return result;
}
- MetricRegistry getMetricRegistryFromCamelRegistry(Registry camelRegistry, String registryName) {
+ static MetricRegistry getMetricRegistryFromCamelRegistry(Registry camelRegistry, String registryName) {
MetricRegistry registry = camelRegistry.lookupByNameAndType(registryName, MetricRegistry.class);
if (registry != null) {
return registry;
@@ -108,7 +116,7 @@ public class MetricsComponent extends DefaultComponent {
return null;
}
- MetricRegistry createMetricRegistry() {
+ static MetricRegistry createMetricRegistry() {
MetricRegistry registry = new MetricRegistry();
final Slf4jReporter reporter = Slf4jReporter.forRegistry(registry)
.outputTo(LOG)
@@ -120,14 +128,15 @@ public class MetricsComponent extends DefaultComponent {
return registry;
}
- public MetricRegistry getMetricRegistry() {
- return metricRegistry;
- }
+ @Override
+ protected void doInit() throws Exception {
+ super.doInit();
- /**
- * To use a custom configured MetricRegistry.
- */
- public void setMetricRegistry(MetricRegistry metricRegistry) {
- this.metricRegistry = metricRegistry;
+ if (metricRegistry == null) {
+ Registry camelRegistry = getCamelContext().getRegistry();
+ metricRegistry = getOrCreateMetricRegistry(camelRegistry, METRIC_REGISTRY_NAME);
+ }
+
+ ObjectHelper.notNull(metricRegistry, "MetricsRegistry", this);
}
}
diff --git a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MetricsComponentTest.java b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MetricsComponentTest.java
index 0489536..12969dc 100644
--- a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MetricsComponentTest.java
+++ b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MetricsComponentTest.java
@@ -75,10 +75,12 @@ public class MetricsComponentTest {
when(camelRegistry.lookupByNameAndType(MetricsComponent.METRIC_REGISTRY_NAME, MetricRegistry.class)).thenReturn(metricRegistry);
when(camelContext.adapt(ExtendedCamelContext.class)).thenReturn(camelContext);
when(camelContext.getBeanIntrospection()).thenReturn(new DefaultBeanIntrospection());
+ when(camelContext.getConfigurerResolver()).thenReturn((name, context) -> null);
Map<String, Object> params = new HashMap<>();
Long value = System.currentTimeMillis();
params.put("mark", value);
+ component.init();
Endpoint result = component.createEndpoint("metrics:meter:long.meter", "meter:long.meter", params);
assertThat(result, is(notNullValue()));
assertThat(result, is(instanceOf(MetricsEndpoint.class)));
@@ -100,9 +102,12 @@ public class MetricsComponentTest {
when(camelRegistry.lookupByNameAndType(MetricsComponent.METRIC_REGISTRY_NAME, MetricRegistry.class)).thenReturn(metricRegistry);
when(camelContext.adapt(ExtendedCamelContext.class)).thenReturn(camelContext);
when(camelContext.getBeanIntrospection()).thenReturn(new DefaultBeanIntrospection());
+ when(camelContext.getConfigurerResolver()).thenReturn((name, context) -> null);
+
Map<String, Object> params = new HashMap<>();
Long value = System.currentTimeMillis();
params.put("mark", value);
+ component.init();
Endpoint result = component.createEndpoint("metrics:meter:long.meter", "meter:long.meter", params);
assertThat(result, is(notNullValue()));
assertThat(result, is(instanceOf(MetricsEndpoint.class)));
@@ -194,7 +199,7 @@ public class MetricsComponentTest {
@Test
public void testGetOrCreateMetricRegistryFoundInCamelRegistry() throws Exception {
when(camelRegistry.lookupByNameAndType("name", MetricRegistry.class)).thenReturn(metricRegistry);
- MetricRegistry result = component.getOrCreateMetricRegistry(camelRegistry, "name");
+ MetricRegistry result = MetricsComponent.getOrCreateMetricRegistry(camelRegistry, "name");
assertThat(result, is(metricRegistry));
inOrder.verify(camelRegistry, times(1)).lookupByNameAndType("name", MetricRegistry.class);
inOrder.verifyNoMoreInteractions();
@@ -204,7 +209,7 @@ public class MetricsComponentTest {
public void testGetOrCreateMetricRegistryFoundInCamelRegistryByType() throws Exception {
when(camelRegistry.lookupByNameAndType("name", MetricRegistry.class)).thenReturn(null);
when(camelRegistry.findByType(MetricRegistry.class)).thenReturn(Collections.singleton(metricRegistry));
- MetricRegistry result = component.getOrCreateMetricRegistry(camelRegistry, "name");
+ MetricRegistry result = MetricsComponent.getOrCreateMetricRegistry(camelRegistry, "name");
assertThat(result, is(metricRegistry));
inOrder.verify(camelRegistry, times(1)).lookupByNameAndType("name", MetricRegistry.class);
inOrder.verify(camelRegistry, times(1)).findByType(MetricRegistry.class);
@@ -215,7 +220,7 @@ public class MetricsComponentTest {
public void testGetOrCreateMetricRegistryNotFoundInCamelRegistry() throws Exception {
when(camelRegistry.lookupByNameAndType("name", MetricRegistry.class)).thenReturn(null);
when(camelRegistry.findByType(MetricRegistry.class)).thenReturn(Collections.<MetricRegistry>emptySet());
- MetricRegistry result = component.getOrCreateMetricRegistry(camelRegistry, "name");
+ MetricRegistry result = MetricsComponent.getOrCreateMetricRegistry(camelRegistry, "name");
assertThat(result, is(notNullValue()));
assertThat(result, is(not(metricRegistry)));
inOrder.verify(camelRegistry, times(1)).lookupByNameAndType("name", MetricRegistry.class);
@@ -226,7 +231,7 @@ public class MetricsComponentTest {
@Test
public void testGetMetricRegistryFromCamelRegistry() throws Exception {
when(camelRegistry.lookupByNameAndType("name", MetricRegistry.class)).thenReturn(metricRegistry);
- MetricRegistry result = component.getMetricRegistryFromCamelRegistry(camelRegistry, "name");
+ MetricRegistry result = MetricsComponent.getMetricRegistryFromCamelRegistry(camelRegistry, "name");
assertThat(result, is(metricRegistry));
inOrder.verify(camelRegistry, times(1)).lookupByNameAndType("name", MetricRegistry.class);
inOrder.verifyNoMoreInteractions();
@@ -234,7 +239,7 @@ public class MetricsComponentTest {
@Test
public void testCreateMetricRegistry() throws Exception {
- MetricRegistry registry = component.createMetricRegistry();
+ MetricRegistry registry = MetricsComponent.createMetricRegistry();
assertThat(registry, is(notNullValue()));
}
}