This is an automated email from the ASF dual-hosted git repository.

slachiewicz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-surefire.git

commit 2ea5a95ee156db94d3a7e17dc9f5cb4707b66fff
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Sun Nov 8 23:41:13 2026 +0000

    Scan inside DefaultDirectoryScanner instead of a copied scanner class
    
    surefire-api already had a DirectoryScanner interface and its
    DefaultDirectoryScanner, the only caller of the copied class. The scan
    is now a static method of DefaultDirectoryScanner over MatchPatterns,
    with the same normalisation, recursion and predicates; the tests moved
    with it.
---
 .../surefire/api/util/DefaultDirectoryScanner.java |  88 +++++++++---
 .../api/util/internal/DirectoryScanner.java        | 154 ---------------------
 .../api/util/DefaultDirectoryScannerTest.java      | 102 ++++++++++++++
 .../api/util/internal/DirectoryScannerTest.java    | 150 --------------------
 4 files changed, 174 insertions(+), 320 deletions(-)

diff --git 
a/surefire-api/src/main/java/org/apache/maven/surefire/api/util/DefaultDirectoryScanner.java
 
b/surefire-api/src/main/java/org/apache/maven/surefire/api/util/DefaultDirectoryScanner.java
index 976c4a31b..4f69d6c4b 100644
--- 
a/surefire-api/src/main/java/org/apache/maven/surefire/api/util/DefaultDirectoryScanner.java
+++ 
b/surefire-api/src/main/java/org/apache/maven/surefire/api/util/DefaultDirectoryScanner.java
@@ -26,6 +26,8 @@
 import java.util.Set;
 
 import org.apache.maven.surefire.api.filter.SpecificTestClassFilter;
