RequiredModelMBeanTest.java 文件源码

java
阅读 18 收藏 0 点赞 0 评论 0

项目:freeVM 作者:
/**
 * Verify that value of attribute return value which return getter method if
 * currencyTimeLimit < 0.
 * <ul>
 * Step by step:
 * <li>1.Create java class, which is not MBean. This class has getter and
 * setter methods.
 * <li>2.Create ModelMBeanAttibuteInfo object for class in 1st step.
 * <li>3.Extract descriptor from ModelMBeanAttibuteInfo class and set
 * additional fields currencyTimeLimit < 0 and setMethod=nameOfSetterMethod.
 * <li>4.Create ModelMBeanInfoSupport object.
 * <li>5.Create RequiredModelMBean object.
 * <li>6.Create notification listener and register it in RequiredModelMBean
 * object for attribute using addAttributeChangeNotificationListener.
 * <li>7.Instance of java class in 1st step sets managed resource for
 * RequiredModelMBean using setManagedResource method.
 * <li>8.Create objectName.
 * <li>9.Register RequiredModelMBean object in MBeanServer with objectName
 * specified in previous step.
 * <li>10.Set attribute on MBeanServer using setAttribute method.
 * <li>11.Verify that notification listener was notified about change
 * attribute.
 * <li>12.Verify that setter method was invoked.
 * <li>13.Verify that getAttribute of MBeanServer returns correct value of
 * attribute and getter method was invoked.
 * <li>14. Invoke again getAttribute of MBeanServer method and verify
 * returned value. Also verify that getter method was invoked.
 * </ul>
 */
public Result testNegative() throws Exception {
    Method getter = class1.getMethod("getG", null);
    Method setter = class1.getMethod("setG", new Class[] { String.class });
    ModelMBeanAttributeInfo attributeInfoForG = new ModelMBeanAttributeInfo(
        "name", "description", getter, setter);
    Descriptor descriptor = attributeInfoForG.getDescriptor();
    descriptor.setField("currencyTimeLimit", "-1");
    descriptor.setField("setMethod", setter.getName());
    descriptor.setField("getMethod", getter.getName());
    attributeInfoForG.setDescriptor(descriptor);
    ModelMBeanAttributeInfo[] attributeInfos = new ModelMBeanAttributeInfo[] { attributeInfoForG };
    ModelMBeanOperationInfo[] operationInfos = new ModelMBeanOperationInfo[] {
        new ModelMBeanOperationInfo("description", setter),
        new ModelMBeanOperationInfo("description", getter) };
    ModelMBeanInfoSupport beanInfoSupport = new ModelMBeanInfoSupport(
        class1.getName(), "description", attributeInfos, null,
        operationInfos, null);
    RequiredModelMBean requiredModelMBean = new RequiredModelMBean(
        beanInfoSupport);
    ManageResourceAndNotification notificationListener = new ManageResourceAndNotification();
    requiredModelMBean.addAttributeChangeNotificationListener(
        notificationListener, attributeInfoForG.getName(),
        ManageResourceAndNotification.handback);
    ManageResourceAndNotification managedResource = new ManageResourceAndNotification();
    requiredModelMBean.setManagedResource(managedResource,
        "ObjectReference");
    ObjectName objectName = new ObjectName("domain", "name", "simple name");
    MBeanServer server = MBeanServerFactory.createMBeanServer();
    server.registerMBean(requiredModelMBean, objectName);
    String newValue = "new value";
    server.setAttribute(objectName, new Attribute(attributeInfoForG
        .getName(), newValue));
    assertTrue(notificationListener.isWasHandleNotificationInvoked());
    assertEquals(managedResource.getG(), newValue);
    managedResource.isGetGWasInvoked();
    assertEquals(server.getAttribute(objectName, attributeInfoForG
        .getName()), newValue);
    assertTrue(managedResource.isGetGWasInvoked());
    assertTrue(server.getMBeanInfo(objectName).getAttributes()[0] == attributeInfoForG);
    assertNull(attributeInfoForG.getDescriptor().getFieldValue("value"));
    return result();
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号