01: /*
02: * JBoss, Home of Professional Open Source.
03: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
04: * as indicated by the @author tags. See the copyright.txt file in the
05: * distribution for a full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * published by the Free Software Foundation; either version 2.1 of
10: * the License, or (at your option) any later version.
11: *
12: * This software is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this software; if not, write to the Free
19: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21: */
22: package test.compliance.registration;
23:
24: import javax.management.InstanceAlreadyExistsException;
25: import javax.management.MBeanRegistrationException;
26: import javax.management.MBeanServer;
27: import javax.management.MBeanServerFactory;
28: import javax.management.MalformedObjectNameException;
29: import javax.management.ObjectName;
30:
31: import junit.framework.TestCase;
32: import test.compliance.registration.support.RegistrationAware;
33:
34: public class RegistrationTEST extends TestCase {
35: public RegistrationTEST(String s) {
36: super (s);
37: }
38:
39: public void testSimpleRegistration() {
40: try {
41: MBeanServer server = MBeanServerFactory.newMBeanServer();
42: RegistrationAware ra = new RegistrationAware();
43: ObjectName name = new ObjectName("test:key=value");
44:
45: server.registerMBean(ra, name);
46:
47: assertTrue("preRegister", ra.isPreRegisterCalled());
48: assertTrue("postRegister", ra.isPostRegisterCalled());
49: assertTrue("postRegisterRegistrationDone", ra
50: .isPostRegisterRegistrationDone());
51: assertEquals(name, ra.getRegisteredObjectName());
52:
53: server.unregisterMBean(name);
54:
55: assertTrue("preDeRegister", ra.isPreDeRegisterCalled());
56: assertTrue("postDeRegister", ra.isPostDeRegisterCalled());
57: } catch (MalformedObjectNameException e) {
58: fail("spurious MalformedObjectNameException");
59: } catch (MBeanRegistrationException e) {
60: fail("strange MBeanRegistrationException linked to: "
61: + e.getTargetException().getMessage());
62: } catch (Exception e) {
63: fail("something else went wrong: " + e.getMessage());
64: }
65: }
66:
67: public void testDuplicateRegistration() {
68:
69: try {
70: MBeanServer server = MBeanServerFactory.newMBeanServer();
71: ObjectName name = new ObjectName("test:key=value");
72:
73: RegistrationAware original = new RegistrationAware();
74: RegistrationAware ra = new RegistrationAware();
75:
76: server.registerMBean(original, name);
77:
78: try {
79: server.registerMBean(ra, name);
80: fail("expected a InstanceAlreadyExistsException");
81: } catch (InstanceAlreadyExistsException e) {
82: }
83:
84: assertTrue("preRegister", ra.isPreRegisterCalled());
85: assertTrue("postRegister", ra.isPostRegisterCalled());
86: assertTrue("postRegisterRegistrationDone", !ra
87: .isPostRegisterRegistrationDone());
88: assertTrue("preDeRegister", !ra.isPreDeRegisterCalled());
89: assertTrue("postDeRegister", !ra.isPostDeRegisterCalled());
90: assertEquals(name, ra.getRegisteredObjectName());
91:
92: server.unregisterMBean(name);
93: } catch (Exception e) {
94: fail("got an unexpected exception: " + e.getMessage());
95: }
96: }
97:
98: }
|