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: * @(#)TestEventNotifier.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029:
030: package com.sun.jbi.management.facade;
031:
032: import com.sun.jbi.EnvironmentContext;
033: import com.sun.jbi.platform.PlatformContext;
034: import com.sun.jbi.management.EventNotifierMBean;
035: import com.sun.jbi.management.system.ScaffoldedEnvironmentContext;
036: import com.sun.jbi.management.system.ScaffoldPlatformContext;
037:
038: import javax.management.MBeanNotificationInfo;
039: import javax.management.MBeanServer;
040: import javax.management.Notification;
041: import javax.management.ObjectName;
042: import javax.management.StandardMBean;
043:
044: /**
045: * Tests for EventNotifier.
046: *
047: * @author Mark S White
048: */
049: public class TestEventNotifier extends junit.framework.TestCase {
050: /**
051: * Current test name.
052: */
053: private String mTestName;
054:
055: /**
056: * Local handle to the EnvironmentContext
057: */
058: private EnvironmentContext mContext;
059:
060: /**
061: * Local handle to the PlatformContext
062: */
063: private PlatformContext mPlatform;
064:
065: /**
066: * Instance of EventNotifier.
067: */
068: private EventNotifier mEventNotifier;
069:
070: /**
071: * MBean object name for EventNotifier
072: */
073: private ObjectName mMBeanName;
074:
075: /**
076: * The constructor for this testcase, forwards the test name to
077: * the jUnit TestCase base class.
078: * @param aTestName String with the name of this test.
079: */
080: public TestEventNotifier(String aTestName) {
081: super (aTestName);
082: }
083:
084: /**
085: * Setup for the test. This creates the EventNotifier instance
086: * and other objects needed for the tests.
087: * @throws Exception when set up fails for any reason.
088: */
089: public void setUp() throws Exception {
090: super .setUp();
091: System.err.println("***** START of test " + mTestName);
092:
093: // Set up scaffolded Environment and Platform contexts
094:
095: mContext = new ScaffoldedEnvironmentContext();
096: mPlatform = mContext.getPlatformContext();
097:
098: // Set up EventNotifier instance and register it as an MBean
099:
100: mEventNotifier = new EventNotifier(mContext, "domain");
101: StandardMBean mbean = null;
102: try {
103: mbean = new StandardMBean(mEventNotifier,
104: EventNotifierMBean.class);
105: } catch (javax.management.NotCompliantMBeanException ex) {
106: fail("Unable to create EventNotifier MBean: " + ex);
107: }
108: try {
109: mMBeanName = new ObjectName("com.sun.jbi:Type=Notification");
110: } catch (javax.management.MalformedObjectNameException ex) {
111: fail("Unable to create ObjectName for EventNotifier MBean: "
112: + ex);
113: }
114: MBeanServer mbs = mContext.getMBeanServer();
115: try {
116: mbs.registerMBean(mbean, mMBeanName);
117: } catch (Throwable ex) {
118: fail("Unable to register EventNotifier MBean: " + ex);
119: }
120: }
121:
122: /**
123: * Cleanup for the test.
124: * @throws Exception when tearDown fails for any reason.
125: */
126: public void tearDown() throws Exception {
127: super .tearDown();
128: MBeanServer mbs = mContext.getMBeanServer();
129: try {
130: mbs.unregisterMBean(mMBeanName);
131: } catch (javax.management.InstanceNotFoundException infEx) {
132: ; // Just ignore this error
133: } catch (javax.management.MBeanRegistrationException mbrEx) {
134: fail("Unable to unregister EventNotifier MBean: " + mbrEx);
135: }
136: System.err.println("***** END of test " + mTestName);
137: }
138:
139: // ============================= test methods ================================
140:
141: /**
142: * Tests getNotificationInfo.
143: * @throws Exception if an unexpected error occurs.
144: */
145: public void testGetNotificationInfo() {
146: // Get the notification info for this MBean
147:
148: MBeanNotificationInfo[] info = mEventNotifier
149: .getNotificationInfo();
150: String[] types = info[0].getNotifTypes();
151: String name = info[0].getName();
152: String desc = info[0].getDescription();
153:
154: // Verify that the notification info is correct
155:
156: assertEquals(
157: "Got more than one MBeanNotificationInfo, expected one: ",
158: 1, info.length);
159: assertEquals("Got wrong number of notification types: ",
160: mEventNotifier.NOTIFICATION_TYPES.length, types.length);
161: assertEquals("Got wrong class name: ",
162: mEventNotifier.NOTIFICATION_CLASS_NAME, name);
163: assertEquals("Got wrong description: ",
164: mEventNotifier.NOTIFICATION_DESCRIPTION, desc);
165: }
166:
167: /**
168: * Tests enableNotifications/disableNotifications.
169: * @throws Exception if an unexpected error occurs.
170: */
171: public void testEnableDisableNotifications() {
172: // Notifications default to enabled. Attempt to enable them again,
173: // this should return a false result as nothing changed.
174:
175: assertFalse("Error, notifications should already be enabled",
176: mEventNotifier.enableNotifications());
177:
178: // Now disable notifications, this should return true.
179:
180: assertTrue("Error, notifications should have been disabled",
181: mEventNotifier.disableNotifications());
182:
183: // Now disable notifications again, this should return false.
184:
185: assertFalse("Error, notifications should already be disabled",
186: mEventNotifier.disableNotifications());
187:
188: // Now enable notifications again, this should return true.
189:
190: assertTrue("Error, notifications should have been enabled",
191: mEventNotifier.enableNotifications());
192: }
193:
194: /**
195: * Tests instanceStarted / instanceStopped.
196: * @throws Exception if an unexpected error occurs.
197: */
198: public void testInstanceStartedStopped() {
199: String instance = "server";
200:
201: // Attempt instanceStopped with no instance active. This should
202: // return false.
203:
204: assertFalse("Error, instance should not be defined",
205: mEventNotifier.instanceStopped(instance, mMBeanName));
206:
207: // Attempt instanceStarted with no instance active. This should
208: // return true.
209:
210: assertTrue("Error, instance should have been added",
211: mEventNotifier.instanceStarted(instance, mMBeanName));
212:
213: // Attempt instanceStarted with the instance already active. This should
214: // return false.
215:
216: assertFalse("Error, instance should already be active",
217: mEventNotifier.instanceStarted(instance, mMBeanName));
218:
219: // Attempt instanceStopped with the instance active. This should
220: // return true.
221:
222: assertTrue("Error, instance should have been removed",
223: mEventNotifier.instanceStopped(instance, mMBeanName));
224: }
225:
226: /**
227: * Tests handleNotification.
228: * @throws Exception if an unexpected error occurs.
229: */
230: public void testHandleNotification() {
231: String type = "com.sun.jbi.event";
232: long seq = 12345;
233: String msg = "This is a notification";
234: Notification notif = new Notification(type, mMBeanName, seq,
235: msg);
236: String handBack = "server";
237:
238: // Call the handler. There's really no way to check for a failure
239: // other than the fact that no exception occurs.
240: mEventNotifier.handleNotification(notif, handBack);
241: }
242:
243: }
|