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.compliance.loading;
023:
024: import java.net.URL;
025: import java.util.Set;
026:
027: import javax.management.Attribute;
028: import javax.management.MBeanException;
029: import javax.management.MBeanServer;
030: import javax.management.MBeanServerFactory;
031: import javax.management.ObjectName;
032: import javax.management.ServiceNotFoundException;
033: import javax.management.loading.MLet;
034:
035: import junit.framework.AssertionFailedError;
036: import junit.framework.TestCase;
037:
038: /**
039: * MLet tests.
040: *
041: * @author <a href="mailto:jplindfo@helsinki.fi">Juha Lindfors</a>
042: */
043: public class MLetTEST extends TestCase {
044: private MBeanServer server;
045:
046: public MLetTEST(String s) {
047: super (s);
048: }
049:
050: protected void setUp() throws Exception {
051: super .setUp();
052: server = MBeanServerFactory.createMBeanServer();
053: }
054:
055: public void testCreateAndRegister() throws Exception {
056:
057: MLet mlet = new MLet();
058: ObjectName name = new ObjectName("test:name=mlet");
059:
060: try {
061: server.registerMBean(mlet, name);
062: } finally {
063: server.unregisterMBean(name);
064: }
065: }
066:
067: public void testMLetLoadClassFromURLInConstructor()
068: throws Exception {
069: // NOTE:
070: // the urls used here are relative to the location of the build.xml
071:
072: final String MBEANS_URL = "file:./output/etc/test/compliance/loading/MyMBeans.jar";
073:
074: // make sure the class is not available
075: try {
076: server.getClassLoaderRepository().loadClass(
077: "test.compliance.loading.support.Trivial");
078:
079: fail("class test.compliance.loading.support.Trivial was already found in CL repository.");
080: } catch (ClassNotFoundException e) {
081: // expected
082: }
083:
084: MBeanServer server = MBeanServerFactory.createMBeanServer();
085: ObjectName name = new ObjectName("test:name=mlet");
086: MLet mlet = new MLet(new URL[] { new URL(MBEANS_URL) });
087:
088: // make sure the class is not available
089: try {
090: server.getClassLoaderRepository().loadClass(
091: "test.compliance.loading.support.Trivial");
092:
093: fail("class test.compliance.loading.support.Trivial found in CL repository after MLet construction.");
094: } catch (ClassNotFoundException e) {
095: // expected
096: }
097:
098: try {
099: server.registerMBean(mlet, name);
100: server.getClassLoaderRepository().loadClass(
101: "test.compliance.loading.support.Trivial");
102: } finally {
103: server.unregisterMBean(name);
104: }
105: // make sure the class is not available
106: try {
107: server.getClassLoaderRepository().loadClass(
108: "test.compliance.loading.support.Trivial");
109:
110: fail("class test.compliance.loading.support.Trivial was still found in CL repository.");
111: } catch (ClassNotFoundException e) {
112: // expected
113: }
114: }
115:
116: public void testBasicMLetFileLoad() throws Exception {
117: // NOTE:
118: // the urls used here are relative to the location of the build.xml
119:
120: final String MLET_URL = "file:./output/etc/test/compliance/loading/BasicConfig.mlet";
121:
122: // make sure the classes are loaded from mlet, not system cl
123: try {
124: server.getClassLoaderRepository().loadClass(
125: "test.compliance.loading.support.Trivial");
126:
127: fail("class test.compliance.loading.support.Trivial was already found in CL repository.");
128: } catch (ClassNotFoundException e) {
129: // expected
130: }
131: // make sure the classes are loaded from mlet, not system cl
132: try {
133: server.getClassLoaderRepository().loadClass(
134: "test.compliance.loading.support.Trivial2");
135:
136: fail("class test.compliance.loading.support.Trivial2 was already found in CL repository.");
137: } catch (ClassNotFoundException e) {
138: // expected
139: }
140:
141: MBeanServer server = MBeanServerFactory.createMBeanServer();
142: MLet mlet = new MLet();
143: ObjectName name = new ObjectName("test:name=mlet");
144: server.registerMBean(mlet, name);
145:
146: Set mbeans = (Set) server.invoke(name, "getMBeansFromURL",
147: new Object[] { MLET_URL }, new String[] { String.class
148: .getName() });
149:
150: try {
151: assertTrue(server.isRegistered(new ObjectName(
152: "test:name=Trivial")));
153: assertTrue(server.isRegistered(new ObjectName(
154: "test:name=Trivial2")));
155: } catch (AssertionFailedError e) {
156: URL[] urls = mlet.getURLs();
157: URL url = null;
158:
159: if (urls != null && urls.length > 0)
160: url = urls[0];
161:
162: fail("FAILS IN RI: SUN JMX RI builds a malformed URL from an MLet text file URL '"
163: + MLET_URL
164: + "' resulting into MLET codebase URL '"
165: + url
166: + "' and therefore fails "
167: + "to load the required classes from the Java archive (MyMBeans.jar)");
168: }
169:
170: assertTrue(server.getMBeanInfo(new ObjectName(
171: "test:name=Trivial")) != null);
172: assertTrue(server.getMBeanInfo(new ObjectName(
173: "test:name=Trivial2")) != null);
174:
175: assertTrue(server.getAttribute(
176: new ObjectName("test:name=Trivial2"), "Something")
177: .equals("foo"));
178:
179: server.invoke(new ObjectName("test:name=Trivial"),
180: "doOperation", new Object[] { "Test" },
181: new String[] { String.class.getName() });
182:
183: server.invoke(new ObjectName("test:name=Trivial2"),
184: "doOperation", new Object[] { "Test" },
185: new String[] { String.class.getName() });
186: }
187:
188: /**
189: * Make sure the versioning MLet installer won't replace MBeans that were
190: * not registered with version information.
191: */
192: public void testConflictingMLetFileLoad() throws Exception {
193: // NOTE:
194: // the urls used here are relative to the location of the build.xml
195:
196: final String MLET_URL1 = "file:./output/etc/test/compliance/loading/BasicConfig2.mlet";
197: final String MLET_URL2 = "file:./output/etc/test/compliance/loading/BasicConfig2.mlet";
198:
199: // make sure the classes are loaded from mlet, not system cl
200: try {
201: server.getClassLoaderRepository().loadClass(
202: "test.compliance.loading.support.Trivial3");
203:
204: fail("class test.compliance.loading.support.Trivial was already found in CL repository.");
205: } catch (ClassNotFoundException e) {
206: // expected
207: }
208: // make sure the classes are loaded from mlet, not system cl
209: try {
210: server.getClassLoaderRepository().loadClass(
211: "test.compliance.loading.support.Trivial4");
212:
213: fail("class test.compliance.loading.support.Trivial2 was already found in CL repository.");
214: } catch (ClassNotFoundException e) {
215: // expected
216: }
217:
218: MBeanServer server = MBeanServerFactory.createMBeanServer();
219: MLet mlet = new MLet();
220: ObjectName name = new ObjectName("test:name=mlet");
221: server.registerMBean(mlet, name);
222:
223: Set mbeans = (Set) server.invoke(name, "getMBeansFromURL",
224: new Object[] { MLET_URL1 }, new String[] { String.class
225: .getName() });
226:
227: ObjectName oname = new ObjectName("test:name=Trivial2");
228: server.setAttribute(oname, new Attribute("Something",
229: "Something"));
230:
231: mlet = new MLet();
232: name = new ObjectName("test:name=mlet2");
233: server.registerMBean(mlet, name);
234:
235: mbeans = (Set) server.invoke(name, "getMBeansFromURL",
236: new Object[] { MLET_URL2 }, new String[] { String.class
237: .getName() });
238:
239: oname = new ObjectName("test:name=Trivial2");
240: String value = (String) server.getAttribute(oname, "Something");
241:
242: assertTrue(value.equals("Something"));
243: }
244:
245: public void testMalformedURLLoad() throws Exception {
246: // NOTE:
247: // the urls used here are relative to the location of the build.xml
248:
249: MBeanServer server = MBeanServerFactory.createMBeanServer();
250: MLet mlet = new MLet();
251: ObjectName name = new ObjectName("test:name=mlet");
252: try {
253: server.registerMBean(mlet, name);
254:
255: server
256: .invoke(
257: name,
258: "getMBeansFromURL",
259: new Object[] { "output/etc/test/compliance/loading/BasicConfig.mlet" },
260: new String[] { String.class.getName() });
261:
262: // should not reach here
263: fail("FAILS IN RI: Malformed URL in getMBeansURL() should result in ServiceNotFoundException thrown.");
264: } catch (AssertionFailedError e) {
265: // defensive: in case assertXXX() or fail() are later added
266: throw e;
267: } catch (MBeanException e) {
268: assertTrue(e.getTargetException() instanceof ServiceNotFoundException);
269: } finally {
270: try {
271: server.unregisterMBean(name);
272: } catch (Exception ignored) {
273: }
274: }
275: }
276:
277: public void testMissingMLetTagInLoad() throws Exception {
278: // NOTE:
279: // the urls used here are relative to the location of the build.xml
280:
281: MBeanServer server = MBeanServerFactory.createMBeanServer();
282: MLet mlet = new MLet();
283: ObjectName name = new ObjectName("test:name=mlet");
284: try {
285: server.registerMBean(mlet, name);
286:
287: server
288: .invoke(
289: name,
290: "getMBeansFromURL",
291: new Object[] { "file:./output/etc/test/compliance/loading/MissingMLET.mlet" },
292: new String[] { String.class.getName() });
293:
294: // should not reach here
295: fail("MLet text file missing the MLET tag should result in ServiceNotFoundException thrown.");
296: } catch (AssertionFailedError e) {
297: // defensive: in case assertXXX() or fail() are added later
298: throw e;
299: } catch (MBeanException e) {
300: assertTrue(e.getTargetException() instanceof ServiceNotFoundException);
301: } finally {
302: try {
303: server.unregisterMBean(name);
304: } catch (Exception ignored) {
305: }
306: }
307: }
308:
309: public void testMissingMandatoryArchiveTagInLoad() throws Exception {
310: // NOTE:
311: // the urls used here are relative to the location of the build.xml
312:
313: MBeanServer server = MBeanServerFactory.createMBeanServer();
314: MLet mlet = new MLet();
315: ObjectName name = new ObjectName("test:name=mlet");
316: try {
317: server.registerMBean(mlet, name);
318:
319: server
320: .invoke(
321: name,
322: "getMBeansFromURL",
323: new Object[] { "file:./output/etc/test/compliance/loading/MissingMandatoryArchive.mlet" },
324: new String[] { String.class.getName() });
325:
326: // should not reach here
327: fail("MLet text file missing mandatory ARCHIVE attribute should result in ServiceNotFoundException thrown.");
328: } catch (AssertionFailedError e) {
329: // defensive: in case assertXXX() or fail() are added later
330: throw e;
331: } catch (MBeanException e) {
332: assertTrue(e.getTargetException() instanceof ServiceNotFoundException);
333: } finally {
334: try {
335: server.unregisterMBean(name);
336: } catch (Exception ignored) {
337: }
338: }
339: }
340:
341: public void testMissingMandatoryCodeTagInLoad() throws Exception {
342: // NOTE:
343: // the urls used here are relative to the location of the build.xml
344:
345: MBeanServer server = MBeanServerFactory.createMBeanServer();
346: MLet mlet = new MLet();
347: ObjectName name = new ObjectName("test:name=mlet");
348: try {
349: server.registerMBean(mlet, name);
350:
351: server
352: .invoke(
353: name,
354: "getMBeansFromURL",
355: new Object[] { "file:./output/etc/test/compliance/loading/MissingMandatoryCode.mlet" },
356: new String[] { String.class.getName() });
357:
358: // should not reach here
359: fail("MLet text file missing mandatory CODE attribute should result in ServiceNotFoundException thrown.");
360: } catch (AssertionFailedError e) {
361: // defensive: in case assertXXX() or fail() are added later
362: throw e;
363: } catch (MBeanException e) {
364: assertTrue(e.getTargetException() instanceof ServiceNotFoundException);
365: } finally {
366: try {
367: server.unregisterMBean(name);
368: } catch (Exception ignored) {
369: }
370: }
371: }
372:
373: public void testArchiveListInMLet() throws Exception {
374: // NOTE:
375: // the urls used here are relative to the location of the build.xml
376:
377: final String MLET_URL = "file:./output/etc/test/compliance/loading/ArchiveList.mlet";
378:
379: MBeanServer server = MBeanServerFactory.createMBeanServer();
380: MLet mlet = new MLet();
381: ObjectName name = new ObjectName("test:name=mlet");
382:
383: try {
384: server.registerMBean(mlet, name);
385:
386: server.invoke(name, "getMBeansFromURL",
387: new Object[] { MLET_URL },
388: new String[] { String.class.getName() });
389:
390: Class c = null;
391:
392: try {
393: c = mlet
394: .loadClass("test.compliance.loading.support.AClass");
395: } catch (ClassNotFoundException e) {
396: URL[] urls = mlet.getURLs();
397: fail("FAILS IN RI: SUN JMX RI builds a malformed URL from an MLet text file URL '"
398: + MLET_URL
399: + "' resulting into MLET codebase URL '"
400: + urls[0]
401: + "' and therefore fails "
402: + "to load the required classes from the Java archive.");
403: }
404:
405: Object o = c.newInstance();
406:
407: server.setAttribute(new ObjectName(
408: "test:name=AnotherTrivial"), new Attribute(
409: "Something", o));
410: o = server.getAttribute(new ObjectName(
411: "test:name=AnotherTrivial"), "Something");
412:
413: assertTrue(o.getClass().isAssignableFrom(c));
414: } finally {
415: try {
416: server.unregisterMBean(name);
417: } catch (Exception ignored) {
418: }
419: }
420: }
421:
422: public void testUnexpectedEndOfFile() throws Exception {
423: // NOTE:
424: // the urls used here are relative to the location of the build.xml
425:
426: MBeanServer server = MBeanServerFactory.createMBeanServer();
427: MLet mlet = new MLet();
428: ObjectName name = new ObjectName("test:name=mlet");
429: try {
430: server.registerMBean(mlet, name);
431:
432: server
433: .invoke(
434: name,
435: "getMBeansFromURL",
436: new Object[] { "file:./output/etc/test/compliance/loading/UnexpectedEnd.mlet" },
437: new String[] { String.class.getName() });
438:
439: // should not reach here
440: fail("Unexpected end of file from mlet text file did not cause an exception.");
441: } catch (AssertionFailedError e) {
442: throw e;
443: } catch (MBeanException e) {
444: assertTrue(e.getTargetException() instanceof ServiceNotFoundException);
445: } finally {
446: try {
447: server.unregisterMBean(name);
448: } catch (Exception ignored) {
449: }
450: }
451: }
452:
453: public void testMissingEndMLetTag() throws Exception {
454: // NOTE:
455: // the urls used here are relative to the location of the build.xml
456:
457: MBeanServer server = MBeanServerFactory.createMBeanServer();
458: MLet mlet = new MLet();
459: ObjectName name = new ObjectName("test:name=mlet");
460: try {
461: server.registerMBean(mlet, name);
462:
463: server
464: .invoke(
465: name,
466: "getMBeansFromURL",
467: new Object[] { "file:./output/etc/test/compliance/loading/MissingEndTag.mlet" },
468: new String[] { String.class.getName() });
469:
470: assertTrue(!server.isRegistered(new ObjectName(
471: "test:name=Trivial")));
472: } catch (AssertionFailedError e) {
473: throw e;
474: } catch (MBeanException e) {
475: assertTrue(e.getTargetException() instanceof ServiceNotFoundException);
476: } finally {
477: try {
478: server.unregisterMBean(name);
479: } catch (Exception ignored) {
480: }
481: }
482: }
483:
484: }
|