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