KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmx > compliance > registration > RegistrationTEST


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * 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 org.jboss.test.jmx.compliance.registration;
23
24 import javax.management.InstanceAlreadyExistsException JavaDoc;
25 import javax.management.MBeanRegistrationException JavaDoc;
26 import javax.management.MBeanServer JavaDoc;
27 import javax.management.MBeanServerFactory JavaDoc;
28 import javax.management.MalformedObjectNameException JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30
31 import org.jboss.test.jmx.compliance.registration.support.RegistrationAware;
32
33 import junit.framework.TestCase;
34
35 public class RegistrationTEST extends TestCase
36 {
37    public RegistrationTEST(String JavaDoc s)
38    {
39       super(s);
40    }
41
42    public void testSimpleRegistration()
43    {
44       try
45       {
46          MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
47          RegistrationAware ra = new RegistrationAware();
48          ObjectName JavaDoc name = new ObjectName JavaDoc("test:key=value");
49
50          server.registerMBean(ra, name);
51
52          assertTrue("preRegister", ra.isPreRegisterCalled());
53          assertTrue("postRegister", ra.isPostRegisterCalled());
54          assertTrue("postRegisterRegistrationDone", ra.isPostRegisterRegistrationDone());
55          assertEquals(name, ra.getRegisteredObjectName());
56
57          server.unregisterMBean(name);
58
59          assertTrue("preDeRegister", ra.isPreDeRegisterCalled());
60          assertTrue("postDeRegister", ra.isPostDeRegisterCalled());
61       }
62       catch (MalformedObjectNameException JavaDoc e)
63       {
64          fail("spurious MalformedObjectNameException");
65       }
66       catch (MBeanRegistrationException JavaDoc e)
67       {
68          fail("strange MBeanRegistrationException linked to: " + e.getTargetException().getMessage());
69       }
70       catch (Exception JavaDoc e)
71       {
72          fail("something else went wrong: " + e.getMessage());
73       }
74    }
75
76    public void testDuplicateRegistration()
77    {
78
79       try
80       {
81          MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
82          ObjectName JavaDoc name = new ObjectName JavaDoc("test:key=value");
83
84          RegistrationAware original = new RegistrationAware();
85          RegistrationAware ra = new RegistrationAware();
86
87          server.registerMBean(original, name);
88
89          try
90          {
91             server.registerMBean(ra, name);
92             fail("expected a InstanceAlreadyExistsException");
93          }
94          catch (InstanceAlreadyExistsException JavaDoc e)
95          {
96          }
97
98          assertTrue("preRegister", ra.isPreRegisterCalled());
99          assertTrue("postRegister", ra.isPostRegisterCalled());
100          assertTrue("postRegisterRegistrationDone", !ra.isPostRegisterRegistrationDone());
101          assertTrue("preDeRegister", !ra.isPreDeRegisterCalled());
102          assertTrue("postDeRegister", !ra.isPostDeRegisterCalled());
103          assertEquals(name, ra.getRegisteredObjectName());
104
105          server.unregisterMBean(name);
106       }
107       catch (Exception JavaDoc e)
108       {
109          fail("got an unexpected exception: " + e.getMessage());
110       }
111    }
112
113 }
114
Popular Tags