Skip to content

Commit

Permalink
re #836: skipping thread safety checks for http4 and sftp
Browse files Browse the repository at this point in the history
  • Loading branch information
ivakegg committed May 7, 2023
1 parent cfcc1db commit 5dc421d
Showing 1 changed file with 138 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.FileSystemManager;
import org.apache.commons.vfs2.FileType;
import org.apache.commons.vfs2.operations.FileOperationProvider;
import org.junit.Test;

import java.io.File;
Expand All @@ -39,6 +40,7 @@
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.BrokenBarrierException;
Expand Down Expand Up @@ -117,136 +119,145 @@ protected Capability[] getRequiredCapabilities() {
return new Capability[] { Capability.READ_CONTENT, Capability.URI };
}

// /**
// * Tests retrieving resources (from JAR searchpath).
// * <p>
// * This is run for all providers, but only when a local provider is present and jar extension is registered it will
// * actually carry out all tests.
// * </p>
// */
// @Test
// public void testGetResourcesJARs() throws Exception {
// final FileSystemManager manager = getManager();
// try {
// // hasProvider("file") cannot be used as it triggers default provider URL
// manager.toFileObject(new File("."));
// } catch (final FileSystemException e) {
// System.out.println("VfsClassLoaderTests no local file provider, skipping.");
// return;
// }
//
// // build search path without using #getBaseFolder()
// // because NestedJarTestCase redefines it
// final File baseDir = getTestDirectoryFile();
// final FileObject nestedJar = manager.resolveFile(baseDir, "nested.jar");
// final FileObject testJar = manager.resolveFile(baseDir, "test.jar");
//
// // test setup needs to know about .jar extension - i.e. NestedJarTestCase
// if (!manager.canCreateFileSystem(nestedJar)) {
// System.out.println("VfsClassLoaderTests no layered .jar provider, skipping.");
// return;
// }
//
// // verify test setup
// assertSame("nested.jar is required for testing", nestedJar.getType(), FileType.FILE);
// assertSame("test.jar is required for testing", testJar.getType(), FileType.FILE);
//
// // System class loader (null) might be unpredictable in regards
// // to returning resources for META-INF/MANIFEST.MF (see VFS-500)
// // so we use our own which is guaranteed to not return any hit
// final ClassLoader mockClassloader = new MockClassloader();
// final FileObject[] search = { nestedJar, testJar };
// final VFSClassLoader loader = new VFSClassLoader(search, getManager(), mockClassloader);
//
// final Enumeration<URL> urls = loader.getResources("META-INF/MANIFEST.MF");
// final URL url1 = urls.nextElement();
// final URL url2 = urls.nextElement();
//
// assertTrue("First resource must refer to nested.jar but was " + url1,
// url1.toString().endsWith("nested.jar!/META-INF/MANIFEST.MF"));
// assertTrue("Second resource must refer to test.jar but was " + url2,
// url2.toString().endsWith("test.jar!/META-INF/MANIFEST.MF"));
// }
//
// /**
// * Tests retrieving resources (from local directory with .jar extension).
// * <p>
// * This test is repeated with various provider configurations but works on local files, only.
// * </p>
// */
// @Test
// public void testGetResourcesNoLayerLocal() throws Exception {
// final FileSystemManager manager = getManager();
// try {
// // hasProvider("file") cannot be used as it triggers default provider URL
// manager.toFileObject(new File("."));
// } catch (final FileSystemException e) {
// System.out.println("VfsClassLoaderTests no local file provider, skipping.");
// return;
// }
// final File baseDir = getTestDirectoryFile();
//
// // setup test folder
// final FileObject dir = manager.resolveFile(baseDir, "read-tests/dir1/subdir4.jar");
// assertSame("subdir4.jar/ is required for testing " + dir, dir.getType(), FileType.FOLDER);
// assertFalse(manager.canCreateFileSystem(dir));
//
// // prepare classloader
// final FileObject[] search = { dir };
// final ClassLoader mockClassloader = new MockClassloader();
// final VFSClassLoader loader = new VFSClassLoader(search, getManager(), mockClassloader);
//
// // verify resource loading
// final Enumeration<URL> urls = loader.getResources("file1.txt");
// final URL url1 = urls.nextElement();
// assertFalse("Only one hit expected", urls.hasMoreElements());
// assertTrue("not pointing to resource " + url1, url1.toString().endsWith("subdir4.jar/file1.txt"));
// }
//
// /**
// * Tests loading a class.
// */
// @Test
// public void testLoadClass() throws Exception {
// final VFSClassLoader loader = createClassLoader();
//
// final Class<?> testClass = loader.loadClass("code.ClassToLoad");
// final Package pack = testClass.getPackage();
// assertEquals("code", pack.getName());
// verifyPackage(pack, false);
//
// final Object testObject = testClass.newInstance();
// assertEquals("**PRIVATE**", testObject.toString());
// }
//
// /**
// * Tests loading a resource.
// */
// @Test
// public void testLoadResource() throws Exception {
// final VFSClassLoader loader = createClassLoader();
//
// final URL resource = loader.getResource("read-tests/file1.txt");
//
// assertNotNull(resource);
// final URLConnection urlCon = resource.openConnection();
// assertSameURLContent(FILE1_CONTENT, urlCon);
// }
//
// /**
// * Tests package sealing.
// */
// @Test
// public void testSealing() throws Exception {
// final VFSClassLoader loader = createClassLoader();
// final Class<?> testClass = loader.loadClass("code.sealed.AnotherClass");
// final Package pack = testClass.getPackage();
// assertEquals("code.sealed", pack.getName());
// verifyPackage(pack, true);
// }
/**
* Tests retrieving resources (from JAR searchpath).
* <p>
* This is run for all providers, but only when a local provider is present and jar extension is registered it will
* actually carry out all tests.
* </p>
*/
@Test
public void testGetResourcesJARs() throws Exception {
final FileSystemManager manager = getManager();
try {
// hasProvider("file") cannot be used as it triggers default provider URL
manager.toFileObject(new File("."));
} catch (final FileSystemException e) {
System.out.println("VfsClassLoaderTests no local file provider, skipping.");
return;
}

// build search path without using #getBaseFolder()
// because NestedJarTestCase redefines it
final File baseDir = getTestDirectoryFile();
final FileObject nestedJar = manager.resolveFile(baseDir, "nested.jar");
final FileObject testJar = manager.resolveFile(baseDir, "test.jar");

// test setup needs to know about .jar extension - i.e. NestedJarTestCase
if (!manager.canCreateFileSystem(nestedJar)) {
System.out.println("VfsClassLoaderTests no layered .jar provider, skipping.");
return;
}

// verify test setup
assertSame("nested.jar is required for testing", nestedJar.getType(), FileType.FILE);
assertSame("test.jar is required for testing", testJar.getType(), FileType.FILE);

// System class loader (null) might be unpredictable in regards
// to returning resources for META-INF/MANIFEST.MF (see VFS-500)
// so we use our own which is guaranteed to not return any hit
final ClassLoader mockClassloader = new MockClassloader();
final FileObject[] search = { nestedJar, testJar };
final VFSClassLoader loader = new VFSClassLoader(search, getManager(), mockClassloader);

final Enumeration<URL> urls = loader.getResources("META-INF/MANIFEST.MF");
final URL url1 = urls.nextElement();
final URL url2 = urls.nextElement();

assertTrue("First resource must refer to nested.jar but was " + url1,
url1.toString().endsWith("nested.jar!/META-INF/MANIFEST.MF"));
assertTrue("Second resource must refer to test.jar but was " + url2,
url2.toString().endsWith("test.jar!/META-INF/MANIFEST.MF"));
}

