diff options
author | Shigeru Chiba <chibash@users.noreply.github.com> | 2018-05-07 19:06:13 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-05-07 19:06:13 +0900 |
commit | 1513c3372bc674d9f9f7ff7cb258ab9b1ccb68c1 (patch) | |
tree | d408d1fbcea7ae0545c0e04547b7c203545560b7 /src/test | |
parent | 40d3223b128887dffafb8d6f28438628ad72e039 (diff) | |
parent | 620a8be65bb786c77fc2016e9a53364f3d65260e (diff) | |
download | javassist-1513c3372bc674d9f9f7ff7cb258ab9b1ccb68c1.tar.gz javassist-1513c3372bc674d9f9f7ff7cb258ab9b1ccb68c1.zip |
Merge pull request #168 from cmelchior/cm/bug/classpath-close
[WIP] Fix leaking file handlers
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/Readme.txt | 20 | ||||
-rw-r--r-- | src/test/javassist/JvstTest.java | 32 | ||||
-rw-r--r-- | src/test/resources/Readme.txt | 16 | ||||
-rwxr-xr-x | src/test/resources/empty.jar | bin | 0 -> 3360 bytes | |||
-rw-r--r-- | src/test/resources/simple.jar | bin | 0 -> 631 bytes |
5 files changed, 66 insertions, 2 deletions
diff --git a/src/test/Readme.txt b/src/test/Readme.txt index 61f7c95f..3218a9d2 100644 --- a/src/test/Readme.txt +++ b/src/test/Readme.txt @@ -1,2 +1,18 @@ -check javassist.JvstTestRoot.PATH and .JAR_PATH and then -run javassist.JvstTest under ./runtest +# How to run tests + +Requirements: +Java JDK 9 +Maven + +1) Build jar file and move it to the top level folder. + + > mvn package + > mv ./target/javassist*-GA.jar ./javaassist.jar + +2) Check that ./src/test/javassist/JvstTestRoot.PATH and .JAR_PATH point to the compiled jar file. + The default is "../../". + +3) Run Tests + + > mvn test + > mvn surefire:test diff --git a/src/test/javassist/JvstTest.java b/src/test/javassist/JvstTest.java index d28c0654..ff58106a 100644 --- a/src/test/javassist/JvstTest.java +++ b/src/test/javassist/JvstTest.java @@ -1,7 +1,9 @@ package javassist; import junit.framework.*; +import java.io.File; import java.io.FileInputStream; +import java.io.InputStream; import java.lang.reflect.Method; import javassist.bytecode.*; import javassist.expr.*; @@ -64,6 +66,36 @@ public class JvstTest extends JvstTestRoot { assertTrue("[class path: ]".equals(pool.toString())); } + public void testReleaseJarClassPathFileHandle() throws Exception { + String jarFileName = "./empty.jar"; + ClassLoader classLoader = getClass().getClassLoader(); + File jarFile = new File(classLoader.getResource(jarFileName).getFile()); + assertTrue(jarFile.exists()); + + // Prepare class pool and force it to open the Jar file + ClassPool pool = ClassPool.getDefault(); + ClassPath cp = pool.appendClassPath(jarFile.getAbsolutePath()); + assertNull(cp.openClassfile("nothere.Dummy")); + + // Assert that it is possible to delete the jar file. + // On Windows deleting an open file will fail, while on on Mac/Linux this is always possible. + // This check will thus only fail on Windos if the file is still open. + assertTrue(jarFile.delete()); + } + + public void testJarClassPath() throws Exception { + String jarFileName = "./simple.jar"; + ClassLoader classLoader = getClass().getClassLoader(); + File jarFile = new File(classLoader.getResource(jarFileName).getFile()); + assertTrue(jarFile.exists()); + + ClassPool pool = ClassPool.getDefault(); + ClassPath cp = pool.appendClassPath(jarFile.getAbsolutePath()); + InputStream is = cp.openClassfile("com.test.Test"); + assertNotNull(is); + is.close(); + } + public void testSubtype() throws Exception { CtClass cc = sloader.get("test1.Subtype"); assertTrue(cc.subtypeOf(cc)); diff --git a/src/test/resources/Readme.txt b/src/test/resources/Readme.txt new file mode 100644 index 00000000..77790dc1 --- /dev/null +++ b/src/test/resources/Readme.txt @@ -0,0 +1,16 @@ +This directory contains files used by the unit tests. + +empty.jar: +An empty, but valid, jar file. + +simple.jar: +Contains a single Java class + +``` +package com.test; + +public class Test { + public Test() { + } +} +``` diff --git a/src/test/resources/empty.jar b/src/test/resources/empty.jar Binary files differnew file mode 100755 index 00000000..3a12dbae --- /dev/null +++ b/src/test/resources/empty.jar diff --git a/src/test/resources/simple.jar b/src/test/resources/simple.jar Binary files differnew file mode 100644 index 00000000..e9455b92 --- /dev/null +++ b/src/test/resources/simple.jar |