"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "platform-tests/src/test/java/org/junit/platform/commons/util/ClasspathScannerTests.java" between
junit5-r5.3.2.tar.gz and junit5-r5.4.0.tar.gz

About: JUnit 5 is a unit testing framework for the Java programming language.

ClasspathScannerTests.java  (junit5-r5.3.2):ClasspathScannerTests.java  (junit5-r5.4.0)
/* /*
* Copyright 2015-2018 the original author or authors. * Copyright 2015-2019 the original author or authors.
* *
* All rights reserved. This program and the accompanying materials are * All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which * made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at * accompanies this distribution and is available at
* *
* http://www.eclipse.org/legal/epl-v20.html * http://www.eclipse.org/legal/epl-v20.html
*/ */
package org.junit.platform.commons.util; package org.junit.platform.commons.util;
skipping to change at line 30 skipping to change at line 30
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.LogRecord; import java.util.logging.LogRecord;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.engine.TrackLogRecords; import org.junit.jupiter.engine.TrackLogRecords;
import org.junit.jupiter.extensions.TempDirectory; import org.junit.platform.commons.function.Try;
import org.junit.jupiter.extensions.TempDirectory.Root;
import org.junit.platform.commons.logging.LogRecordListener; import org.junit.platform.commons.logging.LogRecordListener;
/** /**
* Unit tests for {@link ClasspathScanner}. * Unit tests for {@link ClasspathScanner}.
* *
* @since 1.0 * @since 1.0
*/ */
@TrackLogRecords @TrackLogRecords
class ClasspathScannerTests { class ClasspathScannerTests {
private static final ClassFilter allClasses = ClassFilter.of(type -> true ); private static final ClassFilter allClasses = ClassFilter.of(type -> true );
private final List<Class<?>> loadedClasses = new ArrayList<>(); private final List<Class<?>> loadedClasses = new ArrayList<>();
private final BiFunction<String, ClassLoader, Optional<Class<?>>> trackin private final BiFunction<String, ClassLoader, Try<Class<?>>> trackingClas
gClassLoader = (name, classLoader) -> { sLoader = (name,
Optional<Class<?>> loadedClass = ReflectionUtils.loadClass(name, classLoader) -> ReflectionUtils.tryToLoadClass(name, clas
classLoader); sLoader).ifSuccess(loadedClasses::add);
loadedClass.ifPresent(loadedClasses::add);
return loadedClass;
};
private final ClasspathScanner classpathScanner = new ClasspathScanner(Cl assLoaderUtils::getDefaultClassLoader, private final ClasspathScanner classpathScanner = new ClasspathScanner(Cl assLoaderUtils::getDefaultClassLoader,
trackingClassLoader); trackingClassLoader);
@Test @Test
void scanForClassesInClasspathRootWhenMalformedClassnameInternalErrorOccu rsWithNullDetailedMessage( void scanForClassesInClasspathRootWhenMalformedClassnameInternalErrorOccu rsWithNullDetailedMessage(
LogRecordListener listener) throws Exception { LogRecordListener listener) throws Exception {
Predicate<Class<?>> malformedClassNameSimulationFilter = clazz -> { Predicate<Class<?>> malformedClassNameSimulationFilter = clazz -> {
if (clazz.getSimpleName().equals(ClassForMalformedClassNa meSimulation.class.getSimpleName())) { if (clazz.getSimpleName().equals(ClassForMalformedClassNa meSimulation.class.getSimpleName())) {
skipping to change at line 138 skipping to change at line 133
private void assertDebugMessageLogged(LogRecordListener listener, String regex) { private void assertDebugMessageLogged(LogRecordListener listener, String regex) {
// @formatter:off // @formatter:off
assertThat(listener.stream(ClasspathScanner.class, Level.FINE) assertThat(listener.stream(ClasspathScanner.class, Level.FINE)
.map(LogRecord::getMessage) .map(LogRecord::getMessage)
.filter(m -> m.matches(regex)) .filter(m -> m.matches(regex))
).hasSize(1); ).hasSize(1);
// @formatter:on // @formatter:on
} }
@Test @Test
void scanForClassesInClasspathRootWhenOutOfMemoryErrorOccurs() throws Exc eption { void scanForClassesInClasspathRootWhenOutOfMemoryErrorOccurs() {
Predicate<Class<?>> outOfMemoryErrorSimulationFilter = clazz -> { Predicate<Class<?>> outOfMemoryErrorSimulationFilter = clazz -> {
if (clazz.getSimpleName().equals(ClassForOutOfMemoryError Simulation.class.getSimpleName())) { if (clazz.getSimpleName().equals(ClassForOutOfMemoryError Simulation.class.getSimpleName())) {
throw new OutOfMemoryError(); throw new OutOfMemoryError();
} }
return true; return true;
}; };
ClassFilter classFilter = ClassFilter.of(outOfMemoryErrorSimulati onFilter); ClassFilter classFilter = ClassFilter.of(outOfMemoryErrorSimulati onFilter);
assertThrows(OutOfMemoryError.class, assertThrows(OutOfMemoryError.class,
() -> this.classpathScanner.scanForClassesInClasspathRoot (getTestClasspathRoot(), classFilter)); () -> this.classpathScanner.scanForClassesInClasspathRoot (getTestClasspathRoot(), classFilter));
skipping to change at line 165 skipping to change at line 160
@Test @Test
void scanForClassesInClasspathRootWithinJarWithSpacesInPath() throws Exce ption { void scanForClassesInClasspathRootWithinJarWithSpacesInPath() throws Exce ption {
scanForClassesInClasspathRootWithinJarFile("/folder with spaces/j ar test with spaces.jar"); scanForClassesInClasspathRootWithinJarFile("/folder with spaces/j ar test with spaces.jar");
} }
private void scanForClassesInClasspathRootWithinJarFile(String resourceNa me) throws Exception { private void scanForClassesInClasspathRootWithinJarFile(String resourceNa me) throws Exception {
URL jarfile = getClass().getResource(resourceName); URL jarfile = getClass().getResource(resourceName);
try (URLClassLoader classLoader = new URLClassLoader(new URL[] { jarfile })) { try (URLClassLoader classLoader = new URLClassLoader(new URL[] { jarfile })) {
ClasspathScanner classpathScanner = new ClasspathScanner( ClasspathScanner classpathScanner = new ClasspathScanner(
() -> classLoader, ReflectionUtils::loadClass); () -> classLoader,
ReflectionUtils::tryToLoadClass);
List<Class<?>> classes = classpathScanner.scanForClassesI nClasspathRoot(jarfile.toURI(), allClasses); List<Class<?>> classes = classpathScanner.scanForClassesI nClasspathRoot(jarfile.toURI(), allClasses);
List<String> classNames = classes.stream().map(Class::get Name).collect(Collectors.toList()); List<String> classNames = classes.stream().map(Class::get Name).collect(Collectors.toList());
assertThat(classNames).hasSize(3) // assertThat(classNames).hasSize(3) //
.contains("org.junit.platform.jartest.not included.NotIncluded", .contains("org.junit.platform.jartest.not included.NotIncluded",
"org.junit.platform.jartest.inclu ded.recursive.RecursivelyIncluded", "org.junit.platform.jartest.inclu ded.recursive.RecursivelyIncluded",
"org.junit.platform.jartest.inclu ded.Included"); "org.junit.platform.jartest.inclu ded.Included");
} }
} }
@Test @Test
void scanForClassesInPackage() throws Exception { void scanForClassesInPackage() {
List<Class<?>> classes = classpathScanner.scanForClassesInPackage ("org.junit.platform.commons", allClasses); List<Class<?>> classes = classpathScanner.scanForClassesInPackage ("org.junit.platform.commons", allClasses);
assertThat(classes.size()).isGreaterThanOrEqualTo(20); assertThat(classes.size()).isGreaterThanOrEqualTo(20);
assertTrue(classes.contains(NestedClassToBeFound.class)); assertTrue(classes.contains(NestedClassToBeFound.class));
assertTrue(classes.contains(MemberClassToBeFound.class)); assertTrue(classes.contains(MemberClassToBeFound.class));
} }
@Test @Test
void findAllClassesInPackageWithinJarFile() throws Exception { void findAllClassesInPackageWithinJarFile() throws Exception {
URL jarfile = getClass().getResource("/jartest.jar"); URL jarfile = getClass().getResource("/jartest.jar");
try (URLClassLoader classLoader = new URLClassLoader(new URL[] { jarfile })) { try (URLClassLoader classLoader = new URLClassLoader(new URL[] { jarfile })) {
ClasspathScanner classpathScanner = new ClasspathScanner( ClasspathScanner classpathScanner = new ClasspathScanner(
() -> classLoader, ReflectionUtils::loadClass); () -> classLoader,
ReflectionUtils::tryToLoadClass);
List<Class<?>> classes = classpathScanner.scanForClassesI nPackage("org.junit.platform.jartest.included", List<Class<?>> classes = classpathScanner.scanForClassesI nPackage("org.junit.platform.jartest.included",
allClasses); allClasses);
assertThat(classes).hasSize(2); assertThat(classes).hasSize(2);
List<String> classNames = classes.stream().map(Class::get SimpleName).collect(Collectors.toList()); List<String> classNames = classes.stream().map(Class::get SimpleName).collect(Collectors.toList());
assertTrue(classNames.contains("Included")); assertTrue(classNames.contains("Included"));
assertTrue(classNames.contains("RecursivelyIncluded")); assertTrue(classNames.contains("RecursivelyIncluded"));
} }
} }
@Test @Test
void scanForClassesInDefaultPackage() throws Exception { void scanForClassesInDefaultPackage() {
ClassFilter classFilter = ClassFilter.of(this::inDefaultPackage); ClassFilter classFilter = ClassFilter.of(this::inDefaultPackage);
List<Class<?>> classes = classpathScanner.scanForClassesInPackage ("", classFilter); List<Class<?>> classes = classpathScanner.scanForClassesInPackage ("", classFilter);
assertThat(classes.size()).as("number of classes found in default package").isGreaterThanOrEqualTo(1); assertThat(classes.size()).as("number of classes found in default package").isGreaterThanOrEqualTo(1);
assertTrue(classes.stream().allMatch(this::inDefaultPackage)); assertTrue(classes.stream().allMatch(this::inDefaultPackage));
assertTrue(classes.stream().anyMatch(clazz -> "DefaultPackageTest Case".equals(clazz.getName()))); assertTrue(classes.stream().anyMatch(clazz -> "DefaultPackageTest Case".equals(clazz.getName())));
} }
@Test @Test
void scanForClassesInPackageWithFilter() throws Exception { void scanForClassesInPackageWithFilter() {
ClassFilter thisClassOnly = ClassFilter.of(clazz -> clazz == Clas spathScannerTests.class); ClassFilter thisClassOnly = ClassFilter.of(clazz -> clazz == Clas spathScannerTests.class);
List<Class<?>> classes = classpathScanner.scanForClassesInPackage ("org.junit.platform.commons", thisClassOnly); List<Class<?>> classes = classpathScanner.scanForClassesInPackage ("org.junit.platform.commons", thisClassOnly);
assertSame(ClasspathScannerTests.class, classes.get(0)); assertSame(ClasspathScannerTests.class, classes.get(0));
} }
@Test @Test
void scanForClassesInPackageForNullBasePackage() { void scanForClassesInPackageForNullBasePackage() {
assertThrows(PreconditionViolationException.class, assertThrows(PreconditionViolationException.class,
() -> classpathScanner.scanForClassesInPackage(null, allC lasses)); () -> classpathScanner.scanForClassesInPackage(null, allC lasses));
} }
skipping to change at line 237 skipping to change at line 234
} }
@Test @Test
void scanForClassesInPackageForNullClassFilter() { void scanForClassesInPackageForNullClassFilter() {
assertThrows(PreconditionViolationException.class, assertThrows(PreconditionViolationException.class,
() -> classpathScanner.scanForClassesInPackage("org.junit .platform.commons", null)); () -> classpathScanner.scanForClassesInPackage("org.junit .platform.commons", null));
} }
@Test @Test
void scanForClassesInPackageWhenIOExceptionOccurs() { void scanForClassesInPackageWhenIOExceptionOccurs() {
ClasspathScanner scanner = new ClasspathScanner(ThrowingClassLoad er::new, ReflectionUtils::loadClass); ClasspathScanner scanner = new ClasspathScanner(ThrowingClassLoad er::new, ReflectionUtils::tryToLoadClass);
List<Class<?>> classes = scanner.scanForClassesInPackage("org.jun it.platform.commons", allClasses); List<Class<?>> classes = scanner.scanForClassesInPackage("org.jun it.platform.commons", allClasses);
assertThat(classes).isEmpty(); assertThat(classes).isEmpty();
} }
@Test @Test
void scanForClassesInPackageOnlyLoadsClassesThatAreIncludedByTheClassName Filter() throws Exception { void scanForClassesInPackageOnlyLoadsClassesThatAreIncludedByTheClassName Filter() {
Predicate<String> classNameFilter = name -> ClasspathScannerTests .class.getName().equals(name); Predicate<String> classNameFilter = name -> ClasspathScannerTests .class.getName().equals(name);
ClassFilter classFilter = ClassFilter.of(classNameFilter, type -> true); ClassFilter classFilter = ClassFilter.of(classNameFilter, type -> true);
classpathScanner.scanForClassesInPackage("org.junit.platform.comm ons", classFilter); classpathScanner.scanForClassesInPackage("org.junit.platform.comm ons", classFilter);
assertThat(loadedClasses).containsExactly(ClasspathScannerTests.c lass); assertThat(loadedClasses).containsExactly(ClasspathScannerTests.c lass);
} }
@Test @Test
void findAllClassesInClasspathRoot() throws Exception { void findAllClassesInClasspathRoot() throws Exception {
skipping to change at line 272 skipping to change at line 269
ClassFilter classFilter = ClassFilter.of(this::inDefaultPackage); ClassFilter classFilter = ClassFilter.of(this::inDefaultPackage);
List<Class<?>> classes = classpathScanner.scanForClassesInClasspa thRoot(getTestClasspathRoot(), classFilter); List<Class<?>> classes = classpathScanner.scanForClassesInClasspa thRoot(getTestClasspathRoot(), classFilter);
assertEquals(1, classes.size(), "number of classes found in defau lt package"); assertEquals(1, classes.size(), "number of classes found in defau lt package");
Class<?> testClass = classes.get(0); Class<?> testClass = classes.get(0);
assertTrue(inDefaultPackage(testClass)); assertTrue(inDefaultPackage(testClass));
assertEquals("DefaultPackageTestCase", testClass.getName()); assertEquals("DefaultPackageTestCase", testClass.getName());
} }
@Test @Test
@ExtendWith(TempDirectory.class) void doesNotLoopInfinitelyWithCircularSymlinks(@TempDir Path tempDir) thr
void doesNotLoopInfinitelyWithCircularSymlinks(@Root Path tempDir) throws ows Exception {
Exception {
// Abort if running on Microsoft Windows since we are testing sym bolic links // Abort if running on Microsoft Windows since we are testing sym bolic links
assumeFalse(System.getProperty("os.name").toLowerCase().contains( "win")); assumeFalse(System.getProperty("os.name").toLowerCase().contains( "win"));
Path directory = Files.createDirectory(tempDir.resolve("directory ")); Path directory = Files.createDirectory(tempDir.resolve("directory "));
Path symlink1 = Files.createSymbolicLink(tempDir.resolve("symlink 1"), directory); Path symlink1 = Files.createSymbolicLink(tempDir.resolve("symlink 1"), directory);
Files.createSymbolicLink(directory.resolve("symlink2"), symlink1) ; Files.createSymbolicLink(directory.resolve("symlink2"), symlink1) ;
List<Class<?>> classes = classpathScanner.scanForClassesInClasspa thRoot(symlink1.toUri(), allClasses); List<Class<?>> classes = classpathScanner.scanForClassesInClasspa thRoot(symlink1.toUri(), allClasses);
skipping to change at line 303 skipping to change at line 299
@Test @Test
void findAllClassesInClasspathRootWithFilter() throws Exception { void findAllClassesInClasspathRootWithFilter() throws Exception {
URI root = getTestClasspathRoot(); URI root = getTestClasspathRoot();
List<Class<?>> classes = classpathScanner.scanForClassesInClasspa thRoot(root, allClasses); List<Class<?>> classes = classpathScanner.scanForClassesInClasspa thRoot(root, allClasses);
assertThat(classes.size()).isGreaterThanOrEqualTo(20); assertThat(classes.size()).isGreaterThanOrEqualTo(20);
assertTrue(classes.contains(ClasspathScannerTests.class)); assertTrue(classes.contains(ClasspathScannerTests.class));
} }
@Test @Test
void findAllClassesInClasspathRootForNullRoot() throws Exception { void findAllClassesInClasspathRootForNullRoot() {
assertThrows(PreconditionViolationException.class, assertThrows(PreconditionViolationException.class,
() -> classpathScanner.scanForClassesInClasspathRoot(null , allClasses)); () -> classpathScanner.scanForClassesInClasspathRoot(null , allClasses));
} }
@Test @Test
void findAllClassesInClasspathRootForNonExistingRoot() throws Exception { void findAllClassesInClasspathRootForNonExistingRoot() {
assertThrows(PreconditionViolationException.class, assertThrows(PreconditionViolationException.class,
() -> classpathScanner.scanForClassesInClasspathRoot(Path s.get("does_not_exist").toUri(), allClasses)); () -> classpathScanner.scanForClassesInClasspathRoot(Path s.get("does_not_exist").toUri(), allClasses));
} }
@Test @Test
void findAllClassesInClasspathRootForNullClassFilter() throws Exception { void findAllClassesInClasspathRootForNullClassFilter() {
assertThrows(PreconditionViolationException.class, assertThrows(PreconditionViolationException.class,
() -> classpathScanner.scanForClassesInClasspathRoot(getT estClasspathRoot(), null)); () -> classpathScanner.scanForClassesInClasspathRoot(getT estClasspathRoot(), null));
} }
@Test @Test
void onlyLoadsClassesInClasspathRootThatAreIncludedByTheClassNameFilter() throws Exception { void onlyLoadsClassesInClasspathRootThatAreIncludedByTheClassNameFilter() throws Exception {
ClassFilter classFilter = ClassFilter.of(name -> ClasspathScanner Tests.class.getName().equals(name), ClassFilter classFilter = ClassFilter.of(name -> ClasspathScanner Tests.class.getName().equals(name),
type -> true); type -> true);
URI root = getTestClasspathRoot(); URI root = getTestClasspathRoot();
 End of changes. 17 change blocks. 
28 lines changed or deleted 24 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)