1010import java .time .Duration ;
1111import java .util .Arrays ;
1212import java .util .Collection ;
13- import java .util .Collections ; // For emptyList()
13+ import java .util .Collections ;
1414import java .util .List ;
1515import java .util .concurrent .Callable ;
1616import java .util .concurrent .ExecutionException ;
1919import java .util .concurrent .Future ;
2020import java .util .concurrent .TimeUnit ;
2121import java .util .concurrent .TimeoutException ;
22+ import java .util .regex .Pattern ; // Import Pattern
2223import java .util .stream .Collectors ;
2324import org .junit .jupiter .api .Assertions ;
2425import org .junit .jupiter .api .DynamicTest ;
2526import org .junit .jupiter .api .TestFactory ;
2627
2728public class MavenTest {
2829
29- // Removed the hardcoded PROBLEMS list
30-
3130 // Define the number of threads for the ExecutorService
3231 private static final int MAX_THREADS = 10 ;
3332 // Define the timeout for each test
3433 private static final Duration TEST_TIMEOUT = Duration .ofSeconds (3 );
3534
35+ // Regex pattern to match "p" followed by only digits
36+ private static final Pattern PROBLEM_DIR_PATTERN = Pattern .compile ("p\\ d+" );
37+
3638 // Method to dynamically discover problem names
3739 private static List <String > discoverProblemNames () {
38- // The base package for UVA solutions
3940 String basePackagePath = "com/lzw/solutions/uva/" ;
4041 Path uvaSolutionsPath = null ;
4142
4243 try {
43- // Get the URL for the package directory within the classpath
44- // This works whether running from IDE or jar
4544 URL resource = Thread .currentThread ().getContextClassLoader ().getResource (basePackagePath );
4645 if (resource == null ) {
4746 System .err .println ("Could not find resource path: " + basePackagePath );
4847 return Collections .emptyList ();
4948 }
5049
51- // Convert URL to Path, handling different URL schemes (e.g., jar:file:/...)
52- // For simple file system, this is usually enough
50+ // Handle JAR paths correctly. If it's in a JAR, resource.toURI() will be "jar:file:/..."
51+ // We need to resolve the actual file system path if running from an unzipped structure,
52+ // or return an empty list if it's purely in a JAR and cannot be listed as a directory.
53+ if ("jar" .equals (resource .getProtocol ())) {
54+ System .err .println (
55+ "Cannot discover problems dynamically from within a JAR file. Please ensure 'src/main/java' is accessible on the file system during testing." );
56+ // In a real scenario, you might have a pre-defined list for JAR runs,
57+ // or expect tests to be run against expanded directories.
58+ return Collections .emptyList ();
59+ }
60+
5361 uvaSolutionsPath = Paths .get (resource .toURI ());
62+
5463 } catch (URISyntaxException e ) {
5564 System .err .println ("Error converting resource URL to URI: " + e .getMessage ());
5665 return Collections .emptyList ();
5766 }
5867
5968 if (uvaSolutionsPath == null || !Files .exists (uvaSolutionsPath )) {
60- System .err .println ("UVA Solutions directory not found: " + uvaSolutionsPath );
69+ System .err .println ("UVA Solutions directory not found or not a directory : " + uvaSolutionsPath );
6170 return Collections .emptyList ();
6271 }
6372
@@ -66,8 +75,9 @@ private static List<String> discoverProblemNames() {
6675 File [] problemDirs = uvaDir .listFiles (new FilenameFilter () {
6776 @ Override
6877 public boolean accept (File current , String name ) {
69- // Ensure it's a directory and starts with 'p' (e.g., p100, p140)
70- return new File (current , name ).isDirectory () && name .startsWith ("p" );
78+ // Ensure it's a directory AND matches the "p" + digits pattern
79+ return new File (current , name ).isDirectory ()
80+ && PROBLEM_DIR_PATTERN .matcher (name ).matches ();
7181 }
7282 });
7383
0 commit comments