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.util.Arrays;
012: import javax.management.Descriptor;
013: import javax.management.modelmbean.DescriptorSupport;
014: import javax.management.modelmbean.ModelMBeanAttributeInfo;
015: import javax.management.modelmbean.ModelMBeanConstructorInfo;
016: import javax.management.modelmbean.ModelMBeanInfoSupport;
017: import javax.management.modelmbean.ModelMBeanNotificationInfo;
018: import javax.management.modelmbean.ModelMBeanOperationInfo;
019:
020: import junit.framework.TestCase;
021:
022: /**
023: * @version $Revision: 1.7 $
024: * @see
025: */
026:
027: public class ModelMBeanInfoSupportTest extends TestCase {
028:
029: public ModelMBeanInfoSupportTest(String s) {
030: super (s);
031: }
032:
033: public void setUp() throws Exception {
034: super .setUp();
035: }
036:
037: public void tearDown() throws Exception {
038: super .tearDown();
039: }
040:
041: public void testGetDescriptor() throws Exception {
042: ModelMBeanAttributeInfo[] attributes = new ModelMBeanAttributeInfo[2];
043: attributes[0] = new ModelMBeanAttributeInfo("test",
044: "java.lang.String", "A description", true, true, false);
045: attributes[1] = new ModelMBeanAttributeInfo("test2",
046: "java.lang.String", "A description", true, true, false);
047: // testcase for bug #700905
048: ModelMBeanInfoSupport support = new ModelMBeanInfoSupport(
049: "somepackage.someclass", "Test case", attributes, null,
050: null, null);
051: // this worked ok
052: Descriptor descriptor = support.getDescriptor("test",
053: "attribute");
054: assertNotNull(descriptor);
055: assertEquals("test", descriptor.getFieldValue("name"));
056: // this didn't
057: descriptor = support.getDescriptor("test", null);
058: assertNotNull(descriptor);
059: assertEquals("test", descriptor.getFieldValue("name"));
060: }
061:
062: public void testCaseInsensitiveDescriptorType() {
063: DescriptorSupport ds = new DescriptorSupport(new String[] {
064: "name=TestMetadata", "descriptorType=mbEAN",
065: "displayname=Test Metadata" });
066:
067: ModelMBeanInfoSupport info = new ModelMBeanInfoSupport(
068: "TestMetadata", "An empty model mbean info instance",
069: new ModelMBeanAttributeInfo[0],
070: new ModelMBeanConstructorInfo[0],
071: new ModelMBeanOperationInfo[0],
072: new ModelMBeanNotificationInfo[0], ds);
073: }
074:
075: public void testGetMBeanDescriptorDefault() throws Exception {
076: ModelMBeanInfoSupport info = new ModelMBeanInfoSupport(
077: "TestMetadata", "An empty model mbean info instance",
078: new ModelMBeanAttributeInfo[0],
079: new ModelMBeanConstructorInfo[0],
080: new ModelMBeanOperationInfo[0],
081: new ModelMBeanNotificationInfo[0]);
082: Descriptor dd = info.getMBeanDescriptor();
083: assertFalse("default descriptor is null", dd == null);
084: assertTrue("Expecting 7 fields", dd.getFieldNames().length == 7);
085: String[] deffields = { "name", "descriptorType", "displayName",
086: "persistPolicy", "log", "export", "visibility" };
087: String[] fields = dd.getFieldNames();
088: assertTrue("Expected field names not present", Arrays.asList(
089: fields).containsAll(Arrays.asList(deffields)));
090: assertTrue("Unexpected name", ((String) dd
091: .getFieldValue("name")).compareTo("TestMetadata") == 0);
092: assertTrue("Unexpected descriptorType", ((String) dd
093: .getFieldValue("descriptortype"))
094: .compareToIgnoreCase("mbean") == 0);
095: assertTrue("Unexpected displayName: "
096: + dd.getFieldValue("displayname"), ((String) dd
097: .getFieldValue("displayname"))
098: .compareTo("TestMetadata") == 0);
099: assertTrue("Unexpected persistpolicy", ((String) dd
100: .getFieldValue("persistpolicy"))
101: .compareToIgnoreCase("never") == 0);
102: assertTrue("Unexpected log", ((String) dd.getFieldValue("log"))
103: .compareToIgnoreCase("F") == 0);
104: assertTrue("Unexpected export", ((String) dd
105: .getFieldValue("export")).compareTo("F") == 0);
106: assertTrue("Unexpected visibility", ((String) dd
107: .getFieldValue("visibility")).compareTo("1") == 0);
108:
109: info = new ModelMBeanInfoSupport("TestMetadata",
110: "An empty model mbean info instance",
111: new ModelMBeanAttributeInfo[0],
112: new ModelMBeanConstructorInfo[0],
113: new ModelMBeanOperationInfo[0],
114: new ModelMBeanNotificationInfo[0], null);
115: dd = info.getMBeanDescriptor();
116: assertFalse("default descriptor is null", dd == null);
117: assertTrue("Expecting 7 fields", dd.getFieldNames().length == 7);
118: assertTrue("Expected field names not present", Arrays.asList(
119: fields).containsAll(Arrays.asList(deffields)));
120: assertTrue("Unexpected name", ((String) dd
121: .getFieldValue("name")).compareTo("TestMetadata") == 0);
122: assertTrue("Unexpected descriptorType", ((String) dd
123: .getFieldValue("descriptortype"))
124: .compareToIgnoreCase("mbean") == 0);
125: assertTrue("Unexpected displayName: "
126: + dd.getFieldValue("displayname"), ((String) dd
127: .getFieldValue("displayname"))
128: .compareTo("TestMetadata") == 0);
129: assertTrue("Unexpected persistpolicy", ((String) dd
130: .getFieldValue("persistpolicy"))
131: .compareToIgnoreCase("never") == 0);
132: assertTrue("Unexpected log", ((String) dd.getFieldValue("log"))
133: .compareToIgnoreCase("F") == 0);
134: assertTrue("Unexpected export", ((String) dd
135: .getFieldValue("export")).compareTo("F") == 0);
136: assertTrue("Unexpected visibility", ((String) dd
137: .getFieldValue("visibility")).compareTo("1") == 0);
138:
139: }
140:
141: public void testGetMBeanDescriptorAdditionalValues()
142: throws Exception {
143: String[] fields = { "name", "descriptortype", "displayname",
144: "targetObject", "export", "defaultValue" };
145: Object[] values = { "TestMetadata", "MBEAN",
146: "JMX Metadata for testing purposes", new Double(0),
147: "test metadata", new Double(99.999) };
148: DescriptorSupport ds = new DescriptorSupport(fields, values);
149: ModelMBeanInfoSupport info = new ModelMBeanInfoSupport(
150: "TestMetadata", "An empty model mbean info instance",
151: new ModelMBeanAttributeInfo[0],
152: new ModelMBeanConstructorInfo[0],
153: new ModelMBeanOperationInfo[0],
154: new ModelMBeanNotificationInfo[0], ds);
155: Descriptor d = info.getMBeanDescriptor();
156: assertFalse("descriptor is null", d == null);
157: assertTrue("Expecting 9 fields", d.getFieldNames().length == 9);
158: String[] deffields = { "name", "descriptortype", "displayname",
159: "persistpolicy", "log", "export", "visibility" };
160: String[] infofields = d.getFieldNames();
161: assertTrue("Expected field names not present", Arrays.asList(
162: infofields).containsAll(Arrays.asList(deffields)));
163: assertTrue(
164: "Unexpected name",
165: ((String) d.getFieldValue("name"))
166: .compareTo((String) ds.getFieldValue("name")) == 0);
167: assertTrue("Unexpected descriptorType", ((String) d
168: .getFieldValue("descriptortype"))
169: .compareToIgnoreCase((String) ds
170: .getFieldValue("descriptortype")) == 0);
171: assertTrue("Unexpected displayName: "
172: + d.getFieldValue("displayname"), ((String) d
173: .getFieldValue("displayname")).compareTo((String) ds
174: .getFieldValue("displayname")) == 0);
175: assertTrue("Unexpected targetObject: "
176: + d.getFieldValue("targetObject"), ((Double) d
177: .getFieldValue("targetObject")).equals(new Double(0.0)));
178: assertTrue("Unexpected persistpolicy", ((String) d
179: .getFieldValue("persistpolicy"))
180: .compareToIgnoreCase("never") == 0);
181: assertTrue("Unexpected log", ((String) d.getFieldValue("log"))
182: .compareToIgnoreCase("F") == 0);
183: assertTrue("Unexpected export", ((String) d
184: .getFieldValue("export")).compareTo((String) ds
185: .getFieldValue("export")) == 0);
186: assertTrue("Unexpected defaultValue", ((Double) d
187: .getFieldValue("defaultValue")).equals(new Double(
188: 99.999)));
189: assertTrue("Unexpected visibility", ((String) d
190: .getFieldValue("visibility")).compareTo("1") == 0);
191: }
192:
193: public void testGetMBeanDescriptorCustom() throws Exception {
194: String[] fields = { "name", "descriptortype", "displayname",
195: "persistpolicy", "log", "export", "visibility" };
196: Object[] values = { "TestMetadata", "MBEAN",
197: "JMX Metadata for testing purposes", "onUPDATE", "T",
198: "test metadata", "4" };
199: DescriptorSupport ds = new DescriptorSupport(fields, values);
200: ModelMBeanInfoSupport info = new ModelMBeanInfoSupport(
201: "TestMetadata", "An empty model mbean info instance",
202: new ModelMBeanAttributeInfo[0],
203: new ModelMBeanConstructorInfo[0],
204: new ModelMBeanOperationInfo[0],
205: new ModelMBeanNotificationInfo[0], ds);
206: Descriptor d = info.getMBeanDescriptor();
207: assertFalse("descriptor is null", d == null);
208: assertTrue("Expecting 7 fields", d.getFieldNames().length == 7);
209: String[] deffields = { "name", "descriptortype", "displayname",
210: "persistpolicy", "log", "export", "visibility" };
211: String[] infofields = d.getFieldNames();
212: assertTrue("Expected field names not present", Arrays.asList(
213: infofields).containsAll(Arrays.asList(deffields)));
214: assertTrue(
215: "Unexpected name",
216: ((String) d.getFieldValue("name"))
217: .compareTo((String) ds.getFieldValue("name")) == 0);
218: assertTrue("Unexpected descriptorType", ((String) d
219: .getFieldValue("descriptortype"))
220: .compareToIgnoreCase((String) ds
221: .getFieldValue("descriptortype")) == 0);
222: assertTrue("Unexpected displayName: "
223: + d.getFieldValue("displayname"), ((String) d
224: .getFieldValue("displayname")).compareTo((String) ds
225: .getFieldValue("displayname")) == 0);
226: assertTrue("Unexpected persistpolicy", ((String) d
227: .getFieldValue("persistpolicy"))
228: .compareToIgnoreCase(((String) d
229: .getFieldValue("persistpolicy"))) == 0);
230: assertTrue("Unexpected log",
231: ((String) d.getFieldValue("log"))
232: .compareToIgnoreCase(((String) d
233: .getFieldValue("log"))) == 0);
234: assertTrue("Unexpected export", ((String) d
235: .getFieldValue("export")).compareTo((String) ds
236: .getFieldValue("export")) == 0);
237: assertTrue("Unexpected visibility", ((String) d
238: .getFieldValue("visibility")).compareTo(((String) d
239: .getFieldValue("visibility"))) == 0);
240: }
241: }
|