Search in sources :

Example 1 with ClasspathRootSelector

use of org.junit.platform.engine.discovery.ClasspathRootSelector in project junit5 by junit-team.

the class DiscoveryRequestCreatorTests method convertsScanClasspathOptionWithExplicitRootDirectories.

@Test
void convertsScanClasspathOptionWithExplicitRootDirectories() {
    options.setScanClasspath(true);
    options.setSelectedClasspathEntries(asList(Paths.get("."), Paths.get("..")));
    LauncherDiscoveryRequest request = convert();
    List<ClasspathRootSelector> classpathRootSelectors = request.getSelectorsByType(ClasspathRootSelector.class);
    // @formatter:off
    assertThat(classpathRootSelectors).extracting(ClasspathRootSelector::getClasspathRoot).containsExactly(new File(".").toURI(), new File("..").toURI());
// @formatter:on
}
Also used : ClasspathRootSelector(org.junit.platform.engine.discovery.ClasspathRootSelector) LauncherDiscoveryRequest(org.junit.platform.launcher.LauncherDiscoveryRequest) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 2 with ClasspathRootSelector

use of org.junit.platform.engine.discovery.ClasspathRootSelector in project junit5 by junit-team.

the class DiscoveryRequestCreatorTests method convertsScanClasspathOptionWithAdditionalClasspathEntries.

@Test
void convertsScanClasspathOptionWithAdditionalClasspathEntries() {
    options.setScanClasspath(true);
    options.setAdditionalClasspathEntries(asList(Paths.get("."), Paths.get("..")));
    LauncherDiscoveryRequest request = convert();
    List<ClasspathRootSelector> classpathRootSelectors = request.getSelectorsByType(ClasspathRootSelector.class);
    // @formatter:off
    assertThat(classpathRootSelectors).extracting(ClasspathRootSelector::getClasspathRoot).contains(new File(".").toURI(), new File("..").toURI());
// @formatter:on
}
Also used : ClasspathRootSelector(org.junit.platform.engine.discovery.ClasspathRootSelector) LauncherDiscoveryRequest(org.junit.platform.launcher.LauncherDiscoveryRequest) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 3 with ClasspathRootSelector

use of org.junit.platform.engine.discovery.ClasspathRootSelector in project junit5 by junit-team.

the class TestClassWithTemplate method classpathResolutionForJarFiles.

@Test
void classpathResolutionForJarFiles() throws Exception {
    URL jarUrl = getClass().getResource("/jupiter-testjar.jar");
    Path path = Paths.get(jarUrl.toURI());
    List<ClasspathRootSelector> selectors = selectClasspathRoots(singleton(path));
    ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
    try (URLClassLoader classLoader = new URLClassLoader(new URL[] { jarUrl })) {
        Thread.currentThread().setContextClassLoader(classLoader);
        resolver.resolveSelectors(request().selectors(selectors).build(), engineDescriptor);
        // 
        assertThat(uniqueIds()).contains(// 
        uniqueIdForTopLevelClass("com.example.project.FirstTest")).contains(uniqueIdForTopLevelClass("com.example.project.SecondTest"));
    } finally {
        Thread.currentThread().setContextClassLoader(originalClassLoader);
    }
}
Also used : Path(java.nio.file.Path) ClasspathRootSelector(org.junit.platform.engine.discovery.ClasspathRootSelector) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) URL(java.net.URL) Test(org.junit.jupiter.api.Test) DynamicTest(org.junit.jupiter.api.DynamicTest)

Example 4 with ClasspathRootSelector

use of org.junit.platform.engine.discovery.ClasspathRootSelector in project junit5 by junit-team.

the class DiscoveryRequestCreatorTests method convertsScanClasspathOptionWithoutExplicitRootDirectories.

@Test
void convertsScanClasspathOptionWithoutExplicitRootDirectories() {
    options.setScanClasspath(true);
    LauncherDiscoveryRequest request = convert();
    List<ClasspathRootSelector> classpathRootSelectors = request.getSelectorsByType(ClasspathRootSelector.class);
    // @formatter:off
    assertThat(classpathRootSelectors).extracting(ClasspathRootSelector::getClasspathRoot).hasAtLeastOneElementOfType(URI.class).doesNotContainNull();
// @formatter:on
}
Also used : ClasspathRootSelector(org.junit.platform.engine.discovery.ClasspathRootSelector) LauncherDiscoveryRequest(org.junit.platform.launcher.LauncherDiscoveryRequest) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 5 with ClasspathRootSelector

use of org.junit.platform.engine.discovery.ClasspathRootSelector in project junit5 by junit-team.

the class TestClassWithTemplate method classpathResolution.

@Test
void classpathResolution() throws Exception {
    Path classpath = Paths.get(DiscoverySelectorResolverTests.class.getProtectionDomain().getCodeSource().getLocation().toURI());
    List<ClasspathRootSelector> selectors = selectClasspathRoots(singleton(classpath));
    resolver.resolveSelectors(request().selectors(selectors).build(), engineDescriptor);
    // 150 is completely arbitrary. The actual number is likely much higher.
    // 
    assertThat(engineDescriptor.getDescendants().size()).describedAs(// 
    "Too few test descriptors in classpath").isGreaterThan(150);
    List<UniqueId> uniqueIds = uniqueIds();
    // 
    assertThat(uniqueIds).describedAs(// 
    "Failed to pick up DefaultPackageTestCase via classpath scanning").contains(uniqueIdForClass(ReflectionUtils.loadClass("DefaultPackageTestCase").get()));
    assertThat(uniqueIds).contains(uniqueIdForClass(Class1WithTestCases.class));
    assertThat(uniqueIds).contains(uniqueIdForMethod(Class1WithTestCases.class, "test1()"));
    assertThat(uniqueIds).contains(uniqueIdForClass(Class2WithTestCases.class));
    assertThat(uniqueIds).contains(uniqueIdForMethod(Class2WithTestCases.class, "test2()"));
    assertThat(uniqueIds).contains(uniqueIdForMethod(ClassWithStaticInnerTestCases.ShouldBeDiscovered.class, "test1()"));
}
Also used : Path(java.nio.file.Path) ClasspathRootSelector(org.junit.platform.engine.discovery.ClasspathRootSelector) DiscoverySelectors.selectUniqueId(org.junit.platform.engine.discovery.DiscoverySelectors.selectUniqueId) UniqueId(org.junit.platform.engine.UniqueId) Class2WithTestCases(org.junit.jupiter.engine.descriptor.subpackage.Class2WithTestCases) Class1WithTestCases(org.junit.jupiter.engine.descriptor.subpackage.Class1WithTestCases) Test(org.junit.jupiter.api.Test) DynamicTest(org.junit.jupiter.api.DynamicTest)

Aggregations

Test (org.junit.jupiter.api.Test)5 ClasspathRootSelector (org.junit.platform.engine.discovery.ClasspathRootSelector)5 LauncherDiscoveryRequest (org.junit.platform.launcher.LauncherDiscoveryRequest)3 File (java.io.File)2 Path (java.nio.file.Path)2 DynamicTest (org.junit.jupiter.api.DynamicTest)2 URI (java.net.URI)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 Class1WithTestCases (org.junit.jupiter.engine.descriptor.subpackage.Class1WithTestCases)1 Class2WithTestCases (org.junit.jupiter.engine.descriptor.subpackage.Class2WithTestCases)1 UniqueId (org.junit.platform.engine.UniqueId)1 DiscoverySelectors.selectUniqueId (org.junit.platform.engine.discovery.DiscoverySelectors.selectUniqueId)1