001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)TestMBeanUtils.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.util.jmx;
030:
031: import javax.management.MBeanServer;
032: import javax.management.ObjectName;
033:
034: /**
035: * Tests for the MBeanUtils class.
036: *
037: * @author Sun Microsystems, Inc.
038: */
039: public class TestMBeanUtils extends junit.framework.TestCase {
040: /**
041: * MBean instance.
042: */
043: private AnMBean mAnMBean;
044:
045: /**
046: * MBean object name.
047: */
048: private ObjectName mAnMBeanName;
049:
050: /**
051: * MBean server.
052: */
053: private MBeanServer mMBeanServer;
054:
055: /**
056: * The constructor for this testcase, forwards the test name to
057: * the jUnit TestCase base class.
058: * @param aTestName String with the name of this test.
059: */
060: public TestMBeanUtils(String aTestName) {
061: super (aTestName);
062: }
063:
064: /**
065: * Setup for the test. This creates the data items need to create
066: * the MBeanUtils in the tests.
067: * @throws Exception when set up fails for any reason.
068: */
069: public void setUp() throws Exception {
070: super .setUp();
071:
072: // Create an MBean server and a logger
073: mMBeanServer = javax.management.MBeanServerFactory
074: .createMBeanServer();
075:
076: // Do static initialization of MBeanUtils
077: MBeanUtils.init(mMBeanServer);
078:
079: // Create an instance of an MBean
080: mAnMBean = new AnMBeanImpl();
081:
082: // Create an MBean ObjectName
083: mAnMBeanName = new ObjectName("jbi:type=test,name=anMBean");
084: }
085:
086: /**
087: * Cleanup for the test.
088: * @throws Exception when tearDown fails for any reason.
089: */
090: public void tearDown() throws Exception {
091: super .tearDown();
092: }
093:
094: // ========================= test static methods =============================
095:
096: /**
097: * Test the init method.
098: * @throws Exception if an unexpected error occurs.
099: */
100: public void testInit() throws Exception {
101: MBeanUtils.init(mMBeanServer);
102: }
103:
104: /**
105: * Test the init method with null arguments. An exception is expected.
106: * @throws Exception if an unexpected error occurs.
107: */
108: public void testInitNullArguments() throws Exception {
109: try {
110: MBeanUtils.init(null);
111: fail("Expected exception not received");
112: } catch (java.lang.IllegalArgumentException ex) {
113: // Verification
114: assertTrue(
115: "Incorrect exception received: " + ex.toString(),
116: (-1 < ex.getMessage().indexOf("JBIUT0000")));
117: }
118: }
119:
120: /**
121: * Test the registerStandardMBean method.
122: * @throws Exception if an unexpected error occurs.
123: */
124: public void testRegisterStandardMBean() throws Exception {
125: // First register the MBean with replace=false. This should work
126: // because the MBean is not already registered.
127:
128: MBeanUtils.registerStandardMBean(AnMBean.class, mAnMBean,
129: mAnMBeanName, false);
130: assertTrue("Failure on registerStandardMBean(): ", mMBeanServer
131: .isRegistered(mAnMBeanName));
132:
133: // Now try to register the MBean with replace=true. This should also
134: // work because the MBean will be replaced.
135:
136: MBeanUtils.registerStandardMBean(AnMBean.class, mAnMBean,
137: mAnMBeanName, true);
138: assertTrue("Failure on registerStandardMBean(): ", mMBeanServer
139: .isRegistered(mAnMBeanName));
140: }
141:
142: /**
143: * Test the registerStandardMBean method with an already-registered MBean.
144: * An exception is expected.
145: * @throws Exception if an unexpected error occurs.
146: */
147: public void testRegisterStandardMBeanDuplicate() throws Exception {
148: // First register the MBean with replace=false. This should work
149: // because the MBean is not already registered.
150:
151: MBeanUtils.registerStandardMBean(AnMBean.class, mAnMBean,
152: mAnMBeanName, false);
153:
154: // Now try to register the MBean again with replace=false. This should
155: // fail because the MBean is already registered.
156:
157: try {
158: MBeanUtils.registerStandardMBean(AnMBean.class, mAnMBean,
159: mAnMBeanName, false);
160: fail("Expected exception not received");
161: } catch (javax.jbi.JBIException ex) {
162: // Verification
163: assertTrue(
164: "Incorrect exception received: " + ex.toString(),
165: (-1 < ex.getMessage().indexOf("JBIUT0011")));
166: }
167: }
168:
169: /**
170: * Test the registerStandardMBean method with a non-compliant MBean.
171: * @throws Exception if an unexpected error occurs.
172: */
173: public void testRegisterStandardMBeanNonCompliant()
174: throws Exception {
175: try {
176: MBeanUtils.registerStandardMBean(AnMBean.class, this ,
177: mAnMBeanName, true);
178: fail("Expected exception not received");
179: } catch (javax.jbi.JBIException ex) {
180: // Verification
181: assertTrue(
182: "Incorrect exception received: " + ex.toString(),
183: (-1 < ex.getMessage().indexOf("JBIUT0010")));
184: }
185: }
186:
187: /**
188: * Test the unregisterMBean method.
189: * @throws Exception if an unexpected error occurs.
190: */
191: public void testUnregisterMBean() throws Exception {
192: // First register an MBean and make sure it succeeds
193: MBeanUtils.registerStandardMBean(AnMBean.class, mAnMBean,
194: mAnMBeanName, false);
195: assertTrue("Failure on registerStandardMBean(): ", mMBeanServer
196: .isRegistered(mAnMBeanName));
197:
198: // Now unregister the MBean and make sure it succeeds
199: MBeanUtils.unregisterMBean(mAnMBeanName);
200: assertFalse("Failure on unregisterMBean(): ", mMBeanServer
201: .isRegistered(mAnMBeanName));
202: }
203:
204: }
|