KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > compliance > server > MBeanServerBuilderTestCase


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 package test.compliance.server;
9
10 import junit.framework.TestCase;
11
12 import javax.management.MBeanServer;
13 import javax.management.MBeanServerBuilder;
14 import javax.management.MBeanServerDelegate;
15 import javax.management.MBeanServerFactory;
16 import javax.management.ObjectName;
17
18 import test.compliance.server.support.BuilderTest;
19 import test.compliance.server.support.DelegateListener;
20 import test.compliance.server.support.MBeanServerReplaced;
21 import test.compliance.server.support.MBeanServerWrapper;
22 import test.compliance.server.support.TestMBeanServerBuilder;
23 import test.compliance.server.support.TestMBeanServerDelegate;
24
25 /**
26  * Tests the MBeanServerBuilder
27  *
28  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
29  */

30 public class MBeanServerBuilderTestCase
31   extends TestCase
32 {
33    // Attributes ----------------------------------------------------------------
34

35    private ObjectName name;
36    private ObjectName delegateName;
37    private String defaultBuilder;
38
39    private static final String DEFAULT_DOMAIN = "default";
40    private static final String BUILDER_PROPERTY = "javax.management.builder.initial";
41
42    // Constructor ---------------------------------------------------------------
43

44    /**
45     * Construct the test
46     */

47    public MBeanServerBuilderTestCase(String s)
48    {
49       super(s);
50       try
51       {
52          name = new ObjectName("server:test=MBeanServerBuilder");
53          delegateName = new ObjectName("JMImplementation:type=MBeanServerDelegate");
54       }
55       catch (Exception e)
56       {
57          e.printStackTrace();
58          throw new RuntimeException(e.toString());
59       }
60
61       defaultBuilder = System.getProperty(BUILDER_PROPERTY, "javax.management.MBeanServerBuilder");
62    }
63
64    // Tests ---------------------------------------------------------------------
65

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