[Jsptest-svn-commits] SF.net SVN: jsptest: [231] trunk/jsptest-generic/jsptest-common/src
Status: Alpha
                
                Brought to you by:
                
                    lkoskela
                    
                
            | 
     
      
      
      From: <lko...@us...> - 2008-05-03 20:50:00
      
     
   | 
Revision: 231
          http://jsptest.svn.sourceforge.net/jsptest/?rev=231&view=rev
Author:   lkoskela
Date:     2008-05-03 13:49:55 -0700 (Sat, 03 May 2008)
Log Message:
-----------
Added a couple of direct tests for CustomClassLoader
Modified Paths:
--------------
    trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/utils/CustomClassLoader.java
Added Paths:
-----------
    trunk/jsptest-generic/jsptest-common/src/test/java/net/sf/jsptest/utils/CustomClassLoaderTest.java
Modified: trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/utils/CustomClassLoader.java
===================================================================
--- trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/utils/CustomClassLoader.java	2008-04-25 08:45:19 UTC (rev 230)
+++ trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/utils/CustomClassLoader.java	2008-05-03 20:49:55 UTC (rev 231)
@@ -27,27 +27,31 @@
  */
 public class CustomClassLoader extends ClassLoader {
 
-    private File baseDir;
+	private File baseDir;
 
-    public CustomClassLoader(String baseDir) {
-        this(new File(baseDir));
-    }
+	public CustomClassLoader(String baseDir) {
+		this(new File(baseDir));
+	}
 
-    public CustomClassLoader(File baseDir) {
-        this.baseDir = baseDir;
-    }
+	public CustomClassLoader(File baseDir) {
+		this.baseDir = baseDir;
+	}
 
-    public Class loadClass(String name) throws ClassNotFoundException {
-        File classFile = new File(baseDir, name.replace('.', '/')
-                + ".class");
-        if (!classFile.exists()) {
-            return super.loadClass(name);
-        }
-        try {
-            byte[] data = IO.readToByteArray(classFile);
-            return super.defineClass(name, data, 0, data.length);
-        } catch (Exception e) {
-            throw new RuntimeException(e);
-        }
-    }
+	public Class loadClass(String name) throws ClassNotFoundException {
+		File classFile = new File(baseDir, name.replace('.', '/') + ".class");
+		if (!classFile.exists()) {
+			return super.loadClass(name);
+		}
+		try {
+			byte[] data = IO.readToByteArray(classFile);
+			return defineClass(name, data);
+		} catch (Exception e) {
+			throw new RuntimeException(e);
+		}
+	}
+
+	protected Class defineClass(String name, byte[] data)
+			throws ClassFormatError {
+		return super.defineClass(name, data, 0, data.length);
+	}
 }
\ No newline at end of file
Added: trunk/jsptest-generic/jsptest-common/src/test/java/net/sf/jsptest/utils/CustomClassLoaderTest.java
===================================================================
--- trunk/jsptest-generic/jsptest-common/src/test/java/net/sf/jsptest/utils/CustomClassLoaderTest.java	                        (rev 0)
+++ trunk/jsptest-generic/jsptest-common/src/test/java/net/sf/jsptest/utils/CustomClassLoaderTest.java	2008-05-03 20:49:55 UTC (rev 231)
@@ -0,0 +1,60 @@
+package net.sf.jsptest.utils;
+
+import java.io.File;
+
+import junit.framework.TestCase;
+
+public class CustomClassLoaderTest extends TestCase {
+
+	private File baseDir;
+	protected String definedName;
+	protected byte[] definedData;
+	private String fullyQualifiedClassName;
+
+	protected void setUp() throws Exception {
+		super.setUp();
+		fullyQualifiedClassName = "pkg.Fake";
+		baseDir = new File(System.getProperty("java.io.tmpdir"), "basedir");
+		File packageDir = new File(baseDir, "pkg");
+		packageDir.mkdirs();
+		File classFile = new File(packageDir, "Fake.class");
+		IO.write("Fake", classFile);
+	}
+
+	public void testClassesAreLoadedFromTheGivenBaseDirectory()
+			throws Exception {
+		CustomClassLoader cl = new CustomClassLoader(baseDir) {
+			// override to avoid actual class loading
+			protected Class defineClass(String name, byte[] data)
+					throws ClassFormatError {
+				definedName = name;
+				definedData = data;
+				return CustomClassLoaderTest.class;
+			}
+		};
+
+		verifyClassLoading(cl);
+	}
+
+	public void testBaseDirectoryCanBeGivenAsAbsolutePathName()
+			throws Exception {
+		CustomClassLoader cl = new CustomClassLoader(baseDir.getAbsolutePath()) {
+			// override to avoid actual class loading
+			protected Class defineClass(String name, byte[] data)
+					throws ClassFormatError {
+				definedName = name;
+				definedData = data;
+				return CustomClassLoaderTest.class;
+			}
+		};
+
+		verifyClassLoading(cl);
+	}
+
+	private void verifyClassLoading(CustomClassLoader classLoader)
+			throws ClassNotFoundException {
+		classLoader.loadClass(fullyQualifiedClassName);
+		assertEquals(fullyQualifiedClassName, definedName);
+		assertEquals("Fake", new String(definedData));
+	}
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
 |