001: /*
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */
008:
009: package test.javax.management.remote;
010:
011: import java.io.IOException;
012: import java.net.MalformedURLException;
013: import java.security.AccessController;
014: import java.util.Collections;
015: import java.util.HashSet;
016: import java.util.Map;
017: import java.util.Set;
018: import javax.management.Attribute;
019: import javax.management.AttributeList;
020: import javax.management.AttributeNotFoundException;
021: import javax.management.InstanceNotFoundException;
022: import javax.management.MBeanException;
023: import javax.management.MBeanInfo;
024: import javax.management.MBeanServer;
025: import javax.management.MBeanServerConnection;
026: import javax.management.MBeanServerFactory;
027: import javax.management.ObjectInstance;
028: import javax.management.ObjectName;
029: import javax.management.RuntimeMBeanException;
030: import javax.management.remote.JMXAuthenticator;
031: import javax.management.remote.JMXConnector;
032: import javax.management.remote.JMXConnectorFactory;
033: import javax.management.remote.JMXConnectorServer;
034: import javax.management.remote.JMXConnectorServerFactory;
035: import javax.management.remote.JMXPrincipal;
036: import javax.management.remote.JMXServiceURL;
037: import javax.security.auth.Subject;
038:
039: import test.MX4JTestCase;
040:
041: /**
042: * @version $Revision: 1.7 $
043: */
044: public abstract class JMXConnectorInvocationTestCase extends
045: MX4JTestCase {
046: private JMXConnectorServer connectorServer;
047:
048: public JMXConnectorInvocationTestCase(String name) {
049: super (name);
050: }
051:
052: public abstract JMXServiceURL createJMXConnectorServerAddress()
053: throws MalformedURLException;
054:
055: public abstract Map getEnvironment();
056:
057: private MBeanServerConnection getMBeanServerConnection(
058: MBeanServer server) throws IOException {
059: JMXServiceURL url = createJMXConnectorServerAddress();
060: connectorServer = JMXConnectorServerFactory
061: .newJMXConnectorServer(url, getEnvironment(), server);
062: connectorServer.start();
063: sleep(5000);
064: JMXConnector connector = JMXConnectorFactory.connect(
065: connectorServer.getAddress(), getEnvironment());
066: return connector.getMBeanServerConnection();
067: }
068:
069: protected void tearDown() throws Exception {
070: if (connectorServer != null)
071: connectorServer.stop();
072: sleep(5000);
073: }
074:
075: public void testCallToMBeanServerWithAttributeNotFoundException()
076: throws Exception {
077: MBeanServerConnection mbsc = getMBeanServerConnection(newMBeanServer());
078: try {
079: // The attribute does not exist
080: mbsc.getAttribute(new ObjectName(
081: "JMImplementation:type=MBeanServerDelegate"),
082: "dummy");
083: fail();
084: } catch (AttributeNotFoundException e) {
085: }
086: }
087:
088: public void testCallToMBeanServerWithInstanceNotFoundException()
089: throws Exception {
090: MBeanServerConnection mbsc = getMBeanServerConnection(newMBeanServer());
091: try {
092: // The mbean does not exist
093: mbsc.getAttribute(new ObjectName(":type=dummy"),
094: "ImplementationVersion");
095: fail();
096: } catch (InstanceNotFoundException x) {
097: }
098: }
099:
100: public void testCreateMBeanWith3Params() throws Exception {
101: MBeanServerConnection mbsc = getMBeanServerConnection(newMBeanServer());
102: ObjectName name = ObjectName.getInstance("", "test",
103: "invocation");
104: ObjectInstance instance = mbsc.createMBean(Support.class
105: .getName(), name, null);
106: assertNotNull(instance);
107: }
108:
109: public void testCreateMBeanWith5Params() throws Exception {
110: MBeanServerConnection mbsc = getMBeanServerConnection(newMBeanServer());
111: String value = "mx4j";
112: ObjectName name = ObjectName.getInstance("", "test",
113: "invocation");
114: ObjectInstance instance = mbsc.createMBean(Support.class
115: .getName(), name, null, new Object[] { value },
116: new String[] { String.class.getName() });
117: assertNotNull(instance);
118:
119: // Be sure the argument arrived to the MBean
120: String result = (String) mbsc.getAttribute(name, "Name");
121: assertEquals(result, value);
122: }
123:
124: public void testGetAttribute() throws Exception {
125: MBeanServerConnection mbsc = getMBeanServerConnection(newMBeanServer());
126: String value = "mx4j";
127: ObjectName name = ObjectName.getInstance("", "test",
128: "invocation");
129: ObjectInstance instance = mbsc.createMBean(Support.class
130: .getName(), name, null, new Object[] { value },
131: new String[] { String.class.getName() });
132: assertNotNull(instance);
133:
134: // Be sure the argument arrived to the MBean
135: String result = (String) mbsc.getAttribute(name, "Name");
136: assertEquals(result, value);
137: }
138:
139: public void testGetAttributes() throws Exception {
140: MBeanServerConnection mbsc = getMBeanServerConnection(newMBeanServer());
141: String value = "mx4j";
142: ObjectName name = ObjectName.getInstance("", "test",
143: "invocation");
144: ObjectInstance instance = mbsc.createMBean(Support.class
145: .getName(), name, null, new Object[] { value },
146: new String[] { String.class.getName() });
147: assertNotNull(instance);
148:
149: // Be sure the argument arrived to the MBean
150: String attribute = "Name";
151: String[] attributes = new String[] { attribute };
152: AttributeList result = mbsc.getAttributes(name, attributes);
153: assertNotNull(result);
154: assertEquals(result.size(), attributes.length);
155: assertEquals(((Attribute) result.get(0)).getName(), attribute);
156: assertEquals(((Attribute) result.get(0)).getValue(), value);
157: }
158:
159: public void testGetDefaultDomain() throws Exception {
160: String domain = "DOMAIN";
161: MBeanServer server = MBeanServerFactory.newMBeanServer(domain);
162: MBeanServerConnection mbsc = getMBeanServerConnection(server);
163:
164: String defaultDomain = mbsc.getDefaultDomain();
165: assertEquals(defaultDomain, domain);
166: }
167:
168: public void testGetDomains() throws Exception {
169: MBeanServerConnection mbsc = getMBeanServerConnection(newMBeanServer());
170:
171: String[] domains = mbsc.getDomains();
172: if (domains.length < 1)
173: fail();
174: }
175:
176: public void testGetMBeanCount() throws Exception {
177: MBeanServerConnection mbsc = getMBeanServerConnection(newMBeanServer());
178:
179: Integer count = mbsc.getMBeanCount();
180: if (count.intValue() < 1)
181: fail();
182: }
183:
184: public void testGetMBeanInfo() throws Exception {
185: MBeanServerConnection mbsc = getMBeanServerConnection(newMBeanServer());
186:
187: ObjectName name = ObjectName.getInstance("JMImplementation",
188: "type", "MBeanServerDelegate");
189: MBeanInfo info = mbsc.getMBeanInfo(name);
190: assertNotNull(info);
191: if (info.getAttributes().length < 1)
192: fail();
193: }
194:
195: public void testGetObjectInstance() throws Exception {
196: MBeanServerConnection mbsc = getMBeanServerConnection(newMBeanServer());
197: ObjectName name = ObjectName.getInstance("", "test",
198: "invocation");
199: ObjectInstance instance = mbsc.createMBean(Support.class
200: .getName(), name, null);
201: assertNotNull(instance);
202:
203: ObjectInstance result = mbsc.getObjectInstance(name);
204: assertNotNull(result);
205: assertEquals(result, instance);
206: }
207:
208: public void testInvoke() throws Exception {
209: MBeanServerConnection mbsc = getMBeanServerConnection(newMBeanServer());
210: ObjectName name = ObjectName.getInstance("", "test",
211: "invocation");
212: ObjectInstance instance = mbsc.createMBean(Support.class
213: .getName(), name, null);
214: assertNotNull(instance);
215:
216: String value = "value";
217: String result = (String) mbsc.invoke(name, "echo",
218: new Object[] { value }, new String[] { String.class
219: .getName() });
220: assertNotNull(result);
221: assertEquals(result, value);
222: }
223:
224: public void testInvokeThrowsCustomException() throws Exception {
225: MBeanServerConnection mbsc = getMBeanServerConnection(newMBeanServer());
226: ObjectName name = ObjectName.getInstance("", "test",
227: "invocation");
228: ObjectInstance instance = mbsc.createMBean(Support.class
229: .getName(), name, null);
230: assertNotNull(instance);
231:
232: try {
233: mbsc.invoke(name, "throwCustomException", null, null);
234: } catch (MBeanException x) {
235: Exception xx = x.getTargetException();
236: if (xx != null && xx.getClass() != SupportException.class)
237: fail();
238: }
239: }
240:
241: public void testInvokeThrowsRuntimeException() throws Exception {
242: MBeanServerConnection mbsc = getMBeanServerConnection(newMBeanServer());
243: ObjectName name = ObjectName.getInstance("", "test",
244: "invocation");
245: ObjectInstance instance = mbsc.createMBean(Support.class
246: .getName(), name, null);
247: assertNotNull(instance);
248:
249: try {
250: mbsc.invoke(name, "throwRuntimeException", null, null);
251: } catch (RuntimeMBeanException x) {
252: Exception xx = x.getTargetException();
253: if (xx != null
254: && xx.getClass() != IllegalArgumentException.class)
255: fail();
256: }
257: }
258:
259: public void testIsInstanceOf() throws Exception {
260: MBeanServerConnection mbsc = getMBeanServerConnection(newMBeanServer());
261: ObjectName name = ObjectName.getInstance("", "test",
262: "invocation");
263: ObjectInstance instance = mbsc.createMBean(Support.class
264: .getName(), name, null);
265: assertNotNull(instance);
266:
267: boolean isInstanceOf = mbsc.isInstanceOf(name, Support.class
268: .getName());
269: if (!isInstanceOf)
270: fail("Class");
271:
272: isInstanceOf = mbsc.isInstanceOf(name, SupportMBean.class
273: .getName());
274: if (!isInstanceOf)
275: fail("Interface");
276: }
277:
278: public void testIsRegistered() throws Exception {
279: MBeanServerConnection mbsc = getMBeanServerConnection(newMBeanServer());
280: ObjectName name = ObjectName.getInstance("", "test",
281: "invocation");
282: ObjectInstance instance = mbsc.createMBean(Support.class
283: .getName(), name, null);
284: assertNotNull(instance);
285:
286: boolean isRegistered = mbsc.isRegistered(name);
287: if (!isRegistered)
288: fail();
289: }
290:
291: public void testQueryMBeansNullNull() throws Exception {
292: MBeanServerConnection mbsc = getMBeanServerConnection(newMBeanServer());
293:
294: Set mbeans = mbsc.queryMBeans(null, null);
295: assertNotNull(mbeans);
296: if (mbeans.size() < 1)
297: fail();
298: }
299:
300: public void testQueryMBeansObjectNameNull() throws Exception {
301: MBeanServerConnection mbsc = getMBeanServerConnection(newMBeanServer());
302:
303: Set mbeans = mbsc.queryMBeans(ObjectName
304: .getInstance("JMImplementation:*"), null);
305: assertNotNull(mbeans);
306: if (mbeans.size() < 1)
307: fail();
308: }
309:
310: public void testQueryMBeansObjectNameQueryExp() throws Exception {
311: MBeanServerConnection mbsc = getMBeanServerConnection(newMBeanServer());
312:
313: Set mbeans = mbsc.queryMBeans(ObjectName
314: .getInstance("JMImplementation:*"), new ObjectName(
315: "*:type=MBeanServerDelegate"));
316: assertNotNull(mbeans);
317: if (mbeans.size() != 1)
318: fail();
319: }
320:
321: public void testSetAttribute() throws Exception {
322: MBeanServerConnection mbsc = getMBeanServerConnection(newMBeanServer());
323: ObjectName name = ObjectName.getInstance("", "test",
324: "invocation");
325: ObjectInstance instance = mbsc.createMBean(Support.class
326: .getName(), name, null);
327: assertNotNull(instance);
328:
329: String attribute = "Name";
330: String value = "mx4j";
331: mbsc.setAttribute(name, new Attribute(attribute, value));
332:
333: String result = (String) mbsc.getAttribute(name, attribute);
334: assertEquals(result, value);
335: }
336:
337: public void testSetAttributes() throws Exception {
338: MBeanServerConnection mbsc = getMBeanServerConnection(newMBeanServer());
339: ObjectName name = ObjectName.getInstance("", "test",
340: "invocation");
341: ObjectInstance instance = mbsc.createMBean(Support.class
342: .getName(), name, null);
343: assertNotNull(instance);
344:
345: String attribute = "Name";
346: String value = "mx4j";
347: AttributeList attributes = new AttributeList();
348: attributes.add(new Attribute(attribute, value));
349: AttributeList result = mbsc.setAttributes(name, attributes);
350: assertNotNull(result);
351: assertEquals(result, attributes);
352: }
353:
354: public void testUnregisterMBean() throws Exception {
355: MBeanServerConnection mbsc = getMBeanServerConnection(newMBeanServer());
356: ObjectName name = ObjectName.getInstance("", "test",
357: "invocation");
358: ObjectInstance instance = mbsc.createMBean(Support.class
359: .getName(), name, null);
360: assertNotNull(instance);
361:
362: mbsc.unregisterMBean(name);
363: if (mbsc.isRegistered(name))
364: fail();
365: }
366:
367: public void testAuthenticatedInvoke() throws Exception {
368: final String principalName = "authenticatedPrincipal";
369:
370: JMXServiceURL url = createJMXConnectorServerAddress();
371: Map serverEnv = getEnvironment();
372: serverEnv.put(JMXConnectorServer.AUTHENTICATOR,
373: new JMXAuthenticator() {
374: public Subject authenticate(Object credentials)
375: throws SecurityException {
376: Set principals = new HashSet();
377: principals.add(new JMXPrincipal(principalName));
378: return new Subject(true, principals,
379: Collections.EMPTY_SET,
380: Collections.EMPTY_SET);
381: }
382: });
383:
384: JMXConnectorServer cntorServer = null;
385: try {
386: cntorServer = JMXConnectorServerFactory
387: .newJMXConnectorServer(url, serverEnv,
388: newMBeanServer());
389: cntorServer.start();
390:
391: Map clientEnv = getEnvironment();
392: clientEnv.put(JMXConnector.CREDENTIALS, new String[0]);
393:
394: JMXConnector cntor = JMXConnectorFactory.connect(
395: cntorServer.getAddress(), clientEnv);
396: MBeanServerConnection mbsc = cntor
397: .getMBeanServerConnection();
398:
399: ObjectName name = ObjectName.getInstance("", "test",
400: "invocation");
401: ObjectInstance instance = mbsc.createMBean(Support.class
402: .getName(), name, null);
403: assertNotNull(instance);
404:
405: Boolean result = (Boolean) mbsc.invoke(name,
406: "authenticated", new Object[] { principalName },
407: new String[] { String.class.getName() });
408: assertTrue(result.booleanValue());
409: } finally {
410: if (cntorServer != null)
411: cntorServer.stop();
412: }
413: }
414:
415: public void testDelegatedInvoke() throws Exception {
416: JMXServiceURL url = createJMXConnectorServerAddress();
417: Map serverEnv = getEnvironment();
418: serverEnv.put(JMXConnectorServer.AUTHENTICATOR,
419: new JMXAuthenticator() {
420: public Subject authenticate(Object credentials)
421: throws SecurityException {
422: Set principals = new HashSet();
423: principals.add(new JMXPrincipal(
424: "authenticatedPrincipal"));
425: return new Subject(true, principals,
426: Collections.EMPTY_SET,
427: Collections.EMPTY_SET);
428: }
429: });
430: JMXConnectorServer cntorServer = null;
431: try {
432: cntorServer = JMXConnectorServerFactory
433: .newJMXConnectorServer(url, serverEnv,
434: newMBeanServer());
435: cntorServer.start();
436:
437: Map clientEnv = getEnvironment();
438: clientEnv.put(JMXConnector.CREDENTIALS, new String[0]);
439:
440: JMXConnector cntor = JMXConnectorFactory.connect(
441: cntorServer.getAddress(), clientEnv);
442: Set principals = new HashSet();
443: String delegatedName = "delegatedPrincipal";
444: principals.add(new JMXPrincipal(delegatedName));
445: Subject delegate = new Subject(true, principals,
446: Collections.EMPTY_SET, Collections.EMPTY_SET);
447: MBeanServerConnection mbsc = cntor
448: .getMBeanServerConnection(delegate);
449:
450: ObjectName name = ObjectName.getInstance("", "test",
451: "invocation");
452: ObjectInstance instance = mbsc.createMBean(Support.class
453: .getName(), name, null);
454: assertNotNull(instance);
455:
456: Boolean result = (Boolean) mbsc.invoke(name, "delegated",
457: new Object[] { delegatedName },
458: new String[] { String.class.getName() });
459: assertTrue(result.booleanValue());
460: } finally {
461: if (cntorServer != null)
462: cntorServer.stop();
463: }
464: }
465:
466: public static class SupportException extends Exception {
467: }
468:
469: public interface SupportMBean {
470: public String getName();
471:
472: public void setName(String name);
473:
474: public void throwCustomException() throws SupportException;
475:
476: public void throwRuntimeException()
477: throws IllegalArgumentException;
478:
479: public String echo(String argument);
480:
481: public boolean authenticated(String name);
482:
483: public boolean delegated(String name);
484: }
485:
486: public static class Support implements SupportMBean {
487: private String name;
488:
489: public Support() {
490: }
491:
492: public Support(String name) {
493: this .name = name;
494: }
495:
496: public String getName() {
497: return name;
498: }
499:
500: public void setName(String name) {
501: this .name = name;
502: }
503:
504: public void throwCustomException() throws SupportException {
505: throw new SupportException();
506: }
507:
508: public void throwRuntimeException()
509: throws IllegalArgumentException {
510: throw new IllegalArgumentException();
511: }
512:
513: public String echo(String argument) {
514: return argument;
515: }
516:
517: public boolean authenticated(String name) {
518: Subject subject = Subject.getSubject(AccessController
519: .getContext());
520: if (subject == null)
521: return false;
522: Set principals = subject.getPrincipals();
523: return principals.contains(new JMXPrincipal(name));
524: }
525:
526: public boolean delegated(String name) {
527: Subject subject = Subject.getSubject(AccessController
528: .getContext());
529: if (subject == null)
530: return false;
531: Set principals = subject.getPrincipals();
532: return principals.contains(new JMXPrincipal(name));
533: }
534: }
535: }
|