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