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