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  import static org.junit.Assert.assertNotEquals;
38  
39  public class TestUnpackDependenciesMojo2 extends AbstractDependencyMojoTestCase {
40  
41      private static final String UNPACKABLE_FILE = "test.txt";
42  
43      private static final String UNPACKABLE_FILE_PATH =
44              "target/test-classes/unit/unpack-dependencies-test/" + UNPACKABLE_FILE;
45  
46      private UnpackDependenciesMojo mojo;
47  
48      @Override
49      protected String getTestDirectoryName() {
50          return "unpack-dependencies";
51      }
52  
53      @Override
54      protected boolean shouldCreateFiles() {
55          return true;
56      }
57  
58      @Override
59      protected void setUp() throws Exception {
60          // required for mojo lookups to work
61          super.setUp();
62  
63          MavenProject project = new DependencyProjectStub();
64          getContainer().addComponent(project, MavenProject.class.getName());
65  
66          MavenSession session = newMavenSession(project);
67          getContainer().addComponent(session, MavenSession.class.getName());
68  
69          File testPom = new File(getBasedir(), "target/test-classes/unit/unpack-dependencies-test/plugin-config.xml");
70          mojo = (UnpackDependenciesMojo) lookupMojo("unpack-dependencies", testPom);
71          mojo.outputDirectory = new File(this.testDir, "outputDirectory");
72  
73          // it needs to get the archivermanager
74          stubFactory.setUnpackableFile(lookup(ArchiverManager.class));
75          // i'm using one file repeatedly to archive so I can test the name
76          // programmatically.
77          stubFactory.setSrcFile(new File(getBasedir() + File.separatorChar + UNPACKABLE_FILE_PATH));
78  
79          assertNotNull(mojo);
80          assertNotNull(mojo.getProject());
81  
82          Set<Artifact> artifacts = this.stubFactory.getScopedArtifacts();
83          Set<Artifact> directArtifacts = this.stubFactory.getReleaseAndSnapshotArtifacts();
84          artifacts.addAll(directArtifacts);
85  
86          project.setArtifacts(artifacts);
87          mojo.markersDirectory = new File(this.testDir, "markers");
88      }
89  
90      public File getUnpackedFile(Artifact artifact) {
91          File destDir = DependencyUtil.getFormattedOutputDirectory(
92                  mojo.isUseSubDirectoryPerScope(),
93                  mojo.isUseSubDirectoryPerType(),
94                  mojo.isUseSubDirectoryPerArtifact(),
95                  mojo.useRepositoryLayout,
96                  mojo.stripVersion,
97                  mojo.stripType,
98                  mojo.getOutputDirectory(),
99                  artifact);
100         File unpacked = new File(destDir, DependencyArtifactStubFactory.getUnpackableFileName(artifact));
101         assertTrue(unpacked.exists());
102         return unpacked;
103     }
104 
105     public void testDontOverWriteRelease()
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(release, false);
121     }
122 
123     public void testOverWriteRelease()
124             throws MojoExecutionException, InterruptedException, IOException, MojoFailureException {
125 
126         Set<Artifact> artifacts = new HashSet<>();
127         Artifact release = stubFactory.getReleaseArtifact();
128         assertTrue(release.getFile().setLastModified(System.currentTimeMillis() - 2000));
129 
130         artifacts.add(release);
131 
132         mojo.getProject().setArtifacts(artifacts);
133 
134         mojo.overWriteReleases = true;
135         mojo.overWriteIfNewer = false;
136 
137         mojo.execute();
138 
139         assertUnpacked(release, true);
140     }
141 
142     public void testDontOverWriteSnap()
143             throws MojoExecutionException, InterruptedException, IOException, MojoFailureException {
144 
145         Set<Artifact> artifacts = new HashSet<>();
146         Artifact snap = stubFactory.getSnapshotArtifact();
147         assertTrue(snap.getFile().setLastModified(System.currentTimeMillis() - 2000));
148 
149         artifacts.add(snap);
150 
151         mojo.getProject().setArtifacts(artifacts);
152 
153         mojo.overWriteReleases = false;
154         mojo.overWriteSnapshots = false;
155         mojo.overWriteIfNewer = false;
156 
157         mojo.execute();
158 
159         assertUnpacked(snap, false);
160     }
161 
162     public void testOverWriteSnap()
163             throws MojoExecutionException, InterruptedException, IOException, MojoFailureException {
164 
165         Set<Artifact> artifacts = new HashSet<>();
166         Artifact snap = stubFactory.getSnapshotArtifact();
167         assertTrue(snap.getFile().setLastModified(System.currentTimeMillis() - 2000));
168 
169         artifacts.add(snap);
170 
171         mojo.getProject().setArtifacts(artifacts);
172 
173         mojo.overWriteReleases = false;
174         mojo.overWriteSnapshots = true;
175         mojo.overWriteIfNewer = false;
176 
177         mojo.execute();
178 
179         assertUnpacked(snap, true);
180     }
181 
182     public void testOverWriteIfNewer()
183             throws MojoExecutionException, InterruptedException, IOException, MojoFailureException {
184 
185         Set<Artifact> artifacts = new HashSet<>();
186         Artifact snap = stubFactory.getSnapshotArtifact();
187         assertTrue(snap.getFile().setLastModified(System.currentTimeMillis() - 2000));
188 
189         artifacts.add(snap);
190 
191         mojo.getProject().setArtifacts(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 
214         // make sure it didn't overwrite
215         assertEquals(time, unpackedFile.lastModified());
216 
217         mojo.overWriteIfNewer = true;
218 
219         mojo.execute();
220 
221         assertNotEquals(time, unpackedFile.lastModified());
222     }
223 
224     public void assertUnpacked(Artifact artifact, boolean overWrite)
225             throws InterruptedException, MojoExecutionException, MojoFailureException {
226         File unpackedFile = getUnpackedFile(artifact);
227 
228         Thread.sleep(100);
229         // round down to the last second
230         long time = System.currentTimeMillis();
231         time = time - (time % 1000);
232         assertTrue(unpackedFile.setLastModified(time));
233         // wait at least a second for filesystems that only record to the
234         // nearest second.
235         Thread.sleep(1000);
236 
237         assertEquals(time, unpackedFile.lastModified());
238         mojo.execute();
239 
240         if (overWrite) {
241             assertNotEquals(time, unpackedFile.lastModified());
242         } else {
243             assertEquals(time, unpackedFile.lastModified());
244         }
245     }
246 }