Skip to content

Commit 31245fc

Browse files
committed
Test for getRequestedDependencies in JEE ear project
- Missing test for getEffectiveDependencies - Missing test for getDeclaredDependencies - Add method to create ProjectContext from files
1 parent bb5b100 commit 31245fc

File tree

8 files changed

+263
-17
lines changed

8 files changed

+263
-17
lines changed

components/sbm-core/src/main/java/org/springframework/sbm/build/impl/OpenRewriteMavenBuildFile.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -280,20 +280,20 @@ public List<Dependency> getRequestedDependencies() {
280280
resolvedArtifactId,
281281
d.getScope() != null ? Scope.fromName(d.getScope()) : null
282282
);
283-
// if (dependencies.isEmpty()) {
284-
// // requested dependency from another module in this multi-module project won't be resolvable
285-
// d.setGroupId(resolvedGroupId);
286-
// d.setArtifactId(resolvedArtifactId);
287-
// d.setVersion(resolvedVersion);
288-
//// return d;
289-
// } else {
283+
if (dependencies.isEmpty()) {
284+
// requested dependency from another module in this multi-module project won't be resolvable
285+
d.setGroupId(resolvedGroupId);
286+
d.setArtifactId(resolvedArtifactId);
287+
d.setVersion(resolvedVersion);
288+
}
289+
else {
290290
ResolvedDependency resolvedDependency = dependencies.get(0);
291291
d.setGroupId(resolvedGroupId);
292292
d.setArtifactId(resolvedArtifactId);
293293
d.setVersion(resolvedDependency.getVersion());
294294
d.setClassifier(resolvedDependency.getClassifier());
295295
d.setType(resolvedDependency.getType());
296-
// }
296+
}
297297

298298

