001: /*
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */
008:
009: package test.javax.management.modelmbean;
010:
011: import java.lang.reflect.Method;
012: import java.util.Arrays;
013: import java.util.List;
014: import javax.management.Descriptor;
015: import javax.management.IntrospectionException;
016: import javax.management.modelmbean.DescriptorSupport;
017: import javax.management.modelmbean.ModelMBeanAttributeInfo;
018:
019: import test.MX4JTestCase;
020:
021: /**
022: * Test case of ModelMBeanAttributeInfo. It will try to verify an appropriate
023: * behaviour in particular with respect to the descriptor values
024: *
025: * @version $Revision: 1.7 $
026: * @see
027: */
028:
029: public class ModelMBeanAttributeInfoTest extends MX4JTestCase {
030: public static class BogusNIC {
031: public String getMAC() {
032: return null;
033: }
034:
035: public void setMAC(int mac) {
036: }
037: }
038:
039: public ModelMBeanAttributeInfoTest(String s) {
040: super (s);
041: }
042:
043: public void setUp() throws Exception {
044: super .setUp();
045: }
046:
047: public void tearDown() throws Exception {
048: super .tearDown();
049: }
050:
051: public void testValidDescriptorFields() throws Exception {
052: // testcase for bug #794320
053: // Test that only name and descriptorType are mandatory
054: Descriptor descriptor = new DescriptorSupport(new String[] {
055: "name", "descriptortype", "default" }, new String[] {
056: "attribute1", "attribute", "default" });
057: ModelMBeanAttributeInfo attribute = new ModelMBeanAttributeInfo(
058: "attribute1", "java.lang.String", "An attribute", true,
059: true, false, descriptor);
060: // in case of bug #794320 the descriptor is overrided
061: assertEquals(
062: attribute.getDescriptor().getFieldValue("default"),
063: "default");
064: assertNull(attribute.getDescriptor().getFieldValue("value"));
065: }
066:
067: public void testBadCtor() throws Exception {
068: try {
069: Method macgetter = BogusNIC.class.getMethod("getMAC",
070: new Class[0]);
071: Method macsetter = BogusNIC.class.getMethod("setMAC",
072: new Class[] { int.class });
073: ModelMBeanAttributeInfo attrinfo = new ModelMBeanAttributeInfo(
074: "MAC", "MAC Address", macgetter, macsetter);
075: fail("Expecting an IntrospectionException");
076: } catch (IntrospectionException x) {
077: assertTrue(true); // success;
078: }
079: }
080:
081: public void testValuelessAttribute() throws Exception {
082: ModelMBeanAttributeInfo attrinfo = new ModelMBeanAttributeInfo(
083: "SerialNo", "NIC Card's serial number", null, null);
084: attrinfo = new ModelMBeanAttributeInfo("SerialNo", "String",
085: "NIC Card's serial number", false, false, false);
086: }
087:
088: public void testCaseInsenstiveDescriptorType() {
089: DescriptorSupport ds = new DescriptorSupport(new String[] {
090: "name=PreferredWine", "descriptorType=Attribute",
091: "value=Amarone", "default=Red" });
092: ModelMBeanAttributeInfo attrinfo = new ModelMBeanAttributeInfo(
093: "PreferredWine", "String", "Wine of choice", true,
094: false, false, ds);
095: }
096:
097: public void testGetDescriptor() {
098: DescriptorSupport defds = new DescriptorSupport(new String[] {
099: "name=PreferredWine", "descriptorType=Attribute",
100: "displayName=PreferredWine" });
101: DescriptorSupport ds = new DescriptorSupport(new String[] {
102: "name=PreferredWine", "descriptorType=Attribute",
103: "value=Amarone", "displayName=PreferredWine",
104: "default=Red" });
105:
106: ModelMBeanAttributeInfo attrinfo = new ModelMBeanAttributeInfo(
107: "PreferredWine", "String", "Wine of choice", true,
108: false, false);
109: Descriptor d = attrinfo.getDescriptor();
110: assertTrue("Expecting default descriptor", descriptorsEqual(d,
111: defds));
112:
113: attrinfo = new ModelMBeanAttributeInfo("PreferredWine",
114: "String", "Wine of choice", true, false, false, ds);
115: d = attrinfo.getDescriptor();
116: assertTrue("Expecting copy of ds", descriptorsEqual(d, ds));
117: }
118:
119: private boolean descriptorsEqual(Descriptor done, Descriptor dtwo) {
120: List cifields = Arrays.asList(new String[] { "descriptortype",
121: "persistpolicy", "log" });
122: String[] fields = done.getFieldNames();
123: boolean result = done.getFields().length == dtwo.getFields().length;
124: for (int i = 0; i < fields.length && result == true; i++) {
125: String field = fields[i];
126: Object vone = done.getFieldValue(field);
127: Object vtwo = done.getFieldValue(field);
128: if (vtwo == null) {
129: result = false;
130: } else if (cifields.contains(field)) {
131: if (!(vone instanceof String)
132: || !(vtwo instanceof String)) {
133: result = false;
134: } else {
135: result = ((String) vone)
136: .compareToIgnoreCase((String) vtwo) == 0;
137: }
138: } else {
139: vone.equals(vtwo);
140: }
141: }
142: return result;
143: }
144: }
|