View Javadoc

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