299299
if(d.getScope() == null ) {

components/sbm-core/src/test/java/org/springframework/sbm/project/buildfile/OpenRewriteMavenBuildFileTest.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818
import org.intellij.lang.annotations.Language;
1919
import org.jetbrains.annotations.NotNull;
2020
import org.junit.jupiter.api.*;
21+
import org.junit.jupiter.api.io.TempDir;
2122
import org.mockito.ArgumentCaptor;
2223
import org.mockito.Mockito;
2324
import org.springframework.context.ApplicationEventPublisher;
2425
import org.springframework.sbm.build.api.BuildFile;
2526
import org.springframework.sbm.build.api.DependenciesChangedEvent;
2627
import org.springframework.sbm.build.api.Dependency;
28+
import org.springframework.sbm.build.api.Module;
2729
import org.springframework.sbm.build.api.Plugin;
2830
import org.springframework.sbm.build.util.PomBuilder;
2931
import org.springframework.sbm.engine.context.ProjectContext;
@@ -823,6 +825,82 @@ void testDeleteDependencies() {
823825
assertThat(sut.getDeclaredDependencies()).hasSize(0);
824826
}
825827

828+
@Nested
829+
class GetDependenciesEarMultiModuleTest {
830+
@Test
831+
void getRequestedDependencies() {
832+
ProjectContext context = TestProjectContext.buildFromDir(Path.of("./testcode/jee-ear-project/given"));
833+
834+
835+
// ear module
836+
BuildFile ear = getBuildFileByPackagingType(context, "ear");
837+
838+
List<Dependency> requestedDependenciesInEar = ear.getRequestedDependencies();
839+
840+
Dependency businessLogic = requestedDependenciesInEar.get(0);
841+
assertThat(businessLogic.getGroupId()).isEqualTo("com.example");
842+
assertThat(businessLogic.getArtifactId()).isEqualTo("business-logic");
843+
assertThat(businessLogic.getScope()).isEqualTo("compile");
844+
assertThat(businessLogic.getVersion()).isEqualTo("1.0");
845+
assertThat(businessLogic.getType()).isEqualTo("ejb");
846+
847+
Dependency webapp = requestedDependenciesInEar.get(1);
848+
assertThat(webapp.getGroupId()).isEqualTo("com.example");
849+
assertThat(webapp.getArtifactId()).isEqualTo("webapp");
850+
assertThat(webapp.getScope()).isEqualTo("compile");
851+
assertThat(webapp.getVersion()).isEqualTo("1.0");
852+
assertThat(webapp.getType()).isEqualTo("war");
853+
854+
// business-logic
855+
BuildFile ejb = getBuildFileByPackagingType(context, "ejb");
856+
857+
List<Dependency> requestedDependenciesInEjb = ejb.getRequestedDependencies();
858+
859+
Dependency business = requestedDependenciesInEjb.get(0);
860+
assertThat(business.getGroupId()).isEqualTo("org.apache.tomee");
861+
assertThat(business.getArtifactId()).isEqualTo("javaee-api");
862+
assertThat(business.getScope()).isEqualTo("provided");
863+
assertThat(business.getVersion()).isEqualTo("8.0-6");
864+
assertThat(business.getType()).isEqualTo("jar");
865+
866+
// webapp
867+
BuildFile web = getBuildFileByPackagingType(context, "war");
868+
869+
List<Dependency> requestedDependenciesInWeb = web.getRequestedDependencies();
870+
871+
Dependency jeeApiInWebapp = requestedDependenciesInWeb.get(0);
872+
assertThat(jeeApiInWebapp.getGroupId()).isEqualTo("org.apache.tomee");
873+
assertThat(jeeApiInWebapp.getArtifactId()).isEqualTo("javaee-api");
874+
assertThat(jeeApiInWebapp.getScope()).isEqualTo("provided");
875+
assertThat(jeeApiInWebapp.getVersion()).isEqualTo("7.0");
876+
assertThat(jeeApiInWebapp.getType()).isEqualTo("jar");
877+
878+
Dependency jstlInWebapp = requestedDependenciesInWeb.get(1);
879+
assertThat(jstlInWebapp.getGroupId()).isEqualTo("javax.servlet");
880+
assertThat(jstlInWebapp.getArtifactId()).isEqualTo("jstl");
881+
assertThat(jstlInWebapp.getScope()).isEqualTo("provided");
882+
assertThat(jstlInWebapp.getVersion()).isEqualTo("1.2");
883+
assertThat(jstlInWebapp.getType()).isEqualTo("jar");
884+
885+
Dependency taglibsInWebapp = requestedDependenciesInWeb.get(2);
886+
assertThat(taglibsInWebapp.getGroupId()).isEqualTo("taglibs");
887+
assertThat(taglibsInWebapp.getArtifactId()).isEqualTo("standard");
888+
assertThat(taglibsInWebapp.getScope()).isEqualTo("provided");
889+
assertThat(taglibsInWebapp.getVersion()).isEqualTo("1.1.2");
890+
assertThat(taglibsInWebapp.getType()).isEqualTo("jar");
891+
}
892+
893+
// TODO: Add test for getEffectiveDependencies()
894+
// TODO: Add test for getDeclaredDecpendencies()
895+
896+
@NotNull
897+
private BuildFile getBuildFileByPackagingType(ProjectContext context, String ear1) {
898+
return context.getApplicationModules().stream().map(m -> m.getBuildFile()).filter(b -> {
899+
return b.getPackaging().equals(ear1);
900+
}).findFirst().get();
901+
}
902+
}
903+
826904
/**
827905
* Test get[declared|requested|effective]Dependencies for pom files in multi-module project
828906
*/

components/sbm-core/src/test/java/org/springframework/sbm/project/resource/TestProjectContext.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.openrewrite.maven.utilities.MavenArtifactDownloader;
2222
import org.springframework.context.ApplicationEventPublisher;
2323
import org.springframework.core.annotation.Order;
24+
import org.springframework.core.io.DefaultResourceLoader;
2425
import org.springframework.core.io.Resource;
2526
import org.springframework.sbm.build.impl.OpenRewriteMavenBuildFile;
2627
import org.springframework.sbm.build.impl.RewriteMavenArtifactDownloader;
@@ -39,19 +40,12 @@
3940
import org.springframework.sbm.openrewrite.RewriteExecutionContext;
4041
import org.springframework.sbm.project.RewriteSourceFileWrapper;
4142
import org.springframework.sbm.project.TestDummyResource;
42-
import org.springframework.sbm.project.parser.DependencyHelper;
43-
import org.springframework.sbm.project.parser.JavaProvenanceMarkerFactory;
44-
import org.springframework.sbm.project.parser.MavenProjectParser;
45-
import org.springframework.sbm.project.parser.MavenConfigHandler;
46-
import org.springframework.sbm.project.parser.ProjectContextInitializer;
47-
import org.springframework.sbm.project.parser.ResourceParser;
48-
import org.springframework.sbm.project.parser.RewriteJsonParser;
49-
import org.springframework.sbm.project.parser.RewritePlainTextParser;
50-
import org.springframework.sbm.project.parser.RewriteYamlParser;
43+
import org.springframework.sbm.project.parser.*;
5144
import org.springframework.sbm.properties.parser.RewritePropertiesParser;
5245
import org.springframework.sbm.xml.parser.RewriteXmlParser;
5346

5447
import java.io.ByteArrayInputStream;
48+
import java.io.IOException;
5549
import java.nio.charset.StandardCharsets;
5650
import java.nio.file.Path;
5751
import java.util.*;
@@ -222,6 +216,29 @@ public static Path getDefaultProjectRoot() {
222216

223217
public static String getDefaultPackageName() { return DEFAULT_PACKAGE_NAME; }
224218

219+
public static ProjectContext buildFromDir(Path of) {
220+
final Path absoluteProjectRoot = of.toAbsolutePath().normalize();
221+
ResourceHelper resourceHelper = new ResourceHelper(new DefaultResourceLoader());
222+
SbmApplicationProperties sbmApplicationProperties = new SbmApplicationProperties();
223+
List<String> ignorePatterns = List.of(
224+
"sbm.ignoredPathsPatterns=**/.git/**,**/target/**,**/build/**,**/.gradle/**,**/.idea/**,**/.mvn/**,**/mvnw/**,**/.gitignore.,**/out/**,**/lib/**,**/*.iml,**/node_modules/**".split(
225+
"\\."));
226+
sbmApplicationProperties.setIgnoredPathsPatterns(ignorePatterns);
227+
PathScanner pathScanner = new PathScanner(sbmApplicationProperties, resourceHelper);
228+
List<Resource> scan = pathScanner.scan(absoluteProjectRoot);
229+
Builder builder = TestProjectContext.buildProjectContext();
230+
scan.forEach(r -> {
231+
try {
232+
Path relativePath = absoluteProjectRoot.relativize(r.getFile().toPath());
233+
String content = ResourceHelper.getResourceAsString(r);
234+
builder.addProjectResource(relativePath, content);
235+
} catch (IOException e) {
236+
throw new RuntimeException(e);
237+
}
238+
});
239+
return builder.build();
240+
}
241+
225242
public static class Builder {
226243
private Path projectRoot;
227244
private List<ProjectResourceWrapper> resourceWrapperList = new ArrayList<>();
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
4+
<parent>
5+
<groupId>com.example</groupId>
6+
<artifactId>ear-parent</artifactId>
7+
<version>1.0</version>
8+
<relativePath>../pom.xml</relativePath>
9+
</parent>
10+
<artifactId>business-logic</artifactId>
11+
<packaging>ejb</packaging>
12+
13+
<dependencies>
14+
<dependency>
15+
<groupId>org.apache.tomee</groupId>
16+
<artifactId>javaee-api</artifactId>
17+
<version>[8.0,)</version>
18+
<scope>provided</scope>
19+
</dependency>
20+
</dependencies>
21+
</project>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
4+
<parent>
5+
<groupId>com.example</groupId>
6+
<artifactId>ear-parent</artifactId>
7+
<version>1.0</version>
8+
</parent>
9+
<artifactId>ear</artifactId>
10+
<packaging>ear</packaging>
11+
12+
<properties>
13+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14+
</properties>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>com.example</groupId>
19+
<artifactId>business-logic</artifactId>
20+
<version>${project.version}</version>
21+
<type>ejb</type>
22+
</dependency>
23+
<dependency>
24+
<groupId>com.example</groupId>
25+
<artifactId>webapp</artifactId>
26+
<type>war</type>
27+
<version>${project.version}</version>
28+
</dependency>
29+
</dependencies>
30+
31+
<build>
32+
<plugins>
33+
<plugin>
34+
<artifactId>maven-ear-plugin</artifactId>
35+
<version>2.10.1</version>
36+
<configuration>
37+
<version>6</version>
38+
<generateApplicationXml>true</generateApplicationXml>
39+
<defaultLibBundleDir>lib</defaultLibBundleDir>
40+
<modules>
41+
<webModule>
42+
<groupId>com.example</groupId>
43+
<artifactId>webapp</artifactId>
44+
<contextRoot>/webapp</contextRoot>
45+
</webModule>
46+
<ejbModule>
47+
<groupId>com.example</groupId>
48+
<artifactId>business-logic</artifactId>
49+
</ejbModule>
50+
</modules>
51+
</configuration>
52+
</plugin>
53+
</plugins>
54+
</build>
55+
</project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
4+
<groupId>com.example</groupId>
5+
<artifactId>ear-parent</artifactId>
6+
<version>1.0</version>
7+
<packaging>pom</packaging>
8+
9+
<modules>
10+
<module>business-logic</module>
11+
<module>webapp</module>
12+
<module>ear</module>
13+
</modules>
14+
15+
<properties>
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
</properties>
18+
19+
<build>
20+
<defaultGoal>install</defaultGoal>
21+
<pluginManagement>
22+
<plugins>
23+
<plugin>
24+
<groupId>org.apache.maven.plugins</groupId>
25+
<artifactId>maven-compiler-plugin</artifactId>
26+
<version>3.5.1</version>
27+
<configuration>
28+
<source>1.7</source>
29+
<target>1.7</target>
30+
</configuration>
31+
</plugin>
32+
</plugins>
33+
</pluginManagement>
34+
</build>
35+
36+
</project>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
4+
<parent>
5+
<groupId>com.example</groupId>
6+
<artifactId>ear-parent</artifactId>
7+
<version>1.0</version>
8+
<relativePath>../pom.xml</relativePath>
9+
</parent>
10+
<artifactId>webapp</artifactId>
11+
<packaging>war</packaging>
12+
13+
<dependencies>
14+
<dependency>
15+
<groupId>org.apache.tomee</groupId>
16+
<artifactId>javaee-api</artifactId>
17+
<version>7.0</version>
18+
<scope>provided</scope>
19+
</dependency>
20+
<dependency>
21+
<groupId>javax.servlet</groupId>
22+
<artifactId>jstl</artifactId>
23+
<version>1.2</version>
24+
<scope>provided</scope>
25+
</dependency>
26+
<dependency>
27+
<groupId>taglibs</groupId>
28+
<artifactId>standard</artifactId>
29+
<version>1.1.2</version>
30+
<scope>provided</scope>
31+
</dependency>
32+
</dependencies>
33+
</project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
5+
<display-name>test-service</display-name>
6+
</web-app>

0 commit comments

Comments
 (0)