View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.project;
20  
21  import java.io.File;
22  import java.nio.file.Path;
23  import java.util.List;
24  
25  import org.apache.maven.api.Language;
26  import org.apache.maven.api.ProjectScope;
27  import org.apache.maven.impl.DefaultSourceRoot;
28  import org.apache.maven.model.Resource;
29  import org.junit.jupiter.api.BeforeEach;
30  import org.junit.jupiter.api.Test;
31  
32  import static org.junit.jupiter.api.Assertions.assertEquals;
33  import static org.junit.jupiter.api.Assertions.assertNull;
34  import static org.junit.jupiter.api.Assertions.assertTrue;
35  
36  /**
37   * Test for the fix of issue #2486: Includes are not added to existing project resource.
38   */
39  class ResourceIncludeTest {
40  
41      private MavenProject project;
42  
43      @BeforeEach
44      void setUp() {
45          project = new MavenProject();
46          // Set a dummy pom file to establish the base directory
47          project.setFile(new java.io.File("./pom.xml"));
48  
49          // Set build output directories
50          project.getBuild().setOutputDirectory("target/classes");
51          project.getBuild().setTestOutputDirectory("target/test-classes");
52  
53          // Add a resource source root to the project
54          project.addSourceRoot(
55                  new DefaultSourceRoot(ProjectScope.MAIN, Language.RESOURCES, Path.of("src/main/resources")));
56      }
57  
58      @Test
59      void testAddIncludeToExistingResource() {
60          // Get the first resource
61          List<Resource> resources = project.getResources();
62          assertEquals(1, resources.size(), "Should have one resource");
63  
64          Resource resource = resources.get(0);
65          assertEquals(Path.of("src/main/resources").toString(), resource.getDirectory());
66          assertTrue(resource.getIncludes().isEmpty(), "Initially should have no includes");
67  
68          // Add an include - this should work now
69          resource.addInclude("test");
70  
71          // Verify the include was added
72          assertEquals(1, resource.getIncludes().size(), "Should have one include");
73          assertEquals("test", resource.getIncludes().get(0), "Include should be 'test'");
74  
75          // Verify that getting resources again still shows the include
76          List<Resource> resourcesAfter = project.getResources();
77          assertEquals(1, resourcesAfter.size(), "Should still have one resource");
78          Resource resourceAfter = resourcesAfter.get(0);
79          assertEquals(1, resourceAfter.getIncludes().size(), "Should still have one include");
80          assertEquals("test", resourceAfter.getIncludes().get(0), "Include should still be 'test'");
81      }
82  
83      @Test
84      void testAddMultipleIncludes() {
85          Resource resource = project.getResources().get(0);
86  
87          // Add multiple includes
88          resource.addInclude("*.xml");
89          resource.addInclude("*.properties");
90  
91          // Verify both includes are present
92          assertEquals(2, resource.getIncludes().size(), "Should have two includes");
93          assertTrue(resource.getIncludes().contains("*.xml"), "Should contain *.xml");
94          assertTrue(resource.getIncludes().contains("*.properties"), "Should contain *.properties");
95  
96          // Verify persistence
97          Resource resourceAfter = project.getResources().get(0);
98          assertEquals(2, resourceAfter.getIncludes().size(), "Should still have two includes");
99          assertTrue(resourceAfter.getIncludes().contains("*.xml"), "Should still contain *.xml");
100         assertTrue(resourceAfter.getIncludes().contains("*.properties"), "Should still contain *.properties");
101     }
102 
103     @Test
104     void testRemoveInclude() {
105         Resource resource = project.getResources().get(0);
106 
107         // Add includes
108         resource.addInclude("*.xml");
109         resource.addInclude("*.properties");
110         assertEquals(2, resource.getIncludes().size());
111 
112         // Remove one include
113         resource.removeInclude("*.xml");
114 
115         // Verify only one include remains
116         assertEquals(1, resource.getIncludes().size(), "Should have one include");
117         assertEquals("*.properties", resource.getIncludes().get(0), "Should only have *.properties");
118 
119         // Verify persistence
120         Resource resourceAfter = project.getResources().get(0);
121         assertEquals(1, resourceAfter.getIncludes().size(), "Should still have one include");
122         assertEquals("*.properties", resourceAfter.getIncludes().get(0), "Should still only have *.properties");
123     }
124 
125     @Test
126     void testSetIncludes() {
127         Resource resource = project.getResources().get(0);
128 
129         // Set includes directly
130         resource.setIncludes(List.of("*.txt", "*.md"));
131 
132         // Verify includes were set
133         assertEquals(2, resource.getIncludes().size(), "Should have two includes");
134         assertTrue(resource.getIncludes().contains("*.txt"), "Should contain *.txt");
135         assertTrue(resource.getIncludes().contains("*.md"), "Should contain *.md");
136 
137         // Verify persistence
138         Resource resourceAfter = project.getResources().get(0);
139         assertEquals(2, resourceAfter.getIncludes().size(), "Should still have two includes");
140         assertTrue(resourceAfter.getIncludes().contains("*.txt"), "Should still contain *.txt");
141         assertTrue(resourceAfter.getIncludes().contains("*.md"), "Should still contain *.md");
142     }
143 
144     @Test
145     void testSourceRootOrderingPreserved() {
146         // Add multiple resource source roots
147         project.addSourceRoot(
148                 new DefaultSourceRoot(ProjectScope.MAIN, Language.RESOURCES, Path.of("src/main/resources2")));
149         project.addSourceRoot(
150                 new DefaultSourceRoot(ProjectScope.MAIN, Language.RESOURCES, Path.of("src/main/resources3")));
151 
152         // Verify initial order
153         List<Resource> resources = project.getResources();
154         assertEquals(3, resources.size(), "Should have three resources");
155         assertEquals(Path.of("src/main/resources").toString(), resources.get(0).getDirectory());
156         assertEquals(Path.of("src/main/resources2").toString(), resources.get(1).getDirectory());
157         assertEquals(Path.of("src/main/resources3").toString(), resources.get(2).getDirectory());
158 
159         // Modify the middle resource
160         resources.get(1).addInclude("*.properties");
161 
162         // Verify order is preserved after modification
163         List<Resource> resourcesAfter = project.getResources();
164         assertEquals(3, resourcesAfter.size(), "Should still have three resources");
165         assertEquals(
166                 Path.of("src/main/resources").toString(), resourcesAfter.get(0).getDirectory());
167         assertEquals(
168                 Path.of("src/main/resources2").toString(), resourcesAfter.get(1).getDirectory());
169         assertEquals(
170                 Path.of("src/main/resources3").toString(), resourcesAfter.get(2).getDirectory());
171 
172         // Verify the modification was applied to the correct resource
173         assertTrue(
174                 resourcesAfter.get(1).getIncludes().contains("*.properties"),
175                 "Middle resource should have the include");
176         assertTrue(resourcesAfter.get(0).getIncludes().isEmpty(), "First resource should not have includes");
177         assertTrue(resourcesAfter.get(2).getIncludes().isEmpty(), "Third resource should not have includes");
178     }
179 
180     @Test
181     void testUnderlyingSourceRootsUpdated() {
182         Resource resource = project.getResources().get(0);
183 
184         // Add an include
185         resource.addInclude("*.xml");
186 
187         // Verify that the underlying SourceRoot collection was updated
188         java.util.stream.Stream<org.apache.maven.api.SourceRoot> resourceSourceRoots =
189                 project.getEnabledSourceRoots(ProjectScope.MAIN, Language.RESOURCES);
190 
191         java.util.List<org.apache.maven.api.SourceRoot> sourceRootsList = resourceSourceRoots.toList();
192         assertEquals(1, sourceRootsList.size(), "Should have one resource source root");
193 
194         org.apache.maven.api.SourceRoot sourceRoot = sourceRootsList.get(0);
195         assertTrue(sourceRoot.includes().contains("*.xml"), "Underlying SourceRoot should contain the include");
196     }
197 
198     /*MNG-11062*/
199     @Test
200     void testTargetPathPreservedWithConnectedResource() {
201         // Create resource with targetPath using Resource constructor pattern
202         Resource resourceWithTarget = new Resource();
203         resourceWithTarget.setDirectory("src/main/custom");
204         resourceWithTarget.setTargetPath("custom-output");
205 
206         // Convert through DefaultSourceRoot to ensure targetPath is preserved
207         DefaultSourceRoot sourceRootFromResource =
208                 new DefaultSourceRoot(project.getBaseDirectory(), ProjectScope.MAIN, resourceWithTarget.getDelegate());
209 
210         project.addSourceRoot(sourceRootFromResource);
211 
212         // Get resources - this creates ConnectedResource instances
213         List<Resource> resources = project.getResources();
214         assertEquals(2, resources.size(), "Should have two resources now");
215 
216         // Find the resource with the custom directory
217         Resource customResource = resources.stream()
218                 .filter(r -> r.getDirectory().endsWith("custom"))
219                 .findFirst()
220                 .orElseThrow(() -> new AssertionError("Custom resource not found"));
221 
222         // Verify targetPath was preserved through conversion chain
223         assertEquals(
224                 "custom-output", customResource.getTargetPath(), "targetPath should be preserved in ConnectedResource");
225 
226         // Test that includes modification preserves targetPath (tests ConnectedResource functionality)
227         customResource.addInclude("*.properties");
228         assertEquals(
229                 "custom-output", customResource.getTargetPath(), "targetPath should survive includes modification");
230         assertEquals(1, customResource.getIncludes().size(), "Should have one include");
231 
232         // Verify persistence after getting resources again
233         Resource persistedResource = project.getResources().stream()
234                 .filter(r -> r.getDirectory().endsWith("custom"))
235                 .findFirst()
236                 .orElseThrow();
237         assertEquals(
238                 "custom-output",
239                 persistedResource.getTargetPath(),
240                 "targetPath should persist after resource retrieval");
241         assertTrue(persistedResource.getIncludes().contains("*.properties"), "Include should persist with targetPath");
242     }
243 
244     /*MNG-11062*/
245     @Test
246     void testTargetPathEdgeCases() {
247         // Test null targetPath (should be handled gracefully)
248         Resource nullTargetResource = new Resource();
249         nullTargetResource.setDirectory("src/test/null-target");
250         // targetPath is null by default
251 
252         DefaultSourceRoot nullTargetSourceRoot =
253                 new DefaultSourceRoot(project.getBaseDirectory(), ProjectScope.MAIN, nullTargetResource.getDelegate());
254         project.addSourceRoot(nullTargetSourceRoot);
255 
256         List<Resource> resources = project.getResources();
257         Resource nullTargetResult = resources.stream()
258                 .filter(r -> r.getDirectory().endsWith("null-target"))
259                 .findFirst()
260                 .orElseThrow();
261 
262         // null targetPath should remain null (not cause errors)
263         assertNull(nullTargetResult.getTargetPath(), "Null targetPath should remain null");
264 
265         // Test property placeholder in targetPath
266         Resource placeholderResource = new Resource();
267         placeholderResource.setDirectory("src/test/placeholder");
268         placeholderResource.setTargetPath("${project.build.directory}/custom");
269 
270         DefaultSourceRoot placeholderSourceRoot =
271                 new DefaultSourceRoot(project.getBaseDirectory(), ProjectScope.MAIN, placeholderResource.getDelegate());
272         project.addSourceRoot(placeholderSourceRoot);
273 
274         Resource placeholderResult = project.getResources().stream()
275                 .filter(r -> r.getDirectory().endsWith("placeholder"))
276                 .findFirst()
277                 .orElseThrow();
278 
279         assertEquals(
280                 "${project.build.directory}" + File.separator + "custom",
281                 placeholderResult.getTargetPath(),
282                 "Property placeholder in targetPath should be preserved");
283     }
284 
285     /**
286      * Verifies that resources added via {@code project.addResource()} or
287      * {@code project.getResources().add()} are visible through both
288      * {@code project.getResources()} and {@code project.getBuild().getResources()}.
289      * This is important for Maven 3 compatibility, since plugins may access
290      * resources through either path.
291      *
292      * @see <a href="https://github.com/apache/maven-source-plugin/pull/281">maven-source-plugin#281</a>
293      */
294     @Test
295     void testAddResourceVisibleViaBuildGetResources() {
296         // Initial state: one resource in sources, none added dynamically
297         assertEquals(1, project.getResources().size());
298         int initialBuildResources = project.getBuild().getResources().size();
299 
300         // Add a resource dynamically (simulating what maven-remote-resources-plugin does)
301         Resource dynamicResource = new Resource();
302         dynamicResource.setDirectory("target/maven-shared-archive-resources");
303         project.addResource(dynamicResource);
304 
305         // Verify visible via project.getResources()
306         List<Resource> projectResources = project.getResources();
307         assertEquals(2, projectResources.size(), "Dynamic resource should be visible via project.getResources()");
308 
309         // Verify ALSO visible via project.getBuild().getResources()
310         List<Resource> buildResources = project.getBuild().getResources();
311         assertEquals(
312                 initialBuildResources + 1,
313                 buildResources.size(),
314                 "Dynamic resource should also be visible via project.getBuild().getResources()");
315 
316         boolean found =
317                 buildResources.stream().anyMatch(r -> r.getDirectory().contains("maven-shared-archive-resources"));
318         assertTrue(found, "getBuild().getResources() should contain the dynamically added resource");
319     }
320 
321     /**
322      * Same as above but using {@code project.getResources().add()} path.
323      */
324     @Test
325     void testAddResourceViaListVisibleViaBuildGetResources() {
326         int initialBuildResources = project.getBuild().getResources().size();
327 
328         Resource dynamicResource = new Resource();
329         dynamicResource.setDirectory("target/maven-shared-archive-resources");
330         project.getResources().add(dynamicResource);
331 
332         List<Resource> buildResources = project.getBuild().getResources();
333         assertEquals(
334                 initialBuildResources + 1,
335                 buildResources.size(),
336                 "Resource added via getResources().add() should be visible via getBuild().getResources()");
337 
338         boolean found =
339                 buildResources.stream().anyMatch(r -> r.getDirectory().contains("maven-shared-archive-resources"));
340         assertTrue(found, "getBuild().getResources() should contain the resource added via getResources().add()");
341     }
342 }