001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package test.compliance.server;
023:
024: import junit.framework.TestCase;
025:
026: import javax.management.MBeanServer;
027: import javax.management.MBeanServerBuilder;
028: import javax.management.MBeanServerDelegate;
029: import javax.management.MBeanServerFactory;
030: import javax.management.ObjectName;
031:
032: import test.compliance.server.support.BuilderTest;
033: import test.compliance.server.support.DelegateListener;
034: import test.compliance.server.support.MBeanServerReplaced;
035: import test.compliance.server.support.MBeanServerWrapper;
036: import test.compliance.server.support.TestMBeanServerBuilder;
037: import test.compliance.server.support.TestMBeanServerDelegate;
038:
039: /**
040: * Tests the MBeanServerBuilder
041: *
042: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
043: */
044: public class MBeanServerBuilderTestCase extends TestCase {
045: // Attributes ----------------------------------------------------------------
046:
047: private ObjectName name;
048: private ObjectName delegateName;
049: private String defaultBuilder;
050:
051: private static final String DEFAULT_DOMAIN = "default";
052: private static final String BUILDER_PROPERTY = "javax.management.builder.initial";
053:
054: // Constructor ---------------------------------------------------------------
055:
056: /**
057: * Construct the test
058: */
059: public MBeanServerBuilderTestCase(String s) {
060: super (s);
061: try {
062: name = new ObjectName("server:test=MBeanServerBuilder");
063: delegateName = new ObjectName(
064: "JMImplementation:type=MBeanServerDelegate");
065: } catch (Exception e) {
066: e.printStackTrace();
067: throw new RuntimeException(e.toString());
068: }
069:
070: defaultBuilder = System.getProperty(BUILDER_PROPERTY,
071: "javax.management.MBeanServerBuilder");
072: }
073:
074: // Tests ---------------------------------------------------------------------
075:
076: public void testNewMBeanServerDelegate() throws Exception {
077: MBeanServerBuilder builder = new MBeanServerBuilder();
078: MBeanServerDelegate delegate = builder.newMBeanServerDelegate();
079: }
080:
081: public void testNewMBeanServer() throws Exception {
082: MBeanServerBuilder builder = new MBeanServerBuilder();
083: MBeanServerDelegate delegate = builder.newMBeanServerDelegate();
084: MBeanServer server = builder.newMBeanServer(DEFAULT_DOMAIN,
085: null, delegate);
086: assertTrue(server.getDefaultDomain().equals(DEFAULT_DOMAIN));
087: }
088:
089: public void testNewMBeanServerViaMBeanServerFactory()
090: throws Exception {
091: MBeanServer server = MBeanServerFactory
092: .newMBeanServer(DEFAULT_DOMAIN);
093: assertTrue(server.getDefaultDomain().equals(DEFAULT_DOMAIN));
094: }
095:
096: public void testPreRegisterGetsTheOriginalMBeanServer()
097: throws Exception {
098: MBeanServer server = MBeanServerFactory
099: .newMBeanServer(DEFAULT_DOMAIN);
100:
101: BuilderTest test = new BuilderTest();
102: server.registerMBean(test, name);
103:
104: assertTrue("Registered", server.isRegistered(name));
105: assertTrue("Got the correct MBeanServer", test.server
106: .equals(server));
107: }
108:
109: public void testPreRegisterGetsTheOuterMBeanServer()
110: throws Exception {
111: MBeanServerBuilder builder = new MBeanServerBuilder();
112: MBeanServerDelegate delegate = builder.newMBeanServerDelegate();
113: MBeanServer wrapper = MBeanServerWrapper.getWrapper();
114: MBeanServer server = builder.newMBeanServer(DEFAULT_DOMAIN,
115: wrapper, delegate);
116:
117: MBeanServerWrapper.getHandler(wrapper).server = server;
118:
119: BuilderTest test = new BuilderTest();
120: wrapper.registerMBean(test, name);
121:
122: assertTrue("Registered", server.isRegistered(name));
123: assertTrue("Got the correct MBeanServer", test.server
124: .equals(wrapper));
125: assertTrue("Wrapper invoked", MBeanServerWrapper
126: .getHandler(wrapper).invoked);
127: }
128:
129: public void testMBeanServerDelegateReplaced() throws Exception {
130: MBeanServerBuilder builder = new MBeanServerBuilder();
131: TestMBeanServerDelegate delegate = new TestMBeanServerDelegate();
132: MBeanServer server = builder.newMBeanServer(DEFAULT_DOMAIN,
133: null, delegate);
134:
135: server.getAttribute(delegateName, "MBeanServerId");
136:
137: assertTrue("Delegate replaced", delegate.invoked);
138: }
139:
140: public void testReplaceMBeanServerBuilderMBeanServerDelegate()
141: throws Exception {
142: System.setProperty(BUILDER_PROPERTY,
143: TestMBeanServerBuilder.class.getName());
144: try {
145: MBeanServer server = MBeanServerFactory.newMBeanServer();
146:
147: DelegateListener listener = new DelegateListener();
148: server.addNotificationListener(delegateName, listener,
149: null, null);
150:
151: BuilderTest test = new BuilderTest();
152: server.registerMBean(test, name);
153:
154: assertTrue("Delegate replaced", listener.userData
155: .equals("replaced"));
156: } finally {
157: System.setProperty(BUILDER_PROPERTY, defaultBuilder);
158: }
159: }
160:
161: public void testReplaceMBeanServerBuilderMBeanServer()
162: throws Exception {
163: System.setProperty(BUILDER_PROPERTY,
164: TestMBeanServerBuilder.class.getName());
165: try {
166: MBeanServer server = MBeanServerFactory.newMBeanServer();
167:
168: boolean caught = false;
169: try {
170: server.queryMBeans(null, null);
171: } catch (MBeanServerReplaced e) {
172: caught = true;
173: }
174:
175: assertTrue("MBeanServer replaced", caught);
176: } finally {
177: System.setProperty(BUILDER_PROPERTY, defaultBuilder);
178: }
179: }
180: }
|