Skip to content

Commit 4a3a4fa

Browse files
committed
Adds tests
1 parent f64c4a8 commit 4a3a4fa

File tree

4 files changed

+833
-1
lines changed

4 files changed

+833
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
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.intellij.lang.annotations.Language;
20+
import org.junit.jupiter.api.BeforeEach;
21+
import org.junit.jupiter.api.DisplayName;
22+
import org.junit.jupiter.api.Nested;
23+
import org.junit.jupiter.api.Test;
24+
import org.openrewrite.SourceFile;
25+
import org.springframework.sbm.engine.context.ProjectContext;
26+
import org.springframework.sbm.project.resource.ProjectResourceSet;
27+
import org.springframework.sbm.project.resource.RewriteSourceFileHolder;
28+
import org.springframework.sbm.project.resource.TestProjectContext;
29+
import org.springframework.sbm.project.resource.filter.ProjectResourceFinder;
30+
31+
import java.util.List;
32+
import java.util.concurrent.atomic.AtomicBoolean;
33+
import java.util.function.Consumer;
34+
35+
import static org.assertj.core.api.Assertions.assertThat;
36+
37+
@DisplayName("Test ApplicationModule.searchTestJava")
38+
public class ApplicationModule_searchMainJava_Test {
39+
40+
@Nested
41+
@DisplayName("Project")
42+
class SingleModuleProject {
43+
44+
@Language("xml")
45+
String singlePom = """
46+
<?xml version="1.0" encoding="UTF-8"?>
47+
<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">
48+
<modelVersion>4.0.0</modelVersion>
49+
<groupId>com.acme</groupId>
50+
<artifactId>application</artifactId>
51+
<version>0.0.1-SNAPSHOT</version>
52+
</project>
53+
""";
54+
55+
private TestProjectContext.Builder builder;
56+
57+
@BeforeEach
58+
void beforeEach() {
59+
builder = TestProjectContext.buildProjectContext().withMavenBuildFileSource("pom.xml", singlePom);
60+
}
61+
62+
@Test
63+
@DisplayName("with no classes in src/main/java provides empty ProjectResourceSet to search method")
64+
void withNoClassesInSrcMainJava_providesEmptyProjectResources() {
65+
66+
ProjectContext context = builder.build();
67+
68+
AtomicBoolean wasCalled = new AtomicBoolean(false);
69+
verifySearchMain(context, projectResourceSet -> {
70+
assertThat(projectResourceSet.list()).isEmpty();
71+
}, "");
72+
}
73+
74+
@Test
75+
@DisplayName("with classes in src/main/java provides ProjectResourceSet with classes from src/main/java")
76+
void withClassesInSrcMainJavaProvidesProjectResourcesWithMainClasses() {
77+
78+
ProjectContext context = TestProjectContext
79+
.buildProjectContext()
80+
.withMavenBuildFileSource("pom.xml", singlePom)
81+
.addJavaSource("src/main/java", "public class SomeClass{}")
82+
.build();
83+
84+
verifySearchMain(context, projectResourceSet -> {
85+
assertThat(projectResourceSet.list()).hasSize(1);
86+
assertThat(projectResourceSet.get(0).getSourcePath().toString()).isEqualTo("src/main/java/SomeClass.java");
87+
assertThat(projectResourceSet.get(0).print()).isEqualTo("public class SomeClass{}");
88+
}, "");
89+
}
90+
91+
@Test
92+
@DisplayName("with classes in src/main/java and src/test/java provides ProjectResourceSet with classes from src/main/java")
93+
void withClassesInTestAndMain_providesClassesFromMain() {
94+
ProjectContext context = builder
95+
.addJavaSource("src/main/java", "public class SomeClass{}")
96+
.addJavaSource("src/test/java", "public class SomeClassTest{}")
97+
.build();
98+
99+
verifySearchMain(context, projectResourceSet -> {
100+
assertThat(projectResourceSet.list()).hasSize(1);
101+
assertThat(projectResourceSet.get(0).getSourcePath().toString()).isEqualTo("src/main/java/SomeClass.java");
102+
assertThat(projectResourceSet.get(0).print()).isEqualTo("public class SomeClass{}");
103+
}, "");
104+
}
105+
106+
@Test
107+
@DisplayName("with classes in src/test/java provides empty ProjectResourceSet to search method")
108+
void withClassesInSrcTestJava_providesEmptyProjectResources() {
109+
110+
ProjectContext context = builder
111+
.addProjectResource("src/test/java/SomeClass.java", "public class SomeClass{}")
112+
.build();
113+
114+
verifySearchMain(context, projectResourceSet -> assertThat(projectResourceSet.list()).isEmpty(), "");
115+
}
116+
}
117+
118+
@Nested
119+
@DisplayName("Multi-module project")
120+
class MultiMavenModuleProject {
121+
@Language("xml")
122+
String parentPom = """
123+
<?xml version="1.0" encoding="UTF-8"?>
124+
<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">
125+
<modelVersion>4.0.0</modelVersion>
126+
<groupId>com.acme</groupId>
127+
<artifactId>parent</artifactId>
128+
<version>0.0.1-SNAPSHOT</version>
129+
<packaging>pom</packaging>
130+
<modules>
131+
<module>application</module>
132+
<module>component</module>
133+
</modules>
134+
</project>
135+
""";
136+
137+
@Language("xml")
138+
String componentPom = """
139+
<?xml version="1.0" encoding="UTF-8"?>
140+
<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">
141+
<modelVersion>4.0.0</modelVersion>
142+
<parent>
143+
<groupId>com.acme</groupId>
144+
<artifactId>parent</artifactId>
145+
<version>0.0.1-SNAPSHOT</version>
146+
<relativePath>../</relativePath>
147+
</parent>
148+
<artifactId>component</artifactId>
149+
</project>
150+
""";
151+
152+
@Language("xml")
153+
String applicationPom = """
154+
<?xml version="1.0" encoding="UTF-8"?>
155+
<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">
156+
<modelVersion>4.0.0</modelVersion>
157+
<parent>
158+
<groupId>com.acme</groupId>
159+
<artifactId>parent</artifactId>
160+
<version>0.0.1-SNAPSHOT</version>
161+
<relativePath>../</relativePath>
162+
</parent>
163+
<artifactId>application</artifactId>
164+
<dependencies>
165+
<dependency>
166+
<groupId>com.acme</groupId>
167+
<artifactId>component</artifactId>
168+
<version>0.0.1-SNAPSHOT</version>
169+
</dependency>
170+
<dependency>
171+
<groupId>javax.validation</groupId>
172+
<artifactId>validation-api</artifactId>
173+
<version>2.0.1.Final</version>
174+
</dependency>
175+
</dependencies>
176+
</project>
177+
""";
178+
179+
private TestProjectContext.Builder builder;
180+
181+
@BeforeEach
182+
void beforeEach() {
183+
builder = TestProjectContext
184+
.buildProjectContext()
185+
.withMavenBuildFileSource("pom.xml", parentPom)
186+
.withMavenBuildFileSource("application/pom.xml", applicationPom)
187+
.withMavenBuildFileSource("component/pom.xml", componentPom);
188+
}
189+
190+
191+
@Test
192+
@DisplayName("with no classes provides empty ProjectResourceSet to search method")
193+
void withNoClasses_providesEmptyProjectResources() {
194+
ProjectContext context = builder.build();
195+
verifySearchMain(context, (projectResourceSet) -> assertThat(projectResourceSet.list()).isEmpty(),
196+
"application");
197+
}
198+
199+
@Test
200+
@DisplayName("with classes in src/test/java of other module provides empty ProjectResourceSet to search method")
201+
void withClassesInOtherModules_providesEmptyProjectResources() {
202+
203+
ProjectContext context = builder
204+
.addJavaSource("component/src/main/java", "public class SomeClass{}")
205+
.build();
206+
207+
verifySearchMain(context, (projectResourceSet) -> assertThat(projectResourceSet.list()).isEmpty(),
208+
"application");
209+
}
210+
211+
@Test
212+
@DisplayName("with classes in src/test/java and src/main/java provides ProjectResourceSet with classes from src/main/java")
213+
void withClassesInMainAndTest_providesClassesFromSrcMainJava() {
214+
215+
ProjectContext context = builder
216+
.addJavaSource("application/src/main/java", "public class SomeClass{}")
217+
.addJavaSource("application/src/test/java", "public class SomeClassTest{}")
218+
.build();
219+
220+
verifySearchMain(context,
221+
(projectResourceSet) -> {
222+
assertThat(projectResourceSet.list()).hasSize(1);
223+
assertThat(projectResourceSet.list().get(0).getSourcePath().toString()).isEqualTo("application/src/main/java/SomeClass.java");
224+
assertThat(projectResourceSet.list().get(0).print()).isEqualTo("public class SomeClass{}");
225+
},
226+
"application");
227+
}
228+
229+
@Test
230+
@DisplayName("with classes in src/main/java provides ProjectResourceSet with classes from stc/main/java")
231+
void withClassesInMain_providesClassesFromSrcMainJava() {
232+
233+
ProjectContext context = builder
234+
.addJavaSource("application/src/main/java", "public class SomeClass{}")
235+
.build();
236+
237+
verifySearchMain(context,
238+
projectResourceSet -> {
239+
assertThat(projectResourceSet.list()).hasSize(1);
240+
assertThat(projectResourceSet.list().get(0).getSourcePath().toString()).isEqualTo("application/src/main/java/SomeClass.java");
241+
assertThat(projectResourceSet.list().get(0).print()).isEqualTo("public class SomeClass{}");
242+
},
243+
"application");
244+
}
245+
}
246+
247+
private void verifySearchMain(ProjectContext context, Consumer<ProjectResourceSet> projectResourceSetConsumer, String modulePath) {
248+
AtomicBoolean wasCalled = new AtomicBoolean(false);
249+
searchTest(context, wasCalled, projectResourceSetConsumer, modulePath);
250+
assertThat(wasCalled).isTrue();
251+
}
252+
253+
private void searchTest(ProjectContext context, AtomicBoolean wasCalled, Consumer<ProjectResourceSet> f, String modulePath) {
254+
context
255+
.getApplicationModules()
256+
.getModule(modulePath)
257+
.searchMainJava(
258+
((ProjectResourceFinder<List<RewriteSourceFileHolder<? extends SourceFile>>>) projectResourceSet -> {
259+
f.accept(projectResourceSet);
260+
wasCalled.set(true);
261+
return null;
262+
}));
263+
}
264+
265+
}

components/sbm-core/src/test/java/org/springframework/sbm/build/api/ApplicationModule_searchMainResources_Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
public class ApplicationModule_searchMainResources_Test {
3737

3838
@Nested
39-
@DisplayName("project with resources in src/test/resources and src/main/resources")
39+
@DisplayName("Project")
4040
class SingleModuleProject {
4141

4242
@Language("xml")

0 commit comments

Comments
 (0)