Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Discover tests with another classloader
JUnitPlatformFeature discovers tests before analysis. The discovery procsee will initialize some of the test classes. As they are loaded by the ImageClassloader, the initialization will cause eager class initialization error during native image building time. This commit uses a different classloader than the ImageClassLoader in the JUnitPlatformFeature to make sure the test class initialization at discovery time won't affect the native image class initialization policy.
- Loading branch information
Showing
5 changed files
with
223 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
.../junit-platform-native/src/main/java/org/graalvm/junit/platform/TestsDiscoveryHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/* | ||
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* The Universal Permissive License (UPL), Version 1.0 | ||
* | ||
* Subject to the condition set forth below, permission is hereby granted to any | ||
* person obtaining a copy of this software, associated documentation and/or | ||
* data (collectively the "Software"), free of charge and under any and all | ||
* copyright rights in the Software, and any and all patent rights owned or | ||
* freely licensable by each licensor hereunder covering either (i) the | ||
* unmodified Software as contributed to or provided by such licensor, or (ii) | ||
* the Larger Works (as defined below), to deal in both | ||
* | ||
* (a) the Software, and | ||
* | ||
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if | ||
* one is included with the Software each a "Larger Work" to which the Software | ||
* is contributed by such licensors), | ||
* | ||
* without restriction, including without limitation the rights to copy, create | ||
* derivative works of, display, perform, and distribute the Software and make, | ||
* use, sell, offer for sale, import, export, have made, and have sold the | ||
* Software and the Larger Work(s), and to sublicense the foregoing rights on | ||
* either these or other terms. | ||
* | ||
* This license is subject to the following condition: | ||
* | ||
* The above copyright notice and either this complete permission notice or at a | ||
* minimum a reference to the UPL must be included in all copies or substantial | ||
* portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package org.graalvm.junit.platform; | ||
|
||
import org.junit.platform.engine.DiscoverySelector; | ||
import org.junit.platform.engine.discovery.DiscoverySelectors; | ||
import org.junit.platform.engine.discovery.UniqueIdSelector; | ||
import org.junit.platform.engine.support.descriptor.ClassSource; | ||
import org.junit.platform.launcher.Launcher; | ||
import org.junit.platform.launcher.LauncherDiscoveryRequest; | ||
import org.junit.platform.launcher.TestIdentifier; | ||
import org.junit.platform.launcher.TestPlan; | ||
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder; | ||
import org.junit.platform.launcher.core.LauncherFactory; | ||
import org.junit.platform.launcher.listeners.UniqueIdTrackingListener; | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
public class TestsDiscoveryHelper { | ||
private List<? extends DiscoverySelector> selectors; | ||
private Launcher launcher = LauncherFactory.create(); | ||
private TestPlan testPlan; | ||
|
||
public TestsDiscoveryHelper(boolean debug, List<Path> classpathRoots) { | ||
selectors = getSelectors(debug, classpathRoots); | ||
} | ||
|
||
public Launcher getLauncher() { | ||
return launcher; | ||
} | ||
|
||
public TestPlan discoverTestPlan() { | ||
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request() | ||
.selectors(selectors) | ||
.build(); | ||
testPlan = launcher.discover(request); | ||
return testPlan; | ||
} | ||
|
||
/** | ||
* Use the JUnit Platform Launcher to discover tests and register classes | ||
* for reflection. | ||
* | ||
* @return a List of discovered junit test classes | ||
*/ | ||
public List<Class<?>> discoverTests() { | ||
discoverTestPlan(); | ||
return testPlan.getRoots().stream() | ||
.flatMap(rootIdentifier -> testPlan.getDescendants(rootIdentifier).stream()) | ||
.map(TestIdentifier::getSource) | ||
.filter(Optional::isPresent) | ||
.map(Optional::get) | ||
.filter(ClassSource.class::isInstance) | ||
.map(ClassSource.class::cast) | ||
.map(cs -> cs.getJavaClass()).collect(Collectors.toList()); | ||
} | ||
|
||
private List<? extends DiscoverySelector> getSelectors(boolean debug, List<Path> classpathRoots) { | ||
try { | ||
Path outputDir = Paths.get(System.getProperty(UniqueIdTrackingListener.OUTPUT_DIR_PROPERTY_NAME)); | ||
String prefix = System.getProperty(UniqueIdTrackingListener.OUTPUT_FILE_PREFIX_PROPERTY_NAME, | ||
UniqueIdTrackingListener.DEFAULT_OUTPUT_FILE_PREFIX); | ||
List<UniqueIdSelector> selectors = readAllFiles(outputDir, prefix) | ||
.map(DiscoverySelectors::selectUniqueId) | ||
.collect(Collectors.toList()); | ||
if (!selectors.isEmpty()) { | ||
System.out.printf( | ||
"[junit-platform-native] Running in 'test listener' mode using files matching pattern [%s*] " | ||
+ "found in folder [%s] and its subfolders.%n", | ||
prefix, outputDir.toAbsolutePath()); | ||
return selectors; | ||
} | ||
} catch (Exception ex) { | ||
JUnitPlatformFeature.debug("Failed to read UIDs from UniqueIdTrackingListener output files: " + ex.getMessage()); | ||
} | ||
|
||
System.out.println("[junit-platform-native] Running in 'test discovery' mode. Note that this is a fallback mode."); | ||
if (debug) { | ||
classpathRoots.forEach(entry -> JUnitPlatformFeature.debug("Selecting classpath root: " + entry)); | ||
} | ||
return DiscoverySelectors.selectClasspathRoots(new HashSet<>(classpathRoots)); | ||
} | ||
|
||
private Stream<String> readAllFiles(Path dir, String prefix) throws IOException { | ||
return findFiles(dir, prefix).map(outputFile -> { | ||
try { | ||
return Files.readAllLines(outputFile); | ||
} catch (IOException ex) { | ||
throw new UncheckedIOException(ex); | ||
} | ||
}).flatMap(List::stream); | ||
} | ||
|
||
private static Stream<Path> findFiles(Path dir, String prefix) throws IOException { | ||
if (!Files.exists(dir)) { | ||
return Stream.empty(); | ||
} | ||
return Files.find(dir, Integer.MAX_VALUE, | ||
(path, basicFileAttributes) -> (basicFileAttributes.isRegularFile() | ||
&& path.getFileName().toString().startsWith(prefix))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters