Skip to content

Commit 2c62d51

Browse files
committed
Add Modules.findModulesContaining(Path) and Module.contains(Path)
1 parent 9b7954f commit 2c62d51

File tree

7 files changed

+356
-9
lines changed

7 files changed

+356
-9
lines changed

components/sbm-core/src/main/java/org/springframework/sbm/build/api/ApplicationModules.java

+15-3
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,21 @@ public Module getModule(Path modulePath) {
6060
}
6161

6262
public Optional<Module> findModule(String coordinate) {
63-
return modules.stream()
64-
.filter(m -> m.getBuildFile().getCoordinates().equals(coordinate))
65-
.findFirst();
63+
return modules.stream().filter(m -> m.getBuildFile().getCoordinates().equals(coordinate)).findFirst();
64+
}
65+
66+
public Module getModule(String name) {
67+
if("root".equals(name)) name = "";
68+
return getModule(Path.of(name));
69+
}
70+
71+
/**
72+
* Searches in all modules for a resource with given {@code resourcePath} and returns first match.
73+
*
74+
* @param resourcePath must be an <b>absolute path</b> of the resource
75+
*/
76+
public Optional<Module> findModuleContaining(Path resourcePath) {
77+
return modules.stream().filter(m -> m.contains(resourcePath)).findFirst();
6678
}
6779

