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.openmbean;
023:
024: import junit.framework.TestCase;
025:
026: import java.io.ByteArrayInputStream;
027: import java.io.ByteArrayOutputStream;
028: import java.io.ObjectInputStream;
029: import java.io.ObjectOutputStream;
030: import java.util.Arrays;
031: import java.util.HashSet;
032: import java.util.Set;
033:
034: import javax.management.MBeanParameterInfo;
035: import javax.management.MBeanNotificationInfo;
036: import javax.management.openmbean.OpenMBeanAttributeInfo;
037: import javax.management.openmbean.OpenMBeanAttributeInfoSupport;
038: import javax.management.openmbean.OpenMBeanConstructorInfo;
039: import javax.management.openmbean.OpenMBeanConstructorInfoSupport;
040: import javax.management.openmbean.OpenMBeanInfoSupport;
041: import javax.management.openmbean.OpenMBeanParameterInfo;
042: import javax.management.openmbean.OpenMBeanParameterInfoSupport;
043: import javax.management.openmbean.OpenMBeanOperationInfo;
044: import javax.management.openmbean.OpenMBeanOperationInfoSupport;
045: import javax.management.openmbean.OpenType;
046: import javax.management.openmbean.SimpleType;
047:
048: /**
049: * Open MBean Info tests.<p>
050: *
051: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
052: */
053: public class OpenMBeanInfoSupportTestCase extends TestCase {
054: // Static --------------------------------------------------------------------
055:
056: // Attributes ----------------------------------------------------------------
057:
058: // Constructor ---------------------------------------------------------------
059:
060: /**
061: * Construct the test
062: */
063: public OpenMBeanInfoSupportTestCase(String s) {
064: super (s);
065: }
066:
067: // Tests ---------------------------------------------------------------------
068:
069: public void testOpenMBeanInfoSupport() throws Exception {
070: OpenMBeanInfoSupport info = new OpenMBeanInfoSupport("name",
071: "description", null, null, null, null);
072: assertEquals("name", info.getClassName());
073: assertEquals("description", info.getDescription());
074: assertEquals(0, info.getAttributes().length);
075: assertEquals(0, info.getConstructors().length);
076: assertEquals(0, info.getNotifications().length);
077: assertEquals(0, info.getOperations().length);
078:
079: info = new OpenMBeanInfoSupport("name", "description",
080: new OpenMBeanAttributeInfoSupport[0],
081: new OpenMBeanConstructorInfoSupport[0],
082: new OpenMBeanOperationInfoSupport[0],
083: new MBeanNotificationInfo[0]);
084: assertEquals("name", info.getClassName());
085: assertEquals("description", info.getDescription());
086: assertEquals(0, info.getAttributes().length);
087: assertEquals(0, info.getConstructors().length);
088: assertEquals(0, info.getNotifications().length);
089: assertEquals(0, info.getOperations().length);
090:
091: OpenMBeanParameterInfoSupport[] parms = new OpenMBeanParameterInfoSupport[] { new OpenMBeanParameterInfoSupport(
092: "name", "description", SimpleType.STRING) };
093:
094: OpenMBeanAttributeInfoSupport[] attributes = new OpenMBeanAttributeInfoSupport[] { new OpenMBeanAttributeInfoSupport(
095: "name", "description", SimpleType.STRING, true, true,
096: false) };
097: OpenMBeanConstructorInfoSupport[] constructors = new OpenMBeanConstructorInfoSupport[] { new OpenMBeanConstructorInfoSupport(
098: "name", "description", parms) };
099: OpenMBeanOperationInfoSupport[] operations = new OpenMBeanOperationInfoSupport[] { new OpenMBeanOperationInfoSupport(
100: "name", "description", parms, SimpleType.STRING,
101: OpenMBeanOperationInfoSupport.ACTION_INFO) };
102: MBeanNotificationInfo[] notifications = new MBeanNotificationInfo[] { new MBeanNotificationInfo(
103: new String[] { "type1", "type" }, "name", "description") };
104: info = new OpenMBeanInfoSupport("name", "description",
105: attributes, constructors, operations, notifications);
106: assertEquals("name", info.getClassName());
107: assertEquals("description", info.getDescription());
108: assertEquals(1, info.getAttributes().length);
109: assertEquals(1, info.getConstructors().length);
110: assertEquals(1, info.getNotifications().length);
111: assertEquals(1, info.getOperations().length);
112: }
113:
114: public void testEquals() throws Exception {
115: OpenMBeanParameterInfoSupport[] parms = new OpenMBeanParameterInfoSupport[] { new OpenMBeanParameterInfoSupport(
116: "name", "description", SimpleType.STRING) };
117:
118: OpenMBeanAttributeInfoSupport[] attributes = new OpenMBeanAttributeInfoSupport[] { new OpenMBeanAttributeInfoSupport(
119: "name", "description", SimpleType.STRING, true, true,
120: false) };
121: OpenMBeanConstructorInfoSupport[] constructors = new OpenMBeanConstructorInfoSupport[] { new OpenMBeanConstructorInfoSupport(
122: "name", "description", parms) };
123: OpenMBeanOperationInfoSupport[] operations = new OpenMBeanOperationInfoSupport[] { new OpenMBeanOperationInfoSupport(
124: "name", "description", parms, SimpleType.STRING,
125: OpenMBeanOperationInfoSupport.ACTION_INFO) };
126: MBeanNotificationInfo[] notifications = new MBeanNotificationInfo[] { new MBeanNotificationInfo(
127: new String[] { "type1", "type" }, "name", "description") };
128: OpenMBeanInfoSupport info = new OpenMBeanInfoSupport("name",
129: "description", attributes, constructors, operations,
130: notifications);
131:
132: assertTrue("Null is not equal to any instance", info
133: .equals(null) == false);
134: assertTrue(
135: "Instance is only equal another OpenMBeanInfo instance",
136: info.equals(new Object()) == false);
137: assertTrue("Instance should equal itself", info.equals(info));
138:
139: OpenMBeanInfoSupport info2 = new OpenMBeanInfoSupport("name",
140: "description", attributes, constructors, operations,
141: notifications);
142: assertTrue("Instances with same values should be equal", info
143: .equals(info2));
144: assertTrue("Instances with same values should be equal", info2
145: .equals(info));
146:
147: info2 = new OpenMBeanInfoSupport("name2", "description",
148: attributes, constructors, operations, notifications);
149: assertTrue(
150: "Instances with different class names are not equal",
151: info.equals(info2) == false);
152: assertTrue(
153: "Instances with different class names are not equal",
154: info2.equals(info) == false);
155:
156: info2 = new OpenMBeanInfoSupport("name", "description2",
157: attributes, constructors, operations, notifications);
158: assertTrue("Instances with different descriptions are equal",
159: info.equals(info2));
160: assertTrue("Instances with different descriptions are equal",
161: info2.equals(info));
162:
163: OpenMBeanAttributeInfoSupport[] attributes2 = new OpenMBeanAttributeInfoSupport[] { new OpenMBeanAttributeInfoSupport(
164: "name2", "description", SimpleType.STRING, true, true,
165: false) };
166:
167: info2 = new OpenMBeanInfoSupport("name", "description",
168: attributes2, constructors, operations, notifications);
169: assertTrue("Instances with different attributes are not equal",
170: info.equals(info2) == false);
171: assertTrue("Instances with different attributes are not equal",
172: info2.equals(info) == false);
173:
174: attributes2 = new OpenMBeanAttributeInfoSupport[] {
175: new OpenMBeanAttributeInfoSupport("name2",
176: "description", SimpleType.STRING, true, true,
177: false),
178: new OpenMBeanAttributeInfoSupport("name3",
179: "description", SimpleType.STRING, true, true,
180: false) };
181:
182: info2 = new OpenMBeanInfoSupport("name", "description",
183: attributes2, constructors, operations, notifications);
184: assertTrue(
185: "Instances with different numbers of attributes are not equal",
186: info.equals(info2) == false);
187: assertTrue(
188: "Instances with different numbers of attributes are not equal",
189: info2.equals(info) == false);
190:
191: info2 = new OpenMBeanInfoSupport("name", "description", null,
192: constructors, operations, notifications);
193: assertTrue(
194: "Instances with and without attributes are not equal",
195: info.equals(info2) == false);
196: assertTrue(
197: "Instances with and without attributes are not equal",
198: info2.equals(info) == false);
199:
200: OpenMBeanConstructorInfoSupport[] constructors2 = new OpenMBeanConstructorInfoSupport[] { new OpenMBeanConstructorInfoSupport(
201: "name2", "description", parms) };
202:
203: info2 = new OpenMBeanInfoSupport("name", "description",
204: attributes, constructors2, operations, notifications);
205: assertTrue(
206: "Instances with different constructors are not equal",
207: info.equals(info2) == false);
208: assertTrue(
209: "Instances with different constructors are not equal",
210: info2.equals(info) == false);
211:
212: constructors2 = new OpenMBeanConstructorInfoSupport[] {
213: new OpenMBeanConstructorInfoSupport("name2",
214: "description", parms),
215: new OpenMBeanConstructorInfoSupport("name3",
216: "description", parms) };
217:
218: info2 = new OpenMBeanInfoSupport("name", "description",
219: attributes, constructors2, operations, notifications);
220: assertTrue(
221: "Instances with different numbers of constructors are not equal",
222: info.equals(info2) == false);
223: assertTrue(
224: "Instances with different numbers of constructors are not equal",
225: info2.equals(info) == false);
226:
227: info2 = new OpenMBeanInfoSupport("name", "description",
228: attributes, null, operations, notifications);
229: assertTrue(
230: "Instances with and without constructors are not equal",
231: info.equals(info2) == false);
232: assertTrue(
233: "Instances with and without constructors are not equal",
234: info2.equals(info) == false);
235:
236: OpenMBeanOperationInfoSupport[] operations2 = new OpenMBeanOperationInfoSupport[] { new OpenMBeanOperationInfoSupport(
237: "name2", "description", parms, SimpleType.STRING,
238: OpenMBeanOperationInfoSupport.ACTION_INFO) };
239:
240: info2 = new OpenMBeanInfoSupport("name", "description",
241: attributes, constructors, operations2, notifications);
242: assertTrue("Instances with different operations are not equal",
243: info.equals(info2) == false);
244: assertTrue("Instances with different operations are not equal",
245: info2.equals(info) == false);
246:
247: operations2 = new OpenMBeanOperationInfoSupport[] {
248: new OpenMBeanOperationInfoSupport("name2",
249: "description", parms, SimpleType.STRING,
250: OpenMBeanOperationInfoSupport.ACTION_INFO),
251: new OpenMBeanOperationInfoSupport("name3",
252: "description", parms, SimpleType.STRING,
253: OpenMBeanOperationInfoSupport.ACTION_INFO) };
254:
255: info2 = new OpenMBeanInfoSupport("name", "description",
256: attributes, constructors, operations2, notifications);
257: assertTrue(
258: "Instances with different numbers of operations are not equal",
259: info.equals(info2) == false);
260: assertTrue(
261: "Instances with different numbers of operations are not equal",
262: info2.equals(info) == false);
263:
264: info2 = new OpenMBeanInfoSupport("name", "description",
265: attributes, constructors, null, notifications);
266: assertTrue(
267: "Instances with and without operations are not equal",
268: info.equals(info2) == false);
269: assertTrue(
270: "Instances with and without operations are not equal",
271: info2.equals(info) == false);
272:
273: MBeanNotificationInfo[] notifications2 = new MBeanNotificationInfo[] { new MBeanNotificationInfo(
274: new String[] { "type", "type" }, "name2", "description") };
275:
276: info2 = new OpenMBeanInfoSupport("name", "description",
277: attributes, constructors, operations, notifications2);
278: assertTrue(
279: "Instances with different notifications are not equal",
280: info.equals(info2) == false);
281: assertTrue(
282: "Instances with different notifications are not equal",
283: info2.equals(info) == false);
284:
285: notifications2 = new MBeanNotificationInfo[] {
286: new MBeanNotificationInfo(
287: new String[] { "type", "type" }, "name2",
288: "description"),
289: new MBeanNotificationInfo(
290: new String[] { "type", "type" }, "name3",
291: "description") };
292:
293: info2 = new OpenMBeanInfoSupport("name", "description",
294: attributes, constructors, operations, notifications2);
295: assertTrue(
296: "Instances with different numbers of notifications are not equal",
297: info.equals(info2) == false);
298: assertTrue(
299: "Instances with different numbers of notifications are not equal",
300: info2.equals(info) == false);
301:
302: info2 = new OpenMBeanInfoSupport("name", "description",
303: attributes, constructors, operations, null);
304: assertTrue(
305: "Instances with and without notifications are not equal",
306: info.equals(info2) == false);
307: assertTrue(
308: "Instances with and without notifications are not equal",
309: info2.equals(info) == false);
310: }
311:
312: public void testHashCode() throws Exception {
313: OpenMBeanParameterInfoSupport[] parms = new OpenMBeanParameterInfoSupport[] { new OpenMBeanParameterInfoSupport(
314: "name", "description", SimpleType.STRING) };
315:
316: OpenMBeanAttributeInfoSupport[] attributes = new OpenMBeanAttributeInfoSupport[] { new OpenMBeanAttributeInfoSupport(
317: "name", "description", SimpleType.STRING, true, true,
318: false) };
319: OpenMBeanConstructorInfoSupport[] constructors = new OpenMBeanConstructorInfoSupport[] { new OpenMBeanConstructorInfoSupport(
320: "name", "description", parms) };
321: OpenMBeanOperationInfoSupport[] operations = new OpenMBeanOperationInfoSupport[] { new OpenMBeanOperationInfoSupport(
322: "name", "description", parms, SimpleType.STRING,
323: OpenMBeanOperationInfoSupport.ACTION_INFO) };
324: MBeanNotificationInfo[] notifications = new MBeanNotificationInfo[] { new MBeanNotificationInfo(
325: new String[] { "type1", "type" }, "name", "description") };
326: OpenMBeanInfoSupport info = new OpenMBeanInfoSupport("name",
327: "description", attributes, constructors, operations,
328: notifications);
329:
330: int myHash = "name".hashCode()
331: + new HashSet(Arrays.asList(attributes)).hashCode()
332: + new HashSet(Arrays.asList(constructors)).hashCode()
333: + new HashSet(Arrays.asList(operations)).hashCode()
334: + new HashSet(Arrays.asList(notifications)).hashCode();
335: assertEquals(myHash, info.hashCode());
336: }
337:
338: public void testToString() throws Exception {
339: OpenMBeanParameterInfoSupport[] parms = new OpenMBeanParameterInfoSupport[] { new OpenMBeanParameterInfoSupport(
340: "name", "description", SimpleType.STRING) };
341:
342: OpenMBeanAttributeInfoSupport[] attributes = new OpenMBeanAttributeInfoSupport[] { new OpenMBeanAttributeInfoSupport(
343: "name", "description", SimpleType.STRING, true, true,
344: false) };
345: OpenMBeanConstructorInfoSupport[] constructors = new OpenMBeanConstructorInfoSupport[] { new OpenMBeanConstructorInfoSupport(
346: "name", "description", parms) };
347: OpenMBeanOperationInfoSupport[] operations = new OpenMBeanOperationInfoSupport[] { new OpenMBeanOperationInfoSupport(
348: "name", "description", parms, SimpleType.STRING,
349: OpenMBeanOperationInfoSupport.ACTION_INFO) };
350: MBeanNotificationInfo[] notifications = new MBeanNotificationInfo[] { new MBeanNotificationInfo(
351: new String[] { "type1", "type" }, "name", "description") };
352: OpenMBeanInfoSupport info = new OpenMBeanInfoSupport("NAME",
353: "DESCRIPTION", attributes, constructors, operations,
354: notifications);
355:
356: String toString = info.toString();
357:
358: assertTrue("info.toString() should contain the name", toString
359: .indexOf("NAME") != -1);
360: assertTrue(
361: "info.toString() should contain the attributes",
362: toString.indexOf(Arrays.asList(attributes).toString()) != -1);
363: assertTrue(
364: "info.toString() should contain the constructors",
365: toString
366: .indexOf(Arrays.asList(constructors).toString()) != -1);
367: assertTrue(
368: "info.toString() should contain the operations",
369: toString.indexOf(Arrays.asList(operations).toString()) != -1);
370: assertTrue("info.toString() should contain the notifications",
371: toString.indexOf(Arrays.asList(notifications)
372: .toString()) != -1);
373: }
374:
375: public void testSerialization() throws Exception {
376: OpenMBeanParameterInfoSupport[] parms = new OpenMBeanParameterInfoSupport[] { new OpenMBeanParameterInfoSupport(
377: "name", "description", SimpleType.STRING) };
378:
379: OpenMBeanAttributeInfoSupport[] attributes = new OpenMBeanAttributeInfoSupport[] { new OpenMBeanAttributeInfoSupport(
380: "name", "description", SimpleType.STRING, true, true,
381: false) };
382: OpenMBeanConstructorInfoSupport[] constructors = new OpenMBeanConstructorInfoSupport[] { new OpenMBeanConstructorInfoSupport(
383: "name", "description", parms) };
384: OpenMBeanOperationInfoSupport[] operations = new OpenMBeanOperationInfoSupport[] { new OpenMBeanOperationInfoSupport(
385: "name", "description", parms, SimpleType.STRING,
386: OpenMBeanOperationInfoSupport.ACTION_INFO) };
387: MBeanNotificationInfo[] notifications = new MBeanNotificationInfo[] { new MBeanNotificationInfo(
388: new String[] { "type1", "type" }, "name", "description") };
389: OpenMBeanInfoSupport info = new OpenMBeanInfoSupport("name",
390: "description", attributes, constructors, operations,
391: notifications);
392: // Serialize it
393: ByteArrayOutputStream baos = new ByteArrayOutputStream();
394: ObjectOutputStream oos = new ObjectOutputStream(baos);
395: oos.writeObject(info);
396:
397: // Deserialize it
398: ByteArrayInputStream bais = new ByteArrayInputStream(baos
399: .toByteArray());
400: ObjectInputStream ois = new ObjectInputStream(bais);
401: OpenMBeanInfoSupport result = (OpenMBeanInfoSupport) ois
402: .readObject();
403:
404: assertEquals(info.getClassName(), result.getClassName());
405: assertEquals(Arrays.asList(info.getAttributes()), Arrays
406: .asList(result.getAttributes()));
407: assertEquals(Arrays.asList(info.getConstructors()), Arrays
408: .asList(result.getConstructors()));
409: assertEquals(Arrays.asList(info.getOperations()), Arrays
410: .asList(result.getOperations()));
411:
412: // UGLY!
413: MBeanNotificationInfo origNotification = info
414: .getNotifications()[0];
415: MBeanNotificationInfo resultNotification = result
416: .getNotifications()[0];
417: assertEquals(origNotification.getName(), resultNotification
418: .getName());
419: assertEquals(origNotification.getDescription(),
420: resultNotification.getDescription());
421: assertEquals(Arrays.asList(origNotification.getNotifTypes()),
422: Arrays.asList(resultNotification.getNotifTypes()));
423: }
424:
425: public void testErrors() throws Exception {
426: boolean caught = false;
427: try {
428: MyOpenMBeanAttributeInfoSupport[] infos = new MyOpenMBeanAttributeInfoSupport[] { new MyOpenMBeanAttributeInfoSupport() };
429: OpenMBeanInfoSupport info = new OpenMBeanInfoSupport(
430: "className", "description", infos, null, null, null);
431: } catch (ArrayStoreException e) {
432: caught = true;
433: }
434: if (caught == false)
435: fail("Expected ArrayStoreException for attributes not MBeanAttributeInfo");
436:
437: caught = false;
438: try {
439: MyOpenMBeanConstructorInfoSupport[] infos = new MyOpenMBeanConstructorInfoSupport[] { new MyOpenMBeanConstructorInfoSupport() };
440: OpenMBeanInfoSupport info = new OpenMBeanInfoSupport(
441: "className", "description", null, infos, null, null);
442: } catch (ArrayStoreException e) {
443: caught = true;
444: }
445: if (caught == false)
446: fail("Expected ArrayStoreException for attributes not MBeanConstructorInfo");
447:
448: caught = false;
449: try {
450: MyOpenMBeanOperationInfoSupport[] infos = new MyOpenMBeanOperationInfoSupport[] { new MyOpenMBeanOperationInfoSupport() };
451: OpenMBeanInfoSupport info = new OpenMBeanInfoSupport(
452: "className", "description", null, null, infos, null);
453: } catch (ArrayStoreException e) {
454: caught = true;
455: }
456: if (caught == false)
457: fail("Expected ArrayStoreException for attributes not MBeanOperationInfo");
458: }
459:
460: public static class MyOpenMBeanParameterInfo implements
461: OpenMBeanParameterInfo {
462: public boolean equals(Object o) {
463: return false;
464: }
465:
466: public Object getDefaultValue() {
467: return null;
468: }
469:
470: public String getDescription() {
471: return null;
472: }
473:
474: public Set getLegalValues() {
475: return null;
476: }
477:
478: public Comparable getMaxValue() {
479: return null;
480: }
481:
482: public Comparable getMinValue() {
483: return null;
484: }
485:
486: public String getName() {
487: return null;
488: }
489:
490: public OpenType getOpenType() {
491: return null;
492: }
493:
494: public boolean hasDefaultValue() {
495: return false;
496: }
497:
498: public boolean hasLegalValues() {
499: return false;
500: }
501:
502: public int hashCode() {
503: return 0;
504: }
505:
506: public boolean hasMaxValue() {
507: return false;
508: }
509:
510: public boolean hasMinValue() {
511: return false;
512: }
513:
514: public boolean isValue(Object o) {
515: return false;
516: }
517:
518: public String toString() {
519: return null;
520: }
521: }
522:
523: public static class MyOpenMBeanAttributeInfoSupport extends
524: MyOpenMBeanParameterInfo implements OpenMBeanAttributeInfo {
525: public boolean isIs() {
526: return false;
527: }
528:
529: public boolean isReadable() {
530: return false;
531: }
532:
533: public boolean isWritable() {
534: return false;
535: }
536: }
537:
538: public static class MyOpenMBeanConstructorInfoSupport implements
539: OpenMBeanConstructorInfo {
540: public boolean equals(Object o) {
541: return false;
542: }
543:
544: public String getDescription() {
545: return null;
546: }
547:
548: public String getName() {
549: return null;
550: }
551:
552: public MBeanParameterInfo[] getSignature() {
553: return null;
554: }
555:
556: public int hashCode() {
557: return 0;
558: }
559:
560: public String toString() {
561: return null;
562: }
563: }
564:
565: public static class MyOpenMBeanOperationInfoSupport implements
566: OpenMBeanOperationInfo {
567: public boolean equals(Object o) {
568: return false;
569: }
570:
571: public String getDescription() {
572: return null;
573: }
574:
575: public int getImpact() {
576: return 0;
577: }
578:
579: public String getName() {
580: return null;
581: }
582:
583: public OpenType getReturnOpenType() {
584: return null;
585: }
586:
587: public String getReturnType() {
588: return null;
589: }
590:
591: public MBeanParameterInfo[] getSignature() {
592: return null;
593: }
594:
595: public int hashCode() {
596: return 0;
597: }
598:
599: public String toString() {
600: return null;
601: }
602: }
603: }
|