/
VfsTest.java
134 lines (112 loc) · 4.47 KB
/
VfsTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package org.reflections;
import javassist.bytecode.ClassFile;
import org.junit.Test;
import org.reflections.adapters.JavassistAdapter;
import org.reflections.util.ClasspathHelper;
import org.reflections.vfs.SystemDir;
import org.reflections.vfs.Vfs;
import org.slf4j.Logger;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import static java.text.MessageFormat.format;
import static org.junit.Assert.*;
/**
*
*/
public class VfsTest {
@Test
public void testJarFile() throws Exception {
URL url = new URL(ClasspathHelper.forClass(Logger.class).toExternalForm().replace("jar:", ""));
assertTrue(url.toString().startsWith("file:"));
assertTrue(url.toString().contains(".jar"));
assertTrue(Vfs.DefaultUrlTypes.jarFile.matches(url));
assertFalse(Vfs.DefaultUrlTypes.jarUrl.matches(url));
assertFalse(Vfs.DefaultUrlTypes.directory.matches(url));
Vfs.Dir dir = Vfs.DefaultUrlTypes.jarFile.createDir(url);
testVfsDir(dir);
}
@Test
public void testJarUrl() throws Exception {
URL url = ClasspathHelper.forClass(Logger.class);
assertTrue(url.toString().startsWith("jar:file:"));
assertTrue(url.toString().contains(".jar!"));
assertFalse(Vfs.DefaultUrlTypes.jarFile.matches(url));
assertTrue(Vfs.DefaultUrlTypes.jarUrl.matches(url));
assertFalse(Vfs.DefaultUrlTypes.directory.matches(url));
Vfs.Dir dir = Vfs.DefaultUrlTypes.jarUrl.createDir(url);
testVfsDir(dir);
}
@Test
public void testDirectory() throws Exception {
URL url = ClasspathHelper.forClass(getClass());
assertTrue(url.toString().startsWith("file:"));
assertFalse(url.toString().contains(".jar"));
assertFalse(Vfs.DefaultUrlTypes.jarFile.matches(url));
assertFalse(Vfs.DefaultUrlTypes.jarUrl.matches(url));
assertTrue(Vfs.DefaultUrlTypes.directory.matches(url));
Vfs.Dir dir = Vfs.DefaultUrlTypes.directory.createDir(url);
testVfsDir(dir);
}
@Test
public void testJarInputStream() throws Exception {
URL url = ClasspathHelper.forClass(Logger.class);
assertTrue(Vfs.DefaultUrlTypes.jarInputStream.matches(url));
try {
testVfsDir(Vfs.DefaultUrlTypes.jarInputStream.createDir(url));
fail();
} catch (ReflectionsException e) {
// expected
}
url = new URL(ClasspathHelper.forClass(Logger.class).toExternalForm().replace("jar:", "").replace(".jar!", ".jar"));
assertTrue(Vfs.DefaultUrlTypes.jarInputStream.matches(url));
testVfsDir(Vfs.DefaultUrlTypes.jarInputStream.createDir(url));
url = ClasspathHelper.forClass(getClass());
assertFalse(Vfs.DefaultUrlTypes.jarInputStream.matches(url));
try {
testVfsDir(Vfs.DefaultUrlTypes.jarInputStream.createDir(url));
fail();
} catch (NullPointerException e) {
// expected
}
}
@Test
public void dirWithSpaces() {
Collection<URL> urls = ClasspathHelper.forPackage("dir+with spaces");
assertFalse(urls.isEmpty());
for (URL url : urls) {
Vfs.Dir dir = Vfs.fromURL(url);
assertNotNull(dir);
assertNotNull(dir.getFiles().iterator().next());
}
}
@Test
public void vfsFromDirWithJarInName() throws MalformedURLException {
String tmpFolder = System.getProperty("java.io.tmpdir");
tmpFolder = tmpFolder.endsWith(File.separator) ? tmpFolder : tmpFolder + File.separator;
String dirWithJarInName = tmpFolder + "tony.jarvis";
File newDir = new File(dirWithJarInName);
newDir.mkdir();
try {
Vfs.Dir dir = Vfs.fromURL(new URL(format("file:{0}", dirWithJarInName)));
assertEquals(dirWithJarInName.replace("\\", "/"), dir.getPath());
assertEquals(SystemDir.class, dir.getClass());
} finally {
newDir.delete();
}
}
private void testVfsDir(Vfs.Dir dir) {
JavassistAdapter mdAdapter = new JavassistAdapter();
Vfs.File file = null;
for (Vfs.File f : dir.getFiles()) {
if (f.getRelativePath().endsWith(".class")) {
file = f;
break;
}
}
ClassFile stringCF = mdAdapter.getOrCreateClassObject(file);
String className = mdAdapter.getClassName(stringCF);
assertFalse(className.isEmpty());
}
}