1 22 package org.jboss.test.jmx.compliance.varia; 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.ByteArrayOutputStream ; 26 import java.io.ObjectInputStream ; 27 import java.io.ObjectOutputStream ; 28 29 import javax.management.MalformedObjectNameException ; 30 import javax.management.ObjectInstance ; 31 import javax.management.ObjectName ; 32 33 import junit.framework.TestCase; 34 35 43 public class ObjectInstanceTestCase 44 extends TestCase 45 { 46 48 50 53 public ObjectInstanceTestCase(String s) 54 { 55 super(s); 56 } 57 58 60 63 public void testStringConstructor() 64 { 65 ObjectInstance instance = null; 66 67 try 68 { 69 instance = new ObjectInstance ("test:type=test", "ClassName"); 70 } 71 catch (Exception e) 72 { 73 fail(e.toString()); 74 } 75 76 assertEquals("test:type=test", instance.getObjectName().toString()); 78 assertEquals("ClassName", instance.getClassName()); 79 } 80 81 84 public void testObjectNameConstructor() 85 { 86 ObjectInstance instance = null; 87 ObjectName objectName = null; 88 89 try 90 { 91 objectName = new ObjectName (":type=test"); 92 instance = new ObjectInstance (objectName, "ClassName"); 93 } 94 catch (Exception e) 95 { 96 fail(e.toString()); 97 } 98 99 assertEquals(objectName, instance.getObjectName()); 101 assertEquals("ClassName", instance.getClassName()); 102 } 103 104 107 public void testEquals() 108 { 109 ObjectInstance instanceTest = null; 110 ObjectInstance instanceSame = null; 111 ObjectInstance instanceDiffName = null; 112 ObjectInstance instanceDiffClass = null; 113 114 try 115 { 116 instanceTest = new ObjectInstance ("test:type=test", "ClassName"); 117 instanceSame = new ObjectInstance ("test:type=test", "ClassName"); 118 instanceDiffName = new ObjectInstance ("test:type=different", "ClassName"); 119 instanceDiffClass = new ObjectInstance ("test:type=test", "Another"); 120 } 121 catch (Exception e) 122 { 123 fail(e.toString()); 124 } 125 126 assertEquals(instanceTest, instanceTest); 127 assertEquals(instanceTest, instanceSame); 128 if (instanceTest.equals(instanceDiffName)) 129 fail("ObjectInstance.equals broken for object name"); 130 if (instanceTest.equals(instanceDiffClass)) 131 fail("ObjectInstance.equals broken for class name"); 132 } 133 134 137 public void testErrors() 138 { 139 boolean caught = false; 140 try 141 { 142 new ObjectInstance ("rubbish", "ClassName"); 143 } 144 catch (MalformedObjectNameException e) 145 { 146 caught = true; 147 } 148 catch (Exception e) 149 { 150 fail(e.toString()); 151 } 152 if (caught == false) 153 fail("ObjectInstance(String, String) failed to report malformed object name"); 154 155 try 156 { 157 String NULL = null; 158 new ObjectInstance (NULL, "ClassName"); 159 } 160 catch (MalformedObjectNameException e) 161 { 162 caught = true; 163 } 164 catch (Exception e) 165 { 166 fail(e.toString()); 167 } 168 if (caught == false) 169 fail("ObjectInstance(String, String) failed to report null object name"); 170 } 171 172 175 public void testSerialization() 176 { 177 ObjectInstance original = null; 178 ObjectInstance result = null; 179 ObjectName objectName = null; 180 181 try 182 { 183 objectName = new ObjectName (":type=test"); 184 original = new ObjectInstance (objectName, "ClassName"); 185 186 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 188 ObjectOutputStream oos = new ObjectOutputStream (baos); 189 oos.writeObject(original); 190 191 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 193 ObjectInputStream ois = new ObjectInputStream (bais); 194 result = (ObjectInstance ) ois.readObject(); 195 } 196 catch (Exception e) 197 { 198 fail(e.toString()); 199 } 200 201 assertEquals(original, result); 203 assertEquals(objectName, result.getObjectName()); 204 assertEquals("ClassName", result.getClassName()); 205 } 206 } 207 | Popular Tags |