Author: manik.surtani(a)jboss.com
Date: 2008-01-02 11:53:35 -0500 (Wed, 02 Jan 2008)
New Revision: 4942
Modified:
core/trunk/src/main/java/org/jboss/cache/util/reflect/ClasspathScanner.java
core/trunk/src/test/java/org/jboss/cache/misc/TestingUtil.java
core/trunk/src/test/java/org/jboss/cache/util/reflect/ClasspathScannerTest.java
Log:
More robust classpath scanning + a proper test
Modified: core/trunk/src/main/java/org/jboss/cache/util/reflect/ClasspathScanner.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/util/reflect/ClasspathScanner.java 2008-01-02
16:01:52 UTC (rev 4941)
+++ core/trunk/src/main/java/org/jboss/cache/util/reflect/ClasspathScanner.java 2008-01-02
16:53:35 UTC (rev 4942)
@@ -26,7 +26,7 @@
public class ClasspathScanner
{
private Log log = LogFactory.getLog(ClasspathScanner.class);
- private URLClassLoader classLoader;
+ URLClassLoader classLoader;
/**
* Constructor with the type of annotation to scan for.
@@ -51,7 +51,7 @@
try
{
// only scan the current ClassPath location that contains this file. Could be a
directory or a JAR file.
- URL url = getURLPathFromClassLoader();
+ URL url = getURLPathFromClassLoader("org/jboss/cache/Version.class");
String urlPath = url.getFile();
if (urlPath.endsWith("/"))
{
@@ -81,12 +81,12 @@
return classes;
}
- private URL getURLPathFromClassLoader() throws MalformedURLException
+ URL getURLPathFromClassLoader(String resourceName) throws MalformedURLException
{
- URL u2 = classLoader.findResource("org/jboss/cache/Version.class");
+ URL u2 = classLoader.findResource(resourceName);
String u2String = u2.toString();
- boolean isJar = u2String.startsWith("jar:") &&
u2String.contains(".jar!/");
+ boolean isJar = u2String.startsWith("jar:") &&
(u2String.toLowerCase().contains(".jar!/") ||
u2String.toLowerCase().contains(".zip!/"));
for (URL u : classLoader.getURLs())
{
Modified: core/trunk/src/test/java/org/jboss/cache/misc/TestingUtil.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/misc/TestingUtil.java 2008-01-02 16:01:52 UTC
(rev 4941)
+++ core/trunk/src/test/java/org/jboss/cache/misc/TestingUtil.java 2008-01-02 16:53:35 UTC
(rev 4942)
@@ -387,6 +387,11 @@
public static void recursiveFileRemove(String directoryName)
{
File file = new File(directoryName);
+ recursiveFileRemove(file);
+ }
+
+ public static void recursiveFileRemove(File file)
+ {
if (file.exists())
{
System.out.println("Deleting file " + file);
Modified: core/trunk/src/test/java/org/jboss/cache/util/reflect/ClasspathScannerTest.java
===================================================================
---
core/trunk/src/test/java/org/jboss/cache/util/reflect/ClasspathScannerTest.java 2008-01-02
16:01:52 UTC (rev 4941)
+++
core/trunk/src/test/java/org/jboss/cache/util/reflect/ClasspathScannerTest.java 2008-01-02
16:53:35 UTC (rev 4942)
@@ -4,9 +4,22 @@
import org.jboss.cache.factories.EmptyConstructorFactory;
import org.jboss.cache.factories.TransactionManagerFactory;
import org.jboss.cache.factories.annotations.DefaultFactoryFor;
+import org.jboss.cache.misc.TestingUtil;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.net.URL;
+import java.net.URLClassLoader;
import java.util.Set;
+import java.util.jar.JarOutputStream;
+import java.util.zip.ZipEntry;
/**
* @author Manik Surtani (<a
href="mailto:manik@jboss.org">manik@jboss.org</a>)
@@ -15,6 +28,35 @@
@Test(groups = {"functional"})
public class ClasspathScannerTest
{
+ String tmpDir;
+ File classpathDirectory;
+ File archive;
+ String resourceName = "test.resource";
+
+ @BeforeMethod
+ public void setUp() throws Exception
+ {
+ // create a test directory and add this to my class path:
+ tmpDir = System.getProperty("java.io.tmpdir", "/tmp");
+ String tmpClasspath = tmpDir + File.separator + "tempClasspath";
+ classpathDirectory = new File(tmpClasspath);
+ classpathDirectory.mkdirs();
+ File tempResourceFile = new File(tmpClasspath + File.separator + resourceName);
+
+ PrintWriter writer = new PrintWriter(new FileWriter(tempResourceFile));
+ writer.write("Blah");
+ writer.close();
+
+ assert tempResourceFile.exists();
+ }
+
+ @AfterMethod
+ public void tearDown()
+ {
+ TestingUtil.recursiveFileRemove(classpathDirectory);
+ if (archive != null) TestingUtil.recursiveFileRemove(archive);
+ }
+
public void testScanning()
{
ClasspathScanner cs = new ClasspathScanner();
@@ -24,4 +66,53 @@
assert sc.contains(TransactionManagerFactory.class);
assert sc.contains(EmptyConstructorFactory.class);
}
+
+ public void testScanningClassDirectory() throws IOException
+ {
+ ClasspathScanner cs = new ClasspathScanner();
+ cs.classLoader = new URLClassLoader(new URL[]{classpathDirectory.toURL()},
getClass().getClassLoader());
+
+ URL u = cs.getURLPathFromClassLoader(resourceName);
+
+ assert u.equals(classpathDirectory.toURL());
+ }
+
+ public void testScanningJar() throws IOException
+ {
+ // create a JAR archive
+ archive = new File(tmpDir + File.separator + "lib" + File.separator +
"archive.jar");
+ testScanningArchive();
+ }
+
+ public void testScanningZip() throws IOException
+ {
+ // create a ZIP archive
+ archive = new File(tmpDir + File.separator + "lib" + File.separator +
"archive.zip");
+ testScanningArchive();
+ }
+
+ private void testScanningArchive() throws IOException
+ {
+ archive.getParentFile().mkdirs();
+ FileOutputStream fos = new FileOutputStream(archive);
+ JarOutputStream jos = new JarOutputStream(fos);
+ FileInputStream fis = new FileInputStream(classpathDirectory + File.separator +
resourceName);
+ ZipEntry ze = new ZipEntry(resourceName);
+ jos.putNextEntry(ze);
+
+ byte b;
+ while ((b = (byte) fis.read()) != -1) jos.write(b);
+
+ fis.close();
+ jos.closeEntry();
+ jos.close();
+ fos.close();
+
+ ClasspathScanner cs = new ClasspathScanner();
+ cs.classLoader = new URLClassLoader(new URL[]{archive.toURL()},
getClass().getClassLoader());
+
+ URL u = cs.getURLPathFromClassLoader(resourceName);
+
+ assert u.equals(archive.toURL());
+ }
}
Show replies by date