-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
ClasspathScannerTests.java
411 lines (332 loc) · 15.3 KB
/
ClasspathScannerTests.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
* Copyright 2015-2024 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at
*
* https://www.eclipse.org/legal/epl-v20.html
*/
package org.junit.platform.commons.util;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeFalse;
import static org.junit.platform.commons.test.ConcurrencyTestingUtils.executeConcurrently;
import java.io.IOException;
import java.lang.module.ModuleFinder;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.FileSystemNotFoundException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Predicate;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.spi.ToolProvider;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.fixtures.TrackLogRecords;
import org.junit.jupiter.api.io.TempDir;
import org.junit.platform.commons.PreconditionViolationException;
import org.junit.platform.commons.function.Try;
import org.junit.platform.commons.logging.LogRecordListener;
/**
* Unit tests for {@link ClasspathScanner}.
*
* @since 1.0
*/
@TrackLogRecords
class ClasspathScannerTests {
private static final ClassFilter allClasses = ClassFilter.of(type -> true);
private final List<Class<?>> loadedClasses = new ArrayList<>();
private final BiFunction<String, ClassLoader, Try<Class<?>>> trackingClassLoader = (name,
classLoader) -> ReflectionUtils.tryToLoadClass(name, classLoader).ifSuccess(loadedClasses::add);
private final ClasspathScanner classpathScanner = new ClasspathScanner(ClassLoaderUtils::getDefaultClassLoader,
trackingClassLoader);
@Test
void scanForClassesInClasspathRootWhenMalformedClassnameInternalErrorOccursWithNullDetailedMessage(
LogRecordListener listener) throws Exception {
Predicate<Class<?>> malformedClassNameSimulationFilter = clazz -> {
if (clazz.getSimpleName().equals(ClassForMalformedClassNameSimulation.class.getSimpleName())) {
throw new InternalError();
}
return true;
};
assertClassesScannedWhenExceptionIsThrown(malformedClassNameSimulationFilter);
assertDebugMessageLogged(listener, "Failed to load java.lang.Class for path .+ during classpath scanning.");
}
@Test
void scanForClassesInClasspathRootWhenMalformedClassnameInternalErrorOccurs(LogRecordListener listener)
throws Exception {
Predicate<Class<?>> malformedClassNameSimulationFilter = clazz -> {
if (clazz.getSimpleName().equals(ClassForMalformedClassNameSimulation.class.getSimpleName())) {
throw new InternalError("Malformed class name");
}
return true;
};
assertClassesScannedWhenExceptionIsThrown(malformedClassNameSimulationFilter);
assertDebugMessageLogged(listener, "The java.lang.Class loaded from path .+ has a malformed class name .+");
}
@Test
void scanForClassesInClasspathRootWhenOtherInternalErrorOccurs(LogRecordListener listener) throws Exception {
Predicate<Class<?>> otherInternalErrorSimulationFilter = clazz -> {
if (clazz.getSimpleName().equals(ClassForOtherInternalErrorSimulation.class.getSimpleName())) {
throw new InternalError("other internal error");
}
return true;
};
assertClassesScannedWhenExceptionIsThrown(otherInternalErrorSimulationFilter);
assertDebugMessageLogged(listener, "Failed to load java.lang.Class for path .+ during classpath scanning.");
}
@Test
void scanForClassesInClasspathRootWhenGenericRuntimeExceptionOccurs(LogRecordListener listener) throws Exception {
Predicate<Class<?>> runtimeExceptionSimulationFilter = clazz -> {
if (clazz.getSimpleName().equals(ClassForGenericRuntimeExceptionSimulation.class.getSimpleName())) {
throw new RuntimeException("a generic exception");
}
return true;
};
assertClassesScannedWhenExceptionIsThrown(runtimeExceptionSimulationFilter);
assertDebugMessageLogged(listener, "Failed to load java.lang.Class for path .+ during classpath scanning.");
}
private void assertClassesScannedWhenExceptionIsThrown(Predicate<Class<?>> filter) throws Exception {
var classFilter = ClassFilter.of(filter);
var classes = this.classpathScanner.scanForClassesInClasspathRoot(getTestClasspathRoot(), classFilter);
assertThat(classes).hasSizeGreaterThanOrEqualTo(150);
}
private void assertDebugMessageLogged(LogRecordListener listener, String regex) {
// @formatter:off
assertThat(listener.stream(ClasspathScanner.class, Level.FINE)
.map(LogRecord::getMessage)
.filter(m -> m.matches(regex))
).hasSize(1);
// @formatter:on
}
@Test
void scanForClassesInClasspathRootWhenOutOfMemoryErrorOccurs() {
Predicate<Class<?>> outOfMemoryErrorSimulationFilter = clazz -> {
if (clazz.getSimpleName().equals(ClassForOutOfMemoryErrorSimulation.class.getSimpleName())) {
throw new OutOfMemoryError();
}
return true;
};
var classFilter = ClassFilter.of(outOfMemoryErrorSimulationFilter);
assertThrows(OutOfMemoryError.class,
() -> this.classpathScanner.scanForClassesInClasspathRoot(getTestClasspathRoot(), classFilter));
}
@Test
void scanForClassesInClasspathRootWithinJarFile() throws Exception {
scanForClassesInClasspathRootWithinJarFile("/jartest.jar");
}
@Test
void scanForClassesInClasspathRootWithinJarWithSpacesInPath() throws Exception {
scanForClassesInClasspathRootWithinJarFile("/folder with spaces/jar test with spaces.jar");
}
private void scanForClassesInClasspathRootWithinJarFile(String resourceName) throws Exception {
var jarfile = getClass().getResource(resourceName);
try (var classLoader = new URLClassLoader(new URL[] { jarfile })) {
var classpathScanner = new ClasspathScanner(() -> classLoader, ReflectionUtils::tryToLoadClass);
var classes = classpathScanner.scanForClassesInClasspathRoot(jarfile.toURI(), allClasses);
var classNames = classes.stream().map(Class::getName).collect(Collectors.toList());
assertThat(classNames).hasSize(3) //
.contains("org.junit.platform.jartest.notincluded.NotIncluded",
"org.junit.platform.jartest.included.recursive.RecursivelyIncluded",
"org.junit.platform.jartest.included.Included");
}
}
@Test
void scanForClassesInPackage() {
var classes = classpathScanner.scanForClassesInPackage("org.junit.platform.commons", allClasses);
assertThat(classes).hasSizeGreaterThanOrEqualTo(20);
assertTrue(classes.contains(NestedClassToBeFound.class));
assertTrue(classes.contains(MemberClassToBeFound.class));
}
@Test
// #2500
void scanForClassesInPackageWithinModulesSharingNamePrefix(@TempDir Path temp) throws Exception {
var moduleSourcePath = Path.of(getClass().getResource("/modules-2500/").toURI()).toString();
run("javac", "--module", "foo,foo.bar", "--module-source-path", moduleSourcePath, "-d", temp.toString());
checkModules2500(ModuleFinder.of(temp)); // exploded modules
var foo = temp.resolve("foo.jar");
var bar = temp.resolve("foo.bar.jar");
run("jar", "--create", "--file", foo.toString(), "-C", temp.resolve("foo").toString(), ".");
run("jar", "--create", "--file", bar.toString(), "-C", temp.resolve("foo.bar").toString(), ".");
checkModules2500(ModuleFinder.of(foo, bar)); // jarred modules
System.gc(); // required on Windows in order to release JAR file handles
}
private static int run(String tool, String... args) {
return ToolProvider.findFirst(tool).orElseThrow().run(System.out, System.err, args);
}
private void checkModules2500(ModuleFinder finder) {
var root = "foo.bar";
var before = ModuleFinder.of();
var boot = ModuleLayer.boot();
var configuration = boot.configuration().resolve(before, finder, Set.of(root));
var parent = ClassLoader.getPlatformClassLoader();
var layer = ModuleLayer.defineModulesWithOneLoader(configuration, List.of(boot), parent).layer();
var classpathScanner = new ClasspathScanner(() -> layer.findLoader(root), ReflectionUtils::tryToLoadClass);
{
var classes = classpathScanner.scanForClassesInPackage("foo", allClasses);
var classNames = classes.stream().map(Class::getName).collect(Collectors.toList());
assertThat(classNames).hasSize(2).contains("foo.Foo", "foo.bar.FooBar");
}
{
var classes = classpathScanner.scanForClassesInPackage("foo.bar", allClasses);
var classNames = classes.stream().map(Class::getName).collect(Collectors.toList());
assertThat(classNames).hasSize(1).contains("foo.bar.FooBar");
}
}
@Test
void findAllClassesInPackageWithinJarFileConcurrently() throws Exception {
var jarFile = getClass().getResource("/jartest.jar");
var jarUri = URI.create("jar:" + jarFile);
try (var classLoader = new URLClassLoader(new URL[] { jarFile })) {
var classpathScanner = new ClasspathScanner(() -> classLoader, ReflectionUtils::tryToLoadClass);
var results = executeConcurrently(10,
() -> classpathScanner.scanForClassesInPackage("org.junit.platform.jartest.included", allClasses));
assertThrows(FileSystemNotFoundException.class, () -> FileSystems.getFileSystem(jarUri),
"FileSystem should be closed");
results.forEach(classes -> {
assertThat(classes).hasSize(2);
var classNames = classes.stream().map(Class::getSimpleName).toList();
assertTrue(classNames.contains("Included"));
assertTrue(classNames.contains("RecursivelyIncluded"));
});
}
}
@Test
void scanForClassesInDefaultPackage() {
var classFilter = ClassFilter.of(this::inDefaultPackage);
var classes = classpathScanner.scanForClassesInPackage("", classFilter);
assertThat(classes).as("number of classes found in default package").isNotEmpty();
assertTrue(classes.stream().allMatch(this::inDefaultPackage));
assertTrue(classes.stream().anyMatch(clazz -> "DefaultPackageTestCase".equals(clazz.getName())));
}
@Test
void scanForClassesInPackageWithFilter() {
var thisClassOnly = ClassFilter.of(clazz -> clazz == ClasspathScannerTests.class);
var classes = classpathScanner.scanForClassesInPackage("org.junit.platform.commons", thisClassOnly);
assertSame(ClasspathScannerTests.class, classes.get(0));
}
@Test
void scanForClassesInPackageForNullBasePackage() {
assertThrows(PreconditionViolationException.class,
() -> classpathScanner.scanForClassesInPackage(null, allClasses));
}
@Test
void scanForClassesInPackageForWhitespaceBasePackage() {
assertThrows(PreconditionViolationException.class,
() -> classpathScanner.scanForClassesInPackage(" ", allClasses));
}
@Test
void scanForClassesInPackageForNullClassFilter() {
assertThrows(PreconditionViolationException.class,
() -> classpathScanner.scanForClassesInPackage("org.junit.platform.commons", null));
}
@Test
void scanForClassesInPackageWhenIOExceptionOccurs() {
var scanner = new ClasspathScanner(ThrowingClassLoader::new, ReflectionUtils::tryToLoadClass);
var classes = scanner.scanForClassesInPackage("org.junit.platform.commons", allClasses);
assertThat(classes).isEmpty();
}
@Test
void scanForClassesInPackageOnlyLoadsClassesThatAreIncludedByTheClassNameFilter() {
Predicate<String> classNameFilter = name -> ClasspathScannerTests.class.getName().equals(name);
var classFilter = ClassFilter.of(classNameFilter, type -> true);
classpathScanner.scanForClassesInPackage("org.junit.platform.commons", classFilter);
assertThat(loadedClasses).containsExactly(ClasspathScannerTests.class);
}
@Test
void findAllClassesInClasspathRoot() throws Exception {
var thisClassOnly = ClassFilter.of(clazz -> clazz == ClasspathScannerTests.class);
var root = getTestClasspathRoot();
var classes = classpathScanner.scanForClassesInClasspathRoot(root, thisClassOnly);
assertSame(ClasspathScannerTests.class, classes.get(0));
}
@Test
void findAllClassesInDefaultPackageInClasspathRoot() throws Exception {
var classFilter = ClassFilter.of(this::inDefaultPackage);
var classes = classpathScanner.scanForClassesInClasspathRoot(getTestClasspathRoot(), classFilter);
assertEquals(1, classes.size(), "number of classes found in default package");
var testClass = classes.get(0);
assertTrue(inDefaultPackage(testClass));
assertEquals("DefaultPackageTestCase", testClass.getName());
}
@Test
void doesNotLoopInfinitelyWithCircularSymlinks(@TempDir Path tempDir) throws Exception {
// Abort if running on Microsoft Windows since we are testing symbolic links
assumeFalse(System.getProperty("os.name").toLowerCase().contains("win"));
var directory = Files.createDirectory(tempDir.resolve("directory"));
var symlink1 = Files.createSymbolicLink(tempDir.resolve("symlink1"), directory);
Files.createSymbolicLink(directory.resolve("symlink2"), symlink1);
var classes = classpathScanner.scanForClassesInClasspathRoot(symlink1.toUri(), allClasses);
assertThat(classes).isEmpty();
}
private boolean inDefaultPackage(Class<?> clazz) {
// OpenJDK returns NULL for the default package.
var pkg = clazz.getPackage();
return pkg == null || "".equals(clazz.getPackage().getName());
}
@Test
void findAllClassesInClasspathRootWithFilter() throws Exception {
var root = getTestClasspathRoot();
var classes = classpathScanner.scanForClassesInClasspathRoot(root, allClasses);
assertThat(classes).hasSizeGreaterThanOrEqualTo(20);
assertTrue(classes.contains(ClasspathScannerTests.class));
}
@Test
void findAllClassesInClasspathRootForNullRoot() {
assertThrows(PreconditionViolationException.class,
() -> classpathScanner.scanForClassesInClasspathRoot(null, allClasses));
}
@Test
void findAllClassesInClasspathRootForNonExistingRoot() {
assertThrows(PreconditionViolationException.class,
() -> classpathScanner.scanForClassesInClasspathRoot(Paths.get("does_not_exist").toUri(), allClasses));
}
@Test
void findAllClassesInClasspathRootForNullClassFilter() {
assertThrows(PreconditionViolationException.class,
() -> classpathScanner.scanForClassesInClasspathRoot(getTestClasspathRoot(), null));
}
@Test
void onlyLoadsClassesInClasspathRootThatAreIncludedByTheClassNameFilter() throws Exception {
var classFilter = ClassFilter.of(name -> ClasspathScannerTests.class.getName().equals(name), type -> true);
var root = getTestClasspathRoot();
classpathScanner.scanForClassesInClasspathRoot(root, classFilter);
assertThat(loadedClasses).containsExactly(ClasspathScannerTests.class);
}
private URI getTestClasspathRoot() throws Exception {
var location = getClass().getProtectionDomain().getCodeSource().getLocation();
return location.toURI();
}
class MemberClassToBeFound {
}
static class NestedClassToBeFound {
}
static class ClassForMalformedClassNameSimulation {
}
static class ClassForOtherInternalErrorSimulation {
}
static class ClassForGenericRuntimeExceptionSimulation {
}
static class ClassForOutOfMemoryErrorSimulation {
}
private static class ThrowingClassLoader extends ClassLoader {
@Override
public Enumeration<URL> getResources(String name) throws IOException {
throw new IOException("Demo I/O error");
}
}
}