1
1
package com .ibm .cldk ;
2
2
3
+ import com .google .gson .Gson ;
4
+ import com .google .gson .JsonArray ;
5
+ import com .google .gson .JsonElement ;
6
+ import com .google .gson .JsonObject ;
7
+ import org .json .JSONArray ;
3
8
import org .junit .jupiter .api .BeforeAll ;
4
9
import org .junit .jupiter .api .Test ;
5
10
import org .junit .jupiter .api .Assertions ;
6
11
import org .testcontainers .containers .BindMode ;
7
12
import org .testcontainers .containers .GenericContainer ;
13
+ import org .testcontainers .containers .startupcheck .OneShotStartupCheckStrategy ;
8
14
import org .testcontainers .junit .jupiter .Container ;
9
15
import org .testcontainers .junit .jupiter .Testcontainers ;
10
16
import org .testcontainers .utility .MountableFile ;
11
17
12
18
import java .io .File ;
13
19
import java .io .FileInputStream ;
14
20
import java .io .IOException ;
15
- import java .nio .file .Path ;
16
21
import java .nio .file .Paths ;
17
- import java .text . MessageFormat ;
22
+ import java .time . Duration ;
18
23
import java .util .Properties ;
24
+ import java .util .stream .StreamSupport ;
19
25
20
- import static org .junit .jupiter .api .Assertions .assertThrows ;
21
26
22
27
@ Testcontainers
23
28
@ SuppressWarnings ("resource" )
@@ -28,7 +33,7 @@ public class CodeAnalyzerIntegrationTest {
28
33
*/
29
34
static String codeanalyzerVersion ;
30
35
static final String javaVersion = "17" ;
31
-
36
+ static String javaHomePath ;
32
37
static {
33
38
// Build project first
34
39
try {
@@ -44,15 +49,14 @@ public class CodeAnalyzerIntegrationTest {
44
49
}
45
50
46
51
@ Container
47
- static final GenericContainer <?> container = new GenericContainer <>("openjdk:17-jdk " )
52
+ static final GenericContainer <?> container = new GenericContainer <>("ubuntu:latest " )
48
53
.withCreateContainerCmdModifier (cmd -> cmd .withEntrypoint ("sh" ))
49
54
.withCommand ("-c" , "while true; do sleep 1; done" )
50
- .withFileSystemBind (
51
- String .valueOf (Paths .get (System .getProperty ("user.dir" )).resolve ("build/libs" )),
52
- "/opt/jars" ,
53
- BindMode .READ_WRITE )
55
+ .withCopyFileToContainer (MountableFile .forHostPath (Paths .get (System .getProperty ("user.dir" )).resolve ("build/libs" )), "/opt/jars" )
54
56
.withCopyFileToContainer (MountableFile .forHostPath (Paths .get (System .getProperty ("user.dir" )).resolve ("build/libs" )), "/opt/jars" )
55
57
.withCopyFileToContainer (MountableFile .forHostPath (Paths .get (System .getProperty ("user.dir" )).resolve ("src/test/resources/test-applications/mvnw-corrupt-test" )), "/test-applications/mvnw-corrupt-test" )
58
+ .withCopyFileToContainer (MountableFile .forHostPath (Paths .get (System .getProperty ("user.dir" )).resolve ("src/test/resources/test-applications/plantsbywebsphere" )), "/test-applications/plantsbywebsphere" )
59
+ .withCopyFileToContainer (MountableFile .forHostPath (Paths .get (System .getProperty ("user.dir" )).resolve ("src/test/resources/test-applications/call-graph-test" )), "/test-applications/call-graph-test" )
56
60
.withCopyFileToContainer (MountableFile .forHostPath (Paths .get (System .getProperty ("user.dir" )).resolve ("src/test/resources/test-applications/mvnw-working-test" )), "/test-applications/mvnw-working-test" );
57
61
58
62
@ Container
@@ -61,11 +65,32 @@ public class CodeAnalyzerIntegrationTest {
61
65
.withCommand ("-c" , "while true; do sleep 1; done" )
62
66
.withCopyFileToContainer (MountableFile .forHostPath (Paths .get (System .getProperty ("user.dir" )).resolve ("build/libs" )), "/opt/jars" )
63
67
.withCopyFileToContainer (MountableFile .forHostPath (Paths .get (System .getProperty ("user.dir" )).resolve ("src/test/resources/test-applications/mvnw-corrupt-test" )), "/test-applications/mvnw-corrupt-test" )
64
- .withCopyFileToContainer (MountableFile .forHostPath (Paths .get (System .getProperty ("user.dir" )).resolve ("src/test/resources/test-applications/mvnw-working-test" )), "/test-applications/mvnw-working-test" );
68
+ .withCopyFileToContainer (MountableFile .forHostPath (Paths .get (System .getProperty ("user.dir" )).resolve ("src/test/resources/test-applications/mvnw-working-test" )), "/test-applications/mvnw-working-test" )
69
+ .withCopyFileToContainer (MountableFile .forHostPath (Paths .get (System .getProperty ("user.dir" )).resolve ("src/test/resources/test-applications/daytrader8" )), "/test-applications/daytrader8" );
65
70
71
+ public CodeAnalyzerIntegrationTest () throws IOException , InterruptedException {
72
+ }
66
73
67
74
@ BeforeAll
68
75
static void setUp () {
76
+ // Install Java 17 in the base container
77
+ try {
78
+ container .execInContainer ("apt-get" , "update" );
79
+ container .execInContainer ("apt-get" , "install" , "-y" , "openjdk-17-jdk" );
80
+
81
+ // Get JAVA_HOME dynamically
82
+ var javaHomeResult = container .execInContainer ("bash" , "-c" ,
83
+ "dirname $(dirname $(readlink -f $(which java)))"
84
+ );
85
+ javaHomePath = javaHomeResult .getStdout ().trim ();
86
+ Assertions .assertFalse (javaHomePath .isEmpty (), "Failed to determine JAVA_HOME" );
87
+
88
+ } catch (IOException | InterruptedException e ) {
89
+ throw new RuntimeException (e );
90
+ }
91
+
92
+
93
+ // Get the version of the codeanalyzer jar
69
94
Properties properties = new Properties ();
70
95
try (FileInputStream fis = new FileInputStream (
71
96
Paths .get (System .getProperty ("user.dir" ), "gradle.properties" ).toFile ())) {
@@ -94,18 +119,36 @@ void shouldHaveCodeAnalyzerJar() throws Exception {
94
119
@ Test
95
120
void shouldBeAbleToRunCodeAnalyzer () throws Exception {
96
121
var runCodeAnalyzerJar = container .execInContainer (
97
- "java" ,
98
- "-jar" ,
99
- String .format ("/opt/jars/codeanalyzer-%s.jar" , codeanalyzerVersion ),
100
- "--help"
101
- );
122
+ "bash" , "-c" ,
123
+ String .format ("export JAVA_HOME=%s && java -jar /opt/jars/codeanalyzer-%s.jar --help" ,
124
+ javaHomePath , codeanalyzerVersion
125
+ ));
102
126
103
127
Assertions .assertEquals (0 , runCodeAnalyzerJar .getExitCode (),
104
128
"Command should execute successfully" );
105
129
Assertions .assertTrue (runCodeAnalyzerJar .getStdout ().length () > 0 ,
106
130
"Should have some output" );
107
131
}
108
132
133
+ @ Test
134
+ void callGraphShouldHaveKnownEdges () throws Exception {
135
+ var whatIsInTheTestAppFolder = container .execInContainer ("ls" , "/test-applications/call-graph-test" );
136
+ var runCodeAnalyzerOnCallGraphTest = container .execInContainer (
137
+ "bash" , "-c" ,
138
+ String .format (
139
+ "export JAVA_HOME=%s && java -jar /opt/jars/codeanalyzer-%s.jar --input=/test-applications/call-graph-test --analysis-level=2" ,
140
+ javaHomePath , codeanalyzerVersion
141
+ )
142
+ );
143
+
144
+ // Read the output JSON
145
+ Gson gson = new Gson ();
146
+ JsonObject jsonObject = gson .fromJson (runCodeAnalyzerOnCallGraphTest .getStdout (), JsonObject .class );
147
+ JsonArray systemDepGraph = jsonObject .getAsJsonArray ("system_dependency_graph" );
148
+ Assertions .assertEquals (4 , StreamSupport .stream (systemDepGraph .spliterator (), false ).count (),
149
+ "Expected exactly 4 entries in the system dependency graph" );
150
+ }
151
+
109
152
@ Test
110
153
void corruptMavenShouldNotBuildWithWrapper () throws IOException , InterruptedException {
111
154
// Make executable
@@ -129,19 +172,19 @@ void corruptMavenShouldProduceAnalysisArtifactsWhenMVNCommandIsInPath() throws I
129
172
Assertions .assertTrue (runCodeAnalyzer .getStdout ().contains ("[ERROR]\t Cannot run program \" /test-applications/mvnw-corrupt-test/mvnw\" " ) && runCodeAnalyzer .getStdout ().contains ("/mvn." ));
130
173
// We should correctly identify the build tool used in the mvn command from the system path.
131
174
Assertions .assertTrue (runCodeAnalyzer .getStdout ().contains ("[INFO]\t Building the project using /usr/bin/mvn." ));
132
- }
175
+ }
133
176
134
177
@ Test
135
178
void corruptMavenShouldNotTerminateWithErrorWhenMavenIsNotPresentUnlessAnalysisLevel2 () throws IOException , InterruptedException {
136
179
// When analysis level 2, we should get a Runtime Exception
137
180
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
- );
181
+ "bash" , "-c " ,
182
+ String . format (
183
+ "export JAVA_HOME=%s && java -jar /opt/jars/codeanalyzer-%s.jar --input=/test-applications/mvnw-corrupt-test --output=/tmp/ --analysis-level=2" ,
184
+ javaHomePath , codeanalyzerVersion
185
+ )
186
+ );
187
+
145
188
Assertions .assertEquals (1 , runCodeAnalyzer .getExitCode ());
146
189
Assertions .assertTrue (runCodeAnalyzer .getStderr ().contains ("java.lang.RuntimeException" ));
147
190
}
0 commit comments