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