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.dependency.fromConfiguration;
20  
21  import javax.inject.Inject;
22  
23  import java.io.File;
24  import java.util.ArrayList;
25  import java.util.Collection;
26  import java.util.List;
27  
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.MojoExtension;
31  import org.apache.maven.api.plugin.testing.MojoParameter;
32  import org.apache.maven.api.plugin.testing.MojoTest;
33  import org.apache.maven.artifact.Artifact;
34  import org.apache.maven.execution.MavenSession;
35  import org.apache.maven.plugin.MojoExecutionException;
36  import org.apache.maven.plugins.dependency.testUtils.DependencyArtifactStubFactory;
37  import org.apache.maven.plugins.dependency.utils.markers.UnpackFileMarkerHandler;
38  import org.apache.maven.project.MavenProject;
39  import org.junit.jupiter.api.BeforeEach;
40  import org.junit.jupiter.api.Test;
41  import org.junit.jupiter.api.io.TempDir;
42  
43  import static org.junit.jupiter.api.Assertions.assertEquals;
44  import static org.junit.jupiter.api.Assertions.assertTrue;
45  import static org.junit.jupiter.api.Assertions.fail;
46  
47  @MojoTest(realRepositorySession = true)
48  @Basedir("/unit/unpack-dependencies-test")
49  @MojoParameter(name = "artifact", value = "test:test:1.0")
50  class TestIncludeExcludeUnpackMojo {
51  
52      @TempDir
53      private File tempDir;
54  
55      private DependencyArtifactStubFactory stubFactory;
56  
57      @Inject
58      private MavenSession session;
59  
60      @Inject
61      private MavenProject project;
62  
63      private static final String UNPACKED_FILE_PREFIX = "test";
64  
65      private static final String UNPACKED_FILE_SUFFIX = ".txt";
66  
67      @BeforeEach
68      void setUp() throws Exception {
69          stubFactory = new DependencyArtifactStubFactory(tempDir, true, false);
70          stubFactory.setSrcFile(MojoExtension.getTestFile("test.zip"));
71          stubFactory.createArtifact("test", "test", "1.0", Artifact.SCOPE_COMPILE, "jar", null);
72  
73          session.getRequest().setLocalRepositoryPath(new File(tempDir, "localTestRepo"));
74  
75          project.getBuild().setDirectory(new File(tempDir, "target").getAbsolutePath());
76      }
77  
78      private void assertMarkerFiles(UnpackMojo mojo, Collection<ArtifactItem> items) {
79          for (ArtifactItem item : items) {
80              assertMarkerFile(mojo, item);
81          }
82      }
83  
84      private void assertMarkerFile(UnpackMojo mojo, ArtifactItem item) {
85          UnpackFileMarkerHandler handle = new UnpackFileMarkerHandler(item, mojo.getMarkersDirectory());
86          try {
87              assertTrue(handle.isMarkerSet());
88          } catch (MojoExecutionException e) {
89              fail(e.getLongMessage());
90          }
91      }
92  
93      private void assertUnpacked(UnpackMojo mojo, boolean unpacked, String fileName) {
94          File destFile = new File(mojo.getOutputDirectory().getAbsolutePath(), fileName);
95          assertEquals(unpacked, destFile.exists());
96      }
97  
98      /**
99       * This test will validate that only the 1 and 11 files get unpacked
100      *
101      * @throws Exception in case of errors.
102      */
103     @Test
104     @InjectMojo(goal = "unpack")
105     void testUnpackIncludesManyFiles(UnpackMojo mojo) throws Exception {
106         mojo.setIncludes("**/*1" + UNPACKED_FILE_SUFFIX);
107         mojo.execute();
108         assertUnpacked(mojo, true, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX);
109         assertUnpacked(mojo, true, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX);
110         assertUnpacked(mojo, false, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
111         assertUnpacked(mojo, false, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
112     }
113 
114     /**
115      * This test will verify only the 2 file gets unpacked
116      *
117      * @throws Exception in case of errors.
118      */
119     @Test
120     @InjectMojo(goal = "unpack")
121     void testUnpackIncludesSingleFile(UnpackMojo mojo) throws Exception {
122         mojo.setIncludes("**/test2" + UNPACKED_FILE_SUFFIX);
123         mojo.execute();
124         assertUnpacked(mojo, false, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX);
125         assertUnpacked(mojo, false, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX);
126         assertUnpacked(mojo, true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
127         assertUnpacked(mojo, false, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
128     }
129 
130     /**
131      * This test will verify all files get unpacked
132      *
133      * @throws Exception in case of errors.
134      */
135     @Test
136     @InjectMojo(goal = "unpack")
137     void testUnpackIncludesAllFiles(UnpackMojo mojo) throws Exception {
138         mojo.setIncludes("**/*");
139         mojo.execute();
140         assertUnpacked(mojo, true, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX);
141         assertUnpacked(mojo, true, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX);
142         assertUnpacked(mojo, true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
143         assertUnpacked(mojo, true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
144     }
145 
146     /**
147      * This test will validate that only the 2 and 3 files get unpacked
148      *
149      * @throws Exception in case of errors.
150      */
151     @Test
152     @InjectMojo(goal = "unpack")
153     void testUnpackExcludesManyFiles(UnpackMojo mojo) throws Exception {
154         mojo.setExcludes("**/*1" + UNPACKED_FILE_SUFFIX);
155         mojo.execute();
156         assertUnpacked(mojo, false, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX);
157         assertUnpacked(mojo, false, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX);
158         assertUnpacked(mojo, true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
159         assertUnpacked(mojo, true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
160     }
161 
162     /**
163      * This test will verify only the 1, 11 &amp; 3 files get unpacked
164      *
165      * @throws Exception in case of errors.
166      */
167     @Test
168     @InjectMojo(goal = "unpack")
169     void testUnpackExcludesSingleFile(UnpackMojo mojo) throws Exception {
170         mojo.setExcludes("**/test2" + UNPACKED_FILE_SUFFIX);
171         mojo.execute();
172         assertUnpacked(mojo, true, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX);
173         assertUnpacked(mojo, true, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX);
174         assertUnpacked(mojo, false, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
175         assertUnpacked(mojo, true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
176     }
177 
178     /**
179      * This test will verify no files get unpacked
180      *
181      * @throws Exception in case of errors.
182      */
183     @Test
184     @InjectMojo(goal = "unpack")
185     void testUnpackExcludesAllFiles(UnpackMojo mojo) throws Exception {
186         mojo.setExcludes("**/*");
187         mojo.execute();
188         assertUnpacked(mojo, false, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX);
189         assertUnpacked(mojo, false, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX);
190         assertUnpacked(mojo, false, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
191         assertUnpacked(mojo, false, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
192     }
193 
194     @Test
195     @InjectMojo(goal = "unpack")
196     void testNoIncludeExcludes(UnpackMojo mojo) throws Exception {
197         mojo.execute();
198         assertUnpacked(mojo, true, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX);
199         assertUnpacked(mojo, true, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX);
200         assertUnpacked(mojo, true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
201         assertUnpacked(mojo, true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
202     }
203 
204     @Test
205     @InjectMojo(goal = "unpack")
206     void testIncludeArtifactItemOverride(UnpackMojo mojo) throws Exception {
207         Artifact artifact = stubFactory.createArtifact("test", "test", "1.0", Artifact.SCOPE_COMPILE, "jar", null);
208         ArtifactItem item = new ArtifactItem(artifact);
209         item.setIncludes("**/*");
210         List<ArtifactItem> list = new ArrayList<>(1);
211         list.add(item);
212         mojo.setArtifactItems(list);
213         mojo.setIncludes("**/test2" + UNPACKED_FILE_SUFFIX);
214         mojo.execute();
215         assertUnpacked(mojo, true, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX);
216         assertUnpacked(mojo, true, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX);
217         assertUnpacked(mojo, true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
218         assertUnpacked(mojo, true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
219     }
220 
221     @Test
222     @InjectMojo(goal = "unpack")
223     void testExcludeArtifactItemOverride(UnpackMojo mojo) throws Exception {
224         Artifact artifact = stubFactory.createArtifact("test", "test", "1.0", Artifact.SCOPE_COMPILE, "jar", null);
225         ArtifactItem item = new ArtifactItem(artifact);
226         item.setExcludes("**/*");
227         List<ArtifactItem> list = new ArrayList<>(1);
228         list.add(item);
229         mojo.setArtifactItems(list);
230         mojo.setExcludes("**/test2" + UNPACKED_FILE_SUFFIX);
231         mojo.execute();
232         assertUnpacked(mojo, false, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX);
233         assertUnpacked(mojo, false, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX);
234         assertUnpacked(mojo, false, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
235         assertUnpacked(mojo, false, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
236     }
237 
238     @Test
239     @InjectMojo(goal = "unpack")
240     void testIncludeArtifactItemMultipleMarker(UnpackMojo mojo) throws Exception {
241         List<ArtifactItem> list = new ArrayList<>();
242         Artifact artifact = stubFactory.createArtifact("test", "test", "1.0", Artifact.SCOPE_COMPILE, "jar", null);
243         ArtifactItem item = new ArtifactItem(artifact);
244         item.setOverWrite("false");
245         item.setIncludes("**/test2" + UNPACKED_FILE_SUFFIX);
246         list.add(item);
247         item = new ArtifactItem(artifact);
248         item.setOverWrite("false");
249         item.setIncludes("**/test3" + UNPACKED_FILE_SUFFIX);
250         list.add(item);
251         mojo.setArtifactItems(list);
252         mojo.execute();
253         assertUnpacked(mojo, false, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX);
254         assertUnpacked(mojo, false, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX);
255         assertUnpacked(mojo, true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
256         assertUnpacked(mojo, true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
257         assertMarkerFiles(mojo, mojo.getArtifactItems());
258     }
259 
260     @Test
261     @InjectMojo(goal = "unpack")
262     void testIncludeArtifactItemMultipleExecutions(UnpackMojo mojo) throws Exception {
263         List<ArtifactItem> list = new ArrayList<>();
264         Artifact artifact = stubFactory.createArtifact("test", "test", "1.0", Artifact.SCOPE_COMPILE, "jar", null);
265         ArtifactItem item = new ArtifactItem(artifact);
266         item.setOverWrite("false");
267         item.setIncludes("**/test2" + UNPACKED_FILE_SUFFIX);
268         list.add(item);
269         item = new ArtifactItem(artifact);
270         item.setOverWrite("false");
271         item.setIncludes("**/test3" + UNPACKED_FILE_SUFFIX);
272         list.add(item);
273         mojo.setArtifactItems(list);
274         mojo.execute();
275         assertUnpacked(mojo, false, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX);
276         assertUnpacked(mojo, false, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX);
277         assertUnpacked(mojo, true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
278         assertUnpacked(mojo, true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
279         assertMarkerFiles(mojo, mojo.getArtifactItems());
280 
281         // Now run again and make sure the extracted files haven't gotten overwritten
282         File destFile2 =
283                 new File(mojo.getOutputDirectory().getAbsolutePath(), UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
284         File destFile3 =
285                 new File(mojo.getOutputDirectory().getAbsolutePath(), UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
286         long time = System.currentTimeMillis();
287         time = time - (time % 1000);
288         assertTrue(destFile2.setLastModified(time));
289         assertTrue(destFile3.setLastModified(time));
290         assertEquals(time, destFile2.lastModified());
291         assertEquals(time, destFile3.lastModified());
292         Thread.sleep(100);
293         mojo.execute();
294         assertEquals(time, destFile2.lastModified());
295         assertEquals(time, destFile3.lastModified());
296     }
297 }