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 java.io.File;
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.List;
25  
26  import org.apache.maven.artifact.Artifact;
27  import org.apache.maven.execution.MavenSession;
28  import org.apache.maven.plugin.LegacySupport;
29  import org.apache.maven.plugin.MojoExecutionException;
30  import org.apache.maven.plugins.dependency.AbstractDependencyMojoTestCase;
31  import org.apache.maven.plugins.dependency.testUtils.stubs.DependencyProjectStub;
32  import org.apache.maven.plugins.dependency.utils.markers.UnpackFileMarkerHandler;
33  import org.apache.maven.project.MavenProject;
34  
35  public class TestIncludeExcludeUnpackMojo extends AbstractDependencyMojoTestCase {
36      private final String PACKED_FILE = "test.zip";
37  
38      private final String UNPACKED_FILE_PREFIX = "test";
39  
40      private final String UNPACKED_FILE_SUFFIX = ".txt";
41  
42      private final String PACKED_FILE_PATH = "target/test-classes/unit/unpack-dependencies-test/" + PACKED_FILE;
43  
44      private UnpackMojo mojo;
45  
46      protected void setUp() throws Exception {
47          // required for mojo lookups to work
48          super.setUp("unpack", true, false);
49  
50          MavenProject project = new DependencyProjectStub();
51          getContainer().addComponent(project, MavenProject.class.getName());
52  
53          MavenSession session = newMavenSession(project);
54          getContainer().addComponent(session, MavenSession.class.getName());
55  
56          File testPom = new File(getBasedir(), "target/test-classes/unit/unpack-test/plugin-config.xml");
57          mojo = (UnpackMojo) lookupMojo("unpack", testPom);
58          mojo.setOutputDirectory(new File(this.testDir, "outputDirectory"));
59          // mojo.silent = true;
60  
61          // i'm using one file repeatedly to archive so I can test the name
62          // programmatically.
63          stubFactory.setSrcFile(new File(getBasedir() + File.separatorChar + PACKED_FILE_PATH));
64          Artifact artifact = stubFactory.createArtifact("test", "test", "1.0", Artifact.SCOPE_COMPILE, "jar", null);
65          ArtifactItem item = stubFactory.getArtifactItem(artifact);
66          List<ArtifactItem> list = new ArrayList<>(1);
67          list.add(item);
68          assertNotNull(mojo);
69          assertNotNull(mojo.getProject());
70  
71          mojo.setMarkersDirectory(new File(this.testDir, "markers"));
72          mojo.setArtifactItems(list);
73  
74          LegacySupport legacySupport = lookup(LegacySupport.class);
75          legacySupport.setSession(session);
76          installLocalRepository(legacySupport);
77      }
78  
79      protected void tearDown() {
80          super.tearDown();
81  
82          mojo = null;
83          System.gc();
84      }
85  
86      public void assertMarkerFiles(Collection<ArtifactItem> items, boolean exist) {
87          for (ArtifactItem item : items) {
88              assertMarkerFile(exist, item);
89          }
90      }
91  
92      public void assertMarkerFile(boolean val, ArtifactItem item) {
93          UnpackFileMarkerHandler handle = new UnpackFileMarkerHandler(item, mojo.getMarkersDirectory());
94          try {
95              assertEquals(val, handle.isMarkerSet());
96          } catch (MojoExecutionException e) {
97              fail(e.getLongMessage());
98          }
99      }
100 
101     private void assertUnpacked(boolean unpacked, String fileName) {
102         File destFile = new File(mojo.getOutputDirectory().getAbsolutePath(), fileName);
103         assertEquals(unpacked, destFile.exists());
104     }
105 
106     /**
107      * This test will validate that only the 1 and 11 files get unpacked
108      *
109      * @throws Exception in case of errors.
110      */
111     public void testUnpackIncludesManyFiles() throws Exception {
112         mojo.setIncludes("**/*1" + UNPACKED_FILE_SUFFIX);
113         mojo.execute();
114         assertUnpacked(true, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX);
115         assertUnpacked(true, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX);
116         assertUnpacked(false, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
117         assertUnpacked(false, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
118     }
119 
120     /**
121      * This test will verify only the 2 file gets unpacked
122      *
123      * @throws Exception in case of errors.
124      */
125     public void testUnpackIncludesSingleFile() throws Exception {
126         mojo.setIncludes("**/test2" + UNPACKED_FILE_SUFFIX);
127         mojo.execute();
128         assertUnpacked(false, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX);
129         assertUnpacked(false, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX);
130         assertUnpacked(true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
131         assertUnpacked(false, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
132     }
133 
134     /**
135      * This test will verify all files get unpacked
136      *
137      * @throws Exception in case of errors.
138      */
139     public void testUnpackIncludesAllFiles() throws Exception {
140         mojo.setIncludes("**/*");
141         mojo.execute();
142         assertUnpacked(true, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX);
143         assertUnpacked(true, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX);
144         assertUnpacked(true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
145         assertUnpacked(true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
146     }
147 
148     /**
149      * This test will validate that only the 2 and 3 files get unpacked
150      *
151      * @throws Exception in case of errors.
152      */
153     public void testUnpackExcludesManyFiles() throws Exception {
154         mojo.setExcludes("**/*1" + UNPACKED_FILE_SUFFIX);
155         mojo.execute();
156         assertUnpacked(false, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX);
157         assertUnpacked(false, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX);
158         assertUnpacked(true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
159         assertUnpacked(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     public void testUnpackExcludesSingleFile() throws Exception {
168         mojo.setExcludes("**/test2" + UNPACKED_FILE_SUFFIX);
169         mojo.execute();
170         assertUnpacked(true, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX);
171         assertUnpacked(true, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX);
172         assertUnpacked(false, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
173         assertUnpacked(true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
174     }
175 
176     /**
177      * This test will verify no files get unpacked
178      *
179      * @throws Exception in case of errors.
180      */
181     public void testUnpackExcludesAllFiles() throws Exception {
182         mojo.setExcludes("**/*");
183         mojo.execute();
184         assertUnpacked(false, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX);
185         assertUnpacked(false, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX);
186         assertUnpacked(false, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
187         assertUnpacked(false, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
188     }
189 
190     public void testNoIncludeExcludes() throws Exception {
191         mojo.execute();
192         assertUnpacked(true, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX);
193         assertUnpacked(true, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX);
194         assertUnpacked(true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
195         assertUnpacked(true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
196     }
197 
198     public void testIncludeArtifactItemOverride() throws Exception {
199         Artifact artifact = stubFactory.createArtifact("test", "test", "1.0", Artifact.SCOPE_COMPILE, "jar", null);
200         ArtifactItem item = stubFactory.getArtifactItem(artifact);
201         item.setIncludes("**/*");
202         List<ArtifactItem> list = new ArrayList<>(1);
203         list.add(item);
204         mojo.setArtifactItems(list);
205         mojo.setIncludes("**/test2" + UNPACKED_FILE_SUFFIX);
206         mojo.execute();
207         assertUnpacked(true, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX);
208         assertUnpacked(true, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX);
209         assertUnpacked(true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
210         assertUnpacked(true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
211     }
212 
213     public void testExcludeArtifactItemOverride() throws Exception {
214         Artifact artifact = stubFactory.createArtifact("test", "test", "1.0", Artifact.SCOPE_COMPILE, "jar", null);
215         ArtifactItem item = stubFactory.getArtifactItem(artifact);
216         item.setExcludes("**/*");
217         List<ArtifactItem> list = new ArrayList<>(1);
218         list.add(item);
219         mojo.setArtifactItems(list);
220         mojo.setExcludes("**/test2" + UNPACKED_FILE_SUFFIX);
221         mojo.execute();
222         assertUnpacked(false, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX);
223         assertUnpacked(false, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX);
224         assertUnpacked(false, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
225         assertUnpacked(false, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
226     }
227 
228     public void testIncludeArtifactItemMultipleMarker() throws Exception {
229         List<ArtifactItem> list = new ArrayList<>();
230         Artifact artifact = stubFactory.createArtifact("test", "test", "1.0", Artifact.SCOPE_COMPILE, "jar", null);
231         ArtifactItem item = stubFactory.getArtifactItem(artifact);
232         item.setOverWrite("false");
233         item.setIncludes("**/test2" + UNPACKED_FILE_SUFFIX);
234         list.add(item);
235         item = stubFactory.getArtifactItem(artifact);
236         item.setOverWrite("false");
237         item.setIncludes("**/test3" + UNPACKED_FILE_SUFFIX);
238         list.add(item);
239         mojo.setArtifactItems(list);
240         mojo.execute();
241         assertUnpacked(false, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX);
242         assertUnpacked(false, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX);
243         assertUnpacked(true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
244         assertUnpacked(true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
245         assertMarkerFiles(mojo.getArtifactItems(), true);
246     }
247 
248     public void testIncludeArtifactItemMultipleExecutions() throws Exception {
249         List<ArtifactItem> list = new ArrayList<>();
250         Artifact artifact = stubFactory.createArtifact("test", "test", "1.0", Artifact.SCOPE_COMPILE, "jar", null);
251         ArtifactItem item = stubFactory.getArtifactItem(artifact);
252         item.setOverWrite("false");
253         item.setIncludes("**/test2" + UNPACKED_FILE_SUFFIX);
254         list.add(item);
255         item = stubFactory.getArtifactItem(artifact);
256         item.setOverWrite("false");
257         item.setIncludes("**/test3" + UNPACKED_FILE_SUFFIX);
258         list.add(item);
259         mojo.setArtifactItems(list);
260         mojo.execute();
261         assertUnpacked(false, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX);
262         assertUnpacked(false, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX);
263         assertUnpacked(true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
264         assertUnpacked(true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
265         assertMarkerFiles(mojo.getArtifactItems(), true);
266 
267         // Now run again and make sure the extracted files haven't gotten overwritten
268         File destFile2 =
269                 new File(mojo.getOutputDirectory().getAbsolutePath(), UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX);
270         File destFile3 =
271                 new File(mojo.getOutputDirectory().getAbsolutePath(), UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX);
272         long time = System.currentTimeMillis();
273         time = time - (time % 1000);
274         assertTrue(destFile2.setLastModified(time));
275         assertTrue(destFile3.setLastModified(time));
276         assertEquals(time, destFile2.lastModified());
277         assertEquals(time, destFile3.lastModified());
278         Thread.sleep(100);
279         mojo.execute();
280         assertEquals(time, destFile2.lastModified());
281         assertEquals(time, destFile3.lastModified());
282     }
283 }