1 7 8 package test.compliance.metadata; 9 10 import junit.framework.TestCase; 11 12 import java.io.ByteArrayInputStream; 13 import java.io.ByteArrayOutputStream; 14 import java.io.ObjectInputStream; 15 import java.io.ObjectOutputStream; 16 import java.util.Arrays; 17 18 import javax.management.MBeanNotificationInfo; 19 20 25 public class MBeanNotificationInfoTEST 26 extends TestCase 27 { 28 30 private String[] types1 = new String[] { "type1", "type2" }; 31 private String[] types2 = new String[] { "typex", "type2" }; 32 33 35 37 40 public MBeanNotificationInfoTEST(String s) 41 { 42 super(s); 43 } 44 45 47 public void testMBeanNotificationInfo() 48 throws Exception 49 { 50 MBeanNotificationInfo info = new MBeanNotificationInfo(types1, 51 "name", "description"); 52 assertEquals("name", info.getName()); 53 assertEquals("description", info.getDescription()); 54 assertEquals(Arrays.asList(types1), Arrays.asList(info.getNotifTypes())); 55 } 56 57 public void testHashCode() 58 throws Exception 59 { 60 MBeanNotificationInfo info1 = new MBeanNotificationInfo(types1, "name", "description"); 61 MBeanNotificationInfo info2 = new MBeanNotificationInfo(types1, "name", "description"); 62 63 assertTrue("Different instances with the same hashcode are equal", info1.hashCode() == info2.hashCode()); 64 } 65 66 public void testEquals() 67 throws Exception 68 { 69 MBeanNotificationInfo info = new MBeanNotificationInfo(types1, 70 "name", "description"); 71 72 assertTrue("Null should not be equal", info.equals(null) == false); 73 assertTrue("Only MBeanNotificationInfo should be equal", info.equals(new Object()) == false); 74 75 MBeanNotificationInfo info2 = new MBeanNotificationInfo(types1, 76 "name", "description"); 77 78 assertTrue("Different instances of the same data are equal", info.equals(info2)); 79 assertTrue("Different instances of the same data are equal", info2.equals(info)); 80 81 info2 = new MBeanNotificationInfo(types1, 82 "name", "description2"); 83 84 assertTrue("Different instances with different descriptions are not equal", info.equals(info2) == false); 85 assertTrue("Different instances with different descritpions are not equal", info2.equals(info) == false); 86 87 info2 = new MBeanNotificationInfo(types1, 88 "name2", "description"); 89 90 assertTrue("Instances with different names are not equal", info.equals(info2) == false); 91 assertTrue("Instances with different names are not equal", info2.equals(info) == false); 92 93 info2 = new MBeanNotificationInfo(types2, 94 "name", "description"); 95 96 assertTrue("Instances with different types are not equal", info.equals(info2) == false); 97 assertTrue("Instances with different types are not equal", info2.equals(info) == false); 98 } 99 100 public void testSerialization() 101 throws Exception 102 { 103 MBeanNotificationInfo info = new MBeanNotificationInfo(types1, 104 "name", "description"); 105 106 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 108 ObjectOutputStream oos = new ObjectOutputStream(baos); 109 oos.writeObject(info); 110 111 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); 113 ObjectInputStream ois = new ObjectInputStream(bais); 114 Object result = ois.readObject(); 115 116 assertEquals(info, result); 117 } 118 119 public void testErrors() 120 throws Exception 121 { 122 boolean caught = false; 123 try 124 { 125 MBeanNotificationInfo info = new MBeanNotificationInfo(types1, 126 null, "description"); 127 } 128 catch (IllegalArgumentException e) 129 { 130 caught = true; 131 } 132 if (caught == false) 133 fail("Expected IllegalArgumentException for null name"); 134 135 caught = false; 136 try 137 { 138 MBeanNotificationInfo info = new MBeanNotificationInfo(types1, 139 "", "description"); 140 } 141 catch (IllegalArgumentException e) 142 { 143 caught = true; 144 } 145 if (caught == false) 146 fail("Expected IllegalArgumentException for an empty name"); 147 148 caught = false; 149 try 150 { 151 MBeanNotificationInfo info = new MBeanNotificationInfo(types1, 152 "invalid name", "description"); 153 } 154 catch (IllegalArgumentException e) 155 { 156 caught = true; 157 } 158 if (caught == false) 159 fail("Expected IllegalArgumentException for an 'invalid name'"); 160 } 161 } 162 | Popular Tags |