View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.tools;
12  
13  import javax.management.AttributeNotFoundException;
14  import javax.management.InstanceNotFoundException;
15  import javax.management.MBeanAttributeInfo;
16  import javax.management.MBeanException;
17  import javax.management.MBeanInfo;
18  import javax.management.MBeanServer;
19  import javax.management.ObjectName;
20  import javax.management.ReflectionException;
21  import javax.management.openmbean.CompositeData;
22  
23  import org.junit.jupiter.api.Assertions;
24  import org.junit.jupiter.api.BeforeEach;
25  import org.junit.jupiter.api.Test;
26  import org.mockito.Mockito;
27  
28  class JmxToolsTest {
29  
30    private MBeanServer mbeanServer;
31    private ObjectName objectName;
32    private CompositeData compositeData;
33  
34    @BeforeEach
35    void setUp() throws Exception {
36      mbeanServer = Mockito.mock(MBeanServer.class);
37      objectName = new ObjectName("domain:type=Test");
38      compositeData = Mockito.mock(CompositeData.class);
39    }
40  
41    @Test
42    void testGetAttribute_success() throws Exception {
43      Mockito.when(mbeanServer.getAttribute(objectName, "attr")).thenReturn("value");
44      Assertions.assertEquals("value", JmxTools.getAttribute(mbeanServer, objectName, "attr"));
45    }
46  
47    @Test
48    void testGetAttribute_AttributeNotFoundException() throws Exception {
49      Mockito.when(mbeanServer.getAttribute(objectName, "attr"))
50          .thenThrow(new AttributeNotFoundException());
51      Assertions.assertNull(JmxTools.getAttribute(mbeanServer, objectName, "attr"));
52    }
53  
54    @Test
55    void testInvoke_success() throws Exception {
56      Mockito.when(mbeanServer.invoke(objectName, "method", null, null)).thenReturn("result");
57      Assertions.assertEquals("result",
58          JmxTools.invoke(mbeanServer, objectName, "method", null, null));
59    }
60  
61    @Test
62    void testInvoke_InstanceNotFoundException() throws Exception {
63      Mockito.when(mbeanServer.invoke(objectName, "method", null, null))
64          .thenThrow(new InstanceNotFoundException());
65      Assertions.assertNull(JmxTools.invoke(mbeanServer, objectName, "method", null, null));
66    }
67  
68    @Test
69    void testGetLongAttr_MBeanServer() throws InstanceNotFoundException, AttributeNotFoundException,
70        ReflectionException, MBeanException {
71      Mockito.when(mbeanServer.getAttribute(Mockito.any(), Mockito.any())).thenReturn(42L);
72      Assertions.assertEquals(42L, JmxTools.getLongAttr(mbeanServer, objectName, "attr", 10L));
73    }
74  
75    @Test
76    void testGetLongAttr_MBeanServer_default() {
77      Assertions.assertEquals(10L, JmxTools.getLongAttr(mbeanServer, objectName, "attr", 10L));
78    }
79  
80    @Test
81    void testGetLongAttr_CompositeData() {
82      Mockito.when(compositeData.get("longAttr")).thenReturn(123L);
83      Assertions.assertEquals(123L, JmxTools.getLongAttr(compositeData, "longAttr"));
84    }
85  
86    @Test
87    void testGetLongAttr_CompositeData_default() {
88      Mockito.when(compositeData.get("longAttr")).thenReturn(null);
89      Assertions.assertEquals(0L, JmxTools.getLongAttr(compositeData, "longAttr"));
90    }
91  
92    @Test
93    void testGetIntAttr_MBeanServer() throws InstanceNotFoundException, AttributeNotFoundException,
94        ReflectionException, MBeanException {
95      Mockito.when(mbeanServer.getAttribute(Mockito.any(), Mockito.any())).thenReturn(7);
96      Assertions.assertEquals(7, JmxTools.getIntAttr(mbeanServer, objectName, "attr"));
97    }
98  
99    @Test
100   void testGetIntAttr_CompositeData() {
101     Mockito.when(compositeData.get("intAttr")).thenReturn(5);
102     Assertions.assertEquals(5, JmxTools.getIntAttr(compositeData, "intAttr", 2));
103   }
104 
105   @Test
106   void testGetIntAttr_CompositeData_default() {
107     Mockito.when(compositeData.get("intAttr")).thenReturn(null);
108     Assertions.assertEquals(2, JmxTools.getIntAttr(compositeData, "intAttr", 2));
109   }
110 
111   @Test
112   void testGetStringAttr_MBeanServer() throws InstanceNotFoundException, AttributeNotFoundException,
113       ReflectionException, MBeanException {
114     Mockito.when(mbeanServer.getAttribute(Mockito.any(), Mockito.any())).thenReturn("str");
115     Assertions.assertEquals("str", JmxTools.getStringAttr(mbeanServer, objectName, "attr"));
116   }
117 
118   @Test
119   void testGetStringAttr_CompositeData() {
120     Mockito.when(compositeData.get("strAttr")).thenReturn("hello");
121     Assertions.assertEquals("hello", JmxTools.getStringAttr(compositeData, "strAttr"));
122   }
123 
124   @Test
125   void testGetBooleanAttr_MBeanServer() throws InstanceNotFoundException,
126       AttributeNotFoundException, ReflectionException, MBeanException {
127     Mockito.when(mbeanServer.getAttribute(Mockito.any(), Mockito.any())).thenReturn(true);
128     Assertions.assertTrue(JmxTools.getBooleanAttr(mbeanServer, objectName, "attr"));
129   }
130 
131   @Test
132   void testGetBooleanAttr_CompositeData() {
133     Mockito.when(compositeData.get("boolAttr")).thenReturn(Boolean.TRUE);
134     Assertions.assertTrue(JmxTools.getBooleanAttr(compositeData, "boolAttr"));
135   }
136 
137   @Test
138   void testHasAttribute_true() throws Exception {
139     MBeanAttributeInfo attrInfo =
140         new MBeanAttributeInfo("attr", "java.lang.String", "", true, false, false);
141     MBeanInfo mbeanInfo =
142         new MBeanInfo("class", "", new MBeanAttributeInfo[] {attrInfo}, null, null, null);
143     Mockito.when(mbeanServer.getMBeanInfo(objectName)).thenReturn(mbeanInfo);
144     Assertions.assertTrue(JmxTools.hasAttribute(mbeanServer, objectName, "attr"));
145   }
146 
147   @Test
148   void testHasAttribute_false() throws Exception {
149     MBeanInfo mbeanInfo = new MBeanInfo("class", "", new MBeanAttributeInfo[0], null, null, null);
150     Mockito.when(mbeanServer.getMBeanInfo(objectName)).thenReturn(mbeanInfo);
151     Assertions.assertFalse(JmxTools.hasAttribute(mbeanServer, objectName, "attr"));
152   }
153 }