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.net.MalformedURLException;
012: import java.util.HashMap;
013: import javax.management.remote.JMXConnector;
014: import javax.management.remote.JMXConnectorFactory;
015: import javax.management.remote.JMXProviderException;
016: import javax.management.remote.JMXServiceURL;
017:
018: import test.MX4JTestCase;
019:
020: /**
021: * @version $Revision: 1.9 $
022: */
023: public class JMXConnectorFactoryTest extends MX4JTestCase {
024: public JMXConnectorFactoryTest(String s) {
025: super (s);
026: }
027:
028: public void testInvalidURLs() throws Exception {
029: try {
030: JMXConnectorFactory.connect(null);
031: fail();
032: } catch (NullPointerException x) {
033: }
034: }
035:
036: public void testInvalidPackages() throws Exception {
037: HashMap env = new HashMap();
038: JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://host");
039:
040: // Only Strings
041: env.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,
042: new Object());
043: try {
044: JMXConnectorFactory.newJMXConnector(url, env);
045: fail("Only Strings can be specified as provider packages");
046: } catch (JMXProviderException x) {
047: }
048:
049: // Empty not allowed
050: env.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, "");
051: try {
052: JMXConnectorFactory.newJMXConnector(url, env);
053: fail("Provider package string cannot be empty");
054: } catch (JMXProviderException x) {
055: }
056:
057: // Empty not allowed
058: env.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,
059: "dummy| |dummy");
060: try {
061: JMXConnectorFactory.newJMXConnector(url, env);
062: fail("Provider package string cannot contain an empty string");
063: } catch (JMXProviderException x) {
064: }
065: }
066:
067: public void testInvalidProtocol() throws Exception {
068: JMXServiceURL url = new JMXServiceURL(
069: "service:jmx:dummy://host");
070: try {
071: JMXConnectorFactory.newJMXConnector(url, null);
072: fail();
073: } catch (MalformedURLException x) {
074: }
075: }
076:
077: public void testInvalidClassLoader() throws Exception {
078: HashMap env = new HashMap();
079: JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://host");
080:
081: env.put(JMXConnectorFactory.PROTOCOL_PROVIDER_CLASS_LOADER,
082: new Object());
083: try {
084: JMXConnectorFactory.newJMXConnector(url, env);
085: fail();
086: } catch (IllegalArgumentException x) {
087: }
088: }
089:
090: public void testLoadProviderWithProvidedClassLoader()
091: throws Exception {
092: HashMap env = new HashMap();
093: JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://host");
094: ClassLoader old = Thread.currentThread()
095: .getContextClassLoader();
096: try {
097: Thread.currentThread().setContextClassLoader(
098: getClass().getClassLoader().getParent());
099: env.put(JMXConnectorFactory.PROTOCOL_PROVIDER_CLASS_LOADER,
100: getClass().getClassLoader());
101: JMXConnector connector = JMXConnectorFactory
102: .newJMXConnector(url, env);
103: assertNotNull(connector);
104: } finally {
105: Thread.currentThread().setContextClassLoader(old);
106: }
107: }
108:
109: public void testRMIProvider() throws Exception {
110: JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://host");
111: JMXConnector connector = JMXConnectorFactory.newJMXConnector(
112: url, null);
113: assertNotNull(connector);
114: }
115: }
|