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
+ }
0 commit comments