JmxAttributeGauge implements Gauge<Object> { @Override public Object getValue() { try { return mBeanServerConn.getAttribute(getObjectName(), attributeName); } catch (IOException | JMException e) { return null; } } JmxAttributeGauge(ObjectName objectName, String attributeName);  JmxAttributeGauge(MBeanServerConnection mBeanServerConn, ObjectName objectName, String attributeName); @Override Object getValue();  }
@Test public void returnsJmxAttribute() throws Exception { ObjectName objectName = new ObjectName("java.lang:type=ClassLoading"); JmxAttributeGauge gauge = new JmxAttributeGauge(mBeanServer, objectName, "LoadedClassCount"); assertThat(gauge.getValue()).isInstanceOf(Integer.class); assertThat((Integer) gauge.getValue()).isGreaterThan(0); }
@Test public void returnsNullIfAttributeDoesNotExist() throws Exception { ObjectName objectName = new ObjectName("java.lang:type=ClassLoading"); JmxAttributeGauge gauge = new JmxAttributeGauge(mBeanServer, objectName, "DoesNotExist"); assertThat(gauge.getValue()).isNull(); }
@Test public void returnsNullIfMBeanNotFound() throws Exception { ObjectName objectName = new ObjectName("foo.bar:type=NoSuchMBean"); JmxAttributeGauge gauge = new JmxAttributeGauge(mBeanServer, objectName, "LoadedClassCount"); assertThat(gauge.getValue()).isNull(); }
@Test public void returnsAttributeForObjectNamePattern() throws Exception { ObjectName objectName = new ObjectName("JmxAttributeGaugeTest:name=test1,*"); JmxAttributeGauge gauge = new JmxAttributeGauge(mBeanServer, objectName, "Value"); assertThat(gauge.getValue()).isInstanceOf(Long.class); assertThat((Long) gauge.getValue()).isEqualTo(Long.MAX_VALUE); }
@Test public void returnsNullIfObjectNamePatternAmbiguous() throws Exception { ObjectName objectName = new ObjectName("JmxAttributeGaugeTest:type=test,*"); JmxAttributeGauge gauge = new JmxAttributeGauge(mBeanServer, objectName, "Value"); assertThat(gauge.getValue()).isNull(); }