16
16
package org .springframework .sbm .parsers ;
17
17
18
18
import org .intellij .lang .annotations .Language ;
19
+ import org .junit .jupiter .api .DisplayName ;
19
20
import org .junit .jupiter .api .Nested ;
20
21
import org .junit .jupiter .api .Test ;
21
22
import org .openrewrite .ExecutionContext ;
22
23
import org .openrewrite .InMemoryExecutionContext ;
24
+ import org .openrewrite .java .marker .JavaProject ;
23
25
import org .openrewrite .marker .Marker ;
26
+ import org .openrewrite .maven .MavenExecutionContextView ;
27
+ import org .openrewrite .maven .tree .*;
24
28
import org .openrewrite .xml .tree .Xml ;
25
29
import org .springframework .core .io .Resource ;
26
30
import org .springframework .sbm .test .util .DummyResource ;
30
34
import java .util .HashMap ;
31
35
import java .util .List ;
32
36
import java .util .Map ;
37
+ import java .util .UUID ;
33
38
34
39
import static org .assertj .core .api .Assertions .assertThat ;
40
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
35
41
36
42
/**
37
43
* @author Fabian Krüger
@@ -44,63 +50,63 @@ public class GivenSimpleMavenMultiModuleProject {
44
50
@ Language ("xml" )
45
51
private static final String POM_1 =
46
52
"""
47
- <?xml version="1.0" encoding="UTF-8"?>
48
- <project xmlns="http://maven.apache.org/POM/4.0.0"
49
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
50
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
51
- <modelVersion>4.0.0</modelVersion>
52
-
53
- <groupId>com.example</groupId>
54
- <artifactId>parent-module </artifactId>
55
- <version>1.0</version>
56
- <modules>
57
- <module>module1</module>
58
- </modules>
59
- </project>
60
- """ ;
53
+ <?xml version="1.0" encoding="UTF-8"?>
54
+ <project xmlns="http://maven.apache.org/POM/4.0.0"
55
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
56
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
57
+ <modelVersion>4.0.0</modelVersion>
58
+
59
+ <groupId>com.example</groupId>
60
+ <artifactId>parent</artifactId>
61
+ <version>1.0</version>
62
+ <modules>
63
+ <module>module1</module>
64
+ </modules>
65
+ </project>
66
+ """ ;
61
67
62
68
@ Language ("xml" )
63
69
private static final String POM_2 =
64
70
"""
65
- <?xml version="1.0" encoding="UTF-8"?>
66
- <project xmlns="http://maven.apache.org/POM/4.0.0"
67
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
68
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
69
- <modelVersion>4.0.0</modelVersion>
70
- <parent>
71
- <groupId>com.example</groupId>
72
- <artifactId>parent</artifactId>
73
- <version>1.0</version>
74
- </parent>
75
- <artifactId>module1</artifactId>
76
- <modules>
77
- <module>submodule</module>
78
- </modules>
79
- </project>
80
- """ ;
71
+ <?xml version="1.0" encoding="UTF-8"?>
72
+ <project xmlns="http://maven.apache.org/POM/4.0.0"
73
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
74
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
75
+ <modelVersion>4.0.0</modelVersion>
76
+ <parent>
77
+ <groupId>com.example</groupId>
78
+ <artifactId>parent</artifactId>
79
+ <version>1.0</version>
80
+ </parent>
81
+ <artifactId>module1</artifactId>
82
+ <modules>
83
+ <module>submodule</module>
84
+ </modules>
85
+ </project>
86
+ """ ;
81
87
82
88
@ Language ("xml" )
83
89
private static final String POM_3 =
84
90
"""
85
- <project xmlns="http://maven.apache.org/POM/4.0.0"
86
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
87
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
88
- <modelVersion>4.0.0</modelVersion>
89
- <parent>
90
- <groupId>com.example</groupId>
91
- <artifactId>module1</artifactId>
92
- <version>1.0</version>
93
- </parent>
94
- <artifactId>submodule</artifactId>
95
- </project>
96
- """ ;
97
-
98
- private BuildFileParser sut = new BuildFileParser (new ParserSettings ());
91
+ <project xmlns="http://maven.apache.org/POM/4.0.0"
92
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
93
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
94
+ <modelVersion>4.0.0</modelVersion>
95
+ <parent>
96
+ <groupId>com.example</groupId>
97
+ <artifactId>module1</artifactId>
98
+ <version>1.0</version>
99
+ </parent>
100
+ <artifactId>submodule</artifactId>
101
+ </project>
102
+ """ ;
103
+
104
+ private final BuildFileParser sut = new BuildFileParser (new ParserSettings ());
99
105
100
106
@ Test
101
107
void filterAndSortBuildFiles_shouldReturnSortedListOfFilteredBuildFiles () {
102
108
103
- // the poms have no order
109
+ // the provided resources have no order and contain non-pom files
104
110
List <Resource > resources = List .of (
105
111
new DummyResource ("src/test/resources/dummy/pom.xml" , "" ), // filtered
106
112
new DummyResource ("module1/submodule/pom.xml" , POM_3 ), // pos. 3
@@ -128,12 +134,25 @@ void filterAndSortBuildFiles_shouldReturnSortedListOfFilteredBuildFiles() {
128
134
@ Test
129
135
void parseBuildFiles_shouldReturnSortedListOfParsedBuildFiles () {
130
136
Path baseDir = Path .of ("." ).toAbsolutePath ().normalize ();
137
+ String module1SubmoduleSourcePath = "module1/submodule/pom.xml" ;
138
+ String parentSourcePath = "pom.xml" ;
139
+ String module1SourcePath = "module1/pom.xml" ;
131
140
List <Resource > filteredAndSortedBuildFiles = List .of (
132
- new DummyResource (baseDir , "module1/submodule/pom.xml" , POM_3 ),
133
- new DummyResource (baseDir , "pom.xml" , POM_1 ),
134
- new DummyResource (baseDir , "module1/pom.xml" , POM_2 )
141
+ new DummyResource (baseDir , module1SubmoduleSourcePath , POM_3 ),
142
+ new DummyResource (baseDir , parentSourcePath , POM_1 ),
143
+ new DummyResource (baseDir , module1SourcePath , POM_2 )
135
144
);
136
- Map <Path , List <Marker >> provenanceMarkers = new HashMap <>();
145
+
146
+ // provenance markers
147
+ Path module1SubmodulePomPath = baseDir .resolve (module1SubmoduleSourcePath );
148
+ Path parentPomPath = baseDir .resolve (parentSourcePath );
149
+ Path module1PomXml = baseDir .resolve (module1SourcePath );
150
+ Map <Path , List <Marker >> provenanceMarkers = Map .of (
151
+ module1SubmodulePomPath , List .of (new JavaProject (UUID .randomUUID (), module1SubmoduleSourcePath , null )),
152
+ parentPomPath , List .of (new JavaProject (UUID .randomUUID (), parentSourcePath , null )),
153
+ module1PomXml , List .of (new JavaProject (UUID .randomUUID (), module1SourcePath , null ))
154
+ );
155
+
137
156
ExecutionContext executionContext = new InMemoryExecutionContext (t -> t .printStackTrace ());
138
157
boolean skipMavenParsing = false ;
139
158
Map <Path , Xml .Document > parsedBuildFiles = sut .parseBuildFiles (
@@ -142,7 +161,73 @@ void parseBuildFiles_shouldReturnSortedListOfParsedBuildFiles() {
142
161
executionContext ,
143
162
skipMavenParsing ,
144
163
provenanceMarkers );
164
+
165
+ assertThat (parsedBuildFiles ).hasSize (3 );
166
+ assertThat (parsedBuildFiles .get (module1SubmodulePomPath ).getMarkers ().findFirst (JavaProject .class ).get ().getProjectName ()).isEqualTo (module1SubmoduleSourcePath );
167
+ assertThat (parsedBuildFiles .get (parentPomPath ).getMarkers ().findFirst (JavaProject .class ).get ().getProjectName ()).isEqualTo (parentSourcePath );
168
+ assertThat (parsedBuildFiles .get (module1PomXml ).getMarkers ().findFirst (JavaProject .class ).get ().getProjectName ()).isEqualTo (module1SourcePath );
169
+ }
170
+
171
+ @ Test
172
+ @ DisplayName ("parse without baseDir should throw exception" )
173
+ void parseWithoutBaseDirShouldThrowException () {
174
+ String message = assertThrows (
175
+ IllegalArgumentException .class ,
176
+ () -> sut .parseBuildFiles (null , List .of (), new InMemoryExecutionContext (), false , Map .of ())
177
+ )
178
+ .getMessage ();
179
+ assertThat (message ).isEqualTo ("Base directory must be provided but was null." );
180
+ }
181
+
182
+ @ Test
183
+ @ DisplayName ("parse with empty resources should throw exception" )
184
+ void parseWithEmptyResourcesShouldThrowException () {
185
+ String message = assertThrows (
186
+ IllegalArgumentException .class ,
187
+ () -> sut .parseBuildFiles (Path .of ("." ), List .of (), new InMemoryExecutionContext (), false , Map .of ())
188
+ )
189
+ .getMessage ();
190
+ assertThat (message ).isEqualTo ("No build files provided." );
145
191
}
192
+
193
+ @ Test
194
+ @ DisplayName ("parse with non-pom resources provided should throw exception" )
195
+ void parseWithNonPomResourcesProvidedShouldThrowException () {
196
+ Path baseDir = Path .of ("." ).toAbsolutePath ().normalize ();
197
+ Resource nonPomResource = new DummyResource (baseDir , "src/main/java/SomeClass.java" , "public class SomeClass {}" );
198
+ List <Resource > nonPomResource1 = List .of (nonPomResource );
199
+ String message = assertThrows (
200
+ IllegalArgumentException .class ,
201
+ () -> sut .parseBuildFiles (baseDir , nonPomResource1 , new InMemoryExecutionContext (), false , Map .of ())
202
+ )
203
+ .getMessage ();
204
+ assertThat (message ).isEqualTo ("Provided resources which are not Maven build files: '[" + baseDir +"/src/main/java/SomeClass.java]'" );
205
+ }
206
+
207
+ @ Test
208
+ @ DisplayName ("parse with incomplete provenance markers should throw exception" )
209
+ void parseWithIncompleteProvenanceMarkersShouldThrowException () {
210
+ Path baseDir = Path .of ("." ).toAbsolutePath ().normalize ();
211
+
212
+ Path pom1Path = baseDir .resolve ("pom.xml" );
213
+ Resource pom1 = new DummyResource (pom1Path , "" );
214
+ Path pom2Path = baseDir .resolve ("module1/pom.xml" );
215
+ Resource pom2 = new DummyResource (pom2Path , "" );
216
+ List <Resource > poms = List .of (pom1 , pom2 );
217
+
218
+ Map <Path , List <Marker >> provenanceMarkers = Map .of (
219
+ pom1Path , List .of (new JavaProject (UUID .randomUUID (), "pom.xml" , null ))
220
+ // no marker for module1/pom.xml
221
+ );
222
+
223
+ String message = assertThrows (
224
+ IllegalArgumentException .class ,
225
+ () -> sut .parseBuildFiles (baseDir , poms , new InMemoryExecutionContext (), false , provenanceMarkers )
226
+ )
227
+ .getMessage ();
228
+ assertThat (message ).isEqualTo ("No provenance marker provided for these pom files [" +Path .of ("." ).toAbsolutePath ().normalize ().resolve ("module1/pom.xml]" ));
229
+ }
230
+
146
231
}
147
232
148
233
}
0 commit comments