ReadHandler extends JsonRequestHandler<JmxReadRequest> { private Object getAttribute(MBeanServerExecutor pServerManager, ObjectName pMBeanName, String attribute) throws MBeanException, ReflectionException, IOException, AttributeNotFoundException, InstanceNotFoundException { return pServerManager.call(pMBeanName, MBEAN_ATTRIBUTE_READ_HANDLER, attribute); } ReadHandler(Restrictor pRestrictor); @Override /** {@inheritDoc} */ RequestType getType(); @Override boolean handleAllServersAtOnce(JmxReadRequest pRequest); @Override Object doHandleRequest(MBeanServerConnection pServer, JmxReadRequest pRequest); @Override /** {@inheritDoc} */ Object doHandleRequest(MBeanServerExecutor pServerManager, JmxReadRequest pRequest);  }
@Test public void singleBeanSingleAttribute() throws Exception { JmxReadRequest request = new JmxRequestBuilder(READ, testBeanName.getCanonicalName()). attribute("testAttribute"). build(); MBeanServerConnection connection = createMock(MBeanServerConnection.class); expect(connection.getAttribute(testBeanName,"testAttribute")).andReturn("testValue"); replay(connection); Object res = handler.handleRequest(connection,request); verify(connection); assertEquals("testValue",res); }
@Test public void singleBeanNoAttributes() throws Exception { JmxReadRequest request = new JmxRequestBuilder(READ, testBeanName.getCanonicalName()). attribute(null). build(); MBeanServer server = createMock(MBeanServer.class); String attrs[] = new String[] {"attr0","atrr1","attr2"}; String vals[] = new String[] {"val0", "val1", "val2"}; prepareMBeanInfos(server, testBeanName, attrs); for (int i=0;i<attrs.length;i++) { expect(server.getAttribute(testBeanName,attrs[i])).andReturn(vals[i]); } replay(server); Map res = (Map) handler.handleRequest(getMBeanServerManager(server),request); verify(server); for (int i=0;i<attrs.length;i++) { assertEquals(vals[i],res.get(attrs[i])); } }
@Test public void singleBeanMultiAttributes() throws Exception { JmxReadRequest request = new JmxRequestBuilder(READ, testBeanName.getCanonicalName()). attributes(Arrays.asList("attr0", "attr1")). build(); MBeanServer server = createMock(MBeanServer.class); expect(server.isRegistered(testBeanName)).andStubReturn(true); expect(server.getAttribute(testBeanName,"attr0")).andReturn("val0"); expect(server.getAttribute(testBeanName,"attr1")).andReturn("val1"); replay(server); Map res = (Map) handler.handleRequest(getMBeanServerManager(server),request); verify(server); assertEquals("val0", res.get("attr0")); assertEquals("val1",res.get("attr1")); }
@Test(expectedExceptions = AttributeNotFoundException.class) public void singleBeanMultiAttributesWithAWrongAttributeNameThrowingException() throws Exception { JmxReadRequest request = new JmxRequestBuilder(READ, testBeanName.getCanonicalName()). attributes(Arrays.asList("attr0", "attr1")). option(ConfigKey.IGNORE_ERRORS, "false"). build(); MBeanServer server = createMock(MBeanServer.class); expect(server.isRegistered(testBeanName)).andStubReturn(true); expect(server.getAttribute(testBeanName,"attr0")).andReturn("val0"); expect(server.getAttribute(testBeanName,"attr1")).andThrow(new AttributeNotFoundException("Couldn't find attr1")); replay(server); Map res = (Map) handler.handleRequest(getMBeanServerManager(server),request); }
@Test public void singleBeanMultiAttributesWithAWrongAttributeNameHandlingException() throws Exception { JmxReadRequest request = new JmxRequestBuilder(READ, testBeanName.getCanonicalName()). attributes(Arrays.asList("attr0", "attr1")). option(ConfigKey.IGNORE_ERRORS, "true"). build(); MBeanServer server = createMock(MBeanServer.class); expect(server.isRegistered(testBeanName)).andStubReturn(true); expect(server.getAttribute(testBeanName,"attr0")).andReturn("val0"); expect(server.getAttribute(testBeanName,"attr1")).andThrow(new AttributeNotFoundException("Couldn't find attr1")); replay(server); Map res = (Map) handler.handleRequest(getMBeanServerManager(server),request); verify(server); assertEquals("val0",res.get("attr0")); String err = (String) res.get("attr1"); assertTrue(err != null && err.contains("ERROR")); }
@Test(groups = "java6") public void searchPatternSingleAttribute() throws Exception { ObjectName patternMBean = new ObjectName("java.lang:type=*"); JmxReadRequest request = new JmxRequestBuilder(READ, patternMBean). attribute("mem1"). build(); ObjectName beans[] = { new ObjectName("java.lang:type=Memory"), new ObjectName("java.lang:type=GarbageCollection") }; MBeanServer server = prepareMultiAttributeTest(patternMBean, beans); expect(server.getAttribute(beans[0],"mem1")).andReturn("memval1"); replay(server); Map res = (Map) handler.handleRequest(getMBeanServerManager(server), request); verify(server); assertEquals(1,res.size()); assertEquals("memval1",((Map) res.get("java.lang:type=Memory")).get("mem1")); }
@Test(groups = "java6") public void searchPatternNoAttribute() throws Exception { ObjectName patternMBean = new ObjectName("java.lang:type=*"); JmxReadRequest[] requests = new JmxReadRequest[2]; requests[0] = new JmxRequestBuilder(READ, patternMBean). attribute(null). build(); requests[1] = new JmxRequestBuilder(READ, patternMBean). attributes(Arrays.asList((String) null)). build(); for (JmxReadRequest request : requests) { ObjectName beans[] = { new ObjectName("java.lang:type=Memory"), new ObjectName("java.lang:type=GarbageCollection") }; MBeanServer server = prepareMultiAttributeTest(patternMBean, beans); expect(server.getAttribute(beans[0],"mem0")).andReturn("memval0"); expect(server.getAttribute(beans[0],"mem1")).andReturn("memval1"); expect(server.getAttribute(beans[0],"common")).andReturn("commonVal0"); expect(server.getAttribute(beans[1],"gc0")).andReturn("gcval0"); expect(server.getAttribute(beans[1],"gc1")).andReturn("gcval1"); expect(server.getAttribute(beans[1],"gc3")).andReturn("gcval3"); expect(server.getAttribute(beans[1],"common")).andReturn("commonVal1"); replay(server); Map res = (Map) handler.handleRequest(getMBeanServerManager(server), request); assertEquals("memval0",((Map) res.get("java.lang:type=Memory")).get("mem0")); assertEquals("memval1",((Map) res.get("java.lang:type=Memory")).get("mem1")); assertEquals("commonVal0",((Map) res.get("java.lang:type=Memory")).get("common")); assertEquals("gcval0",((Map) res.get("java.lang:type=GarbageCollection")).get("gc0")); assertEquals("gcval1",((Map) res.get("java.lang:type=GarbageCollection")).get("gc1")); assertEquals("gcval3",((Map) res.get("java.lang:type=GarbageCollection")).get("gc3")); assertEquals("commonVal1",((Map) res.get("java.lang:type=GarbageCollection")).get("common")); verify(server); } }
@Test(groups = "java6") public void searchPatternNoAttributesFound() throws Exception { ObjectName patternMBean = new ObjectName("java.lang:type=*"); JmxReadRequest request = new JmxRequestBuilder(READ, patternMBean). attribute(null). build(); ObjectName beans[] = { new ObjectName("java.lang:type=Memory"), new ObjectName("java.lang:type=GarbageCollection") }; MBeanServer server = createMock(MBeanServer.class); expect(server.queryNames(patternMBean,null)).andReturn(new HashSet(Arrays.asList(beans))); prepareMBeanInfos(server,beans[0],new String[0]); prepareMBeanInfos(server,beans[1],new String[] { "gc0" }); expect(server.getAttribute(beans[1],"gc0")).andReturn("gcval0"); replay(server); Map res = (Map) handler.handleRequest(getMBeanServerManager(server), request); assertEquals(res.size(),1); verify(server); }
@Test(groups = "java6") public void searchPatternMultiAttributes1() throws Exception { ObjectName patternMBean = new ObjectName("java.lang:type=*"); JmxReadRequest request = new JmxRequestBuilder(READ, patternMBean). attributes(Arrays.asList("mem0","gc3")). build(); ObjectName beans[] = { new ObjectName("java.lang:type=Memory"), new ObjectName("java.lang:type=GarbageCollection") }; MBeanServer server = prepareMultiAttributeTest(patternMBean, beans); expect(server.getAttribute(beans[0],"mem0")).andReturn("memval0"); expect(server.getAttribute(beans[1],"gc3")).andReturn("gcval3"); replay(server); Map res = (Map) handler.handleRequest(getMBeanServerManager(server), request); verify(server); assertEquals(2,res.size()); assertEquals("memval0",((Map) res.get("java.lang:type=Memory")).get("mem0")); assertEquals("gcval3",((Map) res.get("java.lang:type=GarbageCollection")).get("gc3")); }
@Test(groups = "java6") public void searchPatternMultiAttributes4() throws Exception { ObjectName patternMBean = new ObjectName("java.lang:type=*"); JmxReadRequest request = new JmxRequestBuilder(READ, patternMBean). attributes(Arrays.asList("common")). build(); ObjectName beans[] = { new ObjectName("java.lang:type=Memory"), new ObjectName("java.lang:type=GarbageCollection") }; MBeanServer server = prepareMultiAttributeTest(patternMBean, beans); expect(server.getAttribute(beans[0],"common")).andReturn("com1"); expect(server.getAttribute(beans[1],"common")).andReturn("com2"); replay(server); Map res = (Map) handler.handleRequest(getMBeanServerManager(server), request); verify(server); assertEquals(2,res.size()); assertEquals("com1",((Map) res.get("java.lang:type=Memory")).get("common")); assertEquals("com2",((Map) res.get("java.lang:type=GarbageCollection")).get("common")); }