View Javadoc
1   package org.apache.maven.plugins.dependency.fromDependencies;
2   
3   /* 
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.    
20   */
21  
22  import org.apache.maven.plugin.MojoFailureException;
23  
24  import org.apache.maven.artifact.Artifact;
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.plugins.dependency.AbstractDependencyMojoTestCase;
27  import org.apache.maven.plugins.dependency.testUtils.DependencyArtifactStubFactory;
28  import org.apache.maven.plugins.dependency.utils.DependencyUtil;
29  import org.apache.maven.project.MavenProject;
30  
31  import java.io.File;
32  import java.io.IOException;
33  import java.util.HashSet;
34  import java.util.Set;
35  
36  public class TestUnpackDependenciesMojo2
37      extends AbstractDependencyMojoTestCase
38  {
39  
40      private final String UNPACKABLE_FILE = "test.txt";
41  
42      private final String UNPACKABLE_FILE_PATH = "target/test-classes/unit/unpack-dependencies-test/" + UNPACKABLE_FILE;
43  
44      private UnpackDependenciesMojo mojo;
45  
46      protected void setUp()
47          throws Exception
48      {
49          // required for mojo lookups to work
50          super.setUp( "unpack-dependencies", true );
51  
52          File testPom = new File( getBasedir(), "target/test-classes/unit/unpack-dependencies-test/plugin-config.xml" );
53          mojo = (UnpackDependenciesMojo) lookupMojo( "unpack-dependencies", testPom );
54          mojo.outputDirectory = new File( this.testDir, "outputDirectory" );
55          mojo.setUseJvmChmod( true );
56          // mojo.silent = true;
57  
58          // it needs to get the archivermanager
59          stubFactory.setUnpackableFile( mojo.getArchiverManager() );
60          // i'm using one file repeatedly to archive so I can test the name
61          // programmatically.
62          stubFactory.setSrcFile( new File( getBasedir() + File.separatorChar + UNPACKABLE_FILE_PATH ) );
63  
64          assertNotNull( mojo );
65          assertNotNull( mojo.getProject() );
66          MavenProject project = mojo.getProject();
67  
68          Set<Artifact> artifacts = this.stubFactory.getScopedArtifacts();
69          Set<Artifact> directArtifacts = this.stubFactory.getReleaseAndSnapshotArtifacts();
70          artifacts.addAll( directArtifacts );
71  
72          project.setArtifacts( artifacts );
73          project.setDependencyArtifacts( directArtifacts );
74          mojo.markersDirectory = new File( this.testDir, "markers" );
75  
76      }
77  
78      protected void tearDown()
79      {
80          super.tearDown();
81  
82          mojo = null;
83          System.gc();
84      }
85  
86      public File getUnpackedFile( Artifact artifact )
87      {
88          File destDir =
89              DependencyUtil.getFormattedOutputDirectory( mojo.isUseSubDirectoryPerScope(),
90                                                          mojo.isUseSubDirectoryPerType(),
91                                                          mojo.isUseSubDirectoryPerArtifact(), mojo.useRepositoryLayout,
92                                                          mojo.stripVersion, mojo.getOutputDirectory(), artifact );
93          File unpacked = new File( destDir, DependencyArtifactStubFactory.getUnpackableFileName( artifact ) );
94          assertTrue( unpacked.exists() );
95          return unpacked;
96      }
97  
98      public void testDontOverWriteRelease()
99          throws MojoExecutionException, InterruptedException, IOException, MojoFailureException
100     {
101 
102         Set<Artifact> artifacts = new HashSet<>();
103         Artifact release = stubFactory.getReleaseArtifact();
104         assertTrue( release.getFile().setLastModified( System.currentTimeMillis() - 2000 ) );
105 
106         artifacts.add( release );
107 
108         mojo.getProject().setArtifacts( artifacts );
109         mojo.getProject().setDependencyArtifacts( artifacts );
110 
111         mojo.overWriteIfNewer = false;
112 
113         mojo.execute();
114 
115         assertUnpacked( release, false );
116     }
117 
118     public void testOverWriteRelease()
119         throws MojoExecutionException, InterruptedException, IOException, MojoFailureException
120     {
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 
143         Set<Artifact> artifacts = new HashSet<>();
144         Artifact snap = stubFactory.getSnapshotArtifact();
145         assertTrue( snap.getFile().setLastModified( System.currentTimeMillis() - 2000 ) );
146 
147         artifacts.add( snap );
148 
149         mojo.getProject().setArtifacts( artifacts );
150         mojo.getProject().setDependencyArtifacts( artifacts );
151 
152         mojo.overWriteReleases = false;
153         mojo.overWriteSnapshots = false;
154         mojo.overWriteIfNewer = false;
155 
156         mojo.execute();
157 
158         assertUnpacked( snap, false );
159     }
160 
161     public void testOverWriteSnap()
162         throws MojoExecutionException, InterruptedException, IOException, MojoFailureException
163     {
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         mojo.getProject().setDependencyArtifacts( artifacts );
173 
174         mojo.overWriteReleases = false;
175         mojo.overWriteSnapshots = true;
176         mojo.overWriteIfNewer = false;
177 
178         mojo.execute();
179 
180         assertUnpacked( snap, true );
181 
182     }
183 
184     public void testOverWriteIfNewer()
185         throws MojoExecutionException, InterruptedException, IOException, MojoFailureException
186     {
187 
188         Set<Artifact> artifacts = new HashSet<>();
189         Artifact snap = stubFactory.getSnapshotArtifact();
190         assertTrue( snap.getFile().setLastModified( System.currentTimeMillis() - 2000 ) );
191 
192         artifacts.add( snap );
193 
194         mojo.getProject().setArtifacts( artifacts );
195         mojo.getProject().setDependencyArtifacts( artifacts );
196 
197         mojo.overWriteReleases = false;
198         mojo.overWriteSnapshots = false;
199         mojo.overWriteIfNewer = false;
200 
201         mojo.execute();
202 
203         File unpackedFile = getUnpackedFile( snap );
204 
205         // round down to the last second
206         long time = System.currentTimeMillis();
207         time = time - ( time % 1000 );
208         // set source to be newer and dest to be a known value.
209         assertTrue( snap.getFile().setLastModified( time + 3000 ) );
210         assertTrue( unpackedFile.setLastModified( time ) );
211         // wait at least a second for filesystems that only record to the
212         // nearest second.
213         Thread.sleep( 1000 );
214 
215         assertEquals( time, unpackedFile.lastModified() );
216         mojo.execute();
217         System.gc();
218         // make sure it didn't overwrite
219         assertEquals( time, unpackedFile.lastModified() );
220 
221         mojo.overWriteIfNewer = true;
222 
223         mojo.execute();
224 
225         assertTrue( time != unpackedFile.lastModified() );
226 
227         System.gc();
228     }
229 
230     public void assertUnpacked( Artifact artifact, boolean overWrite )
231         throws InterruptedException, MojoExecutionException, MojoFailureException
232     {
233         File unpackedFile = getUnpackedFile( artifact );
234 
235         Thread.sleep( 100 );
236         // round down to the last second
237         long time = System.currentTimeMillis();
238         time = time - ( time % 1000 );
239         assertTrue( unpackedFile.setLastModified( time ) );
240         // wait at least a second for filesystems that only record to the
241         // nearest second.
242         Thread.sleep( 1000 );
243 
244         assertEquals( time, unpackedFile.lastModified() );
245         mojo.execute();
246 
247         if ( overWrite )
248         {
249             assertTrue( time != unpackedFile.lastModified() );
250         }
251         else
252         {
253             assertEquals( time, unpackedFile.lastModified() );
254         }
255     }
256 }