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 java.util.ArrayList;
025:
026: import javax.management.MBeanNotificationInfo;
027: import javax.management.MBeanServer;
028: import javax.management.MBeanServerFactory;
029: import javax.management.MBeanServerInvocationHandler;
030: import javax.management.Notification;
031: import javax.management.NotificationEmitter;
032: import javax.management.NotificationFilterSupport;
033: import javax.management.NotificationListener;
034: import javax.management.ObjectName;
035:
036: import junit.framework.TestCase;
037: import test.compliance.server.support.BroadcasterInvocationHandlerTest;
038: import test.compliance.server.support.EmitterInvocationHandlerTest;
039: import test.compliance.server.support.InvocationHandlerTest;
040: import test.compliance.server.support.InvocationHandlerTestMBean;
041: import test.compliance.server.support.ObjectInvocationHandlerTest;
042:
043: /**
044: * Tests the MBeanServerInvocationHandler
045: *
046: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
047: */
048: public class MBeanServerInvocationHandlerTestCase extends TestCase
049: implements NotificationListener {
050: // Attributes ----------------------------------------------------------------
051:
052: private ObjectName invocationHandlerTestName;
053:
054: private ArrayList messages = new ArrayList();
055:
056: // Constructor ---------------------------------------------------------------
057:
058: /**
059: * Construct the test
060: */
061: public MBeanServerInvocationHandlerTestCase(String s) {
062: super (s);
063:
064: try {
065: invocationHandlerTestName = new ObjectName(
066: "MBeanServerInvocationHandlerTestCase:type=InvocationHandlerTest");
067: } catch (Exception e) {
068: throw new RuntimeException(e.toString());
069: }
070: }
071:
072: // Tests ---------------------------------------------------------------------
073:
074: public void testConstructor() throws Exception {
075: MBeanServer server = MBeanServerFactory.newMBeanServer();
076: }
077:
078: public void testGetter() throws Exception {
079: MBeanServer server = MBeanServerFactory.newMBeanServer();
080: InvocationHandlerTest test = new InvocationHandlerTest();
081: server.registerMBean(test, invocationHandlerTestName);
082:
083: InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler
084: .newProxyInstance(server, invocationHandlerTestName,
085: InvocationHandlerTestMBean.class, false);
086: assertEquals("Attribute", proxy.getAttribute());
087: }
088:
089: public void testSetter() throws Exception {
090: MBeanServer server = MBeanServerFactory.newMBeanServer();
091: InvocationHandlerTest test = new InvocationHandlerTest();
092: server.registerMBean(test, invocationHandlerTestName);
093: InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler
094: .newProxyInstance(server, invocationHandlerTestName,
095: InvocationHandlerTestMBean.class, false);
096:
097: proxy.setAttribute("Changed");
098: assertEquals("Changed", test.getAttribute());
099: }
100:
101: public void testGetterPrimitiveBoolean() throws Exception {
102: MBeanServer server = MBeanServerFactory.newMBeanServer();
103: InvocationHandlerTest test = new InvocationHandlerTest();
104: server.registerMBean(test, invocationHandlerTestName);
105:
106: InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler
107: .newProxyInstance(server, invocationHandlerTestName,
108: InvocationHandlerTestMBean.class, false);
109:
110: assertEquals(false, test.isIsPrimitive());
111: test.setIsPrimitive(true);
112:
113: assertEquals(true, proxy.isIsPrimitive());
114: }
115:
116: public void testSetterPrimitiveBoolean() throws Exception {
117: MBeanServer server = MBeanServerFactory.newMBeanServer();
118: InvocationHandlerTest test = new InvocationHandlerTest();
119: server.registerMBean(test, invocationHandlerTestName);
120: InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler
121: .newProxyInstance(server, invocationHandlerTestName,
122: InvocationHandlerTestMBean.class, false);
123:
124: assertEquals(false, test.isIsPrimitive());
125: proxy.setIsPrimitive(true);
126:
127: assertEquals(true, test.isIsPrimitive());
128: }
129:
130: public void testGetterTypeBoolean() throws Exception {
131: MBeanServer server = MBeanServerFactory.newMBeanServer();
132: InvocationHandlerTest test = new InvocationHandlerTest();
133: server.registerMBean(test, invocationHandlerTestName);
134:
135: InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler
136: .newProxyInstance(server, invocationHandlerTestName,
137: InvocationHandlerTestMBean.class, false);
138:
139: assertEquals(null, test.isIsType());
140: test.setIsType(new Boolean(true));
141:
142: assertEquals(true, proxy.isIsType().booleanValue());
143: }
144:
145: public void testSetterTypeBoolean() throws Exception {
146: MBeanServer server = MBeanServerFactory.newMBeanServer();
147: InvocationHandlerTest test = new InvocationHandlerTest();
148: server.registerMBean(test, invocationHandlerTestName);
149: InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler
150: .newProxyInstance(server, invocationHandlerTestName,
151: InvocationHandlerTestMBean.class, false);
152:
153: assertEquals(null, test.isIsType());
154: proxy.setIsType(new Boolean(true));
155:
156: assertEquals(true, test.isIsType().booleanValue());
157: }
158:
159: public void testInvokeNoArgsNoReturn() throws Exception {
160: MBeanServer server = MBeanServerFactory.newMBeanServer();
161: InvocationHandlerTest test = new InvocationHandlerTest();
162: server.registerMBean(test, invocationHandlerTestName);
163: InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler
164: .newProxyInstance(server, invocationHandlerTestName,
165: InvocationHandlerTestMBean.class, false);
166:
167: proxy.invokeNoArgsNoReturn();
168: assertTrue(test.invokeNoArgsNoReturnInvoked);
169: }
170:
171: public void testInvokeNoArgs() throws Exception {
172: MBeanServer server = MBeanServerFactory.newMBeanServer();
173: InvocationHandlerTest test = new InvocationHandlerTest();
174: server.registerMBean(test, invocationHandlerTestName);
175: InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler
176: .newProxyInstance(server, invocationHandlerTestName,
177: InvocationHandlerTestMBean.class, false);
178:
179: assertEquals("invokeNoArgs", proxy.invokeNoArgs());
180: }
181:
182: public void testInvoke() throws Exception {
183: MBeanServer server = MBeanServerFactory.newMBeanServer();
184: InvocationHandlerTest test = new InvocationHandlerTest();
185: server.registerMBean(test, invocationHandlerTestName);
186: InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler
187: .newProxyInstance(server, invocationHandlerTestName,
188: InvocationHandlerTestMBean.class, false);
189:
190: assertEquals("parameter", proxy.invoke("parameter"));
191: }
192:
193: public void testInvokeMixedParameters() throws Exception {
194: MBeanServer server = MBeanServerFactory.newMBeanServer();
195: InvocationHandlerTest test = new InvocationHandlerTest();
196: server.registerMBean(test, invocationHandlerTestName);
197: InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler
198: .newProxyInstance(server, invocationHandlerTestName,
199: InvocationHandlerTestMBean.class, false);
200:
201: Integer parameter = new Integer(20);
202: assertEquals(parameter, proxy.invokeMixedParameters(
203: "parameter", 10, parameter));
204: }
205:
206: public void testNotificationEmitterAdd() throws Exception {
207: MBeanServer server = MBeanServerFactory.newMBeanServer();
208: EmitterInvocationHandlerTest test = new EmitterInvocationHandlerTest();
209: server.registerMBean(test, invocationHandlerTestName);
210: NotificationEmitter proxy = (NotificationEmitter) MBeanServerInvocationHandler
211: .newProxyInstance(server, invocationHandlerTestName,
212: InvocationHandlerTestMBean.class, true);
213:
214: proxy.addNotificationListener(this , null, null);
215:
216: messages.clear();
217: test.sendNotification();
218: assertTrue(messages.size() == 1);
219: }
220:
221: public void testNotificationEmitterRemove() throws Exception {
222: MBeanServer server = MBeanServerFactory.newMBeanServer();
223: EmitterInvocationHandlerTest test = new EmitterInvocationHandlerTest();
224: server.registerMBean(test, invocationHandlerTestName);
225: NotificationEmitter proxy = (NotificationEmitter) MBeanServerInvocationHandler
226: .newProxyInstance(server, invocationHandlerTestName,
227: InvocationHandlerTestMBean.class, true);
228:
229: proxy.addNotificationListener(this , null, null);
230:
231: messages.clear();
232: test.sendNotification();
233: assertTrue(messages.size() == 1);
234:
235: proxy.removeNotificationListener(this );
236:
237: messages.clear();
238: test.sendNotification();
239: assertTrue(messages.size() == 0);
240: }
241:
242: public void testNotificationEmitterRemoveTriplet() throws Exception {
243: MBeanServer server = MBeanServerFactory.newMBeanServer();
244: EmitterInvocationHandlerTest test = new EmitterInvocationHandlerTest();
245: server.registerMBean(test, invocationHandlerTestName);
246: NotificationEmitter proxy = (NotificationEmitter) MBeanServerInvocationHandler
247: .newProxyInstance(server, invocationHandlerTestName,
248: InvocationHandlerTestMBean.class, true);
249:
250: NotificationFilterSupport filter = new NotificationFilterSupport();
251: filter.enableType("test");
252: Object handback = new Object();
253: proxy.addNotificationListener(this , filter, handback);
254:
255: messages.clear();
256: test.sendNotification();
257: assertTrue(messages.size() == 1);
258:
259: proxy.removeNotificationListener(this , filter, handback);
260:
261: messages.clear();
262: test.sendNotification();
263: assertTrue(messages.size() == 0);
264: }
265:
266: public void testNotificationEmitterRemoveTripletFailsOnBroadcaster()
267: throws Exception {
268: MBeanServer server = MBeanServerFactory.newMBeanServer();
269: BroadcasterInvocationHandlerTest test = new BroadcasterInvocationHandlerTest();
270: server.registerMBean(test, invocationHandlerTestName);
271: NotificationEmitter proxy = (NotificationEmitter) MBeanServerInvocationHandler
272: .newProxyInstance(server, invocationHandlerTestName,
273: InvocationHandlerTestMBean.class, true);
274:
275: NotificationFilterSupport filter = new NotificationFilterSupport();
276: filter.enableType("test");
277: Object handback = new Object();
278: proxy.addNotificationListener(this , filter, handback);
279:
280: messages.clear();
281: test.sendNotification();
282: assertTrue(messages.size() == 1);
283:
284: try {
285: proxy.removeNotificationListener(this , filter, handback);
286: fail("removeNotificationListener(NotificationListener, NotificationFilter, Object) "
287: + "should not work for a broadcaster");
288: } catch (Exception ignored) {
289: }
290: }
291:
292: public void testGetNotificationInfo() throws Exception {
293: MBeanServer server = MBeanServerFactory.newMBeanServer();
294: EmitterInvocationHandlerTest test = new EmitterInvocationHandlerTest();
295: server.registerMBean(test, invocationHandlerTestName);
296: NotificationEmitter proxy = (NotificationEmitter) MBeanServerInvocationHandler
297: .newProxyInstance(server, invocationHandlerTestName,
298: InvocationHandlerTestMBean.class, true);
299:
300: MBeanNotificationInfo[] info = proxy.getNotificationInfo();
301: assertEquals("test", info[0].getNotifTypes()[0]);
302: }
303:
304: public void testToString() throws Exception {
305: MBeanServer server = MBeanServerFactory.newMBeanServer();
306: ObjectInvocationHandlerTest test = new ObjectInvocationHandlerTest();
307: server.registerMBean(test, invocationHandlerTestName);
308: InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler
309: .newProxyInstance(server, invocationHandlerTestName,
310: InvocationHandlerTestMBean.class, true);
311:
312: assertEquals("TOSTRING", proxy.toString());
313: }
314:
315: public void testToStringFailsWhenNotExposed() throws Exception {
316: MBeanServer server = MBeanServerFactory.newMBeanServer();
317: InvocationHandlerTest test = new InvocationHandlerTest();
318: server.registerMBean(test, invocationHandlerTestName);
319: InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler
320: .newProxyInstance(server, invocationHandlerTestName,
321: InvocationHandlerTestMBean.class, true);
322:
323: try {
324: proxy.toString();
325: fail("toString() should not work when it is not exposed for management");
326: } catch (Exception ignored) {
327: }
328: }
329:
330: public void testEquals() throws Exception {
331: MBeanServer server = MBeanServerFactory.newMBeanServer();
332: ObjectInvocationHandlerTest test = new ObjectInvocationHandlerTest();
333: server.registerMBean(test, invocationHandlerTestName);
334: InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler
335: .newProxyInstance(server, invocationHandlerTestName,
336: InvocationHandlerTestMBean.class, true);
337:
338: assertTrue(proxy.equals(new Object()));
339: }
340:
341: public void testEqualsFailsWhenNotExposed() throws Exception {
342: MBeanServer server = MBeanServerFactory.newMBeanServer();
343: InvocationHandlerTest test = new InvocationHandlerTest();
344: server.registerMBean(test, invocationHandlerTestName);
345: InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler
346: .newProxyInstance(server, invocationHandlerTestName,
347: InvocationHandlerTestMBean.class, true);
348:
349: try {
350: proxy.equals(new Object());
351: fail("equals(Object) should not work when it is not exposed for management");
352: } catch (Exception ignored) {
353: }
354: }
355:
356: public void testHashCode() throws Exception {
357: MBeanServer server = MBeanServerFactory.newMBeanServer();
358: ObjectInvocationHandlerTest test = new ObjectInvocationHandlerTest();
359: server.registerMBean(test, invocationHandlerTestName);
360: InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler
361: .newProxyInstance(server, invocationHandlerTestName,
362: InvocationHandlerTestMBean.class, true);
363:
364: assertEquals(1234, proxy.hashCode());
365: }
366:
367: public void testHashCodeFailsWhenNotExposed() throws Exception {
368: MBeanServer server = MBeanServerFactory.newMBeanServer();
369: InvocationHandlerTest test = new InvocationHandlerTest();
370: server.registerMBean(test, invocationHandlerTestName);
371: InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler
372: .newProxyInstance(server, invocationHandlerTestName,
373: InvocationHandlerTestMBean.class, true);
374:
375: try {
376: proxy.hashCode();
377: fail("hashCode() should not work when it is not exposed for management");
378: } catch (Exception ignored) {
379: }
380: }
381:
382: // Notification Listener -----------------------------------------------------
383:
384: public void handleNotification(Notification notification,
385: Object handback) {
386: messages.add(notification);
387: }
388: }
|