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