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.implementation.loading;
023:
024: import java.net.URL;
025:
026: import javax.management.MBeanException;
027: import javax.management.MBeanServer;
028: import javax.management.MBeanServerFactory;
029: import javax.management.ObjectName;
030: import javax.management.ReflectionException;
031: import javax.management.loading.DefaultLoaderRepository;
032: import javax.management.loading.MLet;
033:
034: import junit.framework.TestCase;
035:
036: import org.jboss.mx.loading.LoaderRepository;
037: import org.jboss.mx.loading.RepositoryClassLoader;
038: import org.jboss.mx.server.ServerConstants;
039: import org.jboss.mx.util.AgentID;
040:
041: public class LoaderRepositoryTEST extends TestCase implements
042: ServerConstants {
043: public LoaderRepositoryTEST(String s) {
044: super (s);
045: }
046:
047: public void testRemoveDuplicateURL() throws Exception {
048: // NOTE:
049: // the urls used here are relative to the location of the build.xml
050: final URL url = new URL(
051: "file:./output/etc/test/implementation/loading/MyMBeans.jar");
052:
053: // Retrieve the loader repository
054: MBeanServer server = MBeanServerFactory.createMBeanServer();
055: LoaderRepository lr = (LoaderRepository) server
056: .getClassLoaderRepository();
057:
058: // Should not be able to load the class
059: try {
060: lr.loadClass("test.implementation.loading.support.Trivial");
061: fail("test.implementation.loading.support.Trivial is already visible");
062: } catch (ClassNotFoundException expected) {
063: }
064:
065: // Add the URL to the repository twice
066: RepositoryClassLoader ucl1 = lr.newClassLoader(url, true);
067: RepositoryClassLoader ucl2 = lr.newClassLoader(url, true);
068:
069: // Should be able to load the class
070: lr.loadClass("test.implementation.loading.support.Trivial");
071:
072: // Remove one
073: ucl1.unregister();
074:
075: // Should still be able to load the class
076: lr.loadClass("test.implementation.loading.support.Trivial");
077:
078: // Remove the other
079: ucl2.unregister();
080: }
081:
082: public void testClassConflictBetweenMLets() throws Exception {
083: // NOTE:
084: // the urls used here are relative to the location of the build.xml
085:
086: // make sure the classes are loaded from mlet, not system cl
087: String[] classes = {
088: "test.implementation.loading.support.Start",
089: "test.implementation.loading.support.StartMBean",
090: "test.implementation.loading.support.Target",
091: "test.implementation.loading.support.TargetMBean",
092: "test.implementation.loading.support.AClass" };
093:
094: for (int i = 0; i < classes.length; ++i) {
095: try {
096: DefaultLoaderRepository.loadClass(classes[i]);
097:
098: fail("class " + classes[i]
099: + " was already found in CL repository.");
100: } catch (ClassNotFoundException e) {
101: // expected
102: }
103: }
104:
105: try {
106: MBeanServer server = MBeanServerFactory.createMBeanServer();
107: MLet mlet1 = new MLet();
108: MLet mlet2 = new MLet();
109: ObjectName m1Name = new ObjectName(":name=mlet1");
110: ObjectName m2Name = new ObjectName(":name=mlet2");
111:
112: server.registerMBean(mlet1, m1Name);
113: server.registerMBean(mlet2, m2Name);
114:
115: server
116: .invoke(
117: m1Name,
118: "getMBeansFromURL",
119: new Object[] { "file:./output/etc/test/implementation/loading/CCTest1.mlet" },
120: new String[] { String.class.getName() });
121:
122: server
123: .invoke(
124: m2Name,
125: "getMBeansFromURL",
126: new Object[] { "file:./output/etc/test/implementation/loading/CCTest2.mlet" },
127: new String[] { String.class.getName() });
128:
129: server.invoke(new ObjectName(":name=Start"), "startOp",
130: new Object[] { AgentID.get(server) },
131: new String[] { String.class.getName() });
132:
133: //fail("Expected to fail due to two different mlet loaders having a class mismatch.");
134: } catch (MBeanException e) {
135: if (e.getTargetException() instanceof ReflectionException) {
136: // expected, argument type mismatch error since the arg of type AClass is
137: // loaded by diff mlet loader than the target MBean with AClass in its sign.
138: if (System
139: .getProperty(LOADER_REPOSITORY_CLASS_PROPERTY)
140: .equals(UNIFIED_LOADER_REPOSITORY_CLASS))
141: throw e;
142: } else
143: throw e;
144: }
145: }
146:
147: }
|