View Javadoc

1   package org.apache.maven.archiva.proxy;
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.archiva.model.ArtifactReference;
23  import org.apache.maven.archiva.policies.CachedFailuresPolicy;
24  import org.apache.maven.archiva.policies.ChecksumPolicy;
25  import org.apache.maven.archiva.policies.ReleasesPolicy;
26  import org.apache.maven.archiva.policies.SnapshotsPolicy;
27  
28  import java.io.File;
29  
30  /**
31   * SnapshotTransferTest 
32   *
33   * @version $Id: SnapshotTransferTest.java 718864 2008-11-19 06:33:35Z brett $
34   */
35  public class SnapshotTransferTest
36      extends AbstractProxyTestCase
37  {
38      public void testSnapshotNonExistant()
39          throws Exception
40      {
41          String path = "org/apache/maven/test/does-not-exist/1.0-SNAPSHOT/does-not-exist-1.0-SNAPSHOT.jar";
42          setupTestableManagedRepository( path );
43          
44          File expectedFile = new File( managedDefaultDir, path );
45          ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
46  
47          expectedFile.delete();
48          assertFalse( expectedFile.exists() );
49  
50          // Configure Connector (usually done within archiva.xml configuration)
51          saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false);
52  
53          File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
54          assertNotDownloaded( downloadedFile );
55          assertNoTempFiles( expectedFile );
56      }
57  
58      public void testTimestampDrivenSnapshotNotPresentAlready()
59          throws Exception
60      {
61          String path = "org/apache/maven/test/get-timestamped-snapshot/1.0-SNAPSHOT/get-timestamped-snapshot-1.0-SNAPSHOT.jar";
62          setupTestableManagedRepository( path );
63          
64          File expectedFile = new File( managedDefaultDir, path );
65          ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
66  
67          expectedFile.delete();
68          assertFalse( expectedFile.exists() );
69  
70          // Configure Connector (usually done within archiva.xml configuration)
71          saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false);
72  
73          File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
74  
75          File proxiedFile = new File( REPOPATH_PROXIED1, path );
76          assertFileEquals( expectedFile, downloadedFile, proxiedFile );
77          assertNoTempFiles( expectedFile );
78      }
79  
80      public void testNewerTimestampDrivenSnapshotOnFirstRepo()
81          throws Exception
82      {
83          String path = "org/apache/maven/test/get-present-timestamped-snapshot/1.0-SNAPSHOT/get-present-timestamped-snapshot-1.0-SNAPSHOT.jar";
84          setupTestableManagedRepository( path );
85          
86          File expectedFile = new File( managedDefaultDir, path );
87          ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
88  
89          assertTrue( expectedFile.exists() );
90          expectedFile.setLastModified( getPastDate().getTime() );
91  
92          // Configure Connector (usually done within archiva.xml configuration)
93          saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false);
94  
95          File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
96  
97          File proxiedFile = new File( REPOPATH_PROXIED1, path );
98          assertFileEquals( expectedFile, downloadedFile, proxiedFile );
99          assertNoTempFiles( expectedFile );
100     }
101 
102     public void testOlderTimestampDrivenSnapshotOnFirstRepo()
103         throws Exception
104     {
105         String path = "org/apache/maven/test/get-present-timestamped-snapshot/1.0-SNAPSHOT/get-present-timestamped-snapshot-1.0-SNAPSHOT.jar";
106         setupTestableManagedRepository( path );
107         
108         File expectedFile = new File( managedDefaultDir, path );
109         File remoteFile = new File( REPOPATH_PROXIED1, path );
110         
111         setManagedNewerThanRemote( expectedFile, remoteFile );
112         
113         ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
114 
115         // Configure Connector (usually done within archiva.xml configuration)
116         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false );
117 
118         // Attempt to download.
119         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
120 
121         // Should not have downloaded as managed is newer than remote.
122         assertNotDownloaded( downloadedFile );
123         assertNoTempFiles( expectedFile );
124     }
125 
126     /**
127      * TODO: Has problems with wagon implementation not preserving timestamp.
128      */
129     /*
130     public void testNewerTimestampDrivenSnapshotOnSecondRepoThanFirstNotPresentAlready()
131         throws Exception
132     {
133         String path = "org/apache/maven/test/get-timestamped-snapshot-in-both/1.0-SNAPSHOT/get-timestamped-snapshot-in-both-1.0-SNAPSHOT.jar";
134         setupTestableManagedRepository( path );
135         
136         File expectedFile = new File( managedDefaultDir, path );
137         ArtifactReference artifact = createArtifactReference( "default", path );
138 
139         expectedFile.delete();
140         assertFalse( expectedFile.exists() );
141 
142         // Create customized proxy / target repository
143         File targetProxyDir = saveTargetedRepositoryConfig( ID_PROXIED1_TARGET, REPOPATH_PROXIED1,
144                                                             REPOPATH_PROXIED1_TARGET, "default" );
145 
146         new File( targetProxyDir, path ).setLastModified( getPastDate().getTime() );
147 
148         // Configure Connector (usually done within archiva.xml configuration)
149         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1_TARGET, ChecksumPolicy.IGNORED, ReleasesPolicy.IGNORED,
150                        SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
151         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.IGNORED, ReleasesPolicy.IGNORED,
152                        SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
153 
154         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
155 
156         // Should have downloaded the content from proxy2, as proxy1 has an old (by file.lastModified check) version.
157         File proxiedFile = new File( REPOPATH_PROXIED2, path );
158         assertFileEquals( expectedFile, downloadedFile, proxiedFile );
159         assertNoTempFiles( expectedFile );
160     } 
161 
162     public void testOlderTimestampDrivenSnapshotOnSecondRepoThanFirstNotPresentAlready()
163         throws Exception
164     {
165         String path = "org/apache/maven/test/get-timestamped-snapshot-in-both/1.0-SNAPSHOT/get-timestamped-snapshot-in-both-1.0-SNAPSHOT.jar";
166         setupTestableManagedRepository( path );
167         
168         File expectedFile = new File( managedDefaultDir, path );
169         ArtifactReference artifact = createArtifactReference( "default", path );
170 
171         expectedFile.delete();
172         assertFalse( expectedFile.exists() );
173 
174         // Create customized proxy / target repository
175         File targetProxyDir = saveTargetedRepositoryConfig( ID_PROXIED2_TARGET, REPOPATH_PROXIED2,
176                                                             REPOPATH_PROXIED2_TARGET, "default" );
177 
178         new File( targetProxyDir, path ).setLastModified( getPastDate().getTime() );
179 
180         // Configure Connector (usually done within archiva.xml configuration)
181         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.IGNORED, ReleasesPolicy.IGNORED,
182                        SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
183         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2_TARGET, ChecksumPolicy.IGNORED, ReleasesPolicy.IGNORED,
184                        SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
185 
186         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
187 
188         File proxiedFile = new File( REPOPATH_PROXIED1_TARGET, path );
189         assertFileEquals( expectedFile, downloadedFile, proxiedFile );
190         assertNoTempFiles( expectedFile );
191     } */
192 
193     public void testTimestampDrivenSnapshotNotExpired()
194         throws Exception
195     {
196         String path = "org/apache/maven/test/get-present-timestamped-snapshot/1.0-SNAPSHOT/get-present-timestamped-snapshot-1.0-SNAPSHOT.jar";
197         setupTestableManagedRepository( path );
198         
199         File expectedFile = new File( managedDefaultDir, path );
200         ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
201 
202         assertTrue( expectedFile.exists() );
203 
204         File proxiedFile = new File( REPOPATH_PROXIED1, path );
205         proxiedFile.setLastModified( getFutureDate().getTime() );
206 
207         // Configure Connector (usually done within archiva.xml configuration)
208         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false);
209 
210         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
211 
212         assertFileEquals( expectedFile, downloadedFile, proxiedFile );
213         assertNoTempFiles( expectedFile );
214     }
215 
216     public void testTimestampDrivenSnapshotNotUpdated()
217         throws Exception
218     {
219         String path = "org/apache/maven/test/get-present-timestamped-snapshot/1.0-SNAPSHOT/get-present-timestamped-snapshot-1.0-SNAPSHOT.jar";
220         setupTestableManagedRepository( path );
221         
222         File expectedFile = new File( managedDefaultDir, path );
223         File remoteFile = new File( REPOPATH_PROXIED1, path );
224 
225         setManagedNewerThanRemote( expectedFile, remoteFile );
226         long expectedTimestamp = expectedFile.lastModified(); 
227         
228         ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
229 
230         // Configure Connector (usually done within archiva.xml configuration)
231         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false);
232 
233         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
234 
235         assertNotDownloaded( downloadedFile );
236         assertNotModified( expectedFile, expectedTimestamp );
237         assertNoTempFiles( expectedFile );
238     }
239 
240     public void testTimestampDrivenSnapshotNotPresentAlreadyExpiredCacheFailure()
241         throws Exception
242     {
243         String path = "org/apache/maven/test/get-timestamped-snapshot/1.0-SNAPSHOT/get-timestamped-snapshot-1.0-SNAPSHOT.jar";
244         setupTestableManagedRepository( path );
245         
246         File expectedFile = new File( managedDefaultDir, path );
247         ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
248 
249         expectedFile.delete();
250         assertFalse( expectedFile.exists() );
251 
252         // Configure Connector (usually done within archiva.xml configuration)
253         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.IGNORE, ReleasesPolicy.ALWAYS,
254                        SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES , false);
255         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.IGNORE, ReleasesPolicy.ALWAYS,
256                        SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES , false);
257 
258         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
259 
260         File proxiedFile = new File( REPOPATH_PROXIED1, path );
261         assertFileEquals( expectedFile, downloadedFile, proxiedFile );
262         assertNoTempFiles( expectedFile );
263     }
264 
265     public void testMetadataDrivenSnapshotNotPresentAlready()
266         throws Exception
267     {
268         String path = "org/apache/maven/test/get-metadata-snapshot/1.0-SNAPSHOT/get-metadata-snapshot-1.0-20050831.101112-1.jar";
269         setupTestableManagedRepository( path );
270         
271         File expectedFile = new File( managedDefaultDir, path );
272         ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
273 
274         expectedFile.delete();
275         assertFalse( expectedFile.exists() );
276 
277         // Configure Connector (usually done within archiva.xml configuration)
278         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false);
279 
280         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
281 
282         File proxiedFile = new File( REPOPATH_PROXIED1, path );
283         assertFileEquals( expectedFile, downloadedFile, proxiedFile );
284         assertNoTempFiles( expectedFile );
285     }
286 
287     public void testGetMetadataDrivenSnapshotRemoteUpdate()
288         throws Exception
289     {
290         // Metadata driven snapshots (using a full timestamp) are treated like a release. It is the timing of the
291         // updates to the metadata files that triggers which will be downloaded
292 
293         String path = "org/apache/maven/test/get-present-metadata-snapshot/1.0-SNAPSHOT/get-present-metadata-snapshot-1.0-20050831.101112-1.jar";
294         setupTestableManagedRepository( path );
295         
296         File expectedFile = new File( managedDefaultDir, path );
297         ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
298 
299         assertTrue( expectedFile.exists() );
300 
301         expectedFile.setLastModified( getPastDate().getTime() );
302 
303         // Configure Connector (usually done within archiva.xml configuration)
304         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false);
305 
306         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
307 
308         File proxiedFile = new File( REPOPATH_PROXIED1, path );
309         assertFileEquals( expectedFile, downloadedFile, proxiedFile );
310         assertNoTempFiles( expectedFile );
311     }
312 }