1 22 package test.implementation.server; 23 24 import junit.framework.TestCase; 25 26 import test.implementation.server.support.Trivial; 27 28 import java.io.ByteArrayInputStream ; 29 import java.io.ByteArrayOutputStream ; 30 import java.io.IOException ; 31 import java.io.ObjectInputStream ; 32 import java.io.ObjectOutputStream ; 33 34 import javax.management.MBeanServer ; 35 import javax.management.MBeanServerFactory ; 36 import javax.management.ObjectName ; 37 import javax.management.ObjectInstance ; 38 39 import org.jboss.mx.server.ServerObjectInstance; 40 41 48 public class ObjectInstanceTestCase 49 extends TestCase 50 { 51 53 55 58 public ObjectInstanceTestCase(String s) 59 { 60 super(s); 61 } 62 63 65 68 public void testDefaultDomain() 69 { 70 MBeanServer server =null; 71 ObjectName unqualifiedName = null; 72 ObjectName qualifiedName = null; 73 ObjectInstance instance1 = null; 74 ObjectInstance instance2 = null; 75 try 76 { 77 server = MBeanServerFactory.createMBeanServer(); 78 unqualifiedName = new ObjectName (":property=1"); 79 qualifiedName = new ObjectName ("DefaultDomain:property=1"); 80 instance1 = server.registerMBean(new Trivial(), qualifiedName); 81 instance2 = server.getObjectInstance(unqualifiedName); 82 } 83 catch (Exception e) 84 { 85 fail(e.toString()); 86 } 87 88 assertEquals(instance1.getObjectName(),qualifiedName); 89 assertEquals(instance1, instance2); 90 91 if (server != null) 92 MBeanServerFactory.releaseMBeanServer(server); 93 } 94 95 98 public void testDifferentServers() 99 { 100 MBeanServer server =null; 101 ObjectName name = null; 102 ObjectInstance instance1 = null; 103 ObjectInstance instance2 = null; 104 try 105 { 106 server = MBeanServerFactory.createMBeanServer(); 107 name = new ObjectName (":property=1"); 108 instance1 = server.registerMBean(new Trivial(), name); 109 MBeanServerFactory.releaseMBeanServer(server); 110 server = MBeanServerFactory.createMBeanServer(); 111 instance2 = server.registerMBean(new Trivial(), name); 112 } 113 catch (Exception e) 114 { 115 fail(e.toString()); 116 } 117 118 if (instance1.equals(instance2) == true) 119 fail("Instances in different servers are the same"); 120 121 if (server != null) 122 MBeanServerFactory.releaseMBeanServer(server); 123 } 124 125 127 130 public void testEquals() 131 { 132 ObjectInstance oi = null; 134 ServerObjectInstance soi = null; 135 ObjectName name = null; 136 String className = "org.jboss.AClass"; 137 try 138 { 139 name = new ObjectName (":a=a"); 140 oi = new ObjectInstance (name, className); 141 soi = new ServerObjectInstance(name, className, "agentid"); 142 } 143 catch (Exception e) 144 { 145 fail(e.toString()); 146 } 147 assertEquals(oi, soi); 148 } 149 153 public void testSerialization() 154 { 155 ServerObjectInstance original = null; 157 ObjectInstance result = null; 158 ObjectName name = null; 159 String className = "org.jboss.AClassName"; 160 try 161 { 162 name = new ObjectName (":a=a"); 163 original = new ServerObjectInstance(name, className, "agentid"); 164 } 165 catch (Exception e) 166 { 167 fail(e.toString()); 168 } 169 170 try 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 (IOException ioe) 183 { 184 fail(ioe.toString()); 185 } 186 catch (ClassNotFoundException cnfe) 187 { 188 fail(cnfe.toString()); 189 } 190 191 assertEquals("javax.management.ObjectInstance", result.getClass().getName()); 193 assertEquals(name, result.getObjectName()); 194 assertEquals(className, result.getClassName()); 195 } 196 } 197 | Popular Tags |