/**
* Tests retrieving resources (from local directory with .jar extension).
* <p>
* This test is repeated with various provider configurations but works on local files, only.
* </p>
*/
@Test
public void testGetResourcesNoLayerLocal() throws Exception {
final FileSystemManager manager = getManager();
try {
// hasProvider("file") cannot be used as it triggers default provider URL
manager.toFileObject(new File("."));
} catch (final FileSystemException e) {
System.out.println("VfsClassLoaderTests no local file provider, skipping.");
return;
}
final File baseDir = getTestDirectoryFile();

// setup test folder
final FileObject dir = manager.resolveFile(baseDir, "read-tests/dir1/subdir4.jar");
assertSame("subdir4.jar/ is required for testing " + dir, dir.getType(), FileType.FOLDER);
assertFalse(manager.canCreateFileSystem(dir));

// prepare classloader
final FileObject[] search = { dir };
final ClassLoader mockClassloader = new MockClassloader();
final VFSClassLoader loader = new VFSClassLoader(search, getManager(), mockClassloader);

// verify resource loading
final Enumeration<URL> urls = loader.getResources("file1.txt");
final URL url1 = urls.nextElement();
assertFalse("Only one hit expected", urls.hasMoreElements());
assertTrue("not pointing to resource " + url1, url1.toString().endsWith("subdir4.jar/file1.txt"));
}

/**
* Tests loading a class.
*/
@Test
public void testLoadClass() throws Exception {
final VFSClassLoader loader = createClassLoader();

final Class<?> testClass = loader.loadClass("code.ClassToLoad");
final Package pack = testClass.getPackage();
assertEquals("code", pack.getName());
verifyPackage(pack, false);

final Object testObject = testClass.newInstance();
assertEquals("**PRIVATE**", testObject.toString());
}

/**
* Tests loading a resource.
*/
@Test
public void testLoadResource() throws Exception {
final VFSClassLoader loader = createClassLoader();

final URL resource = loader.getResource("read-tests/file1.txt");

assertNotNull(resource);
final URLConnection urlCon = resource.openConnection();
assertSameURLContent(FILE1_CONTENT, urlCon);
}

/**
* Tests package sealing.
*/
@Test
public void testSealing() throws Exception {
final VFSClassLoader loader = createClassLoader();
final Class<?> testClass = loader.loadClass("code.sealed.AnotherClass");
final Package pack = testClass.getPackage();
assertEquals("code.sealed", pack.getName());
verifyPackage(pack, true);
}

@Test
public void testThreadSafety() throws Exception {
final FileSystemManager manager = getManager();

// The http4 and sftp mechanisms do not work with this thread safety test
List<String> schemes = Arrays.asList(manager.getSchemes());
if (schemes.contains("http4") || schemes.contains("sftp")) {
System.out.println("VfsClassLoaderTests skipping thread safety test for schemes :" + schemes);
return;
}

// note THREADS must be an even number
final int THREADS = 20;
final BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(THREADS * 2);
Expand Down

0 comments on commit 5dc421d

Please sign in to comment.