SF.net SVN: mx4j: [2268] trunk/mx4j/jsr003/src/test/java/test/javax/ management/loading/MLetTest.java

[email protected] Fri, 23 Mar 2007 07:29:40 -0700
Newsgroups gmane.comp.java.mx4j.cvs
Message-ID <[email protected]>
Revision: 2268
          http://svn.sourceforge.net/mx4j/?rev=2268&view=rev
Author:   simonebordet
Date:     2007-03-23 07:29:39 -0700 (Fri, 23 Mar 2007)

Log Message:
-----------
Fixed tests relying on having the test classes loaded from a jar. Maven loads them from the test-classes directory.

Modified Paths:
--------------
    trunk/mx4j/jsr003/src/test/java/test/javax/management/loading/MLetTest.java

Modified: trunk/mx4j/jsr003/src/test/java/test/javax/management/loading/MLetTest.java
===================================================================
--- trunk/mx4j/jsr003/src/test/java/test/javax/management/loading/MLetTest.java	2007-03-21 10:32:28 UTC (rev 2267)
+++ trunk/mx4j/jsr003/src/test/java/test/javax/management/loading/MLetTest.java	2007-03-23 14:29:39 UTC (rev 2268)
@@ -9,12 +9,16 @@
 package test.javax.management.loading;
 
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileOutputStream;
+import java.io.InputStream;
 import java.lang.reflect.Method;
 import java.net.URL;
 import java.util.List;
 import java.util.Random;
 import java.util.Set;
+import java.util.jar.JarEntry;
+import java.util.jar.JarOutputStream;
 
 import javax.management.MBeanRegistration;
 import javax.management.MBeanServer;
