vgosain-psg commented on code in PR #1146:
URL: https://github.com/apache/tomcat/pull/1146#discussion_r4591220160


##########
build.xml:
##########
@@ -2025,9 +2025,24 @@
     <attribute name="profile"/>
     <sequential>
       <!-- Set test.name property based on the profile attribute -->
-      <condition property="test.name" value="${test.profile.smoke}">
+
+      <!-- For the smoke profile, discover matching tests by scanning compiled 
classes
+           for @Category(SmokeTest.class). The scanner outputs a 
comma-separated list
+           of .java paths that feeds directly into the batchtest fileset 
include pattern,
+           preserving per-test JVM isolation. -->
+      <local name="test.profile.is.smoke"/>
+      <condition property="test.profile.is.smoke">
         <equals arg1="@{profile}" arg2="smoke"/>
       </condition>
+      <java classname="org.apache.tomcat.categories.CategoryScanner"

Review Comment:
   Done — `fork="true"` added.



##########
test/org/apache/tomcat/categories/CategoryScanner.java:
##########
@@ -0,0 +1,95 @@
+/*
+ * 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.tomcat.categories;
+
+import java.io.File;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.junit.experimental.categories.Category;
+
+/**
+ * Scans compiled test classes for a given JUnit {@link Category} annotation
+ * and prints the matching source file paths as a comma-separated list suitable
+ * for use as an Ant {@code test.name} include pattern.
+ *
+ * <p>Usage: {@code CategoryScanner <category-class-name> <test-classes-dir>}
+ *
+ * <p>Output is written to stdout as a single comma-separated line, e.g.:
+ * {@code org/apache/catalina/core/TestFoo.java,org/apache/coyote/TestBar.java}
+ */
+public class CategoryScanner {
+
+    public static void main(String[] args) throws Exception {
+        if (args.length < 2) {
+            System.err.println("Usage: CategoryScanner <category-class-name> 
<test-classes-dir>");
+            System.exit(1);

Review Comment:
   Fixed — replaced `System.exit` with `throw new IllegalArgumentException`.



##########
test/org/apache/tomcat/categories/CategoryScanner.java:
##########
@@ -0,0 +1,95 @@
+/*
+ * 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.tomcat.categories;
+
+import java.io.File;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.junit.experimental.categories.Category;
+
+/**
+ * Scans compiled test classes for a given JUnit {@link Category} annotation
+ * and prints the matching source file paths as a comma-separated list suitable
+ * for use as an Ant {@code test.name} include pattern.
+ *
+ * <p>Usage: {@code CategoryScanner <category-class-name> <test-classes-dir>}
+ *
+ * <p>Output is written to stdout as a single comma-separated line, e.g.:
+ * {@code org/apache/catalina/core/TestFoo.java,org/apache/coyote/TestBar.java}
+ */
+public class CategoryScanner {
+
+    public static void main(String[] args) throws Exception {
+        if (args.length < 2) {
+            System.err.println("Usage: CategoryScanner <category-class-name> 
<test-classes-dir>");
+            System.exit(1);
+        }
+
+        String categoryClassName = args[0];
+        File testClassesDir = new File(args[1]);
+
+        if (!testClassesDir.isDirectory()) {
+            System.err.println("Not a directory: " + testClassesDir);
+            System.exit(1);
+        }
+
+        try (URLClassLoader loader = new URLClassLoader(
+                new URL[] { testClassesDir.toURI().toURL() },
+                CategoryScanner.class.getClassLoader())) {
+
+            Class<?> categoryClass = loader.loadClass(categoryClassName);
+            List<String> matches = new ArrayList<>();
+            scanDir(testClassesDir, testClassesDir, categoryClass, loader, 
matches);
+            Collections.sort(matches);
+            System.out.print(String.join(",", matches));
+        }
+    }
+
+    private static void scanDir(File root, File dir, Class<?> categoryClass,
+            ClassLoader loader, List<String> results) {
+        File[] files = dir.listFiles();
+        if (files == null) {
+            return;
+        }
+        for (File f : files) {
+            if (f.isDirectory()) {
+                scanDir(root, f, categoryClass, loader, results);
+            } else if (f.getName().endsWith(".class") && 
!f.getName().contains("$")) {
+                String relative = root.toURI().relativize(f.toURI()).getPath();
+                String className = relative.replace('/', 
'.').replace(".class", "");
+                try {
+                    Class<?> cls = loader.loadClass(className);
+                    Category cat = cls.getAnnotation(Category.class);
+                    if (cat != null) {
+                        for (Class<?> c : cat.value()) {
+                            if (c.equals(categoryClass)) {
+                                results.add(relative.replace(".class", 
".java"));
+                                break;
+                            }
+                        }
+                    }
+                } catch (Exception | Error ignored) {
+                    // Skip classes that cannot be loaded (e.g. abstract base 
classes)

Review Comment:
   Fixed — now throws `RuntimeException` with the class name on load failure.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to