KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > implementation > loading > LoaderRepositoryTEST


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package test.implementation.loading;
23
24 import java.net.URL JavaDoc;
25
26 import javax.management.MBeanException JavaDoc;
27 import javax.management.MBeanServer JavaDoc;
28 import javax.management.MBeanServerFactory JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30 import javax.management.ReflectionException JavaDoc;
31 import javax.management.loading.DefaultLoaderRepository JavaDoc;
32 import javax.management.loading.MLet JavaDoc;
33
34 import junit.framework.TestCase;
35
36 import org.jboss.mx.loading.LoaderRepository;
37 import org.jboss.mx.loading.RepositoryClassLoader;
38 import org.jboss.mx.server.ServerConstants;
39 import org.jboss.mx.util.AgentID;
40
41 public class LoaderRepositoryTEST extends TestCase implements ServerConstants
42 {
43    public LoaderRepositoryTEST(String JavaDoc s)
44    {
45       super(s);
46    }
47
48    public void testRemoveDuplicateURL() throws Exception JavaDoc
49    {
50       // NOTE:
51
// the urls used here are relative to the location of the build.xml
52
final URL JavaDoc url = new URL JavaDoc("file:./output/etc/test/implementation/loading/MyMBeans.jar");
53
54       // Retrieve the loader repository
55
MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
56       LoaderRepository JavaDoc lr = (LoaderRepository JavaDoc)server.getClassLoaderRepository();
57
58       // Should not be able to load the class
59
try
60       {
61          lr.loadClass("test.implementation.loading.support.Trivial");
62          fail("test.implementation.loading.support.Trivial is already visible");
63       }
64       catch (ClassNotFoundException JavaDoc expected) {}
65
66       // Add the URL to the repository twice
67
RepositoryClassLoader ucl1 = lr.newClassLoader(url, true);
68       RepositoryClassLoader ucl2 = lr.newClassLoader(url, true);
69
70       // Should be able to load the class
71
lr.loadClass("test.implementation.loading.support.Trivial");
72
73       // Remove one
74
ucl1.unregister();
75
76       // Should still be able to load the class
77
lr.loadClass("test.implementation.loading.support.Trivial");
78
79       // Remove the other
80
ucl2.unregister();
81    }
82
83    public void testClassConflictBetweenMLets() throws Exception JavaDoc
84    {
85       // NOTE:
86
// the urls used here are relative to the location of the build.xml
87

88       // make sure the classes are loaded from mlet, not system cl
89
String JavaDoc[] classes = { "test.implementation.loading.support.Start",
90                            "test.implementation.loading.support.StartMBean",
91                            "test.implementation.loading.support.Target",
92                            "test.implementation.loading.support.TargetMBean",
93                            "test.implementation.loading.support.AClass"
94       };
95             
96       for (int i = 0; i < classes.length; ++i)
97       {
98          try
99          {
100             DefaultLoaderRepository.loadClass(classes[i]);
101
102             fail("class " + classes[i] + " was already found in CL repository.");
103          }
104          catch (ClassNotFoundException JavaDoc e)
105          {
106             // expected
107
}
108       }
109       
110       try
111       {
112          MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
113          MLet JavaDoc mlet1 = new MLet JavaDoc();
114          MLet JavaDoc mlet2 = new MLet JavaDoc();
115          ObjectName JavaDoc m1Name = new ObjectName JavaDoc(":name=mlet1");
116          ObjectName JavaDoc m2Name = new ObjectName JavaDoc(":name=mlet2");
117          
118          server.registerMBean(mlet1, m1Name);
119          server.registerMBean(mlet2, m2Name);
120          
121          server.invoke(m1Name, "getMBeansFromURL",
122          new Object JavaDoc[] { "file:./output/etc/test/implementation/loading/CCTest1.mlet" },
123          new String JavaDoc[] { String JavaDoc.class.getName() }
124          );
125          
126          server.invoke(m2Name, "getMBeansFromURL",
127          new Object JavaDoc[] { "file:./output/etc/test/implementation/loading/CCTest2.mlet" },
128          new String JavaDoc[] { String JavaDoc.class.getName() }
129          );
130          
131          server.invoke(new ObjectName JavaDoc(":name=Start"), "startOp",
132          new Object JavaDoc[] { AgentID.get(server) },
133          new String JavaDoc[] { String JavaDoc.class.getName() }
134          );
135          
136          //fail("Expected to fail due to two different mlet loaders having a class mismatch.");
137
}
138       catch (MBeanException JavaDoc e)
139       {
140          if (e.getTargetException() instanceof ReflectionException JavaDoc)
141          {
142             // expected, argument type mismatch error since the arg of type AClass is
143
// loaded by diff mlet loader than the target MBean with AClass in its sign.
144
if (System.getProperty(LOADER_REPOSITORY_CLASS_PROPERTY).equals(UNIFIED_LOADER_REPOSITORY_CLASS))
145                throw e;
146          }
147          else throw e;
148       }
149    }
150
151 }
152
Popular Tags