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.fromDependencies;
20  
21  import javax.inject.Inject;
22  
23  import java.io.File;
24  import java.io.IOException;
25  import java.util.HashSet;
26  import java.util.Set;
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.MojoTest;
32  import org.apache.maven.artifact.Artifact;
33  import org.apache.maven.execution.MavenSession;
34  import org.apache.maven.plugin.MojoExecutionException;
35  import org.apache.maven.plugin.MojoFailureException;
36  import org.apache.maven.plugins.dependency.testUtils.DependencyArtifactStubFactory;
37  import org.apache.maven.plugins.dependency.utils.DependencyUtil;
38  import org.apache.maven.project.MavenProject;
39  import org.codehaus.plexus.archiver.manager.ArchiverManager;
40  import org.junit.jupiter.api.BeforeEach;
41  import org.junit.jupiter.api.Test;
42  import org.junit.jupiter.api.io.TempDir;
43  
44  import static org.junit.jupiter.api.Assertions.assertEquals;
45  import static org.junit.jupiter.api.Assertions.assertNotEquals;
46  import static org.junit.jupiter.api.Assertions.assertTrue;
47  
48  @MojoTest
49  @Basedir("/unit/unpack-dependencies-test")
50  class TestUnpackDependenciesMojo2 {
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      @Inject
64      private ArchiverManager archiverManager;
65  
66      private static final String UNPACKABLE_FILE = "test.txt";
67  
68      @BeforeEach
69      void setUp() throws Exception {
70          stubFactory = new DependencyArtifactStubFactory(tempDir, true, false);
71          session.getRequest().setLocalRepositoryPath(new File(tempDir, "localTestRepo"));
72  
73          // it needs to get the archivermanager
74          stubFactory.setUnpackableFile(archiverManager);
75          // i'm using one file repeatedly to archive so I can test the name
76          // programmatically.
77          stubFactory.setSrcFile(MojoExtension.getTestFile(UNPACKABLE_FILE));
78  
79          Set<Artifact> artifacts = this.stubFactory.getScopedArtifacts();
80          Set<Artifact> directArtifacts = this.stubFactory.getReleaseAndSnapshotArtifacts();
81          artifacts.addAll(directArtifacts);
82  
83          project.setArtifacts(artifacts);
84  
85          project.getBuild().setDirectory(new File(tempDir, "target").getAbsolutePath());
86      }
87  
88      private File getUnpackedFile(UnpackDependenciesMojo mojo, Artifact artifact) {
89          File destDir = DependencyUtil.getFormattedOutputDirectory(
90                  mojo.isUseSubDirectoryPerScope(),
91                  mojo.isUseSubDirectoryPerType(),
92                  mojo.isUseSubDirectoryPerArtifact(),
93                  mojo.useRepositoryLayout,
94                  mojo.stripVersion,
95                  mojo.stripType,
96                  mojo.getOutputDirectory(),
97                  artifact);
98          File unpacked = new File(destDir, DependencyArtifactStubFactory.getUnpackableFileName(artifact));
99          assertTrue(unpacked.exists());
100         return unpacked;
101     }
102 
103     @Test
104     @InjectMojo(goal = "unpack-dependencies")
105     void testDontOverWriteRelease(UnpackDependenciesMojo mojo)
106             throws MojoExecutionException, InterruptedException, IOException, MojoFailureException {
107 
108         Set<Artifact> artifacts = new HashSet<>();
109         Artifact release = stubFactory.getReleaseArtifact();
110         assertTrue(release.getFile().setLastModified(System.currentTimeMillis() - 2000));
111 
112         artifacts.add(release);
113 
114         mojo.getProject().setArtifacts(artifacts);
115 
116         mojo.overWriteIfNewer = false;
117 
118         mojo.execute();
119 
120         assertUnpacked(mojo, release, false);
121     }
122 
123     @Test
124     @InjectMojo(goal = "unpack-dependencies")
125     void testOverWriteRelease(UnpackDependenciesMojo mojo)
126             throws MojoExecutionException, InterruptedException, IOException, MojoFailureException {
127 
128         Set<Artifact> artifacts = new HashSet<>();
129         Artifact release = stubFactory.getReleaseArtifact();
130         assertTrue(release.getFile().setLastModified(System.currentTimeMillis() - 2000));
131 
132         artifacts.add(release);
133 
134         mojo.getProject().setArtifacts(artifacts);
135 
136         mojo.overWriteReleases = true;
137         mojo.overWriteIfNewer = false;
138 
139         mojo.execute();
140 
141         assertUnpacked(mojo, release, true);
142     }
143 
144     @Test
145     @InjectMojo(goal = "unpack-dependencies")
146     void testDontOverWriteSnap(UnpackDependenciesMojo mojo)
147             throws MojoExecutionException, InterruptedException, IOException, MojoFailureException {
148 
149         Set<Artifact> artifacts = new HashSet<>();
150         Artifact snap = stubFactory.getSnapshotArtifact();
151         assertTrue(snap.getFile().setLastModified(System.currentTimeMillis() - 2000));
152 
153         artifacts.add(snap);
154 
155         mojo.getProject().setArtifacts(artifacts);
156 
157         mojo.overWriteReleases = false;
158         mojo.overWriteSnapshots = false;
159         mojo.overWriteIfNewer = false;
160 
161         mojo.execute();
162 
163         assertUnpacked(mojo, snap, false);
164     }
165 
166     @Test
167     @InjectMojo(goal = "unpack-dependencies")
168     void testOverWriteSnap(UnpackDependenciesMojo mojo)
169             throws MojoExecutionException, InterruptedException, IOException, MojoFailureException {
170 
171         Set<Artifact> artifacts = new HashSet<>();
172         Artifact snap = stubFactory.getSnapshotArtifact();
173         assertTrue(snap.getFile().setLastModified(System.currentTimeMillis() - 2000));
174 
175         artifacts.add(snap);
176 
177         mojo.getProject().setArtifacts(artifacts);
178 
179         mojo.overWriteReleases = false;
180         mojo.overWriteSnapshots = true;
181         mojo.overWriteIfNewer = false;
182 
183         mojo.execute();
184 
185         assertUnpacked(mojo, snap, true);
186     }
187 
188     @Test
189     @InjectMojo(goal = "unpack-dependencies")
190     void testOverWriteIfNewer(UnpackDependenciesMojo mojo)
191             throws MojoExecutionException, InterruptedException, IOException, MojoFailureException {
192 
193         Set<Artifact> artifacts = new HashSet<>();
194         Artifact snap = stubFactory.getSnapshotArtifact();
195         assertTrue(snap.getFile().setLastModified(System.currentTimeMillis() - 2000));
196 
197         artifacts.add(snap);
198 
199         mojo.getProject().setArtifacts(artifacts);
200 
201         mojo.overWriteReleases = false;
202         mojo.overWriteSnapshots = false;
203         mojo.overWriteIfNewer = false;
204 
205         mojo.execute();
206 
207         File unpackedFile = getUnpackedFile(mojo, snap);
208 
209         // round down to the last second
210         long time = System.currentTimeMillis();
211         time = time - (time % 1000);
212         // set source to be newer and dest to be a known value.
213         assertTrue(snap.getFile().setLastModified(time + 3000));
214         assertTrue(unpackedFile.setLastModified(time));
215         // wait at least a second for filesystems that only record to the
216         // nearest second.
217         Thread.sleep(1000);
218 
219         assertEquals(time, unpackedFile.lastModified());
220         mojo.execute();
221 
222         // make sure it didn't overwrite
223         assertEquals(time, unpackedFile.lastModified());
224 
225         mojo.overWriteIfNewer = true;
226 
227         mojo.execute();
228 
229         assertNotEquals(time, unpackedFile.lastModified());
230     }
231 
232     private void assertUnpacked(UnpackDependenciesMojo mojo, Artifact artifact, boolean overWrite)
233             throws InterruptedException, MojoExecutionException, MojoFailureException {
234         File unpackedFile = getUnpackedFile(mojo, artifact);
235 
236         Thread.sleep(100);
237         // round down to the last second
238         long time = System.currentTimeMillis();
239         time = time - (time % 1000);
240         assertTrue(unpackedFile.setLastModified(time));
241         // wait at least a second for filesystems that only record to the
242         // nearest second.
243         Thread.sleep(1000);
244 
245         assertEquals(time, unpackedFile.lastModified());
246         mojo.execute();
247 
248         if (overWrite) {
249             assertNotEquals(time, unpackedFile.lastModified());
250         } else {
251             assertEquals(time, unpackedFile.lastModified());
252         }
253     }
254 }