You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by jo...@apache.org on 2022/08/08 11:39:18 UTC

[sling-org-apache-sling-commons-metrics] branch master updated: SLING-11509 JmxExporter: add support to boolean properties (#5)

This is an automated email from the ASF dual-hosted git repository.

joerghoh pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-metrics.git


The following commit(s) were added to refs/heads/master by this push:
     new 26d51be  SLING-11509 JmxExporter: add support to boolean properties (#5)
26d51be is described below

commit 26d51bec3ebd41d4c85d27338ecc438ec974814d
Author: Jörg Hoh <jo...@users.noreply.github.com>
AuthorDate: Mon Aug 8 13:39:14 2022 +0200

    SLING-11509 JmxExporter: add support to boolean properties (#5)
    
    JmxExporter: add support to boolean properties
    improve general test coverage
---
 .../metrics/internal/JmxExporterFactory.java       |  2 +
 .../metrics/internal/JmxExporterFactoryTest.java   | 51 ++++++++++++++++++++++
 2 files changed, 53 insertions(+)

diff --git a/src/main/java/org/apache/sling/commons/metrics/internal/JmxExporterFactory.java b/src/main/java/org/apache/sling/commons/metrics/internal/JmxExporterFactory.java
index 587fa6b..94a18f4 100644
--- a/src/main/java/org/apache/sling/commons/metrics/internal/JmxExporterFactory.java
+++ b/src/main/java/org/apache/sling/commons/metrics/internal/JmxExporterFactory.java
@@ -132,6 +132,8 @@ public class JmxExporterFactory {
                 supplier = getSupplier(objectname,attr.getName(),"");
             } else if ("double".equals(attr.getType())) {
                 supplier = getSupplier(objectname,attr.getName(), Double.valueOf(0.0));
+            } else if ("boolean".equals(attr.getType())) {
+                supplier = getSupplier(objectname,attr.getName(), Boolean.FALSE);
             }
             
             if (supplier != null) {
diff --git a/src/test/java/org/apache/sling/commons/metrics/internal/JmxExporterFactoryTest.java b/src/test/java/org/apache/sling/commons/metrics/internal/JmxExporterFactoryTest.java
index 38d2220..d72076a 100644
--- a/src/test/java/org/apache/sling/commons/metrics/internal/JmxExporterFactoryTest.java
+++ b/src/test/java/org/apache/sling/commons/metrics/internal/JmxExporterFactoryTest.java
@@ -19,10 +19,13 @@
 package org.apache.sling.commons.metrics.internal;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.mockito.Mockito.never;
 
 import java.lang.management.ManagementFactory;
+import java.util.Collections;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.function.Supplier;
 
@@ -34,6 +37,7 @@ import javax.management.MalformedObjectNameException;
 import javax.management.NotCompliantMBeanException;
 import javax.management.ObjectName;
 
+import org.apache.sling.commons.metrics.Gauge;
 import org.apache.sling.commons.metrics.MetricsService;
 import org.apache.sling.testing.mock.osgi.junit.OsgiContext;
 import org.junit.After;
@@ -64,6 +68,13 @@ public class JmxExporterFactoryTest {
     @Captor
     ArgumentCaptor<Supplier<Double>> doubleSupplierCaptor;
     
+    @Captor
+    ArgumentCaptor<Supplier<Boolean>> booleanSupplierCaptor;
+
+    @Captor
+    ArgumentCaptor<Supplier<List<String>>> listSupplierCaptor;
+
+    
     JmxExporterFactory exporter;
     
     private static final String OBJECT_NAME_0 = "org.apache.sling.whiteboard.jmxexporter.impl0:type=sample1";
@@ -75,8 +86,10 @@ public class JmxExporterFactoryTest {
     
     private static final String EXPECTED_0_INT_NAME  = "org.apache.sling.whiteboard.jmxexporter.impl0.sample1.Int";
     private static final String EXPECTED_0_LONG_NAME = "org.apache.sling.whiteboard.jmxexporter.impl0.sample1.Long";
+    private static final String EXPECTED_0_BOOLEAN_NAME = "org.apache.sling.whiteboard.jmxexporter.impl0.sample1.Boolean";
     private static final String EXPECTED_0_STRING_NAME = "org.apache.sling.whiteboard.jmxexporter.impl0.sample1.String";
     private static final String EXPECTED_0_DOUBLE_NAME = "org.apache.sling.whiteboard.jmxexporter.impl0.sample1.Double";
+    private static final String EXPECTED_0_LIST_NAME = "org.apache.sling.whiteboard.jmxexporter.impl0.sample1.List";
     
     private static final String EXPECTED_1_INT_NAME  = "org.apache.sling.whiteboard.jmxexporter.impl0.impl2.sample2.Int";
     private static final String EXPECTED_1_LONG_NAME = "org.apache.sling.whiteboard.jmxexporter.impl0.impl2.sample2.Long";
@@ -136,10 +149,18 @@ public class JmxExporterFactoryTest {
         Mockito.verify(metrics).gauge(Mockito.eq(EXPECTED_0_STRING_NAME), stringSupplierCaptor.capture());
         assertEquals("sample",stringSupplierCaptor.getValue().get());
         
+        // Boolean
+        Mockito.verify(metrics).gauge(Mockito.eq(EXPECTED_0_BOOLEAN_NAME), booleanSupplierCaptor.capture());
+        assertFalse(booleanSupplierCaptor.getValue().get());
+        
         // Double
         Mockito.verify(metrics).gauge(Mockito.eq(EXPECTED_0_DOUBLE_NAME), doubleSupplierCaptor.capture());
         assertEquals(STATIC_DOUBLE,doubleSupplierCaptor.getValue().get());
         
+        // getList()
+        Mockito.verify(metrics, never()).gauge(Mockito.eq(EXPECTED_0_LIST_NAME), listSupplierCaptor.capture());
+        
+        
         // MBean 1
         Mockito.verify(metrics).gauge(Mockito.eq(EXPECTED_1_INT_NAME), intSupplierCaptor.capture());
         assertEquals(new Integer(1),intSupplierCaptor.getValue().get());
@@ -152,6 +173,25 @@ public class JmxExporterFactoryTest {
         
     }
     
+    @Test
+    public void registerNonExistingMBean() {
+        Map<String,Object> props = new HashMap<>();
+        props.put("objectnames", new String[]{"org.apache.sling:type=nonexistent"}); // there is no such mbean
+        
+        context.registerInjectActivateService(exporter, props);
+        Mockito.verifyNoInteractions(metrics);
+    }
+    
+    @Test
+    public void registerInvalidMBean() {
+        Map<String,Object> props = new HashMap<>();
+        props.put("objectnames", new String[]{"org.apache.sling%type=nonexistent"}); // this is invalid
+        
+        context.registerInjectActivateService(exporter, props);
+        Mockito.verifyNoInteractions(metrics);
+    }
+    
+    
     static class SimpleBean implements SimpleBeanMBean {
 
         
@@ -185,6 +225,14 @@ public class JmxExporterFactoryTest {
             return STATIC_DOUBLE;
         }
         
+        public boolean getBoolean() {
+            return false;
+        }
+
+        public List<String> getList() {
+            return Collections.emptyList();
+        }
+        
     }
     
     
@@ -194,6 +242,9 @@ public class JmxExporterFactoryTest {
         public long getLong();
         public String getString();
         public double getDouble();
+        public boolean getBoolean();
+        
+        public List<String> getList(); // this type is not supported!
         
     }