1
0

Test does not close input stream

This commit is contained in:
Gary Gregory
2024-08-14 08:48:28 -04:00
parent b3ebff64dc
commit 37cd44ccb2

View File

@@ -16,12 +16,16 @@
*/ */
package org.apache.commons.logging; package org.apache.commons.logging;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.commons.io.IOUtils;
/** /**
* test to emulate container and application isolated from container * test to emulate container and application isolated from container
*/ */
@@ -45,35 +49,22 @@ public class LoadTestCase extends TestCase {
} }
private Class<?> def(final String name) throws ClassNotFoundException { private Class<?> def(final String name) throws ClassNotFoundException {
Class<?> result = classes.get(name); Class<?> result = classes.get(name);
if (result != null) { if (result != null) {
return result; return result;
} }
try { try {
final ClassLoader cl = this.getClass().getClassLoader(); final ClassLoader cl = this.getClass().getClassLoader();
final String classFileName = name.replace('.', '/') + ".class"; final String classFileName = name.replace('.', '/') + ".class";
final java.io.InputStream is = cl.getResourceAsStream(classFileName); try (InputStream is = cl.getResourceAsStream(classFileName)) {
final java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream(); final byte[] data = IOUtils.toByteArray(is);
result = super.defineClass(name, data, 0, data.length);
while (is.available() > 0) { classes.put(name, result);
out.write(is.read()); return result;
} }
} catch (final IOException ioe) {
final byte[] data = out.toByteArray();
result = super.defineClass(name, data, 0, data.length);
classes.put(name, result);
return result;
} catch (final java.io.IOException ioe) {
throw new ClassNotFoundException(name + " caused by " + ioe.getMessage()); throw new ClassNotFoundException(name + " caused by " + ioe.getMessage());
} }
} }
// not very trivial to emulate we must implement "findClass", // not very trivial to emulate we must implement "findClass",