|
1 | 1 | package io.dropwizard.metrics;
|
2 | 2 |
|
3 |
| -import org.junit.Test; |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
4 | 4 |
|
5 |
| -import io.dropwizard.metrics.JmxAttributeGauge; |
| 5 | +import java.lang.management.ManagementFactory; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
6 | 8 |
|
7 |
| -import javax.management.AttributeNotFoundException; |
| 9 | +import javax.management.JMException; |
8 | 10 | import javax.management.MBeanServer;
|
| 11 | +import javax.management.ObjectInstance; |
9 | 12 | import javax.management.ObjectName;
|
10 | 13 |
|
11 |
| -import static org.assertj.core.api.Assertions.assertThat; |
12 |
| -import static org.mockito.Mockito.mock; |
13 |
| -import static org.mockito.Mockito.when; |
| 14 | +import org.junit.AfterClass; |
| 15 | +import org.junit.BeforeClass; |
| 16 | +import org.junit.Test; |
14 | 17 |
|
15 | 18 | public class JmxAttributeGaugeTest {
|
16 |
| - private final MBeanServer mBeanServer = mock(MBeanServer.class); |
17 |
| - private final ObjectName objectName = mock(ObjectName.class); |
18 |
| - private final JmxAttributeGauge gauge = new JmxAttributeGauge(mBeanServer, objectName, "attr"); |
19 |
| - private final Object value = mock(Object.class); |
| 19 | + |
| 20 | + private static MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); |
| 21 | + |
| 22 | + private static List<ObjectName> registeredMBeans = new ArrayList<ObjectName>(); |
| 23 | + |
| 24 | + public interface JmxTestMBean { |
| 25 | + Long getValue(); |
| 26 | + } |
| 27 | + |
| 28 | + private static class JmxTest implements JmxTestMBean { |
| 29 | + @Override |
| 30 | + public Long getValue() { |
| 31 | + return Long.MAX_VALUE; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + @BeforeClass |
| 36 | + public static void setUp() throws Exception { |
| 37 | + registerMBean(new ObjectName("JmxAttributeGaugeTest:type=test,name=test1")); |
| 38 | + registerMBean(new ObjectName("JmxAttributeGaugeTest:type=test,name=test2")); |
| 39 | + } |
| 40 | + |
| 41 | + @AfterClass |
| 42 | + public static void tearDown() { |
| 43 | + for (ObjectName objectName : registeredMBeans) { |
| 44 | + try { |
| 45 | + mBeanServer.unregisterMBean(objectName); |
| 46 | + } catch (Exception e) { |
| 47 | + // ignore |
| 48 | + } |
| 49 | + } |
| 50 | + } |
20 | 51 |
|
21 | 52 | @Test
|
22 |
| - public void returnsAJmxAttribute() throws Exception { |
23 |
| - when(mBeanServer.getAttribute(objectName, "attr")).thenReturn(value); |
| 53 | + public void returnsJmxAttribute() throws Exception { |
| 54 | + ObjectName objectName = new ObjectName("java.lang:type=ClassLoading"); |
| 55 | + JmxAttributeGauge gauge = new JmxAttributeGauge(mBeanServer, objectName, "LoadedClassCount"); |
24 | 56 |
|
25 |
| - assertThat(gauge.getValue()) |
26 |
| - .isEqualTo(value); |
| 57 | + assertThat(gauge.getValue()).isInstanceOf(Integer.class); |
| 58 | + assertThat((Integer) gauge.getValue()).isGreaterThan(0); |
27 | 59 | }
|
28 | 60 |
|
29 | 61 | @Test
|
30 |
| - public void returnsNullIfThereIsAnException() throws Exception { |
31 |
| - when(mBeanServer.getAttribute(objectName, "attr")).thenThrow(new AttributeNotFoundException()); |
| 62 | + public void returnsNullIfAttributeDoesNotExist() throws Exception { |
| 63 | + ObjectName objectName = new ObjectName("java.lang:type=ClassLoading"); |
| 64 | + JmxAttributeGauge gauge = new JmxAttributeGauge(mBeanServer, objectName, "DoesNotExist"); |
32 | 65 |
|
33 |
| - assertThat(gauge.getValue()) |
34 |
| - .isNull(); |
| 66 | + assertThat(gauge.getValue()).isNull(); |
35 | 67 | }
|
| 68 | + |
| 69 | + @Test |
| 70 | + public void returnsNullIfMBeanNotFound() throws Exception { |
| 71 | + ObjectName objectName = new ObjectName("foo.bar:type=NoSuchMBean"); |
| 72 | + JmxAttributeGauge gauge = new JmxAttributeGauge(mBeanServer, objectName, "LoadedClassCount"); |
| 73 | + |
| 74 | + assertThat(gauge.getValue()).isNull(); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void returnsAttributeForObjectNamePattern() throws Exception { |
| 79 | + ObjectName objectName = new ObjectName("JmxAttributeGaugeTest:name=test1,*"); |
| 80 | + JmxAttributeGauge gauge = new JmxAttributeGauge(mBeanServer, objectName, "Value"); |
| 81 | + |
| 82 | + assertThat(gauge.getValue()).isInstanceOf(Long.class); |
| 83 | + assertThat((Long) gauge.getValue()).isEqualTo(Long.MAX_VALUE); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void returnsNullIfObjectNamePatternAmbiguous() throws Exception { |
| 88 | + ObjectName objectName = new ObjectName("JmxAttributeGaugeTest:type=test,*"); |
| 89 | + JmxAttributeGauge gauge = new JmxAttributeGauge(mBeanServer, objectName, "Value"); |
| 90 | + |
| 91 | + assertThat(gauge.getValue()).isNull(); |
| 92 | + } |
| 93 | + |
| 94 | + private static void registerMBean(ObjectName objectName) throws JMException { |
| 95 | + ObjectInstance objectInstance = mBeanServer.registerMBean(new JmxTest(), objectName); |
| 96 | + registeredMBeans.add(objectInstance.getObjectName()); |
| 97 | + } |
| 98 | + |
36 | 99 | }
|
0 commit comments