1 7 8 package test.compliance.registration; 9 10 import javax.management.InstanceAlreadyExistsException; 11 import javax.management.MBeanRegistrationException; 12 import javax.management.MBeanServer; 13 import javax.management.MBeanServerFactory; 14 import javax.management.MalformedObjectNameException; 15 import javax.management.ObjectName; 16 17 import junit.framework.TestCase; 18 import test.compliance.registration.support.RegistrationAware; 19 20 public class RegistrationTEST extends TestCase 21 { 22 public RegistrationTEST(String s) 23 { 24 super(s); 25 } 26 27 public void testSimpleRegistration() 28 { 29 try 30 { 31 MBeanServer server = MBeanServerFactory.newMBeanServer(); 32 RegistrationAware ra = new RegistrationAware(); 33 ObjectName name = new ObjectName("test:key=value"); 34 35 server.registerMBean(ra, name); 36 37 assertTrue("preRegister", ra.isPreRegisterCalled()); 38 assertTrue("postRegister", ra.isPostRegisterCalled()); 39 assertTrue("postRegisterRegistrationDone", ra.isPostRegisterRegistrationDone()); 40 assertEquals(name, ra.getRegisteredObjectName()); 41 42 server.unregisterMBean(name); 43 44 assertTrue("preDeRegister", ra.isPreDeRegisterCalled()); 45 assertTrue("postDeRegister", ra.isPostDeRegisterCalled()); 46 } 47 catch (MalformedObjectNameException e) 48 { 49 fail("spurious MalformedObjectNameException"); 50 } 51 catch (MBeanRegistrationException e) 52 { 53 fail("strange MBeanRegistrationException linked to: " + e.getTargetException().getMessage()); 54 } 55 catch (Exception e) 56 { 57 fail("something else went wrong: " + e.getMessage()); 58 } 59 } 60 61 public void testDuplicateRegistration() 62 { 63 64 try 65 { 66 MBeanServer server = MBeanServerFactory.newMBeanServer(); 67 ObjectName name = new ObjectName("test:key=value"); 68 69 RegistrationAware original = new RegistrationAware(); 70 RegistrationAware ra = new RegistrationAware(); 71 72 server.registerMBean(original, name); 73 74 try 75 { 76 server.registerMBean(ra, name); 77 fail("expected a InstanceAlreadyExistsException"); 78 } 79 catch (InstanceAlreadyExistsException e) 80 { 81 } 82 83 assertTrue("preRegister", ra.isPreRegisterCalled()); 84 assertTrue("postRegister", ra.isPostRegisterCalled()); 85 assertTrue("postRegisterRegistrationDone", !ra.isPostRegisterRegistrationDone()); 86 assertTrue("preDeRegister", !ra.isPreDeRegisterCalled()); 87 assertTrue("postDeRegister", !ra.isPostDeRegisterCalled()); 88 assertEquals(name, ra.getRegisteredObjectName()); 89 90 server.unregisterMBean(name); 91 } 92 catch (Exception e) 93 { 94 fail("got an unexpected exception: " + e.getMessage()); 95 } 96 } 97 98 } 99 | Popular Tags |