KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmx > compliance > server > MBeanServerBuilderTestCase


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.server;
23
24 import javax.management.MBeanServer JavaDoc;
25 import javax.management.MBeanServerBuilder JavaDoc;
26 import javax.management.MBeanServerDelegate JavaDoc;
27 import javax.management.MBeanServerFactory JavaDoc;
28 import javax.management.ObjectName JavaDoc;
29
30 import junit.framework.TestCase;
31
32 import org.jboss.test.jmx.compliance.server.support.BuilderTest;
33 import org.jboss.test.jmx.compliance.server.support.DelegateListener;
34 import org.jboss.test.jmx.compliance.server.support.MBeanServerReplaced;
35 import org.jboss.test.jmx.compliance.server.support.MBeanServerWrapper;
36 import org.jboss.test.jmx.compliance.server.support.TestMBeanServerBuilder;
37 import org.jboss.test.jmx.compliance.server.support.TestMBeanServerDelegate;
38
39
40 /**
41  * Tests the MBeanServerBuilder
42  *
43  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
44  */

45 public class MBeanServerBuilderTestCase
46   extends TestCase
47 {
48    // Attributes ----------------------------------------------------------------
49

50    private ObjectName JavaDoc name;
51    private ObjectName JavaDoc delegateName;
52    private String JavaDoc defaultBuilder;
53
54    private static final String JavaDoc DEFAULT_DOMAIN = "default";
55    private static final String JavaDoc BUILDER_PROPERTY = "javax.management.builder.initial";
56
57    // Constructor ---------------------------------------------------------------
58

59    /**
60     * Construct the test
61     */

62    public MBeanServerBuilderTestCase(String JavaDoc s)
63    {
64       super(s);
65       try
66       {
67          name = new ObjectName JavaDoc("server:test=MBeanServerBuilder");
68          delegateName = new ObjectName JavaDoc("JMImplementation:type=MBeanServerDelegate");
69       }
70       catch (Exception JavaDoc e)
71       {
72          e.printStackTrace();
73          throw new RuntimeException JavaDoc(e.toString());
74       }
75
76       defaultBuilder = System.getProperty(BUILDER_PROPERTY, "javax.management.MBeanServerBuilder");
77    }
78
79    // Tests ---------------------------------------------------------------------
80

81    public void testNewMBeanServerDelegate()
82       throws Exception JavaDoc
83    {
84       MBeanServerBuilder JavaDoc builder = new MBeanServerBuilder JavaDoc();
85       MBeanServerDelegate JavaDoc delegate = builder.newMBeanServerDelegate();
86       assertNotNull(delegate);
87    }
88
89    public void testNewMBeanServer()
90       throws Exception JavaDoc
91    {
92       MBeanServerBuilder JavaDoc builder = new MBeanServerBuilder JavaDoc();
93       MBeanServerDelegate JavaDoc delegate = builder.newMBeanServerDelegate();
94       MBeanServer JavaDoc server = builder.newMBeanServer(DEFAULT_DOMAIN, null, delegate);
95       assertTrue(server.getDefaultDomain().equals(DEFAULT_DOMAIN));
96    }
97
98    public void testNewMBeanServerViaMBeanServerFactory()
99       throws Exception JavaDoc
100    {
101       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer(DEFAULT_DOMAIN);
102       assertTrue(server.getDefaultDomain().equals(DEFAULT_DOMAIN));
103    }
104
105    public void testPreRegisterGetsTheOriginalMBeanServer()
106       throws Exception JavaDoc
107    {
108       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer(DEFAULT_DOMAIN);
109
110       BuilderTest test = new BuilderTest();
111       server.registerMBean(test, name);
112
113       assertTrue("Registered", server.isRegistered(name));
114       assertTrue("Got the correct MBeanServer", test.server.equals(server));
115    }
116
117    public void testPreRegisterGetsTheOuterMBeanServer()
118       throws Exception JavaDoc
119    {
120       MBeanServerBuilder JavaDoc builder = new MBeanServerBuilder JavaDoc();
121       MBeanServerDelegate JavaDoc delegate = builder.newMBeanServerDelegate();
122       MBeanServer JavaDoc wrapper = MBeanServerWrapper.getWrapper();
123       MBeanServer JavaDoc server = builder.newMBeanServer(DEFAULT_DOMAIN, wrapper, delegate);
124
125       MBeanServerWrapper.getHandler(wrapper).server = server;
126
127       BuilderTest test = new BuilderTest();
128       wrapper.registerMBean(test, name);
129
130       assertTrue("Registered", server.isRegistered(name));
131       assertTrue("Got the correct MBeanServer", test.server.equals(wrapper));
132       assertTrue("Wrapper invoked", MBeanServerWrapper.getHandler(wrapper).invoked);
133    }
134
135    public void testMBeanServerDelegateReplaced()
136       throws Exception JavaDoc
137    {
138       MBeanServerBuilder JavaDoc builder = new MBeanServerBuilder JavaDoc();
139       TestMBeanServerDelegate delegate = new TestMBeanServerDelegate();
140       MBeanServer JavaDoc server = builder.newMBeanServer(DEFAULT_DOMAIN, null, delegate);
141
142       server.getAttribute(delegateName, "MBeanServerId");
143
144       assertTrue("Delegate replaced", delegate.invoked);
145    }
146
147    public void testReplaceMBeanServerBuilderMBeanServerDelegate()
148       throws Exception JavaDoc
149    {
150       System.setProperty(BUILDER_PROPERTY, TestMBeanServerBuilder.class.getName());
151       try
152       {
153          MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
154
155          DelegateListener listener = new DelegateListener();
156          server.addNotificationListener(delegateName, listener, null, null);
157
158          BuilderTest test = new BuilderTest();
159          server.registerMBean(test, name);
160
161          assertTrue("Delegate replaced", listener.userData.equals("replaced"));
162       }
163       finally
164       {
165          System.setProperty(BUILDER_PROPERTY, defaultBuilder);
166       }
167    }
168
169    public void testReplaceMBeanServerBuilderMBeanServer()
170       throws Exception JavaDoc
171    {
172       System.setProperty(BUILDER_PROPERTY, TestMBeanServerBuilder.class.getName());
173       try
174       {
175          MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
176
177          boolean caught = false;
178          try
179          {
180             server.queryMBeans(null, null);
181          }
182          catch (MBeanServerReplaced e)
183          {
184             caught = true;
185          }
186
187          assertTrue("MBeanServer replaced", caught);
188       }
189       finally
190       {
191          System.setProperty(BUILDER_PROPERTY, defaultBuilder);
192       }
193    }
194 }
195
Popular Tags