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