KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jbossmx > compliance > registration > RegistrationTestCase


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.jbossmx.compliance.registration;
23
24 import org.jboss.test.jbossmx.compliance.TestCase;
25
26 import javax.management.MBeanServer JavaDoc;
27 import javax.management.MBeanServerFactory JavaDoc;
28 import javax.management.ObjectName JavaDoc;
29 import javax.management.MalformedObjectNameException JavaDoc;
30 import javax.management.InstanceAlreadyExistsException JavaDoc;
31 import javax.management.MBeanRegistrationException JavaDoc;
32 import javax.management.NotCompliantMBeanException JavaDoc;
33 import javax.management.InstanceNotFoundException JavaDoc;
34
35 import org.jboss.test.jbossmx.compliance.registration.support.RegistrationAware;
36
37 public class RegistrationTestCase
38    extends TestCase
39 {
40    public RegistrationTestCase(String JavaDoc s)
41    {
42       super(s);
43    }
44
45    public void testSimpleRegistration()
46    {
47       try
48       {
49          MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
50          RegistrationAware ra = new RegistrationAware();
51          ObjectName JavaDoc name = new ObjectName JavaDoc("test:key=value");
52
53          server.registerMBean(ra, name);
54
55          assertTrue("preRegister", ra.isPreRegisterCalled());
56          assertTrue("postRegister", ra.isPostRegisterCalled());
57          assertTrue("postRegisterRegistrationDone", ra.isPostRegisterRegistrationDone());
58          assertEquals(name, ra.getRegisteredObjectName());
59
60          server.unregisterMBean(name);
61
62          assertTrue("preDeRegister", ra.isPreDeRegisterCalled());
63          assertTrue("postDeRegister", ra.isPostDeRegisterCalled());
64       }
65       catch (MalformedObjectNameException JavaDoc e)
66       {
67          fail("spurious MalformedObjectNameException");
68       }
69       catch (MBeanRegistrationException JavaDoc e)
70       {
71          fail("strange MBeanRegistrationException linked to: " + e.getTargetException().getMessage());
72       }
73       catch (Exception JavaDoc e)
74       {
75          fail("something else went wrong: " + e.getMessage());
76       }
77    }
78
79    public void testDuplicateRegistration()
80    {
81
82       try
83       {
84          MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
85          ObjectName JavaDoc name = new ObjectName JavaDoc("test:key=value");
86
87          RegistrationAware original = new RegistrationAware();
88          RegistrationAware ra = new RegistrationAware();
89
90          server.registerMBean(original, name);
91
92          try
93          {
94             server.registerMBean(ra, name);
95             fail("expected a InstanceAlreadyExistsException");
96          }
97          catch (InstanceAlreadyExistsException JavaDoc e)
98          {
99          }
100
101          assertTrue("preRegister", ra.isPreRegisterCalled());
102          assertTrue("postRegister", ra.isPostRegisterCalled());
103          assertTrue("postRegisterRegistrationDone", !ra.isPostRegisterRegistrationDone());
104          assertTrue("preDeRegister", !ra.isPreDeRegisterCalled());
105          assertTrue("postDeRegister", !ra.isPostDeRegisterCalled());
106          assertEquals(name, ra.getRegisteredObjectName());
107
108          server.unregisterMBean(name);
109       }
110       catch (Exception JavaDoc e)
111       {
112          fail("got an unexpected exception: " + e.getMessage());
113       }
114    }
115
116 }
117
Popular Tags