@@ -27,6 +27,7 @@ public class CodeAnalyzerIntegrationTest {
2727 * Creates a Java 11 test container that mounts the build/libs folder.
2828 */
2929 static String codeanalyzerVersion ;
30+ static final String javaVersion = "17" ;
3031
3132 static {
3233 // Build project first
@@ -43,7 +44,7 @@ public class CodeAnalyzerIntegrationTest {
4344 }
4445
4546 @ Container
46- static final GenericContainer <?> container = new GenericContainer <>("openjdk:11 -jdk" )
47+ static final GenericContainer <?> container = new GenericContainer <>("openjdk:17 -jdk" )
4748 .withCreateContainerCmdModifier (cmd -> cmd .withEntrypoint ("sh" ))
4849 .withCommand ("-c" , "while true; do sleep 1; done" )
4950 .withFileSystemBind (
@@ -76,9 +77,11 @@ static void setUp() {
7677 }
7778
7879 @ Test
79- void shouldHaveJava11Installed () throws Exception {
80- var result = container .execInContainer ("java" , "-version" );
81- Assertions .assertTrue (result .getStderr ().contains ("openjdk version \" 11" ));
80+ void shouldHaveCorrectJavaVersionInstalled () throws Exception {
81+ var baseContainerresult = container .execInContainer ("java" , "-version" );
82+ var mvnContainerresult = mavenContainer .execInContainer ("java" , "-version" );
83+ Assertions .assertTrue (baseContainerresult .getStderr ().contains ("openjdk version \" " + javaVersion ), "Base container Java version should be " + javaVersion );
84+ Assertions .assertTrue (mvnContainerresult .getStderr ().contains ("openjdk version \" " + javaVersion ), "Maven container Java version should be " + javaVersion );
8285 }
8386
8487 @ Test
@@ -112,38 +115,34 @@ void corruptMavenShouldNotBuildWithWrapper() throws IOException, InterruptedExce
112115 Assertions .assertNotEquals (0 , mavenProjectBuildWithWrapper .getExitCode ());
113116 }
114117
115- // @Test
116- // void corruptMavenShouldProduceAnalysisArtifactsWhenMVNCommandIsInPath() throws IOException, InterruptedException {
117- // // Let's start by building the project by itself
118- // var corruptMavenProjectBuild = mavenContainer.withWorkingDirectory("/test-applications/mvnw-corrupt-test").execInContainer("mvn", "-f", "/test-applications/mvnw-corrupt-test/pom.xml", "clean", "compile");
119- // Assertions.assertEquals(0, corruptMavenProjectBuild.getExitCode(), "Failed to build the project with system's default Maven.");
120- // // NOw run codeanalyzer and assert if analysis.json is generated.
121- // 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");
122- // var codeAnalyzerOutputDirContents = mavenContainer.execInContainer("ls", "/tmp/analysis.json");
123- // String codeAnalyzerOutputDirContentsStdOut = codeAnalyzerOutputDirContents.getStdout();
124- // Assertions.assertTrue(codeAnalyzerOutputDirContentsStdOut.length() > 0, "Could not find 'analysis.json'.");
125- // Assertions.assertTrue(codeAnalyzerOutputDirContentsStdOut.contains("Building the project using") && codeAnalyzerOutputDirContentsStdOut.contains("/mvn."));
126- // Assertions.assertFalse(codeAnalyzerOutputDirContentsStdOut.contains("Building the project using") && codeAnalyzerOutputDirContentsStdOut.contains("/test-applications/mvnw-corrupt-test/mvnw."));
127- // }
128- //
129- // @Test
130- // void corruptMavenShouldNotTerminateWithErrorWhenMavenIsNotPresentUnlessAnalysisLevel2() throws IOException, InterruptedException {
131- // // When analysis level 2, we should get a Runtime Exception
132- // assertThrows(RuntimeException.class, () ->
133- // container.execInContainer(
134- // "java",
135- // "-jar",
136- // String.format("/opt/jars/codeanalyzer-%s.jar", codeanalyzerVersion),
137- // "--input=/test-applications/mvnw-corrupt-test",
138- // "--output=/tmp/",
139- // "--analysis-level=2"
140- // )
141- // );
142- // // When analysis level is 1, we should still be able to generate an analysis.json file.
143- // container.execInContainer("java", "-jar", String.format("/opt/jars/codeanalyzer-%s.jar", codeanalyzerVersion), "--input=/test-applications/mvnw-corrupt-test", "--output=/tmp/", "--analysis-level=1");
144- // var codeAnalyzerOutputDirContents = container.execInContainer("ls", "/tmp/analysis.json");
145- // String codeAnalyzerOutputDirContentsStdOut = codeAnalyzerOutputDirContents.getStdout();
146- // Assertions.assertTrue(codeAnalyzerOutputDirContentsStdOut.length() > 0, "Could not find 'analysis.json'.");
147- // Assertions.assertTrue(codeAnalyzerOutputDirContentsStdOut.contains("Could not find Maven or a valid Maven Wrapper"));
148- // }
118+ @ Test
119+ void corruptMavenShouldProduceAnalysisArtifactsWhenMVNCommandIsInPath () throws IOException , InterruptedException {
120+ // Let's start by building the project by itself
121+ var corruptMavenProjectBuild = mavenContainer .withWorkingDirectory ("/test-applications/mvnw-corrupt-test" ).execInContainer ("mvn" , "-f" , "/test-applications/mvnw-corrupt-test/pom.xml" , "clean" , "compile" );
122+ Assertions .assertEquals (0 , corruptMavenProjectBuild .getExitCode (), "Failed to build the project with system's default Maven." );
123+ // NOw run codeanalyzer and assert if analysis.json is generated.
124+ var runCodeAnalyzer = mavenContainer .execInContainer ("java" , "-jar" , String .format ("/opt/jars/codeanalyzer-%s.jar" , codeanalyzerVersion ), "--input=/test-applications/mvnw-corrupt-test" , "--output=/tmp/" , "--analysis-level=2" , "--verbose" , "--no-build" );
125+ var codeAnalyzerOutputDirContents = mavenContainer .execInContainer ("ls" , "/tmp/analysis.json" );
126+ String codeAnalyzerOutputDirContentsStdOut = codeAnalyzerOutputDirContents .getStdout ();
127+ Assertions .assertTrue (codeAnalyzerOutputDirContentsStdOut .length () > 0 , "Could not find 'analysis.json'." );
128+ // mvnw is corrupt, so we should see an error message in the output.
129+ Assertions .assertTrue (runCodeAnalyzer .getStdout ().contains ("[ERROR]\t Cannot run program \" /test-applications/mvnw-corrupt-test/mvnw\" " ) && runCodeAnalyzer .getStdout ().contains ("/mvn." ));
130+ // We should correctly identify the build tool used in the mvn command from the system path.
131+ Assertions .assertTrue (runCodeAnalyzer .getStdout ().contains ("[INFO]\t Building the project using /usr/bin/mvn." ));
132+ }
133+
134+ @ Test
135+ void corruptMavenShouldNotTerminateWithErrorWhenMavenIsNotPresentUnlessAnalysisLevel2 () throws IOException , InterruptedException {
136+ // When analysis level 2, we should get a Runtime Exception
137+ var runCodeAnalyzer = container .execInContainer (
138+ "java" ,
139+ "-jar" ,
140+ String .format ("/opt/jars/codeanalyzer-%s.jar" , codeanalyzerVersion ),
141+ "--input=/test-applications/mvnw-corrupt-test" ,
142+ "--output=/tmp/" ,
143+ "--analysis-level=2"
144+ );
145+ Assertions .assertEquals (1 , runCodeAnalyzer .getExitCode ());
146+ Assertions .assertTrue (runCodeAnalyzer .getStderr ().contains ("java.lang.RuntimeException" ));
147+ }
149148}
0 commit comments