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