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
11
12
import java .io .File ;
12
13
import java .io .FileInputStream ;
16
17
import java .text .MessageFormat ;
17
18
import java .util .Properties ;
18
19
20
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
21
+
19
22
@ Testcontainers
20
23
@ SuppressWarnings ("resource" )
21
24
public class CodeAnalyzerIntegrationTest {
@@ -46,7 +49,19 @@ public class CodeAnalyzerIntegrationTest {
46
49
.withFileSystemBind (
47
50
String .valueOf (Paths .get (System .getProperty ("user.dir" )).resolve ("build/libs" )),
48
51
"/opt/jars" ,
49
- BindMode .READ_WRITE );
52
+ BindMode .READ_WRITE )
53
+ .withCopyFileToContainer (MountableFile .forHostPath (Paths .get (System .getProperty ("user.dir" )).resolve ("build/libs" )), "/opt/jars" )
54
+ .withCopyFileToContainer (MountableFile .forHostPath (Paths .get (System .getProperty ("user.dir" )).resolve ("src/test/resources/test-applications/mvnw-corrupt-test" )), "/test-applications/mvnw-corrupt-test" )
55
+ .withCopyFileToContainer (MountableFile .forHostPath (Paths .get (System .getProperty ("user.dir" )).resolve ("src/test/resources/test-applications/mvnw-working-test" )), "/test-applications/mvnw-working-test" );
56
+
57
+ @ Container
58
+ static final GenericContainer <?> mavenContainer = new GenericContainer <>("maven:3.8.3-openjdk-17" )
59
+ .withCreateContainerCmdModifier (cmd -> cmd .withEntrypoint ("sh" ))
60
+ .withCommand ("-c" , "while true; do sleep 1; done" )
61
+ .withCopyFileToContainer (MountableFile .forHostPath (Paths .get (System .getProperty ("user.dir" )).resolve ("build/libs" )), "/opt/jars" )
62
+ .withCopyFileToContainer (MountableFile .forHostPath (Paths .get (System .getProperty ("user.dir" )).resolve ("src/test/resources/test-applications/mvnw-corrupt-test" )), "/test-applications/mvnw-corrupt-test" )
63
+ .withCopyFileToContainer (MountableFile .forHostPath (Paths .get (System .getProperty ("user.dir" )).resolve ("src/test/resources/test-applications/mvnw-working-test" )), "/test-applications/mvnw-working-test" );
64
+
50
65
51
66
@ BeforeAll
52
67
static void setUp () {
@@ -73,7 +88,8 @@ void shouldHaveCodeAnalyzerJar() throws Exception {
73
88
Assertions .assertTrue (dirContents .getStdout ().contains ("codeanalyzer" ), "Codeanalyzer.jar not found in the container." );
74
89
}
75
90
76
- @ Test void shouldBeAbleToRunCodeAnalyzer () throws Exception {
91
+ @ Test
92
+ void shouldBeAbleToRunCodeAnalyzer () throws Exception {
77
93
var runCodeAnalyzerJar = container .execInContainer (
78
94
"java" ,
79
95
"-jar" ,
@@ -84,5 +100,50 @@ void shouldHaveCodeAnalyzerJar() throws Exception {
84
100
Assertions .assertEquals (0 , runCodeAnalyzerJar .getExitCode (),
85
101
"Command should execute successfully" );
86
102
Assertions .assertTrue (runCodeAnalyzerJar .getStdout ().length () > 0 ,
87
- "Should have some output" ); }
103
+ "Should have some output" );
104
+ }
105
+
106
+ @ Test
107
+ void corruptMavenShouldNotBuildWithWrapper () throws IOException , InterruptedException {
108
+ // Make executable
109
+ mavenContainer .execInContainer ("chmod" , "+x" , "/test-applications/mvnw-corrupt-test/mvnw" );
110
+ // Let's start by building the project by itself
111
+ var mavenProjectBuildWithWrapper = mavenContainer .withWorkingDirectory ("/test-applications/mvnw-corrupt-test" ).execInContainer ("/test-applications/mvnw-corrupt-test/mvnw" , "clean" , "compile" );
112
+ Assertions .assertNotEquals (0 , mavenProjectBuildWithWrapper .getExitCode ());
113
+ }
114
+
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
+ // }
88
149
}
0 commit comments