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.plugins.source;
20  
21  import java.io.File;
22  import java.nio.file.Paths;
23  import java.util.Collections;
24  
25  import org.apache.maven.api.Project;
26  import org.apache.maven.api.ProjectScope;
27  import org.apache.maven.api.di.Provides;
28  import org.apache.maven.api.plugin.testing.Basedir;
29  import org.apache.maven.api.plugin.testing.InjectMojo;
30  import org.apache.maven.api.plugin.testing.MojoParameter;
31  import org.apache.maven.api.plugin.testing.MojoTest;
32  import org.apache.maven.api.plugin.testing.stubs.SessionMock;
33  import org.apache.maven.api.services.ProjectManager;
34  import org.apache.maven.internal.impl.InternalSession;
35  import org.junit.jupiter.api.Test;
36  
37  import static org.apache.maven.api.plugin.testing.MojoExtension.getBasedir;
38  import static org.junit.jupiter.api.Assertions.assertFalse;
39  import static org.mockito.ArgumentMatchers.any;
40  import static org.mockito.ArgumentMatchers.eq;
41  import static org.mockito.Mockito.mock;
42  import static org.mockito.Mockito.when;
43  
44  /**
45   * @author <a href="mailto:oching@exist.com">Maria Odea Ching</a>
46   */
47  @MojoTest
48  public class SourceJarMojoTest extends AbstractSourcePluginTestCase {
49  
50      private String[] addMavenDescriptor(String project, String... listOfElements) {
51          final String METAINF = "META-INF/";
52          final String MAVENSOURCE = "maven/source/maven-source-plugin-test-";
53          int length = listOfElements.length;
54          String[] result = new String[length + 5];
55          System.arraycopy(listOfElements, 0, result, 0, length);
56          result[length] = METAINF + "maven/";
57          result[length + 1] = METAINF + "maven/source/";
58          result[length + 2] = METAINF + MAVENSOURCE + project + "/";
59          result[length + 3] = METAINF + MAVENSOURCE + project + "/pom.properties";
60          result[length + 4] = METAINF + MAVENSOURCE + project + "/pom.xml";
61          return result;
62      }
63  
64      @Test
65      @InjectMojo(goal = "jar")
66      @Basedir("${basedir}/target/test-classes/unit/project-001")
67      @MojoParameter(name = "classifier", value = "sources")
68      void testDefaultConfiguration(AbstractSourceJarMojo mojo) throws Exception {
69          mojo.execute();
70  
71          File target = new File(getBasedir(), "target");
72          assertSourceArchive(target, "project-001");
73          assertJarContent(
74                  getSourceArchive(target, "project-001"),
75                  addMavenDescriptor(
76                          "project-001",
77                          "default-configuration.properties",
78                          "foo/project001/App.java",
79                          "foo/project001/",
80                          "foo/",
81                          "META-INF/MANIFEST.MF",
82                          "META-INF/"));
83      }
84  
85      @Test
86      @InjectMojo(goal = "jar")
87      @Basedir("${basedir}/target/test-classes/unit/project-003")
88      public void testExcludes(AbstractSourceJarMojo mojo) throws Exception {
89          mojo.execute();
90  
91          File target = new File(getBasedir(), "target");
92          assertSourceArchive(target, "project-003");
93          assertJarContent(
94                  getSourceArchive(target, "project-003"),
95                  addMavenDescriptor(
96                          "project-003",
97                          "default-configuration.properties",
98                          "foo/project003/App.java",
99                          "foo/project003/",
100                         "foo/",
101                         "META-INF/MANIFEST.MF",
102                         "META-INF/"));
103     }
104 
105     @Test
106     @InjectMojo(goal = "jar")
107     @Basedir("${basedir}/target/test-classes/unit/project-005")
108     public void testNoSources(AbstractSourceJarMojo mojo) throws Exception {
109         mojo.execute();
110 
111         // Now make sure that no archive got created
112         final File expectedFile = getTestTargetDir("project-005");
113         assertFalse(
114                 expectedFile.exists(),
115                 "Source archive should not have been created[" + expectedFile.getAbsolutePath() + "]");
116     }
117 
118     @Test
119     @InjectMojo(goal = "jar")
120     @Basedir("${basedir}/target/test-classes/unit/project-007")
121     public void testIncludes(AbstractSourceJarMojo mojo) throws Exception {
122         mojo.execute();
123 
124         File target = new File(getBasedir(), "target");
125         assertSourceArchive(target, "project-007");
126         assertJarContent(
127                 getSourceArchive(target, "project-007"),
128                 addMavenDescriptor(
129                         "project-007",
130                         "templates/configuration-template.properties",
131                         "foo/project007/App.java",
132                         "templates/",
133                         "foo/project007/",
134                         "foo/",
135                         "META-INF/MANIFEST.MF",
136                         "META-INF/"));
137     }
138 
139     @Test
140     @InjectMojo(goal = "jar")
141     @Basedir("${basedir}/target/test-classes/unit/project-009")
142     public void testIncludePom(AbstractSourceJarMojo mojo) throws Exception {
143         mojo.execute();
144 
145         File target = new File(getBasedir(), "target");
146         assertSourceArchive(target, "project-009");
147         assertJarContent(
148                 getSourceArchive(target, "project-009"),
149                 addMavenDescriptor(
150                         "project-009",
151                         "default-configuration.properties",
152                         "pom.xml",
153                         "foo/project009/App.java",
154                         "foo/project009/",
155                         "foo/",
156                         "META-INF/MANIFEST.MF",
157                         "META-INF/"));
158     }
159 
160     @Test
161     @InjectMojo(goal = "jar")
162     @Basedir("${basedir}/target/test-classes/unit/project-010")
163     public void testIncludeMavenDescriptorWhenExplicitlyConfigured(AbstractSourceJarMojo mojo) throws Exception {
164         mojo.execute();
165 
166         File target = new File(getBasedir(), "target");
167         assertSourceArchive(target, "project-010");
168         assertJarContent(
169                 getSourceArchive(target, "project-010"),
170                 addMavenDescriptor(
171                         "project-010",
172                         "default-configuration.properties",
173                         "foo/project010/App.java",
174                         "foo/project010/",
175                         "foo/",
176                         "META-INF/MANIFEST.MF",
177                         "META-INF/"));
178     }
179 
180     @Provides
181     InternalSession createSession() {
182         InternalSession session = SessionMock.getMockSession("target/local-repo");
183         ProjectManager projectManager = mock(ProjectManager.class);
184         when(session.getService(ProjectManager.class)).thenReturn(projectManager);
185         when(projectManager.getCompileSourceRoots(any(), eq(ProjectScope.MAIN))).thenAnswer(iom -> {
186             Project p = iom.getArgument(0, Project.class);
187             return Collections.singletonList(
188                     Paths.get(getBasedir()).resolve(p.getModel().getBuild().getSourceDirectory()));
189         });
190         when(projectManager.getResources(any(), eq(ProjectScope.MAIN))).thenAnswer(iom -> {
191             Project p = iom.getArgument(0, Project.class);
192             return p.getBuild().getResources().stream()
193                     .map(r -> r.withDirectory(
194                             Paths.get(getBasedir()).resolve(r.getDirectory()).toString()))
195                     .toList();
196         });
197         return session;
198     }
199 }