6880
public List<Module> getModules(Module module) {

components/sbm-core/src/main/java/org/springframework/sbm/build/api/Module.java

+9
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.springframework.sbm.build.impl.JavaSourceSetImpl;
2121
import org.springframework.sbm.build.impl.MavenBuildFileUtil;
2222
import org.springframework.sbm.build.impl.OpenRewriteMavenBuildFile;
23+
import org.springframework.sbm.common.util.Verify;
2324
import org.springframework.sbm.java.api.JavaSource;
2425
import org.springframework.sbm.java.api.JavaSourceLocation;
2526
import org.springframework.sbm.java.refactoring.JavaRefactoringFactory;
@@ -169,6 +170,14 @@ public <T> T searchTestJava(ProjectResourceFinder<T> finder) {
169170
return finder.apply(resourceSet);
170171
}
171172

173+
/**
174+
* Checks if any resources of this {@code Module} matches the given {@code resourcePath}.
175+
*/
176+
public boolean contains(Path resourcePath) {
177+
Verify.absolutePath(resourcePath);
178+
return getModuleResources().stream().anyMatch(r -> r.getAbsolutePath().toString().equals(resourcePath.toString()));
179+
}
180+
172181
/**
173182
* Class provides filtering on the list of resources in a {@code ProjectResourceSet}.
174183
* As all read methods rely on {@code stream()}, only this stream has to be filtered. :fingers_crossed:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2021 - 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.sbm.common.util;
18+
19+
import java.nio.file.Path;
20+
21+
public class Verify {
22+
public static void absolutePath(Path resourcePath) {
23+
if(!resourcePath.isAbsolute()) throw new IllegalArgumentException("Given path must be absolute, but was " + resourcePath + "'.");
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2021 - 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.sbm.build.api;
18+
19+
import org.assertj.core.api.Assertions;
20+
import org.junit.jupiter.api.Test;
21+
22+
import java.nio.file.Path;
23+
import java.util.List;
24+
import java.util.Optional;
25+
26+
import static org.mockito.Mockito.mock;
27+
import static org.mockito.Mockito.when;
28+
29+
class ApplicationModules_findModuleContaining_Test {
30+
31+
@Test
32+
void test_renameMe() {
33+
Module module1 = mock(Module.class);
34+
Module module2 = mock(Module.class);
35+
Module module3 = mock(Module.class);
36+
List<Module> modules = List.of(module1, module2, module3);
37+
ApplicationModules sut = new ApplicationModules(modules);
38+
Path resourcePath = Path.of("xyz");
39+
40+
when(module1.contains(resourcePath)).thenReturn(false);
41+
when(module2.contains(resourcePath)).thenReturn(false);
42+
when(module3.contains(resourcePath)).thenReturn(true);
43+
44+
Optional<Module> module = sut.findModuleContaining(resourcePath);
45+
46+
Assertions.assertThat(module.get()).isSameAs(module3);
47+
}
48+
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* Copyright 2021 - 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.sbm.build.api;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.springframework.sbm.build.util.PomBuilder;
21+
import org.springframework.sbm.engine.context.ProjectContext;
22+
import org.springframework.sbm.project.resource.TestProjectContext;
23+
24+
import java.nio.file.Path;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
28+
29+
class Module_getModuleResources_Test {
30+
@Test
31+
void singleModuleProject() {
32+
String rootPom = PomBuilder
33+
.buiildPom("com.example:parent:1.0")
34+
.type("jar")
35+
.withModules("module1", "module2")
36+
.build();
37+
38+
String javaClass = """
39+
package com.example;
40+
public class SomeClass {}
41+
""";
42+
String testjavaClass = """
43+
package com.example;
44+
public class SomeClassTest {}
45+
""";
46+
ProjectContext context = TestProjectContext
47+
.buildProjectContext()
48+
.withMavenBuildFileSource("pom.xml", rootPom)
49+
.addJavaSource("src/main/java", javaClass)
50+
.addProjectResource("src/main/resources/resource-found.txt", "")
51+
.addJavaSource("src/test/java", testjavaClass)
52+
.addProjectResource("src/test/resources/test-resource-found.txt", "")
53+
.addProjectResource("not-in-source-set.txt", "")
54+
.build();
55+
56+
Module root = context.getApplicationModules().getModule("root");
57+
Path rootPath = TestProjectContext.getDefaultProjectRoot();
58+
assertThat(root.contains(rootPath.resolve("pom.xml"))).isTrue();
59+
assertThat(root.contains(rootPath.resolve("src/main/java/com/example/SomeClass.java"))).isTrue();
60+
assertThat(root.contains(rootPath.resolve("src/test/java/com/example/SomeClassTest.java"))).isTrue();
61+
assertThat(root.contains(rootPath.resolve("src/main/resources/resource-found.txt"))).isTrue();
62+
assertThat(root.contains(rootPath.resolve("src/test/resources/test-resource-found.txt"))).isTrue();
63+
assertThat(root.contains(rootPath.resolve("not-in-source-set.txt"))).isFalse();
64+
}
65+
66+
@Test
67+
void multiModuleProject() {
68+
String rootPom = PomBuilder
69+
.buiildPom("com.example:parent:1.0")
70+
.type("pom")
71+
.withModules("module1", "module2")
72+
.build();
73+
74+
String module1Pom = PomBuilder
75+
.buiildPom("com.example:parent:1.0", "module1")
76+
.dependencies("com.example:module2:1.0")
77+
.build();
78+
79+
String module2Pom = PomBuilder.buiildPom("com.example:parent:1.0", "module2").build();
80+
81+
String javaClass = """
82+
package com.example;
83+
public class SomeClass {}
84+
""";
85+
String testjavaClass = """
86+
package com.example;
87+
public class SomeClassTest {}
88+
""";
89+
ProjectContext context = TestProjectContext
90+
.buildProjectContext()
91+
.withMavenBuildFileSource("module1/pom.xml", module1Pom)
92+
.addJavaSource("module1/src/test/java", testjavaClass)
93+
.addProjectResource("module1/src/test/resources/test-resource-found.txt", "")
94+
95+
.withMavenBuildFileSource("module2/pom.xml", module2Pom)
96+
.addJavaSource("module2/src/main/java", javaClass)
97+
.addProjectResource("module2/src/main/resources/resource-found.txt", "")
98+
99+
.withMavenBuildFileSource("pom.xml", rootPom)
100+
.addProjectResource("not-in-source-set.txt", "")
101+
102+
103+
.build();
104+
105+
Module root = context.getApplicationModules().getModule("root");
106+
Path rootPath = TestProjectContext.getDefaultProjectRoot();
107+
assertThat(root.contains(rootPath.resolve("pom.xml"))).isTrue();
108+
assertThat(root.contains(rootPath.resolve("src/main/java/com/example/SomeClass.java"))).isFalse();
109+
110+
Module module1 = context.getApplicationModules().getModule("module2");
111+
112+
assertThat(module1.contains(rootPath.resolve("module2//main/java/com/example/SomeClass.java"))).isFalse();
113+
assertThat(
114+
module1.contains(rootPath.resolve("module2/src/test/java/com/example/SomeClassTest.java"))).isFalse();
115+
assertThat(module1.contains(rootPath.resolve("module2/src/main/resources/resource-found.txt"))).isTrue();
116+
assertThat(root.contains(rootPath.resolve("src/test/resources/test-resource-found.txt"))).isFalse();
117+
assertThat(root.contains(rootPath.resolve("not-in-source-set.txt"))).isFalse();
118+
}
119+
120+
@Test
121+
void shouldThrowIllegalArgumentExceptionForRelativePath() {
122+
ProjectContext context = TestProjectContext.buildProjectContext().build();
123+
Path relativePath = Path.of(".");
124+
assertThatIllegalArgumentException().isThrownBy(() -> {
125+
context.getApplicationModules().getModule("root").contains(relativePath);
126+
});
127+
}
128+
129+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright 2021 - 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.sbm.build.util;import org.springframework.sbm.project.parser.DependencyHelper;
18+
19+
import java.util.Arrays;
20+
import java.util.List;
21+
22+
public class PomBuilder {
23+
private String coordinate;
24+
private List<String> modules;
25+
private String type;
26+
private String parent;
27+
private String artifactId;
28+
private List<String> dependencies;
29+
30+
public static PomBuilder buiildPom(String coordinate) {
31+
PomBuilder pomBuilder = new PomBuilder();
32+
pomBuilder.coordinate = coordinate;
33+
return pomBuilder;
34+
}
35+
36+
public static PomBuilder buiildPom(String parent, String artifactId) {
37+
PomBuilder pomBuilder = new PomBuilder();
38+
pomBuilder.parent = parent;
39+
pomBuilder.artifactId = artifactId;
40+
return pomBuilder;
41+
}
42+
43+
public PomBuilder withModules(String... modules) {
44+
this.modules = Arrays.asList(modules);
45+
return this;
46+
}
47+
48+
public String build() {
49+
StringBuilder sb = new StringBuilder();
50+
sb.append(
51+
"""
52+
<?xml version="1.0" encoding="UTF-8"?>
53+
<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/xsd/maven-4.0.0.xsd">
54+
<modelVersion>4.0.0</modelVersion>
55+
"""
56+
);
57+
58+
if(parent != null) {
59+
String[] coord = parent.split(":");
60+
sb.append(" <parent>").append("\n");
61+
sb.append(" <groupId>").append(coord[0]).append("</groupId>").append("\n");
62+
sb.append(" <artifactId>").append(coord[1]).append("</artifactId>").append("\n");
63+
sb.append(" <version>").append(coord[2]).append("</version>").append("\n");
64+
sb.append(" </parent>").append("\n");
65+
sb.append(" <artifactId>").append(artifactId).append("</artifactId>").append("\n");
66+
} else {
67+
String[] coord = coordinate.split(":");
68+
sb.append(" <groupId>").append(coord[0]).append("</groupId>").append("\n");
69+
sb.append(" <artifactId>").append(coord[1]).append("</artifactId>").append("\n");
70+
sb.append(" <version>").append(coord[2]).append("</version>").append("\n");
71+
}
72+
73+
74+
if(type != null ){
75+
sb.append(" <type>").append(type).append("</type>").append("\n");
76+
}
77+
78+
if(modules != null && !modules.isEmpty()) {
79+
sb.append(" <modules>").append("\n");
80+
modules.forEach(m -> sb.append(" <module>").append(m).append("</module>\n"));
81+
sb.append(" </modules>").append("\n");
82+
}
83+
84+
if(dependencies != null) {
85+
String dependenciesRendered = buildDependencies(dependencies);
86+
sb.append(dependenciesRendered);
87+
}
88+
89+
sb.append("</project>");
90+
return sb.toString();
91+
}
92+
93+
String buildDependencies(List<String> dependencyCoordinates) {
94+
DependencyHelper dependencyHelper = new DependencyHelper();
95+
StringBuilder dependenciesSection = new StringBuilder();
96+
dependenciesSection.append(" ").append("<dependencies>").append("\n");
97+
dependencyHelper.mapCoordinatesToDependencies(dependencyCoordinates).stream().forEach(dependency -> {
98+
dependenciesSection.append(" ").append(" ").append("<dependency>").append("\n");
99+
dependenciesSection.append(" ").append(" ").append(" ").append("<groupId>").append(dependency.getGroupId()).append("</groupId>").append("\n");
100+
dependenciesSection.append(" ").append(" ").append(" ").append("<artifactId>").append(dependency.getArtifactId()).append("</artifactId>").append("\n");
101+
dependenciesSection.append(" ").append(" ").append(" ").append("<version>").append(dependency.getVersion()).append("</version>").append("\n");
102+
dependenciesSection.append(" ").append(" ").append("</dependency>").append("\n");
103+
});
104+
dependenciesSection.append(" ").append("</dependencies>").append("\n");
105+
String dependenciesText = dependenciesSection.toString();
106+
return dependenciesText;
107+
}
108+
109+
public PomBuilder type(String type) {
110+
this.type = type;
111+
return this;
112+
}
113+
114+
public PomBuilder dependencies(String... s) {
115+
this.dependencies = Arrays.asList(s);
116+
return this;
117+
}
118+
}

0 commit comments

Comments
 (0)