|
21 | 21 | import java.util.concurrent.TimeUnit;
|
22 | 22 |
|
23 | 23 | import org.junit.jupiter.api.Test;
|
24 |
| -import org.junit.jupiter.api.condition.EnabledIf; |
25 | 24 |
|
26 | 25 | import org.springframework.util.FileCopyUtils;
|
27 | 26 |
|
|
36 | 35 | class SampleAntApplicationIT {
|
37 | 36 |
|
38 | 37 | @Test
|
39 |
| - @EnabledIf("isTestEnabled") |
40 | 38 | void runJar() throws Exception {
|
41 |
| - File buildDir = new File("build"); |
42 |
| - System.out.println("Build directory exists: " + buildDir.exists()); |
43 |
| - System.out.println("Build directory files: " + java.util.Arrays.toString(buildDir.list())); |
44 |
| - |
45 |
| - File antDir = new File("build/ant"); |
46 |
| - System.out.println("Ant directory exists: " + antDir.exists()); |
47 |
| - System.out.println("Ant directory files: " + java.util.Arrays.toString(antDir.list())); |
48 |
| - |
49 | 39 | File libs = new File("build/ant/libs");
|
50 |
| - System.out.println("Libs directory exists: " + libs.exists()); |
51 |
| - System.out.println("Libs directory files: " + java.util.Arrays.toString(libs.list())); |
52 |
| - |
53 |
| - // Look for any JAR files in the libs directory |
54 |
| - File[] jarFiles = libs.listFiles((dir, name) -> name.endsWith(".jar")); |
55 |
| - System.out.println("JAR files in libs: " + java.util.Arrays.toString(jarFiles)); |
56 |
| - |
57 |
| - String jarName = "spring-boot-smoke-test-ant.jar"; |
58 |
| - if (jarFiles != null && jarFiles.length > 0) { |
59 |
| - // Use the first JAR file found |
60 |
| - jarName = jarFiles[0].getName(); |
61 |
| - System.out.println("Using JAR file: " + jarName); |
62 |
| - } |
63 |
| - |
64 |
| - ProcessBuilder processBuilder = new ProcessBuilder("java", "-jar", jarName); |
65 |
| - // Set PATH to include Java bin directory |
66 |
| - java.util.Map<String, String> env = processBuilder.environment(); |
67 |
| - String currentPath = env.get("PATH"); |
68 |
| - env.put("PATH", "/usr/lib/jvm/java-17-openjdk-amd64/bin" + ((currentPath != null) ? ":" + currentPath : "")); |
| 40 | + String javaExecutable = findJavaExecutable(); |
| 41 | + ProcessBuilder processBuilder = new ProcessBuilder(javaExecutable, "-jar", "spring-boot-smoke-test-ant.jar"); |
69 | 42 | Process process = processBuilder.directory(libs).start();
|
70 | 43 | process.waitFor(5, TimeUnit.MINUTES);
|
71 | 44 | assertThat(process.exitValue()).isZero();
|
72 | 45 | String output = FileCopyUtils.copyToString(new InputStreamReader(process.getInputStream()));
|
73 |
| - System.out.println("Application output: '" + output + "'"); |
74 | 46 | assertThat(output).contains("Spring Boot Ant Example");
|
75 | 47 | }
|
76 | 48 |
|
77 | 49 | private String findJavaExecutable() {
|
78 |
| - // Use the same Java executable that's running the current JVM |
79 |
| - String javaHome = System.getProperty("java.home"); |
80 |
| - if (javaHome != null) { |
81 |
| - File javaExecutable = new File(javaHome, "bin/java"); |
82 |
| - if (javaExecutable.exists()) { |
83 |
| - return javaExecutable.getAbsolutePath(); |
84 |
| - } |
85 |
| - } |
86 |
| - |
87 |
| - // Fallback to PATH |
| 50 | + // Fallback to PATH first in CI environment |
88 | 51 | return "java";
|
89 | 52 | }
|
90 | 53 |
|
91 |
| - boolean isJavaExecutableAvailable() { |
92 |
| - try { |
93 |
| - String javaExecutable = findJavaExecutable(); |
94 |
| - if (!javaExecutable.equals("java")) { |
95 |
| - return new File(javaExecutable).exists(); |
96 |
| - } |
97 |
| - // Try to execute java to see if it's available in PATH |
98 |
| - ProcessBuilder pb = new ProcessBuilder("java", "-version"); |
99 |
| - Process process = pb.start(); |
100 |
| - return process.waitFor() == 0; |
101 |
| - } |
102 |
| - catch (Exception ex) { |
103 |
| - return false; |
104 |
| - } |
105 |
| - } |
106 |
| - |
107 |
| - boolean isJarAvailable() { |
108 |
| - File libs = new File("build/ant/libs"); |
109 |
| - if (!libs.exists()) { |
110 |
| - return false; |
111 |
| - } |
112 |
| - File[] jarFiles = libs.listFiles((dir, name) -> name.endsWith(".jar")); |
113 |
| - return jarFiles != null && jarFiles.length > 0; |
114 |
| - } |
115 |
| - |
116 |
| - boolean isTestEnabled() { |
117 |
| - return isJavaExecutableAvailable() && isJarAvailable(); |
118 |
| - } |
119 |
| - |
120 | 54 | }
|
0 commit comments