Skip to content

Commit efb6cd8

Browse files
committed
Move more common logic into SearchPathUtils
1 parent 1e060d0 commit efb6cd8

File tree

5 files changed

+98
-86
lines changed

5 files changed

+98
-86
lines changed

junit-platform-commons/src/main/java/org/junit/platform/commons/util/ClasspathFilters.java

Lines changed: 0 additions & 62 deletions
This file was deleted.

junit-platform-commons/src/main/java/org/junit/platform/commons/util/DefaultClasspathScanner.java

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
package org.junit.platform.commons.util;
1212

1313
import static java.util.stream.Collectors.joining;
14+
import static org.junit.platform.commons.util.SearchPathUtils.PACKAGE_SEPARATOR_CHAR;
15+
import static org.junit.platform.commons.util.SearchPathUtils.PACKAGE_SEPARATOR_STRING;
16+
import static org.junit.platform.commons.util.SearchPathUtils.determineSimpleClassName;
1417
import static org.junit.platform.commons.util.StringUtils.isNotBlank;
1518

1619
import java.io.IOException;
@@ -31,7 +34,6 @@
3134
import java.util.function.Supplier;
3235
import java.util.stream.Stream;
3336

34-
import org.junit.platform.commons.JUnitException;
3537
import org.junit.platform.commons.PreconditionViolationException;
3638
import org.junit.platform.commons.function.Try;
3739
import org.junit.platform.commons.io.Resource;
@@ -57,8 +59,6 @@ class DefaultClasspathScanner implements ClasspathScanner {
5759
private static final char CLASSPATH_RESOURCE_PATH_SEPARATOR = '/';
5860
private static final String CLASSPATH_RESOURCE_PATH_SEPARATOR_STRING = String.valueOf(
5961
CLASSPATH_RESOURCE_PATH_SEPARATOR);
60-
private static final char PACKAGE_SEPARATOR_CHAR = '.';
61-
private static final String PACKAGE_SEPARATOR_STRING = String.valueOf(PACKAGE_SEPARATOR_CHAR);
6262

6363
/**
6464
* Malformed class name InternalError like reported in #401.
@@ -132,7 +132,7 @@ private List<Class<?>> findClassesForUris(List<URI> baseUris, String basePackage
132132
private List<Class<?>> findClassesForUri(URI baseUri, String basePackageName, ClassFilter classFilter) {
133133
List<Class<?>> classes = new ArrayList<>();
134134
// @formatter:off
135-
walkFilesForUri(baseUri, ClasspathFilters.classFiles(),
135+
walkFilesForUri(baseUri, SearchPathUtils::isClassOrSourceFile,
136136
(baseDir, file) ->
137137
processClassFileSafely(baseDir, basePackageName, classFilter, file, classes::add));
138138
// @formatter:on
@@ -156,7 +156,7 @@ private List<Resource> findResourcesForUris(List<URI> baseUris, String basePacka
156156
private List<Resource> findResourcesForUri(URI baseUri, String basePackageName, ResourceFilter resourceFilter) {
157157
List<Resource> resources = new ArrayList<>();
158158
// @formatter:off
159-
walkFilesForUri(baseUri, ClasspathFilters.resourceFiles(),
159+
walkFilesForUri(baseUri, SearchPathUtils::isResourceFile,
160160
(baseDir, file) ->
161161
processResourceFileSafely(baseDir, basePackageName, resourceFilter, file, resources::add));
162162
// @formatter:on
@@ -253,19 +253,6 @@ private String determineFullyQualifiedResourceName(Path baseDir, String basePack
253253
// @formatter:on
254254
}
255255

256-
private String determineSimpleClassName(Path file) {
257-
String fileName = file.getFileName().toString();
258-
return determineSimpleClassName(fileName);
259-
}
260-
261-
static String determineSimpleClassName(String fileName) {
262-
int lastDot = fileName.lastIndexOf('.');
263-
if (lastDot < 0) {
264-
throw new JUnitException("Expected file name with file extension, but got: " + fileName);
265-
}
266-
return fileName.substring(0, lastDot);
267-
}
268-
269256
private String determineSimpleResourceName(Path resourceFile) {
270257
return resourceFile.getFileName().toString();
271258
}

junit-platform-commons/src/main/java/org/junit/platform/commons/util/ModuleUtils.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.lang.module.ResolvedModule;
2525
import java.net.URI;
2626
import java.net.URISyntaxException;
27+
import java.nio.file.Path;
2728
import java.util.ArrayList;
2829
import java.util.LinkedHashSet;
2930
import java.util.List;
@@ -253,11 +254,10 @@ List<Class<?>> scan(ModuleReference reference) {
253254
try (ModuleReader reader = reference.open()) {
254255
try (Stream<String> names = reader.list()) {
255256
// @formatter:off
256-
return names.filter(ClasspathFilters::isClassOrSourceFileName)
257-
.map(DefaultClasspathScanner::determineSimpleClassName)
258-
.map(name -> name.replace('/', '.'))
259-
.filter(name -> !"module-info".equals(name))
260-
.filter(name -> !name.endsWith("package-info"))
257+
return names.filter(name -> !name.endsWith("/")) // remove directories
258+
.map(Path::of)
259+
.filter(SearchPathUtils::isClassOrSourceFile)
260+
.map(SearchPathUtils::determineFullyQualifiedClassName)
261261
.filter(classFilter::match)
262262
.<Class<?>> map(this::loadClassUnchecked)
263263
.filter(classFilter::match)
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright 2015-2025 the original author or authors.
3+
*
4+
* All rights reserved. This program and the accompanying materials are
5+
* made available under the terms of the Eclipse Public License v2.0 which
6+
* accompanies this distribution and is available at
7+
*
8+
* https://www.eclipse.org/legal/epl-v20.html
9+
*/
10+
11+
package org.junit.platform.commons.util;
12+
13+
import static java.util.stream.Collectors.joining;
14+
15+
import java.nio.file.Path;
16+
import java.util.stream.IntStream;
17+
18+
import org.junit.platform.commons.JUnitException;
19+
20+
/**
21+
* @since 1.11
22+
*/
23+
class SearchPathUtils {
24+
25+
static final char PACKAGE_SEPARATOR_CHAR = '.';
26+
static final String PACKAGE_SEPARATOR_STRING = String.valueOf(PACKAGE_SEPARATOR_CHAR);
27+
private static final char FILE_NAME_EXTENSION_SEPARATOR_CHAR = '.';
28+
29+
private static final String CLASS_FILE_SUFFIX = ".class";
30+
private static final String SOURCE_FILE_SUFFIX = ".java";
31+
32+
private static final String PACKAGE_INFO_FILE_NAME = "package-info";
33+
private static final String MODULE_INFO_FILE_NAME = "module-info";
34+
35+
// System property defined since Java 12: https://bugs.java/bugdatabase/JDK-8210877
36+
private static final boolean SOURCE_MODE = System.getProperty("jdk.launcher.sourcefile") != null;
37+
38+
static boolean isResourceFile(Path file) {
39+
return !isClassFile(file);
40+
}
41+
42+
static boolean isClassOrSourceFile(Path file) {
43+
var fileName = file.getFileName().toString();
44+
return isClassOrSourceFile(fileName) && !isModuleInfoOrPackageInfo(fileName);
45+
}
46+
47+
private static boolean isModuleInfoOrPackageInfo(String fileName) {
48+
var fileNameWithoutExtension = removeExtension(fileName);
49+
return PACKAGE_INFO_FILE_NAME.equals(fileNameWithoutExtension) //
50+
|| MODULE_INFO_FILE_NAME.equals(fileNameWithoutExtension);
51+
}
52+
53+
static String determineFullyQualifiedClassName(Path path) {
54+
var simpleClassName = determineSimpleClassName(path);
55+
var parent = path.getParent();
56+
return parent == null ? simpleClassName : joinPathNamesWithPackageSeparator(parent.resolve(simpleClassName));
57+
}
58+
59+
private static String joinPathNamesWithPackageSeparator(Path path) {
60+
return IntStream.range(0, path.getNameCount()) //
61+
.mapToObj(i -> path.getName(i).toString()) //
62+
.collect(joining(PACKAGE_SEPARATOR_STRING));
63+
}
64+
65+
static String determineSimpleClassName(Path file) {
66+
return removeExtension(file.getFileName().toString());
67+
}
68+
69+
private static String removeExtension(String fileName) {
70+
int lastDot = fileName.lastIndexOf(FILE_NAME_EXTENSION_SEPARATOR_CHAR);
71+
if (lastDot < 0) {
72+
throw new JUnitException("Expected file name with file extension, but got: " + fileName);
73+
}
74+
return fileName.substring(0, lastDot);
75+
}
76+
77+
private static boolean isClassOrSourceFile(String name) {
78+
return name.endsWith(CLASS_FILE_SUFFIX) || (SOURCE_MODE && name.endsWith(SOURCE_FILE_SUFFIX));
79+
}
80+
81+
private static boolean isClassFile(Path file) {
82+
return file.getFileName().toString().endsWith(CLASS_FILE_SUFFIX);
83+
}
84+
85+
private SearchPathUtils() {
86+
}
87+
}

platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/JUnitStartTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ void junitRunClass(@FilePrefix("junit-run-class") OutputFiles outputFiles) throw
103103

104104
@Test
105105
@EnabledOnJre(JRE.JAVA_25)
106-
void junitRunModule(@FilePrefix("junit-run-Module") OutputFiles outputFiles) throws Exception {
106+
void junitRunModule(@FilePrefix("junit-run-module") OutputFiles outputFiles) throws Exception {
107107
var result = ProcessStarters.java() //
108108
.workingDir(workspace) //
109109
.putEnvironment("NO_COLOR", "1") // --disable-ansi-colors

0 commit comments

Comments
 (0)