001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package test.compliance.metadata;
023:
024: import java.io.ByteArrayInputStream;
025: import java.io.ByteArrayOutputStream;
026: import java.io.ObjectInputStream;
027: import java.io.ObjectOutputStream;
028: import java.util.Arrays;
029:
030: import javax.management.MBeanAttributeInfo;
031: import javax.management.MBeanConstructorInfo;
032: import javax.management.MBeanInfo;
033: import javax.management.MBeanNotificationInfo;
034: import javax.management.MBeanOperationInfo;
035: import javax.management.MBeanParameterInfo;
036:
037: import junit.framework.TestCase;
038:
039: /**
040: * MBean Info tests.<p>
041: *
042: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
043: */
044: public class MBeanInfoTEST extends TestCase {
045: // Static --------------------------------------------------------------------
046:
047: // Attributes ----------------------------------------------------------------
048:
049: // Constructor ---------------------------------------------------------------
050:
051: /**
052: * Construct the test
053: */
054: public MBeanInfoTEST(String s) {
055: super (s);
056: }
057:
058: // Tests ---------------------------------------------------------------------
059:
060: public void testMBeanInfo() throws Exception {
061: MBeanInfo info = new MBeanInfo("name", "description", null,
062: null, null, null);
063: assertEquals("name", info.getClassName());
064: assertEquals("description", info.getDescription());
065: assertEquals(0, info.getAttributes().length);
066: assertEquals(0, info.getConstructors().length);
067: assertEquals(0, info.getNotifications().length);
068: assertEquals(0, info.getOperations().length);
069:
070: info = new MBeanInfo("name", "description",
071: new MBeanAttributeInfo[0], new MBeanConstructorInfo[0],
072: new MBeanOperationInfo[0], new MBeanNotificationInfo[0]);
073: assertEquals("name", info.getClassName());
074: assertEquals("description", info.getDescription());
075: assertEquals(0, info.getAttributes().length);
076: assertEquals(0, info.getConstructors().length);
077: assertEquals(0, info.getNotifications().length);
078: assertEquals(0, info.getOperations().length);
079:
080: MBeanParameterInfo[] parms = new MBeanParameterInfo[] { new MBeanParameterInfo(
081: "name", "type", "description") };
082:
083: MBeanAttributeInfo[] attributes = new MBeanAttributeInfo[] { new MBeanAttributeInfo(
084: "name", "type", "description", true, true, false) };
085: MBeanConstructorInfo[] constructors = new MBeanConstructorInfo[] { new MBeanConstructorInfo(
086: "name", "description", parms) };
087: MBeanOperationInfo[] operations = new MBeanOperationInfo[] { new MBeanOperationInfo(
088: "name", "description", parms, "type",
089: MBeanOperationInfo.ACTION_INFO) };
090: MBeanNotificationInfo[] notifications = new MBeanNotificationInfo[] { new MBeanNotificationInfo(
091: new String[] { "type1", "type" }, "name", "description") };
092: info = new MBeanInfo("name", "description", attributes,
093: constructors, operations, notifications);
094: assertEquals("name", info.getClassName());
095: assertEquals("description", info.getDescription());
096: assertEquals(1, info.getAttributes().length);
097: assertEquals(1, info.getConstructors().length);
098: assertEquals(1, info.getNotifications().length);
099: assertEquals(1, info.getOperations().length);
100: }
101:
102: public void testEquals() throws Exception {
103: MBeanParameterInfo[] parms = new MBeanParameterInfo[] { new MBeanParameterInfo(
104: "name", "type", "description") };
105:
106: MBeanAttributeInfo[] attributes = new MBeanAttributeInfo[] { new MBeanAttributeInfo(
107: "name", "type", "description", true, true, false) };
108: MBeanConstructorInfo[] constructors = new MBeanConstructorInfo[] { new MBeanConstructorInfo(
109: "name", "description", parms) };
110: MBeanOperationInfo[] operations = new MBeanOperationInfo[] { new MBeanOperationInfo(
111: "name", "description", parms, "type",
112: MBeanOperationInfo.ACTION_INFO) };
113: MBeanNotificationInfo[] notifications = new MBeanNotificationInfo[] { new MBeanNotificationInfo(
114: new String[] { "type1", "type" }, "name", "description") };
115: MBeanInfo info = new MBeanInfo("name", "description",
116: attributes, constructors, operations, notifications);
117:
118: assertTrue("Null is not equal to any instance", info
119: .equals(null) == false);
120: assertTrue("Instance is only equal another MBeanInfo instance",
121: info.equals(new Object()) == false);
122: assertTrue("Instance should equal itself", info.equals(info));
123:
124: MBeanInfo info2 = new MBeanInfo("name", "description",
125: attributes, constructors, operations, notifications);
126: assertTrue("Instances with same values should be equal", info
127: .equals(info2));
128: assertTrue("Instances with same values should be equal", info2
129: .equals(info));
130:
131: info2 = new MBeanInfo("name2", "description", attributes,
132: constructors, operations, notifications);
133: assertTrue(
134: "Instances with different class names are not equal",
135: info.equals(info2) == false);
136: assertTrue(
137: "Instances with different class names are not equal",
138: info2.equals(info) == false);
139:
140: info2 = new MBeanInfo("name", "description2", attributes,
141: constructors, operations, notifications);
142: assertTrue(
143: "Instances with different descriptions are not equal",
144: info.equals(info2) == false);
145: assertTrue(
146: "Instances with different descriptions are not equal",
147: info2.equals(info) == false);
148:
149: MBeanAttributeInfo[] attributes2 = new MBeanAttributeInfo[] { new MBeanAttributeInfo(
150: "name2", "type", "description", true, true, false) };
151:
152: info2 = new MBeanInfo("name", "description", attributes2,
153: constructors, operations, notifications);
154: assertTrue("Instances with different attributes are not equal",
155: info.equals(info2) == false);
156: assertTrue("Instances with different attributes are not equal",
157: info2.equals(info) == false);
158:
159: attributes2 = new MBeanAttributeInfo[] {
160: new MBeanAttributeInfo("name2", "type", "description",
161: true, true, false),
162: new MBeanAttributeInfo("name3", "type", "description",
163: true, true, false) };
164:
165: info2 = new MBeanInfo("name", "description", attributes2,
166: constructors, operations, notifications);
167: assertTrue(
168: "Instances with different numbers of attributes are not equal",
169: info.equals(info2) == false);
170: assertTrue(
171: "Instances with different numbers of attributes are not equal",
172: info2.equals(info) == false);
173:
174: info2 = new MBeanInfo("name", "description", null,
175: constructors, operations, notifications);
176: assertTrue(
177: "Instances with and without attributes are not equal",
178: info.equals(info2) == false);
179: assertTrue(
180: "Instances with and without attributes are not equal",
181: info2.equals(info) == false);
182:
183: MBeanConstructorInfo[] constructors2 = new MBeanConstructorInfo[] { new MBeanConstructorInfo(
184: "name2", "description", parms) };
185:
186: info2 = new MBeanInfo("name", "description", attributes,
187: constructors2, operations, notifications);
188: assertTrue(
189: "Instances with different constructors are not equal",
190: info.equals(info2) == false);
191: assertTrue(
192: "Instances with different constructors are not equal",
193: info2.equals(info) == false);
194:
195: constructors2 = new MBeanConstructorInfo[] {
196: new MBeanConstructorInfo("name2", "description", parms),
197: new MBeanConstructorInfo("name3", "description", parms) };
198:
199: info2 = new MBeanInfo("name", "description", attributes,
200: constructors2, operations, notifications);
201: assertTrue(
202: "Instances with different numbers of constructors are not equal",
203: info.equals(info2) == false);
204: assertTrue(
205: "Instances with different numbers of constructors are not equal",
206: info2.equals(info) == false);
207:
208: info2 = new MBeanInfo("name", "description", attributes, null,
209: operations, notifications);
210: assertTrue(
211: "Instances with and without constructors are not equal",
212: info.equals(info2) == false);
213: assertTrue(
214: "Instances with and without constructors are not equal",
215: info2.equals(info) == false);
216:
217: MBeanOperationInfo[] operations2 = new MBeanOperationInfo[] { new MBeanOperationInfo(
218: "name2", "description", parms, "type",
219: MBeanOperationInfo.ACTION_INFO) };
220:
221: info2 = new MBeanInfo("name", "description", attributes,
222: constructors, operations2, notifications);
223: assertTrue("Instances with different operations are not equal",
224: info.equals(info2) == false);
225: assertTrue("Instances with different operations are not equal",
226: info2.equals(info) == false);
227:
228: operations2 = new MBeanOperationInfo[] {
229: new MBeanOperationInfo("name2", "description", parms,
230: "type", MBeanOperationInfo.ACTION_INFO),
231: new MBeanOperationInfo("name3", "description", parms,
232: "type", MBeanOperationInfo.ACTION_INFO) };
233:
234: info2 = new MBeanInfo("name", "description", attributes,
235: constructors, operations2, notifications);
236: assertTrue(
237: "Instances with different numbers of operations are not equal",
238: info.equals(info2) == false);
239: assertTrue(
240: "Instances with different numbers of operations are not equal",
241: info2.equals(info) == false);
242:
243: info2 = new MBeanInfo("name", "description", attributes,
244: constructors, null, notifications);
245: assertTrue(
246: "Instances with and without operations are not equal",
247: info.equals(info2) == false);
248: assertTrue(
249: "Instances with and without operations are not equal",
250: info2.equals(info) == false);
251:
252: MBeanNotificationInfo[] notifications2 = new MBeanNotificationInfo[] { new MBeanNotificationInfo(
253: new String[] { "type", "type" }, "name2", "description") };
254:
255: info2 = new MBeanInfo("name", "description", attributes,
256: constructors, operations, notifications2);
257: assertTrue(
258: "Instances with different notifications are not equal",
259: info.equals(info2) == false);
260: assertTrue(
261: "Instances with different notifications are not equal",
262: info2.equals(info) == false);
263:
264: notifications2 = new MBeanNotificationInfo[] {
265: new MBeanNotificationInfo(
266: new String[] { "type", "type" }, "name2",
267: "description"),
268: new MBeanNotificationInfo(
269: new String[] { "type", "type" }, "name3",
270: "description") };
271:
272: info2 = new MBeanInfo("name", "description", attributes,
273: constructors, operations, notifications2);
274: assertTrue(
275: "Instances with different numbers of notifications are not equal",
276: info.equals(info2) == false);
277: assertTrue(
278: "Instances with different numbers of notifications are not equal",
279: info2.equals(info) == false);
280:
281: info2 = new MBeanInfo("name", "description", attributes,
282: constructors, operations, null);
283: assertTrue(
284: "Instances with and without notifications are not equal",
285: info.equals(info2) == false);
286: assertTrue(
287: "Instances with and without notifications are not equal",
288: info2.equals(info) == false);
289: }
290:
291: public void testHashCode() throws Exception {
292: MBeanParameterInfo[] parms = new MBeanParameterInfo[] { new MBeanParameterInfo(
293: "name", "type", "description") };
294:
295: MBeanAttributeInfo[] attributes = new MBeanAttributeInfo[] { new MBeanAttributeInfo(
296: "name", "type", "description", true, true, false) };
297: MBeanConstructorInfo[] constructors = new MBeanConstructorInfo[] { new MBeanConstructorInfo(
298: "name", "description", parms) };
299: MBeanOperationInfo[] operations = new MBeanOperationInfo[] { new MBeanOperationInfo(
300: "name", "description", parms, "type",
301: MBeanOperationInfo.ACTION_INFO) };
302: MBeanNotificationInfo[] notifications = new MBeanNotificationInfo[] { new MBeanNotificationInfo(
303: new String[] { "type1", "type" }, "name", "description") };
304: MBeanInfo info1 = new MBeanInfo("name", "description",
305: attributes, constructors, operations, notifications);
306: MBeanInfo info2 = new MBeanInfo("name", "description",
307: attributes, constructors, operations, notifications);
308:
309: assertTrue(
310: "Different instances with the same hashcode are equal",
311: info1.hashCode() == info2.hashCode());
312: }
313:
314: public void testSerialization() throws Exception {
315: MBeanParameterInfo[] parms = new MBeanParameterInfo[] { new MBeanParameterInfo(
316: "name", "type", "description") };
317:
318: MBeanAttributeInfo[] attributes = new MBeanAttributeInfo[] { new MBeanAttributeInfo(
319: "name", "type", "description", true, true, false) };
320: MBeanConstructorInfo[] constructors = new MBeanConstructorInfo[] { new MBeanConstructorInfo(
321: "name", "description", parms) };
322: MBeanOperationInfo[] operations = new MBeanOperationInfo[] { new MBeanOperationInfo(
323: "name", "description", parms, "type",
324: MBeanOperationInfo.ACTION_INFO) };
325: MBeanNotificationInfo[] notifications = new MBeanNotificationInfo[] { new MBeanNotificationInfo(
326: new String[] { "type1", "type" }, "name", "description") };
327: MBeanInfo info = new MBeanInfo("name", "description",
328: attributes, constructors, operations, notifications);
329: // Serialize it
330: ByteArrayOutputStream baos = new ByteArrayOutputStream();
331: ObjectOutputStream oos = new ObjectOutputStream(baos);
332: oos.writeObject(info);
333:
334: // Deserialize it
335: ByteArrayInputStream bais = new ByteArrayInputStream(baos
336: .toByteArray());
337: ObjectInputStream ois = new ObjectInputStream(bais);
338: MBeanInfo result = (MBeanInfo) ois.readObject();
339:
340: assertEquals(info.getClassName(), result.getClassName());
341: assertEquals(Arrays.asList(info.getAttributes()), Arrays
342: .asList(result.getAttributes()));
343: assertEquals(Arrays.asList(info.getConstructors()), Arrays
344: .asList(result.getConstructors()));
345: assertEquals(Arrays.asList(info.getOperations()), Arrays
346: .asList(result.getOperations()));
347:
348: // UGLY!
349: MBeanNotificationInfo origNotification = info
350: .getNotifications()[0];
351: MBeanNotificationInfo resultNotification = result
352: .getNotifications()[0];
353: assertEquals(origNotification.getName(), resultNotification
354: .getName());
355: assertEquals(origNotification.getDescription(),
356: resultNotification.getDescription());
357: assertEquals(Arrays.asList(origNotification.getNotifTypes()),
358: Arrays.asList(resultNotification.getNotifTypes()));
359: }
360: }
|