+import org.apache.maven.surefire.api.util.internal.MatchPatterns;
+import org.apache.maven.surefire.api.util.internal.SelectorUtils;
 
 /**
  * Scans directories looking for tests.
@@ -95,22 +97,10 @@ private static Class<?> loadClass(ClassLoader classLoader, 
String className) {
     String[] collectTests() {
         String[] tests = EMPTY_STRING_ARRAY;
         if (basedir.exists()) {
-            org.apache.maven.surefire.api.util.internal.DirectoryScanner 
scanner =
-                    new 
org.apache.maven.surefire.api.util.internal.DirectoryScanner();
-
-            scanner.setBasedir(basedir);
-
-            if (includes != null) {
-                scanner.setIncludes(processIncludesExcludes(includes));
-            }
-
-            if (excludes != null) {
-                scanner.setExcludes(processIncludesExcludes(excludes));
-            }
-
-            scanner.scan();
-
-            tests = scanner.getIncludedFiles();
+            tests = scan(
+                    basedir,
+                    includes == null ? null : 
processIncludesExcludes(includes),
+                    excludes == null ? null : 
processIncludesExcludes(excludes));
             for (int i = 0; i < tests.length; i++) {
                 String test = tests[i];
                 test = test.substring(0, test.indexOf("."));
@@ -120,6 +110,72 @@ String[] collectTests() {
         return tests;
     }
 
+    /**
+     * Lists the files below {@code basedir} that match an include pattern and 
no exclude pattern, relative to it and
+     * with {@link File#separator}, in directory listing order. Patterns are 
Ant patterns ({@code *}, {@code ?},
+     * {@code **}) or {@code %regex[...]}, see {@link 
SelectorUtils#matchPath}; {@code /} and {@code \\} both separate,
+     * a pattern ending in a separator takes everything below, no includes 
mean everything. Matching is
+     * case-sensitive and symbolic links are followed.
+     *
+     * @throws IllegalStateException if {@code basedir} is not an existing 
directory
+     * @throws NullPointerException if a pattern is {@code null}
+     */
+    static String[] scan(File basedir, String[] includes, String[] excludes) {
+        if (!basedir.exists()) {
+            throw new IllegalStateException("basedir " + basedir + " does not 
exist");
+        }
+        if (!basedir.isDirectory()) {
+            throw new IllegalStateException("basedir " + basedir + " is not a 
directory");
+        }
+        String[] inc = normalize(includes, "includes");
+        MatchPatterns included = MatchPatterns.from(inc == null || inc.length 
== 0 ? new String[] {"**"} : inc);
+        String[] exc = normalize(excludes, "excludes");
+        MatchPatterns excluded = MatchPatterns.from(exc == null ? 
EMPTY_STRING_ARRAY : exc);
+        List<String> files = new ArrayList<>();
+        scandir(basedir, "", included, excluded, files);
+        return files.toArray(EMPTY_STRING_ARRAY);
+    }
+
+    private static String[] normalize(String[] patterns, String listName) {
+        if (patterns == null) {
+            return null;
+        }
+        String[] result = new String[patterns.length];
+        for (int i = 0; i < patterns.length; i++) {
+            if (patterns[i] == null) {
+                throw new NullPointerException(
+                        "If a non-null " + listName + " list is given, all 
elements must be" + " non-null");
+            }
+            String pattern = patterns[i].trim().replace('/', 
File.separatorChar).replace('\\', File.separatorChar);
+            if (pattern.endsWith(File.separator)) {
+                pattern += "**";
+            }
+            result[i] = pattern;
+        }
+        return result;
+    }
+
+    private static void scandir(
+            File dir, String vpath, MatchPatterns included, MatchPatterns 
excluded, List<String> files) {
+        String[] names = dir.list();
+        if (names == null) {
+            // the directory could not be read: nothing to scan there
+            return;
+        }
+        for (String name : names) {
+            String path = vpath + name;
+            File file = new File(dir, name);
+            if (file.isDirectory()) {
+                if (included.matches(path, true) && !excluded.matches(path, 
true)
+                        || included.matchesPatternStart(path, true)) {
+                    scandir(file, path + File.separator, included, excluded, 
files);
+                }
+            } else if (file.isFile() && included.matches(path, true) && 
!excluded.matches(path, true)) {
+                files.add(path);
+            }
+        }
+    }
+
     private static String[] processIncludesExcludes(List<String> list) {
         List<String> newList = new ArrayList<>();
         for (String include : list) {
diff --git 
a/surefire-api/src/main/java/org/apache/maven/surefire/api/util/internal/DirectoryScanner.java
 
b/surefire-api/src/main/java/org/apache/maven/surefire/api/util/internal/DirectoryScanner.java
deleted file mode 100644
index 6be6b8080..000000000
--- 
a/surefire-api/src/main/java/org/apache/maven/surefire/api/util/internal/DirectoryScanner.java
+++ /dev/null
@@ -1,154 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.maven.surefire.api.util.internal;
-
-import java.io.File;
-import java.util.ArrayList;
-import java.util.List;
-
-/**
- * Scans a directory for files matching include patterns and not matching 
exclude patterns, in Ant syntax
- * ({@code *}, {@code ?}, {@code **}) or as {@code %regex[...]}, see {@link 
SelectorUtils#matchPath}. Matching is
- * case-sensitive and symbolic links are followed.
- * <p>Reduced from the maven-shared-utils scanner to what Surefire uses, so 
that surefire-api, which ends up on the
- * test class path, needs no dependency for it.
- */
-public class DirectoryScanner {
-
-    private File basedir;
-
-    private String[] includes;
-
-    private String[] excludes;
-
-    private MatchPatterns includesPatterns;
-
-    private MatchPatterns excludesPatterns;
-
-    private List<String> filesIncluded;
-
-    /**
-     * @param basedir the directory to scan; {@link #scan()} fails if it does 
not exist or is not a directory
-     */
-    public void setBasedir(final File basedir) {
-        this.basedir = basedir;
-    }
-
-    public File getBasedir() {
-        return basedir;
-    }
-
-    /**
-     * @param includes patterns relative to the base directory, with {@code /} 
or {@code \} as separator; a pattern
-     *                 ending in a separator includes everything below. {@code 
null} includes everything.
-     * @throws NullPointerException if an element is {@code null}
-     */
-    public void setIncludes(final String... includes) {
-        this.includes = normalize(includes, "includes");
-    }
-
-    /**
-     * @param excludes patterns relative to the base directory, like {@link 
#setIncludes}; {@code null} excludes nothing
-     * @throws NullPointerException if an element is {@code null}
-     */
-    public void setExcludes(final String... excludes) {
-        this.excludes = normalize(excludes, "excludes");
-    }
-
-    private static String[] normalize(final String[] patterns, final String 
listName) {
-        if (patterns == null) {
-            return null;
-        }
-        String[] result = new String[patterns.length];
-        for (int i = 0; i < patterns.length; i++) {
-            if (patterns[i] == null) {
-                throw new NullPointerException(
-                        "If a non-null " + listName + " list is given, all 
elements must be" + " non-null");
-            }
-            String pattern = patterns[i].trim().replace('/', 
File.separatorChar).replace('\\', File.separatorChar);
-            if (pattern.endsWith(File.separator)) {
-                pattern += "**";
-            }
-            result[i] = pattern;
-        }
-        return result;
-    }
-
-    /**
-     * Scans the base directory. Afterwards {@link #getIncludedFiles()} lists 
the files that match an include pattern
-     * and no exclude pattern.
-     *
-     * @throws IllegalStateException if no base directory was set, or it does 
not exist or is not a directory
-     */
-    public void scan() throws IllegalStateException {
-        if (basedir == null) {
-            throw new IllegalStateException("No basedir set");
-        }
-        if (!basedir.exists()) {
-            throw new IllegalStateException("basedir " + basedir + " does not 
exist");
-        }
-        if (!basedir.isDirectory()) {
-            throw new IllegalStateException("basedir " + basedir + " is not a 
directory");
-        }
-        includesPatterns =
-                MatchPatterns.from(includes == null || includes.length == 0 ? 
new String[] {"**"} : includes);
-        excludesPatterns = MatchPatterns.from(excludes == null ? new String[0] 
: excludes);
-        filesIncluded = new ArrayList<>();
-        scandir(basedir, "");
-    }
-
-    private void scandir(final File dir, final String vpath) {
-        String[] newfiles = dir.list();
-        if (newfiles == null) {
-            // the directory could not be read: nothing to scan there
-            return;
-        }
-        for (final String newfile : newfiles) {
-            final String name = vpath + newfile;
-            final File file = new File(dir, newfile);
-            if (file.isDirectory()) {
-                if (isIncluded(name) && !isExcluded(name) || 
couldHoldIncluded(name)) {
-                    scandir(file, name + File.separator);
-                }
-            } else if (file.isFile() && isIncluded(name) && !isExcluded(name)) 
{
-                filesIncluded.add(name);
-            }
-        }
-    }
-
-    private boolean isIncluded(final String name) {
-        return includesPatterns.matches(name, true);
-    }
-
-    private boolean couldHoldIncluded(final String name) {
-        return includesPatterns.matchesPatternStart(name, true);
-    }
-
-    private boolean isExcluded(final String name) {
-        return excludesPatterns.matches(name, true);
-    }
-
-    /**
-     * @return the matched files, relative to the base directory with {@link 
File#separator}, in directory listing
-     *         order; empty before {@link #scan()}
-     */
-    public String[] getIncludedFiles() {
-        return filesIncluded == null ? new String[0] : 
filesIncluded.toArray(new String[0]);
-    }
-}
diff --git 
a/surefire-api/src/test/java/org/apache/maven/surefire/api/util/DefaultDirectoryScannerTest.java
 
b/surefire-api/src/test/java/org/apache/maven/surefire/api/util/DefaultDirectoryScannerTest.java
index 30fe76980..dbc98ce9b 100644
--- 
a/surefire-api/src/test/java/org/apache/maven/surefire/api/util/DefaultDirectoryScannerTest.java
+++ 
b/surefire-api/src/test/java/org/apache/maven/surefire/api/util/DefaultDirectoryScannerTest.java
@@ -20,15 +20,21 @@
 
 import java.io.File;
 import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
 import org.apache.maven.surefire.api.testset.TestSetFailedException;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
 
+import static org.apache.maven.surefire.api.util.DefaultDirectoryScanner.scan;
+import static org.assertj.core.api.Assertions.assertThat;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
 /**
  * Test of the directory scanner.
@@ -50,4 +56,100 @@ public void testLocateTestClasses() throws IOException, 
TestSetFailedException {
         System.out.println("classNames " + Arrays.asList(classNames));
         assertEquals(4, classNames.length);
     }
+
+    @TempDir
+    Path tmp;
+
+    private File tree() throws IOException {
+        for (String f : new String[] {
+            "file1.txt",
+            "file2.txt",
+            "folder1/file3.txt",
+            "folder1/file4.dat",
+            "folder1/sub/File5Test.class",
+            "folder2/File6Test.class",
+            "folder2/Helper.class",
+            ".hidden/file7.txt"
+        }) {
+            Path p = tmp.resolve(f);
+            Files.createDirectories(p.getParent());
+            Files.write(p, new byte[0]);
+        }
+        return tmp.toFile();
+    }
+
+    private static String p(String slashPath) {
+        return slashPath.replace('/', File.separatorChar);
+    }
+
+    @Test
+    void scanIncludesEverythingByDefault() throws IOException {
+        File base = tree();
+        assertThat(scan(base, null, null))
+                .hasSize(8)
+                .contains(p("folder1/sub/File5Test.class"), 
p(".hidden/file7.txt"));
+        assertThat(scan(base, new String[0], null)).hasSize(8);
+    }
+
+    @Test
+    void scanAntPatternsAcrossDirectories() throws IOException {
+        File base = tree();
+        assertThat(scan(base, new String[] {"**/*Test.class"}, null))
+                .containsExactlyInAnyOrder(p("folder1/sub/File5Test.class"), 
p("folder2/File6Test.class"));
+        assertThat(scan(base, new String[] {"*.txt"}, 
null)).containsExactlyInAnyOrder("file1.txt", "file2.txt");
+        assertThat(scan(base, new String[] {"folder1/*"}, null))
+                .containsExactlyInAnyOrder(p("folder1/file3.txt"), 
p("folder1/file4.dat"));
+        assertThat(scan(base, new String[] {"folder?/*.class"}, null))
+                .containsExactlyInAnyOrder(p("folder2/File6Test.class"), 
p("folder2/Helper.class"));
+    }
+
+    @Test
+    void scanExcludesWin() throws IOException {
+        File base = tree();
+        assertThat(scan(base, new String[] {"**/*.class"}, new String[] 
{"**/Helper.class"}))
+                .containsExactlyInAnyOrder(p("folder1/sub/File5Test.class"), 
p("folder2/File6Test.class"));
+        assertThat(scan(base, null, new String[] {"folder1/**", ".hidden/**"}))
+                .containsExactlyInAnyOrder(
+                        "file1.txt", "file2.txt", 
p("folder2/File6Test.class"), p("folder2/Helper.class"));
+    }
+
+    @Test
+    void scanTrailingSeparatorMeansEverythingBelow() throws IOException {
+        File base = tree();
+        assertThat(scan(base, new String[] {"folder1/"}, null))
+                .containsExactlyInAnyOrder(
+                        p("folder1/file3.txt"), p("folder1/file4.dat"), 
p("folder1/sub/File5Test.class"));
+        assertThat(scan(base, new String[] {"folder1\\sub\\"}, 
null)).containsExactly(p("folder1/sub/File5Test.class"));
+    }
+
+    @Test
+    void scanRegexPatterns() throws IOException {
+        // both slashes become the separator, inside a regex too, so the 
pattern avoids backslashes
+        assertThat(scan(tree(), new String[] 
{"%regex[.*File[0-9]Test[.]class]"}, null))
+                .containsExactlyInAnyOrder(p("folder1/sub/File5Test.class"), 
p("folder2/File6Test.class"));
+    }
+
+    @Test
+    void scanIsCaseSensitive() throws IOException {
+        File base = tree();
+        assertThat(scan(base, new String[] {"**/*test.class"}, 
null)).isEmpty();
+        assertThat(scan(base, new String[] {"FILE1.txt"}, null)).isEmpty();
+    }
+
+    @Test
+    void scanRejectsNullPatternsAndBadBasedir() throws IOException {
+        File base = tree();
+        assertThrows(NullPointerException.class, () -> scan(base, new String[] 
{"a", null}, null));
+        assertThrows(NullPointerException.class, () -> scan(base, null, new 
String[] {null}));
+        assertThrows(IllegalStateException.class, () -> scan(new File(base, 
"missing"), null, null));
+        assertThrows(IllegalStateException.class, () -> scan(new File(base, 
"file1.txt"), null, null));
+    }
+
+    @Test
+    void collectTestsTurnsClassFilesIntoClassNames() throws IOException {
+        File base = tree();
+        DefaultDirectoryScanner scanner = new DefaultDirectoryScanner(
+                base, Arrays.asList("**/*Test.java,**/Helper.class"), 
Arrays.asList("**/File6*"), null);
+        
assertThat(scanner.collectTests()).containsExactlyInAnyOrder("folder1.sub.File5Test",
 "folder2.Helper");
+    }
 }
diff --git 
a/surefire-api/src/test/java/org/apache/maven/surefire/api/util/internal/DirectoryScannerTest.java
 
b/surefire-api/src/test/java/org/apache/maven/surefire/api/util/internal/DirectoryScannerTest.java
deleted file mode 100644
index b54e0b527..000000000
--- 
a/surefire-api/src/test/java/org/apache/maven/surefire/api/util/internal/DirectoryScannerTest.java
+++ /dev/null
@@ -1,150 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.maven.surefire.api.util.internal;
-
-import java.io.File;
-import java.io.IOException;
-import java.nio.file.Files;
-import java.nio.file.Path;
-
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.io.TempDir;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.jupiter.api.Assertions.assertThrows;
-
-class DirectoryScannerTest {
-
-    @TempDir
-    Path basedir;
-
-    @BeforeEach
-    void createTestData() throws IOException {
-        for (String f : new String[] {
-            "file1.txt",
-            "file2.txt",
-            "folder1/file3.txt",
-            "folder1/file4.dat",
-            "folder1/sub/File5Test.class",
-            "folder2/File6Test.class",
-            "folder2/Helper.class",
-            ".hidden/file7.txt"
-        }) {
-            Path p = basedir.resolve(f);
-            Files.createDirectories(p.getParent());
-            Files.write(p, new byte[0]);
-        }
-    }
-
-    private String[] scan(String[] includes, String[] excludes) {
-        DirectoryScanner scanner = new DirectoryScanner();
-        scanner.setBasedir(basedir.toFile());
-        scanner.setIncludes(includes);
-        scanner.setExcludes(excludes);
-        scanner.scan();
-        return scanner.getIncludedFiles();
-    }
-
-    private static String p(String slashPath) {
-        return slashPath.replace('/', File.separatorChar);
-    }
-
-    @Test
-    void includesEverythingByDefault() {
-        assertThat(scan(null, null))
-                .containsExactlyInAnyOrder(
-                        p("file1.txt"),
-                        p("file2.txt"),
-                        p("folder1/file3.txt"),
-                        p("folder1/file4.dat"),
-                        p("folder1/sub/File5Test.class"),
-                        p("folder2/File6Test.class"),
-                        p("folder2/Helper.class"),
-                        p(".hidden/file7.txt"));
-    }
-
-    @Test
-    void emptyIncludesMeanEverything() {
-        assertThat(scan(new String[0], null)).hasSize(8);
-    }
-
-    @Test
-    void antPatternsAcrossDirectories() {
-        assertThat(scan(new String[] {"**/*Test.class"}, null))
-                .containsExactlyInAnyOrder(p("folder1/sub/File5Test.class"), 
p("folder2/File6Test.class"));
-        assertThat(scan(new String[] {"*.txt"}, 
null)).containsExactlyInAnyOrder("file1.txt", "file2.txt");
-        assertThat(scan(new String[] {"folder1/*"}, null))
-                .containsExactlyInAnyOrder(p("folder1/file3.txt"), 
p("folder1/file4.dat"));
-        assertThat(scan(new String[] {"folder?/*.class"}, null))
-                .containsExactlyInAnyOrder(p("folder2/File6Test.class"), 
p("folder2/Helper.class"));
-    }
-
-    @Test
-    void excludesWin() {
-        assertThat(scan(new String[] {"**/*.class"}, new String[] 
{"**/Helper.class"}))
-                .containsExactlyInAnyOrder(p("folder1/sub/File5Test.class"), 
p("folder2/File6Test.class"));
-        assertThat(scan(null, new String[] {"folder1/**", ".hidden/**"}))
-                .containsExactlyInAnyOrder(
-                        "file1.txt", "file2.txt", 
p("folder2/File6Test.class"), p("folder2/Helper.class"));
-    }
-
-    @Test
-    void trailingSeparatorMeansEverythingBelow() {
-        assertThat(scan(new String[] {"folder1/"}, null))
-                .containsExactlyInAnyOrder(
-                        p("folder1/file3.txt"), p("folder1/file4.dat"), 
p("folder1/sub/File5Test.class"));
-        assertThat(scan(new String[] {"folder1\\sub\\"}, 
null)).containsExactly(p("folder1/sub/File5Test.class"));
-    }
-
-    @Test
-    void regexPatterns() {
-        // setIncludes turns \\ into the separator even inside a regex, so the 
pattern avoids backslashes
-        assertThat(scan(new String[] {"%regex[.*File[0-9]Test[.]class]"}, 
null))
-                .containsExactlyInAnyOrder(p("folder1/sub/File5Test.class"), 
p("folder2/File6Test.class"));
-    }
-
-    @Test
-    void matchingIsCaseSensitive() {
-        assertThat(scan(new String[] {"**/*test.class"}, null)).isEmpty();
-        assertThat(scan(new String[] {"FILE1.txt"}, null)).isEmpty();
-    }
-
-    @Test
-    void nullElementsAreRejected() {
-        DirectoryScanner scanner = new DirectoryScanner();
-        assertThrows(NullPointerException.class, () -> 
scanner.setIncludes("a", null));
-        assertThrows(NullPointerException.class, () -> 
scanner.setExcludes((String) null));
-    }
-
-    @Test
-    void basedirMustBeAnExistingDirectory() {
-        DirectoryScanner scanner = new DirectoryScanner();
-        assertThrows(IllegalStateException.class, scanner::scan);
-        scanner.setBasedir(basedir.resolve("missing").toFile());
-        assertThrows(IllegalStateException.class, scanner::scan);
-        scanner.setBasedir(basedir.resolve("file1.txt").toFile());
-        assertThrows(IllegalStateException.class, scanner::scan);
-    }
-
-    @Test
-    void noFilesBeforeScan() {
-        assertThat(new DirectoryScanner().getIncludedFiles()).isEmpty();
-    }
-}

Reply via email to