1 7 8 package test.compliance.varia; 9 10 import java.io.ByteArrayInputStream; 11 import java.io.ByteArrayOutputStream; 12 import java.io.ObjectInputStream; 13 import java.io.ObjectOutputStream; 14 15 import javax.management.MalformedObjectNameException; 16 import javax.management.ObjectInstance; 17 import javax.management.ObjectName; 18 19 import junit.framework.TestCase; 20 21 29 public class ObjectInstanceTestCase 30 extends TestCase 31 { 32 34 36 39 public ObjectInstanceTestCase(String s) 40 { 41 super(s); 42 } 43 44 46 49 public void testStringConstructor() 50 { 51 ObjectInstance instance = null; 52 53 try 54 { 55 instance = new ObjectInstance("test:type=test", "ClassName"); 56 } 57 catch (Exception e) 58 { 59 fail(e.toString()); 60 } 61 62 assertEquals("test:type=test", instance.getObjectName().toString()); 64 assertEquals("ClassName", instance.getClassName()); 65 } 66 67 70 public void testObjectNameConstructor() 71 { 72 ObjectInstance instance = null; 73 ObjectName objectName = null; 74 75 try 76 { 77 objectName = new ObjectName(":type=test"); 78 instance = new ObjectInstance(objectName, "ClassName"); 79 } 80 catch (Exception e) 81 { 82 fail(e.toString()); 83 } 84 85 assertEquals(objectName, instance.getObjectName()); 87 assertEquals("ClassName", instance.getClassName()); 88 } 89 90 93 public void testEquals() 94 { 95 ObjectInstance instanceTest = null; 96 ObjectInstance instanceSame = null; 97 ObjectInstance instanceDiffName = null; 98 ObjectInstance instanceDiffClass = null; 99 100 try 101 { 102 instanceTest = new ObjectInstance("test:type=test", "ClassName"); 103 instanceSame = new ObjectInstance("test:type=test", "ClassName"); 104 instanceDiffName = new ObjectInstance("test:type=different", "ClassName"); 105 instanceDiffClass = new ObjectInstance("test:type=test", "Another"); 106 } 107 catch (Exception e) 108 { 109 fail(e.toString()); 110 } 111 112 assertEquals(instanceTest, instanceTest); 113 assertEquals(instanceTest, instanceSame); 114 if (instanceTest.equals(instanceDiffName)) 115 fail("ObjectInstance.equals broken for object name"); 116 if (instanceTest.equals(instanceDiffClass)) 117 fail("ObjectInstance.equals broken for class name"); 118 } 119 120 123 public void testErrors() 124 { 125 boolean caught = false; 126 try 127 { 128 new ObjectInstance("rubbish", "ClassName"); 129 } 130 catch (MalformedObjectNameException e) 131 { 132 caught = true; 133 } 134 catch (Exception e) 135 { 136 fail(e.toString()); 137 } 138 if (caught == false) 139 fail("ObjectInstance(String, String) failed to report malformed object name"); 140 141 try 142 { 143 String NULL = null; 144 new ObjectInstance(NULL, "ClassName"); 145 } 146 catch (MalformedObjectNameException e) 147 { 148 caught = true; 149 } 150 catch (Exception e) 151 { 152 fail(e.toString()); 153 } 154 if (caught == false) 155 fail("ObjectInstance(String, String) failed to report null object name"); 156 } 157 158 161 public void testSerialization() 162 { 163 ObjectInstance original = null; 164 ObjectInstance result = null; 165 ObjectName objectName = null; 166 167 try 168 { 169 objectName = new ObjectName(":type=test"); 170 original = new ObjectInstance(objectName, "ClassName"); 171 172 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 174 ObjectOutputStream oos = new ObjectOutputStream(baos); 175 oos.writeObject(original); 176 177 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); 179 ObjectInputStream ois = new ObjectInputStream(bais); 180 result = (ObjectInstance) ois.readObject(); 181 } 182 catch (Exception e) 183 { 184 fail(e.toString()); 185 } 186 187 assertEquals(original, result); 189 assertEquals(objectName, result.getObjectName()); 190 assertEquals("ClassName", result.getClassName()); 191 } 192 } 193 | Popular Tags |