10
10
import java .time .Duration ;
11
11
import java .util .Arrays ;
12
12
import java .util .Collection ;
13
- import java .util .Collections ; // For emptyList()
13
+ import java .util .Collections ;
14
14
import java .util .List ;
15
15
import java .util .concurrent .Callable ;
16
16
import java .util .concurrent .ExecutionException ;
19
19
import java .util .concurrent .Future ;
20
20
import java .util .concurrent .TimeUnit ;
21
21
import java .util .concurrent .TimeoutException ;
22
+ import java .util .regex .Pattern ; // Import Pattern
22
23
import java .util .stream .Collectors ;
23
24
import org .junit .jupiter .api .Assertions ;
24
25
import org .junit .jupiter .api .DynamicTest ;
25
26
import org .junit .jupiter .api .TestFactory ;
26
27
27
28
public class MavenTest {
28
29
29
- // Removed the hardcoded PROBLEMS list
30
-
31
30
// Define the number of threads for the ExecutorService
32
31
private static final int MAX_THREADS = 10 ;
33
32
// Define the timeout for each test
34
33
private static final Duration TEST_TIMEOUT = Duration .ofSeconds (3 );
35
34
35
+ // Regex pattern to match "p" followed by only digits
36
+ private static final Pattern PROBLEM_DIR_PATTERN = Pattern .compile ("p\\ d+" );
37
+
36
38
// Method to dynamically discover problem names
37
39
private static List <String > discoverProblemNames () {
38
- // The base package for UVA solutions
39
40
String basePackagePath = "com/lzw/solutions/uva/" ;
40
41
Path uvaSolutionsPath = null ;
41
42
42
43
try {
43
- // Get the URL for the package directory within the classpath
44
- // This works whether running from IDE or jar
45
44
URL resource = Thread .currentThread ().getContextClassLoader ().getResource (basePackagePath );
46
45
if (resource == null ) {
47
46
System .err .println ("Could not find resource path: " + basePackagePath );
48
47
return Collections .emptyList ();
49
48
}
50
49
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
+
53
61
uvaSolutionsPath = Paths .get (resource .toURI ());
62
+
54
63
} catch (URISyntaxException e ) {
55
64
System .err .println ("Error converting resource URL to URI: " + e .getMessage ());
56
65
return Collections .emptyList ();
57
66
}
58
67
59
68
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 );
61
70
return Collections .emptyList ();
62
71
}
63
72
@@ -66,8 +75,9 @@ private static List<String> discoverProblemNames() {
66
75
File [] problemDirs = uvaDir .listFiles (new FilenameFilter () {
67
76
@ Override
68
77
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 ();
71
81
}
72
82
});
73
83
0 commit comments