aboutsummaryrefslogtreecommitdiffstats
path: root/guava-tests/test/com/google/common/reflect/ClassPathTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'guava-tests/test/com/google/common/reflect/ClassPathTest.java')
-rw-r--r--guava-tests/test/com/google/common/reflect/ClassPathTest.java188
1 files changed, 136 insertions, 52 deletions
diff --git a/guava-tests/test/com/google/common/reflect/ClassPathTest.java b/guava-tests/test/com/google/common/reflect/ClassPathTest.java
index ccf7508..bd63635 100644
--- a/guava-tests/test/com/google/common/reflect/ClassPathTest.java
+++ b/guava-tests/test/com/google/common/reflect/ClassPathTest.java
@@ -20,9 +20,10 @@ import static org.truth0.Truth.ASSERT;
import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
+import com.google.common.io.Closer;
+import com.google.common.io.Resources;
import com.google.common.reflect.ClassPath.ClassInfo;
import com.google.common.reflect.ClassPath.ResourceInfo;
import com.google.common.reflect.subpackage.ClassInSubPackage;
@@ -34,16 +35,20 @@ import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.File;
+import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
-import java.util.Collection;
import java.util.Map;
import java.util.Set;
+import java.util.jar.Attributes;
+import java.util.jar.JarFile;
+import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
+import java.util.zip.ZipEntry;
/**
* Functional tests of {@link ClassPath}.
@@ -55,29 +60,58 @@ public class ClassPathTest extends TestCase {
Map<String, ResourceInfo> byToString = Maps.newHashMap();
ClassPath classpath = ClassPath.from(getClass().getClassLoader());
for (ResourceInfo resource : classpath.getResources()) {
+ ASSERT.that(resource.getResourceName()).isNotEqualTo(JarFile.MANIFEST_NAME);
+ ASSERT.that(resource.toString()).isNotEqualTo(JarFile.MANIFEST_NAME);
byName.put(resource.getResourceName(), resource);
byToString.put(resource.toString(), resource);
// TODO: This will fail on maven resources in the classes directory on a mac.
// assertNotNull(resource.url());
}
String testResourceName = "com/google/common/reflect/test.txt";
- ASSERT.<String, Collection<String>>that(byName.keySet()).has().allOf(
+ ASSERT.that(byName.keySet()).has().allOf(
"com/google/common/reflect/ClassPath.class",
"com/google/common/reflect/ClassPathTest.class",
"com/google/common/reflect/ClassPathTest$Nested.class",
testResourceName);
- assertFalse(byName.keySet().contains("META-INF/MANIFEST.MF"));
- ASSERT.<String, Collection<String>>that(byToString.keySet()).has().allOf(
+ ASSERT.that(byToString.keySet()).has().allOf(
"com.google.common.reflect.ClassPath",
"com.google.common.reflect.ClassPathTest",
- "com/google/common/reflect/ClassPathTest$Nested.class",
+ "com.google.common.reflect.ClassPathTest$Nested",
testResourceName);
- assertFalse(byToString.keySet().contains("META-INF/MANIFEST.MF"));
assertEquals(getClass().getClassLoader().getResource(testResourceName),
byName.get("com/google/common/reflect/test.txt").url());
}
- public void testGetClasses() throws Exception {
+ public void testGetAllClasses() throws Exception {
+ Set<String> names = Sets.newHashSet();
+ Set<String> strings = Sets.newHashSet();
+ Set<Class<?>> classes = Sets.newHashSet();
+ Set<String> packageNames = Sets.newHashSet();
+ Set<String> simpleNames = Sets.newHashSet();
+ ClassPath classpath = ClassPath.from(getClass().getClassLoader());
+ for (ClassInfo classInfo : classpath.getAllClasses()) {
+ if (!classInfo.getPackageName().equals(ClassPathTest.class.getPackage().getName())) {
+ continue;
+ }
+ names.add(classInfo.getName());
+ strings.add(classInfo.toString());
+ classes.add(classInfo.load());
+ packageNames.add(classInfo.getPackageName());
+ simpleNames.add(classInfo.getSimpleName());
+ }
+ class LocalClass {}
+ Class<?> anonymousClass = new Object() {}.getClass();
+ ASSERT.that(names).has().allOf(anonymousClass.getName(), LocalClass.class.getName(),
+ ClassPath.class.getName(), ClassPathTest.class.getName());
+ ASSERT.that(strings).has().allOf(anonymousClass.getName(), LocalClass.class.getName(),
+ ClassPath.class.getName(), ClassPathTest.class.getName());
+ ASSERT.that(classes).has().allOf(anonymousClass, LocalClass.class, ClassPath.class,
+ ClassPathTest.class);
+ ASSERT.that(packageNames).has().exactly(ClassPath.class.getPackage().getName());
+ ASSERT.that(simpleNames).has().allOf("", "Local", "ClassPath", "ClassPathTest");
+ }
+
+ public void testGetTopLevelClasses() throws Exception {
Set<String> names = Sets.newHashSet();
Set<String> strings = Sets.newHashSet();
Set<Class<?>> classes = Sets.newHashSet();
@@ -92,25 +126,28 @@ public class ClassPathTest extends TestCase {
packageNames.add(classInfo.getPackageName());
simpleNames.add(classInfo.getSimpleName());
}
- ASSERT.<String, Collection<String>>that(names).has().allOf(ClassPath.class.getName(), ClassPathTest.class.getName());
- ASSERT.<String, Collection<String>>that(strings).has().allOf(ClassPath.class.getName(), ClassPathTest.class.getName());
- ASSERT.<Class<?>, Collection<Class<?>>>that(classes).has().allOf(ClassPath.class, ClassPathTest.class);
- ASSERT.<String, Collection<String>>that(packageNames).has().item(ClassPath.class.getPackage().getName());
- ASSERT.<String, Collection<String>>that(simpleNames).has().allOf("ClassPath", "ClassPathTest");
+ ASSERT.that(names).has().allOf(ClassPath.class.getName(), ClassPathTest.class.getName());
+ ASSERT.that(strings).has().allOf(ClassPath.class.getName(), ClassPathTest.class.getName());
+ ASSERT.that(classes).has().allOf(ClassPath.class, ClassPathTest.class);
+ ASSERT.that(packageNames).has().item(ClassPath.class.getPackage().getName());
+ ASSERT.that(simpleNames).has().allOf("ClassPath", "ClassPathTest");
assertFalse(classes.contains(ClassInSubPackage.class));
}
- public void testGetClassesRecursive() throws Exception {
+ public void testGetTopLevelClassesRecursive() throws Exception {
Set<Class<?>> classes = Sets.newHashSet();
ClassPath classpath = ClassPath.from(ClassPathTest.class.getClassLoader());
for (ClassInfo classInfo
: classpath.getTopLevelClassesRecursive(ClassPathTest.class.getPackage().getName())) {
+ if (classInfo.getName().contains("ClassPathTest")) {
+ System.err.println("");
+ }
classes.add(classInfo.load());
}
- ASSERT.<Class<?>, Collection<Class<?>>>that(classes).has().allOf(ClassPathTest.class, ClassInSubPackage.class);
+ ASSERT.that(classes).has().allOf(ClassPathTest.class, ClassInSubPackage.class);
}
- public void testGetClasses_diamond() throws Exception {
+ public void testGetTopLevelClasses_diamond() throws Exception {
ClassLoader parent = ClassPathTest.class.getClassLoader();
ClassLoader sub1 = new ClassLoader(parent) {};
ClassLoader sub2 = new ClassLoader(parent) {};
@@ -131,7 +168,7 @@ public class ClassPathTest extends TestCase {
}
public void testClassPathEntries_emptyURLClassLoader_noParent() {
- ASSERT.<URI, Collection<URI>>that(ClassPath.getClassPathEntries(new URLClassLoader(new URL[0], null)).keySet())
+ ASSERT.that(ClassPath.getClassPathEntries(new URLClassLoader(new URL[0], null)).keySet())
.isEmpty();
}
@@ -151,7 +188,7 @@ public class ClassPathTest extends TestCase {
URLClassLoader child = new URLClassLoader(new URL[] {url2}, parent) {};
ImmutableMap<URI, ClassLoader> classPathEntries = ClassPath.getClassPathEntries(child);
assertEquals(ImmutableMap.of(url1.toURI(), parent, url2.toURI(), child), classPathEntries);
- ASSERT.<URI, Collection<URI>>that(classPathEntries.keySet()).has().allOf(url1.toURI(), url2.toURI()).inOrder();
+ ASSERT.that(classPathEntries.keySet()).has().exactly(url1.toURI(), url2.toURI()).inOrder();
}
public void testClassPathEntries_duplicateUri_parentWins() throws Exception {
@@ -162,7 +199,7 @@ public class ClassPathTest extends TestCase {
}
public void testClassPathEntries_notURLClassLoader_noParent() {
- ASSERT.<URI, Collection<URI>>that(ClassPath.getClassPathEntries(new ClassLoader(null) {}).keySet()).isEmpty();
+ ASSERT.that(ClassPath.getClassPathEntries(new ClassLoader(null) {}).keySet()).isEmpty();
}
public void testClassPathEntries_notURLClassLoader_withParent() throws Exception {
@@ -192,56 +229,69 @@ public class ClassPathTest extends TestCase {
ClassPath.getClassPathEntries(new ClassLoader(parent) {}));
}
- public void testBrowseFromFile_fileNotExists() throws IOException {
+ public void testScan_classPathCycle() throws IOException {
+ File jarFile = File.createTempFile("with_circular_class_path", ".jar");
+ try {
+ writeSelfReferencingJarFile(jarFile, "test.txt");
+ ClassPath.Scanner scanner = new ClassPath.Scanner();
+ scanner.scan(jarFile.toURI(), ClassPathTest.class.getClassLoader());
+ assertEquals(1, scanner.getResources().size());
+ } finally {
+ jarFile.delete();
+ }
+ }
+
+ public void testScanFromFile_fileNotExists() throws IOException {
ClassLoader classLoader = ClassPathTest.class.getClassLoader();
- ImmutableSet.Builder<ResourceInfo> resources = ImmutableSet.builder();
- ClassPath.browseFrom(new File("no/such/file/anywhere"), classLoader, resources);
- ASSERT.<ResourceInfo, Collection<ResourceInfo>>that(resources.build()).isEmpty();
+ ClassPath.Scanner scanner = new ClassPath.Scanner();
+ scanner.scanFrom(new File("no/such/file/anywhere"), classLoader);
+ ASSERT.that(scanner.getResources()).isEmpty();
}
- public void testBrowseFromFile_notJarFile() throws IOException {
+ public void testScanFromFile_notJarFile() throws IOException {
ClassLoader classLoader = ClassPathTest.class.getClassLoader();
- ImmutableSet.Builder<ResourceInfo> resources = ImmutableSet.builder();
File notJar = File.createTempFile("not_a_jar", "txt");
+ ClassPath.Scanner scanner = new ClassPath.Scanner();
try {
- ClassPath.browseFrom(notJar, classLoader, resources);
+ scanner.scanFrom(notJar, classLoader);
} finally {
notJar.delete();
}
- ASSERT.<ResourceInfo, Collection<ResourceInfo>>that(resources.build()).isEmpty();
+ ASSERT.that(scanner.getResources()).isEmpty();
}
public void testGetClassPathEntry() throws URISyntaxException {
assertEquals(URI.create("file:/usr/test/dep.jar"),
- ClassPath.getClassPathEntry(new File("/home/build/outer.jar"), "file:/usr/test/dep.jar"));
+ ClassPath.Scanner.getClassPathEntry(
+ new File("/home/build/outer.jar"), "file:/usr/test/dep.jar"));
assertEquals(URI.create("file:/home/build/a.jar"),
- ClassPath.getClassPathEntry(new File("/home/build/outer.jar"), "a.jar"));
+ ClassPath.Scanner.getClassPathEntry(new File("/home/build/outer.jar"), "a.jar"));
assertEquals(URI.create("file:/home/build/x/y/z"),
- ClassPath.getClassPathEntry(new File("/home/build/outer.jar"), "x/y/z"));
+ ClassPath.Scanner.getClassPathEntry(new File("/home/build/outer.jar"), "x/y/z"));
assertEquals(URI.create("file:/home/build/x/y/z.jar"),
- ClassPath.getClassPathEntry(new File("/home/build/outer.jar"), "x/y/z.jar"));
+ ClassPath.Scanner.getClassPathEntry(new File("/home/build/outer.jar"), "x/y/z.jar"));
}
public void testGetClassPathFromManifest_nullManifest() {
- ASSERT.<URI, Collection<URI>>that(ClassPath.getClassPathFromManifest(new File("some.jar"), null)).isEmpty();
+ ASSERT.that(ClassPath.Scanner.getClassPathFromManifest(new File("some.jar"), null)).isEmpty();
}
public void testGetClassPathFromManifest_noClassPath() throws IOException {
File jarFile = new File("base.jar");
- ASSERT.<URI, Collection<URI>>that(ClassPath.getClassPathFromManifest(jarFile, manifest("")))
+ ASSERT.that(ClassPath.Scanner.getClassPathFromManifest(jarFile, manifest("")))
.isEmpty();
}
public void testGetClassPathFromManifest_emptyClassPath() throws IOException {
File jarFile = new File("base.jar");
- ASSERT.<URI, Collection<URI>>that(ClassPath.getClassPathFromManifest(jarFile, manifestClasspath("")))
+ ASSERT.that(ClassPath.Scanner.getClassPathFromManifest(jarFile, manifestClasspath("")))
.isEmpty();
}
public void testGetClassPathFromManifest_badClassPath() throws IOException {
File jarFile = new File("base.jar");
Manifest manifest = manifestClasspath("an_invalid^path");
- ASSERT.<URI, Collection<URI>>that(ClassPath.getClassPathFromManifest(jarFile, manifest))
+ ASSERT.that(ClassPath.Scanner.getClassPathFromManifest(jarFile, manifest))
.isEmpty();
}
@@ -249,45 +299,45 @@ public class ClassPathTest extends TestCase {
File jarFile = new File("base/some.jar");
// with/relative/directory is the Class-Path value in the mf file.
Manifest manifest = manifestClasspath("with/relative/dir");
- ASSERT.<URI, Collection<URI>>that(ClassPath.getClassPathFromManifest(jarFile, manifest))
- .has().allOf(new File("base/with/relative/dir").toURI()).inOrder();
+ ASSERT.that(ClassPath.Scanner.getClassPathFromManifest(jarFile, manifest))
+ .has().exactly(new File("base/with/relative/dir").toURI()).inOrder();
}
public void testGetClassPathFromManifest_relativeJar() throws IOException {
File jarFile = new File("base/some.jar");
// with/relative/directory is the Class-Path value in the mf file.
Manifest manifest = manifestClasspath("with/relative.jar");
- ASSERT.<URI, Collection<URI>>that(ClassPath.getClassPathFromManifest(jarFile, manifest))
- .has().allOf(new File("base/with/relative.jar").toURI()).inOrder();
+ ASSERT.that(ClassPath.Scanner.getClassPathFromManifest(jarFile, manifest))
+ .has().exactly(new File("base/with/relative.jar").toURI()).inOrder();
}
public void testGetClassPathFromManifest_jarInCurrentDirectory() throws IOException {
File jarFile = new File("base/some.jar");
// with/relative/directory is the Class-Path value in the mf file.
Manifest manifest = manifestClasspath("current.jar");
- ASSERT.<URI, Collection<URI>>that(ClassPath.getClassPathFromManifest(jarFile, manifest))
- .has().allOf(new File("base/current.jar").toURI()).inOrder();
+ ASSERT.that(ClassPath.Scanner.getClassPathFromManifest(jarFile, manifest))
+ .has().exactly(new File("base/current.jar").toURI()).inOrder();
}
public void testGetClassPathFromManifest_absoluteDirectory() throws IOException {
File jarFile = new File("base/some.jar");
Manifest manifest = manifestClasspath("file:/with/absolute/dir");
- ASSERT.<URI, Collection<URI>>that(ClassPath.getClassPathFromManifest(jarFile, manifest))
- .has().allOf(new File("/with/absolute/dir").toURI()).inOrder();
+ ASSERT.that(ClassPath.Scanner.getClassPathFromManifest(jarFile, manifest))
+ .has().exactly(new File("/with/absolute/dir").toURI()).inOrder();
}
public void testGetClassPathFromManifest_absoluteJar() throws IOException {
File jarFile = new File("base/some.jar");
Manifest manifest = manifestClasspath("file:/with/absolute.jar");
- ASSERT.<URI, Collection<URI>>that(ClassPath.getClassPathFromManifest(jarFile, manifest))
- .has().allOf(new File("/with/absolute.jar").toURI()).inOrder();
+ ASSERT.that(ClassPath.Scanner.getClassPathFromManifest(jarFile, manifest))
+ .has().exactly(new File("/with/absolute.jar").toURI()).inOrder();
}
public void testGetClassPathFromManifest_multiplePaths() throws IOException {
File jarFile = new File("base/some.jar");
Manifest manifest = manifestClasspath("file:/with/absolute.jar relative.jar relative/dir");
- ASSERT.<URI, Collection<URI>>that(ClassPath.getClassPathFromManifest(jarFile, manifest))
- .has().allOf(
+ ASSERT.that(ClassPath.Scanner.getClassPathFromManifest(jarFile, manifest))
+ .has().exactly(
new File("/with/absolute.jar").toURI(),
new File("base/relative.jar").toURI(),
new File("base/relative/dir").toURI())
@@ -297,15 +347,15 @@ public class ClassPathTest extends TestCase {
public void testGetClassPathFromManifest_leadingBlanks() throws IOException {
File jarFile = new File("base/some.jar");
Manifest manifest = manifestClasspath(" relative.jar");
- ASSERT.<URI, Collection<URI>>that(ClassPath.getClassPathFromManifest(jarFile, manifest))
- .has().allOf(new File("base/relative.jar").toURI()).inOrder();
+ ASSERT.that(ClassPath.Scanner.getClassPathFromManifest(jarFile, manifest))
+ .has().exactly(new File("base/relative.jar").toURI()).inOrder();
}
public void testGetClassPathFromManifest_trailingBlanks() throws IOException {
File jarFile = new File("base/some.jar");
Manifest manifest = manifestClasspath("relative.jar ");
- ASSERT.<URI, Collection<URI>>that(ClassPath.getClassPathFromManifest(jarFile, manifest))
- .has().allOf(new File("base/relative.jar").toURI()).inOrder();
+ ASSERT.that(ClassPath.Scanner.getClassPathFromManifest(jarFile, manifest))
+ .has().exactly(new File("base/relative.jar").toURI()).inOrder();
}
public void testGetClassName() {
@@ -315,7 +365,7 @@ public class ClassPathTest extends TestCase {
public void testResourceInfo_of() {
assertEquals(ClassInfo.class, resourceInfo(ClassPathTest.class).getClass());
assertEquals(ClassInfo.class, resourceInfo(ClassPath.class).getClass());
- assertEquals(ResourceInfo.class, resourceInfo(Nested.class).getClass());
+ assertEquals(ClassInfo.class, resourceInfo(Nested.class).getClass());
}
public void testGetSimpleName() {
@@ -323,6 +373,17 @@ public class ClassPathTest extends TestCase {
new ClassInfo("Foo.class", getClass().getClassLoader()).getSimpleName());
assertEquals("Foo",
new ClassInfo("a/b/Foo.class", getClass().getClassLoader()).getSimpleName());
+ assertEquals("Foo",
+ new ClassInfo("a/b/Bar$Foo.class", getClass().getClassLoader()).getSimpleName());
+ assertEquals("",
+ new ClassInfo("a/b/Bar$1.class", getClass().getClassLoader()).getSimpleName());
+ assertEquals("Foo",
+ new ClassInfo("a/b/Bar$Foo.class", getClass().getClassLoader()).getSimpleName());
+ assertEquals("",
+ new ClassInfo("a/b/Bar$1.class", getClass().getClassLoader()).getSimpleName());
+ assertEquals("Local",
+ new ClassInfo("a/b/Bar$1Local.class", getClass().getClassLoader()).getSimpleName());
+
}
public void testGetPackageName() {
@@ -366,6 +427,29 @@ public class ClassPathTest extends TestCase {
return manifest("Class-Path: " + classpath + "\n");
}
+ private static void writeSelfReferencingJarFile(File jarFile, String... entries)
+ throws IOException {
+ Manifest manifest = new Manifest();
+ // Without version, the manifest is silently ignored. Ugh!
+ manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
+ manifest.getMainAttributes().put(Attributes.Name.CLASS_PATH, jarFile.getName());
+
+ Closer closer = Closer.create();
+ try {
+ FileOutputStream fileOut = closer.register(new FileOutputStream(jarFile));
+ JarOutputStream jarOut = closer.register(new JarOutputStream(fileOut));
+ for (String entry : entries) {
+ jarOut.putNextEntry(new ZipEntry(entry));
+ Resources.copy(ClassPathTest.class.getResource(entry), jarOut);
+ jarOut.closeEntry();
+ }
+ } catch (Throwable e) {
+ throw closer.rethrow(e);
+ } finally {
+ closer.close();
+ }
+ }
+
private static Manifest manifest(String content) throws IOException {
InputStream in = new ByteArrayInputStream(content.getBytes(Charsets.US_ASCII.name()));
Manifest manifest = new Manifest();