@Test public void testGetSetMultiple() throws Exception { MultipleAttributes obj = new MultipleAttributes(); JmxClient client = new JmxClient(DEFAULT_PORT); try { server.register(obj); int x = 2134; obj.x = x; int y = 242634; obj.y = y; List<Attribute> attributes = client.getAttributes(DOMAIN_NAME, OBJECT_NAME, new String[] {"x", "y"}); assertEquals(2, attributes.size()); assertEquals(x, attributes.get(0).getValue()); assertEquals(y, attributes.get(1).getValue()); int x2 = x + 1; int y2 = y + 1; attributes.clear(); attributes.add(new Attribute("x", x2)); attributes.add(new Attribute("y", y2)); client.setAttributes(DOMAIN_NAME, OBJECT_NAME, attributes); attributes = client.getAttributes(DOMAIN_NAME, OBJECT_NAME, new String[] {"x", "y"}); assertEquals(2, attributes.size()); assertEquals("x", attributes.get(0).getName()); assertEquals(x2, attributes.get(0).getValue()); assertEquals("y", attributes.get(1).getName()); assertEquals(y2, attributes.get(1).getValue()); } finally { server.unregister(obj); client.close(); } }
@Test(expected = ReflectionException.class) public void testSetThrows() throws Exception { AttributeThrows setThrow = new AttributeThrows(); JmxClient client = new JmxClient(DEFAULT_PORT); try { server.register(setThrow); client.setAttribute(DOMAIN_NAME, OBJECT_NAME, "throws", 1); } finally { server.unregister(setThrow); client.close(); } }
@Test(expected = AttributeNotFoundException.class) public void testUnknownSetter() throws Exception { TestObject testObj = new TestObject(); JmxClient client = new JmxClient(DEFAULT_PORT); try { server.register(testObj); client.setAttribute(DOMAIN_NAME, OBJECT_NAME, "unknown-attribute", 1); } finally { server.unregister(testObj); client.close(); } }
@Test public void testIsMethod() throws Exception { IsMethod isMethod = new IsMethod(); JmxClient client = new JmxClient(DEFAULT_PORT); try { server.register(isMethod); assertFalse((Boolean) client.getAttribute(DOMAIN_NAME, OBJECT_NAME, "flag")); client.setAttribute(DOMAIN_NAME, OBJECT_NAME, "flag", true); assertTrue((Boolean) client.getAttribute(DOMAIN_NAME, OBJECT_NAME, "flag")); } finally { server.unregister(isMethod); client.close(); } }
@Test public void testAttributeFieldSets() throws Exception { AttributeField attributeField = new AttributeField(); JmxClient client = new JmxClient(DEFAULT_PORT); try { server.register(attributeField); try { client.setAttribute(DOMAIN_NAME, OBJECT_NAME, "readOnly", 1); fail("Should have thrown"); } catch (AttributeNotFoundException e) { // expected } assertEquals(READ_WRITE_DEFAULT, client.getAttribute(DOMAIN_NAME, OBJECT_NAME, "readWrite")); int val = 530534543; client.setAttribute(DOMAIN_NAME, OBJECT_NAME, "readWrite", val); assertEquals(val, client.getAttribute(DOMAIN_NAME, OBJECT_NAME, "readWrite")); assertEquals(val, attributeField.readWrite); val = 342323423; client.setAttribute(DOMAIN_NAME, OBJECT_NAME, "writeOnly", val); assertEquals(val, attributeField.writeOnly); try { client.setAttribute(DOMAIN_NAME, OBJECT_NAME, "neither", 1); fail("Should have thrown"); } catch (AttributeNotFoundException e) { // expected } try { client.setAttribute(DOMAIN_NAME, OBJECT_NAME, "noAnnotation", 1); fail("Should have thrown"); } catch (AttributeNotFoundException e) { // expected } } finally { server.unregister(attributeField); client.close(); } }