/** * Switches the {@link Thread#getContextClassLoader() context ClassLoader} for the * managed resources {@link ClassLoader} before allowing the invocation to occur. * @see javax.management.modelmbean.ModelMBean#getAttributes */ @Override public AttributeList getAttributes(String[] attrNames) { ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(this.managedResourceClassLoader); return super.getAttributes(attrNames); } finally { Thread.currentThread().setContextClassLoader(currentClassLoader); } }
/** * Create an instance of a class that implements {@code ModelMBean}. * <p>This method is called to obtain a {@code ModelMBean} instance to * use when registering a bean. This method is called once per bean during the * registration phase and must return a new instance of {@code ModelMBean} * @return a new instance of a class that implements {@code ModelMBean} * @throws javax.management.MBeanException if creation of the ModelMBean failed */ protected ModelMBean createModelMBean() throws MBeanException { return (this.exposeManagedResourceClassLoader ? new SpringModelMBean() : new RequiredModelMBean()); }
@Test public void testMetricDescription() throws Exception { ModelMBeanInfo inf = getMBeanInfoFromAssembler(); ModelMBeanAttributeInfo metric = inf.getAttribute(QUEUE_SIZE_METRIC); ModelMBeanOperationInfo operation = inf.getOperation("getQueueSize"); assertEquals("The description for the queue size metric is incorrect", "The QueueSize metric", metric.getDescription()); assertEquals("The description for the getter operation of the queue size metric is incorrect", "The QueueSize metric", operation.getDescription()); }
@Test public void testGetAgeIsReadOnly() throws Exception { ModelMBeanInfo info = getMBeanInfoFromAssembler(); ModelMBeanAttributeInfo attr = info.getAttribute(AGE_ATTRIBUTE); assertTrue("Age is not readable", attr.isReadable()); assertFalse("Age is not writable", attr.isWritable()); }
@Test public void testAttributeDescriptor() throws Exception { ModelMBeanInfo info = getMBeanInfoFromAssembler(); Descriptor desc = info.getAttribute(NAME_ATTRIBUTE).getDescriptor(); assertEquals("Default value should be foo", "foo", desc.getFieldValue("default")); assertEquals("Currency Time Limit should be 20", "20", desc.getFieldValue("currencyTimeLimit")); assertEquals("Persist Policy should be OnUpdate", "OnUpdate", desc.getFieldValue("persistPolicy")); assertEquals("Persist Period should be 300", "300", desc.getFieldValue("persistPeriod")); }
/** * Tests the situation where the attribute is only defined on the getter. */ @Test public void testReadOnlyAttribute() throws Exception { ModelMBeanInfo inf = getMBeanInfoFromAssembler(); ModelMBeanAttributeInfo attr = inf.getAttribute(AGE_ATTRIBUTE); assertFalse("The age attribute should not be writable", attr.isWritable()); }
@Test public void testOperationDescriptor() throws Exception { ModelMBeanInfo info = getMBeanInfoFromAssembler(); Descriptor desc = info.getOperation("myOperation").getDescriptor(); assertEquals("Currency Time Limit should be 30", "30", desc.getFieldValue("currencyTimeLimit")); assertEquals("Role should be \"operation\"", "operation", desc.getFieldValue("role")); }
@Test public void testAttributeDescriptionOnGetter() throws Exception { ModelMBeanInfo inf = getMBeanInfoFromAssembler(); ModelMBeanAttributeInfo attr = inf.getAttribute(NAME_ATTRIBUTE); assertEquals("The description for the name attribute is incorrect", "The Name Attribute", attr.getDescription()); }
@Test public void testGetAgeIsReadOnly() throws Exception { ModelMBeanInfo info = getMBeanInfoFromAssembler(); ModelMBeanAttributeInfo attr = info.getAttribute(AGE_ATTRIBUTE); assertTrue("Age is not readable", attr.isReadable()); assertTrue("Age is not writable", attr.isWritable()); }
@Test public void testMetricDescriptorDefaults() throws Exception { ModelMBeanInfo info = getMBeanInfoFromAssembler(); Descriptor desc = info.getAttribute(CACHE_ENTRIES_METRIC).getDescriptor(); assertNull("Currency Time Limit should not be populated", desc.getFieldValue("currencyTimeLimit")); assertNull("Persist Policy should not be populated", desc.getFieldValue("persistPolicy")); assertNull("Persist Period should not be populated", desc.getFieldValue("persistPeriod")); assertNull("Unit should not be populated", desc.getFieldValue("units")); assertEquals("Display Name should be populated by default via JMX", CACHE_ENTRIES_METRIC,desc.getFieldValue("displayName")); assertEquals("Metric Type should be GAUGE", "GAUGE",desc.getFieldValue("metricType")); assertNull("Metric Category should not be populated", desc.getFieldValue("metricCategory")); }
@Test public void testAttributeDescriptionOnSetter() throws Exception { ModelMBeanInfo inf = getMBeanInfoFromAssembler(); ModelMBeanAttributeInfo attr = inf.getAttribute(AGE_ATTRIBUTE); assertEquals("The description for the age attribute is incorrect", "The Age Attribute", attr.getDescription()); }
@Test public void testGetAgeIsReadOnly() throws Exception { ModelMBeanInfo info = getMBeanInfoFromAssembler(); ModelMBeanAttributeInfo attr = info.getAttribute(AGE_ATTRIBUTE); assertTrue(attr.isReadable()); assertFalse(attr.isWritable()); }
@Test public void testMetricDescriptor() throws Exception { ModelMBeanInfo info = getMBeanInfoFromAssembler(); Descriptor desc = info.getAttribute(QUEUE_SIZE_METRIC).getDescriptor(); assertEquals("Currency Time Limit should be 20", "20", desc.getFieldValue("currencyTimeLimit")); assertEquals("Persist Policy should be OnUpdate", "OnUpdate", desc.getFieldValue("persistPolicy")); assertEquals("Persist Period should be 300", "300", desc.getFieldValue("persistPeriod")); assertEquals("Unit should be messages", "messages",desc.getFieldValue("units")); assertEquals("Display Name should be Queue Size", "Queue Size",desc.getFieldValue("displayName")); assertEquals("Metric Type should be COUNTER", "COUNTER",desc.getFieldValue("metricType")); assertEquals("Metric Category should be utilization", "utilization",desc.getFieldValue("metricCategory")); }
@Test public void testAttributeFromInterface() throws Exception { ModelMBeanInfo inf = getMBeanInfoFromAssembler(); ModelMBeanAttributeInfo attr = inf.getAttribute("Colour"); assertTrue("The name attribute should be writable", attr.isWritable()); assertTrue("The name attribute should be readable", attr.isReadable()); }
@Test public void testReadWriteAttribute() throws Exception { ModelMBeanInfo inf = getMBeanInfoFromAssembler(); ModelMBeanAttributeInfo attr = inf.getAttribute(NAME_ATTRIBUTE); assertTrue("The name attribute should be writable", attr.isWritable()); assertTrue("The name attribute should be readable", attr.isReadable()); }
@Test public void testGetAgeIsReadOnly() throws Exception { ModelMBeanInfo info = getMBeanInfoFromAssembler(); ModelMBeanAttributeInfo attr = info.getAttribute(AGE_ATTRIBUTE); assertTrue("Age is not readable", attr.isReadable()); assertFalse("Age is not writable", attr.isWritable()); }
@Test public void testGetAgeIsReadOnly() throws Exception { ModelMBeanInfo info = getMBeanInfoFromAssembler(); ModelMBeanAttributeInfo attr = info.getAttribute(AGE_ATTRIBUTE); assertTrue(attr.isReadable()); assertFalse(attr.isWritable()); }
@Test public void testGetAgeIsReadOnly() throws Exception { ModelMBeanInfo info = getMBeanInfoFromAssembler(); ModelMBeanAttributeInfo attr = info.getAttribute(AGE_ATTRIBUTE); assertTrue("Age is not readable", attr.isReadable()); assertFalse("Age is not writable", attr.isWritable()); }
@Test public void testGetAgeIsReadOnly() throws Exception { ModelMBeanInfo info = getMBeanInfoFromAssembler(); ModelMBeanAttributeInfo attr = info.getAttribute(AGE_ATTRIBUTE); assertTrue("Age is not readable", attr.isReadable()); assertFalse("Age is not writable", attr.isWritable()); }
@Test public void testSupermanIsReadOnly() throws Exception { ModelMBeanInfo info = getMBeanInfoFromAssembler(); ModelMBeanAttributeInfo attr = info.getAttribute("Superman"); assertTrue(attr.isReadable()); assertFalse(attr.isWritable()); }