@@ -306,12 +310,9 @@
 
     public void testCodebaseForGetMBeansFromURL() throws Exception
     {
-        Class cls = Simple.class;
-        String className = cls.getName();
-        URL url = cls.getProtectionDomain().getCodeSource().getLocation();
-        String urlString = url.toExternalForm();
-        int index = urlString.lastIndexOf('/') + 1;
-        String jar = urlString.substring(index);
+        Class mbeanClass = Simple.class;
+        String className = mbeanClass.getName();
+        String jar = "whatever.jar";
 
         String codebase = ".";
         String content = "<MLET CODE=\"" + className + "\" NAME=\":name=test\" ARCHIVE=\"" + jar + "\" CODEBASE=\"" + codebase + "\"/>";
@@ -345,14 +346,39 @@
 
     public void testGetMBeansFromURL() throws Exception
     {
-        Class cls = Simple.class;
-        String className = cls.getName();
-        URL url = cls.getProtectionDomain().getCodeSource().getLocation();
-        String urlString = url.toExternalForm();
-        int index = urlString.lastIndexOf('/') + 1;
-        String jar = urlString.substring(index);
-        String codebase = urlString.substring(0, index);
+        Class mbeanClass = Simple.class;
+        String className = mbeanClass.getName();
+        String classPath = className.replace('.', '/') + ".class";
+        URL classURL = mbeanClass.getProtectionDomain().getCodeSource().getLocation();
+        String classFullPath = classURL.getPath() + classPath;
 
+        Class mbeanInterface = SimpleMBean.class;
+        String interfaceName = mbeanInterface.getName();
+        String interfacePath = interfaceName.replace('.', '/') + ".class";
+        URL interfaceURL = mbeanInterface.getProtectionDomain().getCodeSource().getLocation();
+        String interfaceFullPath = interfaceURL.getPath() + interfacePath;
+
+        File archive = File.createTempFile("mlet", ".jar");
+        archive.deleteOnExit();
+        JarOutputStream jarOutputStream = new JarOutputStream(new FileOutputStream(archive));
+
+        byte[] buffer = new byte[1024];
+        int read = -1;
+        JarEntry entry = new JarEntry(classPath);
+        jarOutputStream.putNextEntry(entry);
+        InputStream inputStream = new FileInputStream(classFullPath);
+        while ((read = inputStream.read(buffer)) >= 0)
+            jarOutputStream.write(buffer, 0, read);
+        entry = new JarEntry(interfacePath);
+        jarOutputStream.putNextEntry(entry);
+        inputStream = new FileInputStream(interfaceFullPath);
+        while ((read = inputStream.read(buffer)) >= 0)
+            jarOutputStream.write(buffer, 0, read);
+        jarOutputStream.close();
+
+        String jar = archive.getName();
+        String codebase = archive.getParentFile().getCanonicalPath();
+
         // Write an MLet file
         File mletFile = File.createTempFile("mlet", null);
         mletFile.deleteOnExit();
@@ -366,25 +392,63 @@
 
         ObjectName mletName = new ObjectName(":loader=mlet1");
 
-        MLet mlet = new MLet();
+        MLet mlet = new MLet(new URL[0], null, false);
         server.registerMBean(mlet, mletName);
 
         Set mbeans = mlet.getMBeansFromURL(mletFile.toURL());
         if (mbeans.size() != 1) fail("Loaded wrong number of MBeans");
-        ObjectInstance instance = (ObjectInstance)mbeans.iterator().next();
+        Object result = mbeans.iterator().next();
+        if (!(result instanceof ObjectInstance)) fail("MBean could not be created: " + result);
+        ObjectInstance instance = (ObjectInstance)result;
         if (!instance.getClassName().equals(className)) fail("Loaded a different MBean");
     }
 
     public void testGetMBeansFromURLWithNoName() throws Exception
     {
-        Class cls = SimpleRegistration.class;
-        String className = cls.getName();
-        URL url = cls.getProtectionDomain().getCodeSource().getLocation();
-        String urlString = url.toExternalForm();
-        int index = urlString.lastIndexOf('/') + 1;
-        String jar = urlString.substring(index);
-        String codebase = urlString.substring(0, index);
+        Class mbeanParentClass = Simple.class;
+        String parentClassName = mbeanParentClass.getName();
+        String parentClassPath = parentClassName.replace('.', '/') + ".class";
+        URL parentClassURL = mbeanParentClass.getProtectionDomain().getCodeSource().getLocation();
+        String parentClassFullPath = parentClassURL.getPath() + parentClassPath;
 
+        Class mbeanInterface = SimpleMBean.class;
+        String interfaceName = mbeanInterface.getName();
+        String interfacePath = interfaceName.replace('.', '/') + ".class";
+        URL interfaceURL = mbeanInterface.getProtectionDomain().getCodeSource().getLocation();
+        String interfaceFullPath = interfaceURL.getPath() + interfacePath;
+
+        Class mbeanClass = SimpleRegistration.class;
+        String className = mbeanClass.getName();
+        String classPath = className.replace('.', '/') + ".class";
+        URL classURL = mbeanClass.getProtectionDomain().getCodeSource().getLocation();
+        String classFullPath = classURL.getPath() + classPath;
+
+        File archive = File.createTempFile("mlet", ".jar");
+        archive.deleteOnExit();
+        JarOutputStream jarOutputStream = new JarOutputStream(new FileOutputStream(archive));
+
+        byte[] buffer = new byte[1024];
+        int read = -1;
+        JarEntry entry = new JarEntry(parentClassPath);
+        jarOutputStream.putNextEntry(entry);
+        InputStream inputStream = new FileInputStream(parentClassFullPath);
+        while ((read = inputStream.read(buffer)) >= 0)
+            jarOutputStream.write(buffer, 0, read);
+        entry = new JarEntry(interfacePath);
+        jarOutputStream.putNextEntry(entry);
+        inputStream = new FileInputStream(interfaceFullPath);
+        while ((read = inputStream.read(buffer)) >= 0)
+            jarOutputStream.write(buffer, 0, read);
+        entry = new JarEntry(classPath);
+        jarOutputStream.putNextEntry(entry);
+        inputStream = new FileInputStream(classFullPath);
+        while ((read = inputStream.read(buffer)) >= 0)
+            jarOutputStream.write(buffer, 0, read);
+        jarOutputStream.close();
+
+        String jar = archive.getName();
+        String codebase = archive.getParentFile().getCanonicalPath();
+
         // Write an MLet file
         File mletFile = File.createTempFile("mletnoname", null);
         mletFile.deleteOnExit();
@@ -397,12 +461,14 @@
         MBeanServer server = newMBeanServer();
 
         ObjectName mletName = new ObjectName(":loader=mlet1");
-        MLet mlet = new MLet();
+        MLet mlet = new MLet(new URL[0], false);
         server.registerMBean(mlet, mletName);
 
         Set mbeans = mlet.getMBeansFromURL(mletFile.toURL());
         if (mbeans.size() != 1) fail("Loaded wrong number of MBeans");
-        ObjectInstance instance = (ObjectInstance)mbeans.iterator().next();
+        Object result = mbeans.iterator().next();
+        if (!(result instanceof ObjectInstance)) fail("MBean could not be created: " + result);
+        ObjectInstance instance = (ObjectInstance)result;
         if (!instance.getClassName().equals(className)) fail("Loaded a different MBean");
     }
 


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV