7
7
import org .testcontainers .containers .GenericContainer ;
8
8
import org .testcontainers .junit .jupiter .Container ;
9
9
import org .testcontainers .junit .jupiter .Testcontainers ;
10
+ import org .testcontainers .utility .MountableFile ;
10
11
12
+ import java .io .File ;
11
13
import java .io .FileInputStream ;
12
14
import java .io .IOException ;
15
+ import java .net .URL ;
13
16
import java .nio .file .Path ;
14
17
import java .nio .file .Paths ;
18
+ import java .util .Objects ;
15
19
import java .util .Properties ;
16
20
21
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
22
+
17
23
@ Testcontainers
18
24
@ SuppressWarnings ("resource" )
19
25
public class MavenApplicationIntegrationTest {
@@ -22,40 +28,88 @@ public class MavenApplicationIntegrationTest {
22
28
* Creates a Java 11 test container that mounts the build/libs folder.
23
29
*/
24
30
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
+ }
25
45
@ 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
+
38
51
39
52
@ 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 " )
41
54
.withCreateContainerCmdModifier (cmd -> cmd .withEntrypoint ("sh" ))
42
55
.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" );
47
59
48
60
@ BeforeAll
49
61
static void setUp () {
50
62
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 ())) {
54
65
properties .load (fis );
55
66
} catch (IOException e ) {
56
67
throw new RuntimeException (e );
57
68
}
58
-
59
69
codeanalyzerVersion = properties .getProperty ("version" );
60
70
}
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
+ }
61
115
}
0 commit comments