Skip to content

Commit 98ea868

Browse files
committed
Add Integration Tests to replicate behaviour.
Signed-off-by: Rahul Krishna <[email protected]>
1 parent 7b38a27 commit 98ea868

File tree

11 files changed

+571
-1110
lines changed

11 files changed

+571
-1110
lines changed

build.gradle

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ sourceSets {
6262
}
6363
}
6464

65+
tasks.register('integrationTest', Test) {
66+
description = 'Runs integration tests.'
67+
group = 'verification'
68+
testClassesDirs = sourceSets.integrationTest.output.classesDirs
69+
classpath = sourceSets.integrationTest.runtimeClasspath
70+
}
71+
72+
tasks.check { dependsOn integrationTest }
73+
6574
// Remove that nagging bin folder vscode seems to generate every single time
6675
clean.doFirst {
6776
delete "${rootDir}/bin"

src/it/java/com/ibm/cldk/CodeAnalyzerIntegrationTest.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.testcontainers.junit.jupiter.Container;
99
import org.testcontainers.junit.jupiter.Testcontainers;
1010

11+
import java.io.File;
1112
import java.io.FileInputStream;
1213
import java.io.IOException;
1314
import java.nio.file.Path;
@@ -23,10 +24,25 @@ public class CodeAnalyzerIntegrationTest {
2324
* Creates a Java 11 test container that mounts the build/libs folder.
2425
*/
2526
static String codeanalyzerVersion;
27+
28+
static {
29+
// Build project first
30+
try {
31+
Process process = new ProcessBuilder("./gradlew", "clean", "fatJar")
32+
.directory(new File(System.getProperty("user.dir")))
33+
.start();
34+
if (process.waitFor() != 0) {
35+
throw new RuntimeException("Build failed");
36+
}
37+
} catch (IOException | InterruptedException e) {
38+
throw new RuntimeException("Failed to build codeanalyzer", e);
39+
}
40+
}
41+
2642
@Container
2743
static final GenericContainer<?> container = new GenericContainer<>("openjdk:11-jdk")
2844
.withCreateContainerCmdModifier(cmd -> cmd.withEntrypoint("sh"))
29-
.withCommand("-c", "while true; do sleep 1; done") // Keep container running
45+
.withCommand("-c", "while true; do sleep 1; done")
3046
.withFileSystemBind(
3147
String.valueOf(Paths.get(System.getProperty("user.dir")).resolve("build/libs")),
3248
"/opt/jars",
@@ -35,14 +51,12 @@ public class CodeAnalyzerIntegrationTest {
3551
@BeforeAll
3652
static void setUp() {
3753
Properties properties = new Properties();
38-
Path propertiesPath = Paths.get(System.getProperty("user.dir"), "gradle.properties");
39-
40-
try (FileInputStream fis = new FileInputStream(propertiesPath.toFile())) {
54+
try (FileInputStream fis = new FileInputStream(
55+
Paths.get(System.getProperty("user.dir"), "gradle.properties").toFile())) {
4156
properties.load(fis);
4257
} catch (IOException e) {
4358
throw new RuntimeException(e);
4459
}
45-
4660
codeanalyzerVersion = properties.getProperty("version");
4761
}
4862

src/it/java/com/ibm/cldk/MavenApplicationIntegrationTest.java

Lines changed: 75 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@
77
import org.testcontainers.containers.GenericContainer;
88
import org.testcontainers.junit.jupiter.Container;
99
import org.testcontainers.junit.jupiter.Testcontainers;
10+
import org.testcontainers.utility.MountableFile;
1011

12+
import java.io.File;
1113
import java.io.FileInputStream;
1214
import java.io.IOException;
15+
import java.net.URL;
1316
import java.nio.file.Path;
1417
import java.nio.file.Paths;
18+
import java.util.Objects;
1519
import java.util.Properties;
1620

21+
import static org.junit.jupiter.api.Assertions.assertThrows;
22+
1723
@Testcontainers
1824
@SuppressWarnings("resource")
1925
public class MavenApplicationIntegrationTest {
@@ -22,40 +28,88 @@ public class MavenApplicationIntegrationTest {
2228
* Creates a Java 11 test container that mounts the build/libs folder.
2329
*/
2430
static String codeanalyzerVersion;
31+
32+
static {
33+
// Build project first
34+
try {
35+
Process process = new ProcessBuilder("./gradlew", "clean", "fatJar")
36+
.directory(new File(System.getProperty("user.dir")))
37+
.start();
38+
if (process.waitFor() != 0) {
39+
throw new RuntimeException("Build failed");
40+
}
41+
} catch (IOException | InterruptedException e) {
42+
throw new RuntimeException("Failed to build codeanalyzer", e);
43+
}
44+
}
2545
@Container
26-
static final GenericContainer<?> baseJavaContainer = new GenericContainer<>("openjdk:11-jdk")
27-
.withCreateContainerCmdModifier(cmd -> cmd.withEntrypoint("sh"))
28-
.withCommand("-c", "while true; do sleep 1; done") // Keep container running
29-
.withFileSystemBind(
30-
String.valueOf(Paths.get(System.getProperty("user.dir")).resolve("build/libs")),
31-
"/opt/jars",
32-
BindMode.READ_WRITE)
33-
// Copy the java project to the
34-
.withFileSystemBind(
35-
MavenApplicationIntegrationTest.class.getResource("/test-applications/simple-maven-project").getPath(),
36-
"/projects/simple-maven-project",
37-
BindMode.READ_ONLY);
46+
static final GenericContainer<?> baseJavaContainerNoMaven = new GenericContainer<>("openjdk:17-jdk").withCreateContainerCmdModifier(cmd -> cmd.withEntrypoint("sh")).withCommand("-c", "while true; do sleep 1; done") // Keep container running
47+
.withCopyFileToContainer(MountableFile.forHostPath(Paths.get(System.getProperty("user.dir")).resolve("build/libs")), "/opt/jars")
48+
.withCopyFileToContainer(MountableFile.forHostPath(Paths.get(System.getProperty("user.dir")).resolve("src/it/resources/test-applications/mvnw-corrupt-test")), "/test-applications/mvnw-corrupt-test")
49+
.withCopyFileToContainer(MountableFile.forHostPath(Paths.get(System.getProperty("user.dir")).resolve("src/it/resources/test-applications/mvnw-working-test")), "/test-applications/mvnw-working-test");
50+
3851

3952
@Container
40-
static final GenericContainer<?> mavenContainer = new GenericContainer<>("maven:3.9-eclipse-temurin-11")
53+
static final GenericContainer<?> mavenContainer = new GenericContainer<>("maven:3.8.3-openjdk-17")
4154
.withCreateContainerCmdModifier(cmd -> cmd.withEntrypoint("sh"))
4255
.withCommand("-c", "while true; do sleep 1; done")
43-
.withFileSystemBind(
44-
String.valueOf(Paths.get(System.getProperty("user.dir")).resolve("build/libs")),
45-
"/opt/jars",
46-
BindMode.READ_WRITE);
56+
.withCopyFileToContainer(MountableFile.forHostPath(Paths.get(System.getProperty("user.dir")).resolve("build/libs")), "/opt/jars")
57+
.withCopyFileToContainer(MountableFile.forHostPath(Paths.get(System.getProperty("user.dir")).resolve("src/it/resources/test-applications/mvnw-corrupt-test")), "/test-applications/mvnw-corrupt-test")
58+
.withCopyFileToContainer(MountableFile.forHostPath(Paths.get(System.getProperty("user.dir")).resolve("src/it/resources/test-applications/mvnw-working-test")), "/test-applications/mvnw-working-test");
4759

4860
@BeforeAll
4961
static void setUp() {
5062
Properties properties = new Properties();
51-
Path propertiesPath = Paths.get(System.getProperty("user.dir"), "gradle.properties");
52-
53-
try (FileInputStream fis = new FileInputStream(propertiesPath.toFile())) {
63+
try (FileInputStream fis = new FileInputStream(
64+
Paths.get(System.getProperty("user.dir"), "gradle.properties").toFile())) {
5465
properties.load(fis);
5566
} catch (IOException e) {
5667
throw new RuntimeException(e);
5768
}
58-
5969
codeanalyzerVersion = properties.getProperty("version");
6070
}
71+
72+
@Test
73+
void corruptMavenShouldNotBuildWithWrapper() throws IOException, InterruptedException {
74+
// Make executable
75+
mavenContainer.execInContainer("chmod", "+x", "/test-applications/mvnw-corrupt-test/mvnw");
76+
// Let's start by building the project by itself
77+
var mavenProjectBuildWithWrapper = mavenContainer.withWorkingDirectory("/test-applications/mvnw-corrupt-test").execInContainer("/test-applications/mvnw-corrupt-test/mvnw", "clean", "compile");
78+
Assertions.assertNotEquals(0, mavenProjectBuildWithWrapper.getExitCode());
79+
}
80+
81+
@Test
82+
void corruptMavenShouldProduceAnalysisArtifactsWhenMVNCommandIsInPath() throws IOException, InterruptedException {
83+
// Let's start by building the project by itself
84+
var corruptMavenProjectBuild = mavenContainer.withWorkingDirectory("/test-applications/mvnw-corrupt-test").execInContainer("mvn", "-f", "/test-applications/mvnw-corrupt-test/pom.xml", "clean", "compile");
85+
Assertions.assertEquals(0, corruptMavenProjectBuild.getExitCode(), "Failed to build the project with system's default Maven.");
86+
// NOw run codeanalyzer and assert if analysis.json is generated.
87+
mavenContainer.execInContainer("java", "-jar", String.format("/opt/jars/codeanalyzer-%s.jar", codeanalyzerVersion), "--input=/test-applications/mvnw-corrupt-test", "--output=/tmp/", "--analysis-level=2", "--no-build");
88+
var codeAnalyzerOutputDirContents = mavenContainer.execInContainer("ls", "/tmp/analysis.json");
89+
String codeAnalyzerOutputDirContentsStdOut = codeAnalyzerOutputDirContents.getStdout();
90+
Assertions.assertTrue(codeAnalyzerOutputDirContentsStdOut.length() > 0, "Could not find 'analysis.json'.");
91+
Assertions.assertTrue(codeAnalyzerOutputDirContentsStdOut.contains("Building the project using") && codeAnalyzerOutputDirContentsStdOut.contains("/mvn."));
92+
Assertions.assertFalse(codeAnalyzerOutputDirContentsStdOut.contains("Building the project using") && codeAnalyzerOutputDirContentsStdOut.contains("/test-applications/mvnw-corrupt-test/mvnw."));
93+
}
94+
95+
@Test
96+
void corruptMavenShouldNotTerminateWithErrorWhenMavenIsNotPresentUnlessAnalysisLevel2() throws IOException, InterruptedException {
97+
// When analysis level 2, we should get a Runtime Exception
98+
assertThrows(RuntimeException.class, () ->
99+
baseJavaContainerNoMaven.execInContainer(
100+
"java",
101+
"-jar",
102+
String.format("/opt/jars/codeanalyzer-%s.jar", codeanalyzerVersion),
103+
"--input=/test-applications/mvnw-corrupt-test",
104+
"--output=/tmp/",
105+
"--analysis-level=2"
106+
)
107+
);
108+
// When analysis level is 1, we should still be able to generate an analysis.json file.
109+
baseJavaContainerNoMaven.execInContainer("java", "-jar", String.format("/opt/jars/codeanalyzer-%s.jar", codeanalyzerVersion), "--input=/test-applications/mvnw-corrupt-test", "--output=/tmp/", "--analysis-level=1");
110+
var codeAnalyzerOutputDirContents = baseJavaContainerNoMaven.execInContainer("ls", "/tmp/analysis.json");
111+
String codeAnalyzerOutputDirContentsStdOut = codeAnalyzerOutputDirContents.getStdout();
112+
Assertions.assertTrue(codeAnalyzerOutputDirContentsStdOut.length() > 0, "Could not find 'analysis.json'.");
113+
Assertions.assertTrue(codeAnalyzerOutputDirContentsStdOut.contains("Could not find Maven or a valid Maven Wrapper"));
114+
}
61115
}

0 commit comments

Comments
 (0)