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.loading;
010:
011: import java.lang.reflect.Method;
012: import javax.management.MBeanServer;
013: import javax.management.MBeanServerFactory;
014: import javax.management.ObjectName;
015: import javax.management.loading.ClassLoaderRepository;
016: import javax.management.loading.MLet;
017:
018: import junit.framework.TestCase;
019:
020: /**
021: * @version $Revision: 1.9 $
022: */
023: public class ClassLoaderRepositoryTest extends TestCase {
024: public ClassLoaderRepositoryTest(String s) {
025: super (s);
026: }
027:
028: public void testSingleMBeanServer() throws Exception {
029: MBeanServer server = MBeanServerFactory.newMBeanServer();
030: ClassLoaderRepository clr = server.getClassLoaderRepository();
031: Method method = clr.getClass().getDeclaredMethod("getSize",
032: new Class[0]);
033: method.setAccessible(true);
034: Integer size = (Integer) method.invoke(clr, new Object[0]);
035: int initial = size.intValue();
036:
037: ObjectName name1 = new ObjectName(":name=mlet1");
038: ObjectName name2 = new ObjectName(":name=mlet2");
039:
040: MLet mlet1 = new MLet();
041: server.registerMBean(mlet1, name1);
042:
043: // Check that the mlet was registered as classloader
044: size = (Integer) method.invoke(clr, new Object[0]);
045: if (size.intValue() != initial + 1)
046: fail("ClassLoader not registered in ClassLoaderRepository");
047:
048: // Add another classloader
049: MLet mlet2 = new MLet();
050: server.registerMBean(mlet2, name2);
051:
052: size = (Integer) method.invoke(clr, new Object[0]);
053: if (size.intValue() != initial + 2)
054: fail("ClassLoader not registered in ClassLoaderRepository");
055: }
056:
057: public void testMultipleMBeanServer() throws Exception {
058: MBeanServer server1 = MBeanServerFactory
059: .newMBeanServer("domain1");
060: MBeanServer server2 = MBeanServerFactory
061: .newMBeanServer("domain2");
062: ClassLoaderRepository clr1 = server1.getClassLoaderRepository();
063: ClassLoaderRepository clr2 = server2.getClassLoaderRepository();
064: Method method = clr1.getClass().getDeclaredMethod("getSize",
065: new Class[0]);
066: method.setAccessible(true);
067: Integer size1 = (Integer) method.invoke(clr1, new Object[0]);
068: int initial1 = size1.intValue();
069: Integer size2 = (Integer) method.invoke(clr2, new Object[0]);
070: int initial2 = size2.intValue();
071:
072: ObjectName name1 = new ObjectName(":name=mlet1");
073: ObjectName name2 = new ObjectName(":name=mlet2");
074:
075: MLet mlet1 = new MLet();
076: server1.registerMBean(mlet1, name1);
077:
078: MLet mlet2 = new MLet();
079: server2.registerMBean(mlet2, name2);
080:
081: // Check that the mlet was registered as classloader
082: size1 = (Integer) method.invoke(clr1, new Object[0]);
083: if (size1.intValue() != initial1 + 1)
084: fail("ClassLoader not registered in ClassLoaderRepository");
085:
086: size2 = (Integer) method.invoke(clr2, new Object[0]);
087: if (size2.intValue() != initial2 + 1)
088: fail("ClassLoader not registered in ClassLoaderRepository");
089: }
090:
091: public void testMultipleRegistrationOfSameClassLoader()
092: throws Exception {
093: MBeanServer server = MBeanServerFactory.newMBeanServer();
094: ClassLoaderRepository clr = server.getClassLoaderRepository();
095: Method method = clr.getClass().getDeclaredMethod("getSize",
096: new Class[0]);
097: method.setAccessible(true);
098: Integer size = (Integer) method.invoke(clr, new Object[0]);
099: int initial = size.intValue();
100:
101: ObjectName name1 = new ObjectName(":name=mlet1");
102: ObjectName name2 = new ObjectName(":name=mlet2");
103:
104: MLet mlet1 = new MLet();
105: server.registerMBean(mlet1, name1);
106: server.registerMBean(mlet1, name2);
107:
108: // Check that the mlet was registered only once
109: size = (Integer) method.invoke(clr, new Object[0]);
110: if (size.intValue() != initial + 1)
111: fail("Same ClassLoader was registered more than once");
112: }
113: }
|