Thread: [Jsptest-svn-commits] SF.net SVN: jsptest: [216] trunk/jsptest-generic/jsptest-common/src
Status: Alpha
Brought to you by:
lkoskela
From: <lko...@us...> - 2008-04-14 11:36:10
|
Revision: 216 http://jsptest.svn.sourceforge.net/jsptest/?rev=216&view=rev Author: lkoskela Date: 2008-04-14 04:36:05 -0700 (Mon, 14 Apr 2008) Log Message: ----------- Added direct unit tests for Path. Fixed a NullPointerException that was thrown when certain JSP/Servlet libraries were loaded by the bootstrap classloader (platform-dependent behavior, exhibited by at least Apple's 1.5.0_13 JVM). Modified Paths: -------------- trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/utils/Path.java Added Paths: ----------- trunk/jsptest-generic/jsptest-common/src/test/java/net/sf/jsptest/utils/PathTest.java trunk/jsptest-generic/jsptest-common/src/test/resources/ trunk/jsptest-generic/jsptest-common/src/test/resources/PathTest.res Modified: trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/utils/Path.java =================================================================== --- trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/utils/Path.java 2008-04-14 11:34:17 UTC (rev 215) +++ trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/utils/Path.java 2008-04-14 11:36:05 UTC (rev 216) @@ -8,64 +8,75 @@ public class Path extends ArrayList { - private static final String SEPARATOR = System - .getProperty("path.separator"); + private static final String SEPARATOR = System + .getProperty("path.separator"); - public void addSystemProperty(String name) { - String property = System.getProperty(name); - if (property != null) { - String[] systemClasspath = property.split(SEPARATOR); - for (int i = 0; i < systemClasspath.length; i++) { - add(new File(systemClasspath[i])); - } - } - } + public void addSystemProperty(String name) { + String property = System.getProperty(name); + if (property != null) { + String[] systemClasspath = property.split(SEPARATOR); + for (int i = 0; i < systemClasspath.length; i++) { + String entry = systemClasspath[i]; + File file = new File(entry); + if (file.exists()) { + add(file.getAbsolutePath()); + } else { + add(entry); + } + } + } + } - public boolean add(Object pathElement) { - if (contains(pathElement)) { - return false; - } - return super.add(pathElement); - } + public boolean add(Object pathElement) { + if (contains(pathElement)) { + return false; + } + return super.add(pathElement); + } - public boolean add(File file) { - return add(file.getAbsolutePath()); - } + public boolean add(File file) { + return add(file.getAbsolutePath()); + } - public String[] toStringArray() { - return (String[]) toArray(new String[size()]); - } + public String[] toStringArray() { + return (String[]) toArray(new String[size()]); + } - public void addContainer(Class klass) { - String resource = "/" - + klass.getName().replaceAll("\\.", "/") + ".class"; - addJarFile(klass.getResource(resource)); - try { - Enumeration en = klass.getClassLoader().getResources( - resource); - while (en.hasMoreElements()) { - addJarFile((URL) en.nextElement()); - } - } catch (IOException e) { - e.printStackTrace(); - } - } + public void addContainer(Class klass) { + String resource = resourcePathFor(klass); + addJarFile(klass.getResource(resource)); + try { + ClassLoader context = klass.getClassLoader(); + if (context != null) { + Enumeration en = context.getResources(resource); + while (en.hasMoreElements()) { + addJarFile((URL) en.nextElement()); + } + } + } catch (IOException e) { + e.printStackTrace(); + } + } - private void addJarFile(URL url) { - if (url != null) { - addJarFile(url.toExternalForm()); - } - } + private String resourcePathFor(Class klass) { + String resource = klass.getName().replaceAll("\\.", "/"); + return "/" + resource + ".class"; + } - private void addJarFile(String url) { - String prefix = "jar:file:"; - if (url.startsWith(prefix)) { - String file = url.substring(prefix.length()); - if (file.contains("!")) { - file = file.substring(0, file.indexOf('!')); - } - add(new File(file)); - } - } + private void addJarFile(URL url) { + if (url != null) { + addJarFile(url.toExternalForm()); + } + } + private void addJarFile(String url) { + String prefix = "jar:file:"; + if (url.startsWith(prefix)) { + String file = url.substring(prefix.length()); + if (file.contains("!")) { + file = file.substring(0, file.indexOf('!')); + } + add(new File(file)); + } + } } Added: trunk/jsptest-generic/jsptest-common/src/test/java/net/sf/jsptest/utils/PathTest.java =================================================================== --- trunk/jsptest-generic/jsptest-common/src/test/java/net/sf/jsptest/utils/PathTest.java (rev 0) +++ trunk/jsptest-generic/jsptest-common/src/test/java/net/sf/jsptest/utils/PathTest.java 2008-04-14 11:36:05 UTC (rev 216) @@ -0,0 +1,80 @@ +package net.sf.jsptest.utils; + +import java.io.File; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import junit.framework.TestCase; + +public class PathTest extends TestCase { + + private Path path; + private File file1; + private File file2; + + protected void setUp() throws Exception { + super.setUp(); + path = new Path(); + file1 = File.createTempFile("test", ".tmp"); + file2 = File.createTempFile("test", ".tmp"); + } + + protected void tearDown() throws Exception { + file1.delete(); + file2.delete(); + super.tearDown(); + } + + public void testEmptyPath() throws Exception { + assertEquals(0, path.toStringArray().length); + } + + public void testAddingFileObjects() throws Exception { + path.add(file1); + path.add(file2); + pathShouldBe(file1.getAbsolutePath(), file2.getAbsolutePath()); + } + + public void testAddingObjects() throws Exception { + path.add(file1.getAbsolutePath()); + path.add(file2.getAbsolutePath()); + pathShouldBe(file1.getAbsolutePath(), file2.getAbsolutePath()); + } + + public void testAddedEntriesDontHaveToBeExistingFiles() throws Exception { + String nonExistingPath = "nosuch/file.txt"; + String nonExistingAbsolutePath = "/no/such/file.txt"; + path.add(nonExistingPath); + path.add(nonExistingAbsolutePath); + pathShouldBe(nonExistingPath, nonExistingAbsolutePath); + } + + public void testAddingSystemProperties() throws Exception { + System.setProperty("NO_SUCH_FILE", "no/such/file.txt"); + System.setProperty("EXISTING_FILE", "src/test/resources/PathTest.res"); + path.addSystemProperty("NO_SUCH_FILE"); + path.addSystemProperty("EXISTING_FILE"); + pathShouldBe(System.getProperty("NO_SUCH_FILE"), new File(System + .getProperty("EXISTING_FILE")).getAbsolutePath()); + } + + public void testAddContainer() throws Exception { + path.addContainer(junit.framework.Assert.class); + assertEquals(1, path.toStringArray().length); + assertTrue(path.toString().indexOf("junit") != -1); + } + + public void testAddContainerWithClassLoadedByBootstrapClassLoader() + throws Exception { + path.addContainer(String.class); + assertEquals(1, path.toStringArray().length); + } + + private void pathShouldBe(String firstEntry, String secondEntry) { + List expected = new ArrayList(); + expected.add(firstEntry); + expected.add(secondEntry); + assertEquals(expected, Arrays.asList(path.toStringArray())); + } +} Added: trunk/jsptest-generic/jsptest-common/src/test/resources/PathTest.res =================================================================== --- trunk/jsptest-generic/jsptest-common/src/test/resources/PathTest.res (rev 0) +++ trunk/jsptest-generic/jsptest-common/src/test/resources/PathTest.res 2008-04-14 11:36:05 UTC (rev 216) @@ -0,0 +1 @@ +needed for PathTest \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
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. |