|
| 1 | +import static org.junit.jupiter.api.Assertions.*; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStream; |
| 6 | +import java.io.InputStreamReader; |
| 7 | +import java.nio.file.Files; |
| 8 | +import java.nio.file.Paths; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.List; |
| 11 | +import java.util.regex.Pattern; |
| 12 | +import java.util.stream.Collectors; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | + |
| 15 | +public class StartWithAgentTest { |
| 16 | + |
| 17 | + private static final Pattern WARNING_PATTERN = Pattern.compile("^Warning: Version [^ ]+ of dd-java-agent is not compatible with Java [^ ]+ and will not be installed\\.$"); |
| 18 | + private static final String UPGRADE_MESSAGE = "Please upgrade your Java version to 8+ or use the 0.x version of dd-java-agent in your build tool or download it from https://dtdg.co/java-tracer-v0"; |
| 19 | + |
| 20 | + @Test |
| 21 | + void ensureThatApplicationStartsWithAgentOnJava7() throws InterruptedException, IOException { |
| 22 | + String expectedMessage = "Woho! Started on Java 7"; |
| 23 | + Process process = startAndWaitForJvmWithAgentForJava("JAVA_7_HOME", expectedMessage); |
| 24 | + int exitCode = process.waitFor(); |
| 25 | + List<String> output = getLines(process.getInputStream()); |
| 26 | + List<String> errors = getLines(process.getErrorStream()); |
| 27 | + logProcessOutput(output, errors); |
| 28 | + assertEquals(0, exitCode, "Command failed with unexpected exit code"); |
| 29 | + assertTrue(output.contains(expectedMessage), "Output does not contain '" + expectedMessage + "'"); |
| 30 | + assertTrue(output.stream().anyMatch(WARNING_PATTERN.asPredicate()), "Output does not contain line matching '" + WARNING_PATTERN + "'"); |
| 31 | + assertTrue(output.contains(UPGRADE_MESSAGE), "Output does not contain '" + UPGRADE_MESSAGE + "'"); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + void ensureThatApplicationStartsWithAgentOnJava8() throws InterruptedException, IOException { |
| 36 | + ensureThatApplicationStartsWithoutWarning("8"); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + void ensureThatApplicationStartsWithAgentOnJava11() throws InterruptedException, IOException { |
| 41 | + ensureThatApplicationStartsWithoutWarning("11"); |
| 42 | + } |
| 43 | + |
| 44 | + private static void ensureThatApplicationStartsWithoutWarning(String version) throws InterruptedException, IOException { |
| 45 | + String expectedMessage = "Woho! Started on Java " + version; |
| 46 | + Process process = startAndWaitForJvmWithAgentForJava("JAVA_" + version + "_HOME", expectedMessage); |
| 47 | + int exitCode = process.waitFor(); |
| 48 | + List<String> output = getLines(process.getInputStream()); |
| 49 | + List<String> errors = getLines(process.getErrorStream()); |
| 50 | + logProcessOutput(output, errors); |
| 51 | + assertEquals(0, exitCode, "Command failed with unexpected exit code"); |
| 52 | + assertTrue(output.contains(expectedMessage), "Output does not contain '" + expectedMessage + "'"); |
| 53 | + assertFalse(output.stream().anyMatch(WARNING_PATTERN.asPredicate()), "Output contains unexpected line matching '" + WARNING_PATTERN + "'"); |
| 54 | + assertFalse(output.contains(UPGRADE_MESSAGE), "Output contains unexpected line '" + UPGRADE_MESSAGE + "'"); |
| 55 | + } |
| 56 | + |
| 57 | + private static Process startAndWaitForJvmWithAgentForJava(String javaHomeEnv, String message) throws IOException { |
| 58 | + String javaHome = System.getenv(javaHomeEnv); |
| 59 | + checkFile(javaHome, javaHomeEnv); |
| 60 | + String javaAgent = System.getProperty("test.published.dependencies.agent"); |
| 61 | + checkFile(javaAgent, "test.published.dependencies.agent"); |
| 62 | + String jarFile = System.getProperty("test.published.dependencies.jar"); |
| 63 | + checkFile(jarFile, "test.published.dependencies.jar"); |
| 64 | + |
| 65 | + List<String> commandLine = new ArrayList<>(); |
| 66 | + commandLine.add(Paths.get(javaHome).resolve("bin").resolve("java").toString()); |
| 67 | + commandLine.add("-Xmx256M"); |
| 68 | + commandLine.add("-javaagent:" + javaAgent); |
| 69 | + commandLine.add("-jar"); |
| 70 | + commandLine.add(jarFile); |
| 71 | + commandLine.add(message); |
| 72 | + ProcessBuilder builder = new ProcessBuilder(commandLine); |
| 73 | + builder.environment().put("JAVA_HOME", javaHome); |
| 74 | + Process process = builder.start(); |
| 75 | + return process; |
| 76 | + } |
| 77 | + |
| 78 | + private static void checkFile(String file, String name) { |
| 79 | + assertNotNull(file, name + " should be set"); |
| 80 | + assertFalse(file.isEmpty(), name + " should not be empty "); |
| 81 | + assertTrue(Files.exists(Paths.get(file)), name + " [" + file + "] should be an existing path"); |
| 82 | + } |
| 83 | + |
| 84 | + private static List<String> getLines(InputStream inputStream) { |
| 85 | + return new BufferedReader(new InputStreamReader(inputStream)).lines().collect(Collectors.toList()); |
| 86 | + } |
| 87 | + |
| 88 | + private static void logProcessOutput(List<String> output, List<String> errors) { |
| 89 | + System.out.println("-------------------"); |
| 90 | + System.out.println("Sub process output:"); |
| 91 | + output.forEach(System.out::println); |
| 92 | + System.out.println("-------------------"); |
| 93 | + System.out.println("Sub process errors:"); |
| 94 | + errors.forEach(System.out::println); |
| 95 | + System.out.println("-------------------"); |
| 96 | + System.out.println("Sub process done."); |
| 97 | + } |
| 98 | +} |
0 commit comments