1 package org.apache.maven.archiva.proxy;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.maven.archiva.common.utils.VersionUtil;
24 import org.apache.maven.archiva.model.ArchivaRepositoryMetadata;
25 import org.apache.maven.archiva.model.ProjectReference;
26 import org.apache.maven.archiva.model.SnapshotVersion;
27 import org.apache.maven.archiva.model.VersionedReference;
28 import org.apache.maven.archiva.policies.CachedFailuresPolicy;
29 import org.apache.maven.archiva.policies.ChecksumPolicy;
30 import org.apache.maven.archiva.policies.ReleasesPolicy;
31 import org.apache.maven.archiva.policies.SnapshotsPolicy;
32 import org.apache.maven.archiva.repository.metadata.MetadataTools;
33 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataException;
34 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataReader;
35 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataWriter;
36 import org.apache.maven.wagon.TransferFailedException;
37 import org.custommonkey.xmlunit.DetailedDiff;
38 import org.custommonkey.xmlunit.Diff;
41 import java.io.StringWriter;
42 import java.util.Arrays;
45 * MetadataTransferTest - Tests the various fetching / merging concepts surrounding the maven-metadata.xml files
46 * present in the repository.
48 * Test Case Naming is as follows.
51 * public void testGet[Release|Snapshot|Project]Metadata[Not]Proxied[Not|On]Local[Not|On|Multiple]Remote
55 * Which should leave the following matrix of test cases.
57 * Metadata | Proxied | Local | Remote
58 * ----------+----------+-------+---------
59 * Release | Not | Not | n/a (1)
60 * Release | Not | On | n/a (1)
61 * Release | | Not | Not
62 * Release | | Not | On
63 * Release | | Not | Multiple
64 * Release | | On | Not
66 * Release | | On | Multiple
67 * Snapshot | Not | Not | n/a (1)
68 * Snapshot | Not | On | n/a (1)
69 * Snapshot | | Not | Not
70 * Snapshot | | Not | On
71 * Snapshot | | Not | Multiple
72 * Snapshot | | On | Not
73 * Snapshot | | On | On
74 * Snapshot | | On | Multiple
75 * Project | Not | Not | n/a (1)
76 * Project | Not | On | n/a (1)
77 * Project | | Not | Not
78 * Project | | Not | On
79 * Project | | Not | Multiple
80 * Project | | On | Not
82 * Project | | On | Multiple
84 * (1) If it isn't proxied, no point in having a remote.
87 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
90 public class MetadataTransferTest
91 extends AbstractProxyTestCase
96 private MetadataTools metadataTools;
98 // TODO: same test for other fetch* methods
99 public void testFetchFromTwoProxiesWhenFirstConnectionFails()
102 // Project metadata that does not exist locally, but has multiple versions in remote repos
103 String requestedResource = "org/apache/maven/test/get-default-layout/maven-metadata.xml";
104 setupTestableManagedRepository( requestedResource );
106 saveRemoteRepositoryConfig( "badproxied1", "Bad Proxied 1", "test://bad.machine.com/repo/", "default" );
108 // Configure Connector (usually done within archiva.xml configuration)
109 saveConnector( ID_DEFAULT_MANAGED, "badproxied1", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
110 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO );
111 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
112 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO );
114 assertResourceNotFound( requestedResource );
115 assertNoRepoMetadata( "badproxied1", requestedResource );
116 assertNoRepoMetadata( ID_PROXIED2, requestedResource );
118 // ensure that a hard failure in the first proxy connector is skipped and the second repository checked
119 File expectedFile = new File( managedDefaultDir.getAbsoluteFile(),
120 metadataTools.getRepositorySpecificName( "badproxied1", requestedResource ) );
121 wagonMock.get( requestedResource, new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ) );
122 wagonMockControl.setThrowable( new TransferFailedException( "can't connect" ) );
124 wagonMockControl.replay();
126 assertFetchProject( requestedResource );
128 wagonMockControl.verify();
130 assertProjectMetadataContents( requestedResource, new String[] { "1.0.1" }, "1.0.1", "1.0.1" );
131 assertNoRepoMetadata( "badproxied1", requestedResource );
132 assertRepoProjectMetadata( ID_PROXIED2, requestedResource, new String[] { "1.0.1" } );
136 * Attempt to get the project metadata for non-existant artifact.
138 * Expected result: the maven-metadata.xml file is not created on the managed repository, nor returned
139 * to the requesting client.
141 public void testGetProjectMetadataNotProxiedNotLocal()
144 // The artifactId "get-default-metadata-nonexistant" does not exist (intentionally).
145 String requestedResource = "org/apache/maven/test/get-default-metadata-nonexistant/maven-metadata.xml";
146 setupTestableManagedRepository( requestedResource );
148 assertResourceNotFound( requestedResource );
150 // No proxy setup, nothing fetched, failure expected.
151 assertFetchProjectFailed( requestedResource );
153 // No local artifactId, and no fetch, should equal no metadata file downloaded / created / updated.
154 assertResourceNotFound( requestedResource );
157 public void testGetProjectMetadataNotProxiedOnLocal()
160 // Project metadata that exists and has multiple versions
161 String requestedResource = "org/apache/maven/test/get-project-metadata/maven-metadata.xml";
162 setupTestableManagedRepository( requestedResource );
164 assertResourceExists( requestedResource );
166 // No proxy setup, nothing fetched from remote, but local exists.
167 assertFetchProject( requestedResource );
169 // Nothing fetched. Should only contain contents of what is in the repository.
170 // A metadata update is not performed in this use case. Local metadata content is only
171 // updated via the metadata updater consumer.
172 assertProjectMetadataContents( requestedResource, new String[] { "1.0" }, null, null );
175 public void testGetProjectMetadataProxiedNotLocalMultipleRemotes()
178 // Project metadata that does not exist locally, but has multiple versions in remote repos
179 String requestedResource = "org/apache/maven/test/get-default-layout/maven-metadata.xml";
180 setupTestableManagedRepository( requestedResource );
182 // Configure Connector (usually done within archiva.xml configuration)
183 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
184 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO );
185 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
186 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO );
188 assertResourceNotFound( requestedResource );
189 assertNoRepoMetadata( ID_PROXIED1, requestedResource );
190 assertNoRepoMetadata( ID_PROXIED2, requestedResource );
192 // Two proxies setup, metadata fetched from both remotes.
193 assertFetchProject( requestedResource );
195 // Nothing fetched. Should only contain contents of what is in the repository.
196 assertProjectMetadataContents( requestedResource, new String[] { "1.0", "1.0.1" }, "1.0.1", "1.0.1" );
197 assertRepoProjectMetadata( ID_PROXIED1, requestedResource, new String[] { "1.0" } );
198 assertRepoProjectMetadata( ID_PROXIED2, requestedResource, new String[] { "1.0.1" } );
201 public void testGetProjectMetadataProxiedNotLocalNotRemote()
204 // Non-existant project metadata that does not exist locally and doesn't exist on remotes.
205 String requestedResource = "org/apache/maven/test/get-bogus-artifact/maven-metadata.xml";
206 setupTestableManagedRepository( requestedResource );
208 // Configure Connector (usually done within archiva.xml configuration)
209 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
210 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO );
211 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
212 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO );
214 assertResourceNotFound( requestedResource );
215 assertNoRepoMetadata( ID_PROXIED1, requestedResource );
216 assertNoRepoMetadata( ID_PROXIED2, requestedResource );
218 // Two proxies setup, nothing fetched from remotes, local does not exist.
219 assertFetchProjectFailed( requestedResource );
221 // Nothing fetched. Nothing should exist.
222 assertResourceNotFound( requestedResource );
223 assertNoRepoMetadata( ID_PROXIED1, requestedResource );
224 assertNoRepoMetadata( ID_PROXIED2, requestedResource );
227 public void testGetProjectMetadataProxiedNotLocalOnRemote()
230 // New project metadata that does not exist locally but exists on remote.
231 String requestedResource = "org/apache/maven/test/get-found-in-proxy/maven-metadata.xml";
232 setupTestableManagedRepository( requestedResource );
234 // Configure Connector (usually done within archiva.xml configuration)
235 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
236 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO );
238 assertResourceNotFound( requestedResource );
239 assertNoRepoMetadata( ID_PROXIED1, requestedResource );
241 // One proxy setup, metadata fetched from remote, local does not exist.
242 assertFetchProject( requestedResource );
244 // Remote fetched. Local created/updated.
245 assertProjectMetadataContents( requestedResource, new String[] { "1.0.5" }, "1.0.5", "1.0.5" );
246 assertRepoProjectMetadata( ID_PROXIED1, requestedResource, new String[] { "1.0.5" } );
249 public void testGetProjectMetadataProxiedOnLocalMultipleRemote()
252 // Project metadata that exist locally, and has multiple versions in remote repos
253 String requestedResource = "org/apache/maven/test/get-on-multiple-repos/maven-metadata.xml";
254 setupTestableManagedRepository( requestedResource );
256 // Configure Connector (usually done within archiva.xml configuration)
257 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
258 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO );
259 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
260 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO );
262 assertProjectMetadataContents( requestedResource, new String[] { "1.0" }, null, null );
263 assertNoRepoMetadata( ID_PROXIED1, requestedResource );
264 assertNoRepoMetadata( ID_PROXIED2, requestedResource );
266 // Two proxies setup, metadata fetched from both remotes.
267 assertFetchProject( requestedResource );
269 // metadata fetched from both repos, and merged with local version.
270 assertProjectMetadataContents( requestedResource, new String[] { "1.0", "1.0.1", "2.0" }, "2.0", "2.0" );
271 assertRepoProjectMetadata( ID_PROXIED1, requestedResource, new String[] { "1.0", "2.0" } );
272 assertRepoProjectMetadata( ID_PROXIED2, requestedResource, new String[] { "1.0", "1.0.1" } );
275 public void testGetProjectMetadataProxiedOnLocalNotRemote()
278 // Project metadata that exist locally, and does not exist in remote repos.
279 String requestedResource = "org/apache/maven/test/get-not-on-remotes/maven-metadata.xml";
280 setupTestableManagedRepository( requestedResource );
282 // Configure Connector (usually done within archiva.xml configuration)
283 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
284 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO );
285 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
286 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO );
288 assertProjectMetadataContents( requestedResource, new String[] { "1.0-beta-2" }, null, null );
289 assertNoRepoMetadata( ID_PROXIED1, requestedResource );
290 assertNoRepoMetadata( ID_PROXIED2, requestedResource );
292 // Two proxies setup, metadata fetch from remotes fail (because they dont exist).
293 assertFetchProject( requestedResource );
295 // metadata not fetched from both repos, and local version exists.
296 // Since there was no updated metadata content from a remote/proxy, a metadata update on
297 // the local file never ran. Local only updates are performed via the metadata updater consumer.
298 assertProjectMetadataContents( requestedResource, new String[] { "1.0-beta-2" }, null, null );
299 assertNoRepoMetadata( ID_PROXIED1, requestedResource );
300 assertNoRepoMetadata( ID_PROXIED2, requestedResource );
303 public void testGetProjectMetadataProxiedOnLocalOnRemote()
306 // Project metadata that exist locally and exists on remote.
307 String requestedResource = "org/apache/maven/test/get-on-local-on-remote/maven-metadata.xml";
308 setupTestableManagedRepository( requestedResource );
310 // Configure Connector (usually done within archiva.xml configuration)
311 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
312 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO );
314 assertProjectMetadataContents( requestedResource, new String[] { "1.0.8", "1.0.22" }, null, null );
315 assertNoRepoMetadata( ID_PROXIED1, requestedResource );
317 // One proxy setup, metadata fetched from remote, local exists.
318 assertFetchProject( requestedResource );
320 // Remote fetched. Local updated.
321 assertProjectMetadataContents( requestedResource, new String[] { "1.0.8", "1.0.22", "2.0" }, "2.0", "2.0" );
322 assertRepoProjectMetadata( ID_PROXIED1, requestedResource, new String[] { "1.0.22", "2.0" } );
326 * A request for a release maven-metadata.xml file that does not exist locally, and the managed
327 * repository has no proxied repositories set up.
329 * Expected result: the maven-metadata.xml file is not created on the managed repository, nor returned
330 * to the requesting client.
332 public void testGetReleaseMetadataNotProxiedNotLocal()
335 // The artifactId "get-default-metadata-nonexistant" does not exist (intentionally).
336 String requestedResource = "org/apache/maven/test/get-default-metadata-nonexistant/1.0/maven-metadata.xml";
337 setupTestableManagedRepository( requestedResource );
339 assertNoMetadata( requestedResource );
341 // No proxy setup, nothing fetched, failure expected.
342 assertFetchVersionedFailed( requestedResource );
344 // No local artifactId, and no fetch, should equal no metadata file downloaded / created / updated.
345 assertNoMetadata( requestedResource );
349 * A request for a maven-metadata.xml file that does exist locally, and the managed
350 * repository has no proxied repositories set up.
352 * Expected result: the maven-metadata.xml file is updated locally, based off of the managed repository
353 * information, and then returned to the client.
355 public void testGetReleaseMetadataNotProxiedOnLocal()
358 String requestedResource = "org/apache/maven/test/get-default-metadata/1.0/maven-metadata.xml";
359 setupTestableManagedRepository( requestedResource );
361 assertResourceExists( requestedResource );
363 assertFetchVersioned( requestedResource );
365 assertReleaseMetadataContents( requestedResource );
369 * A request for a release maven-metadata.xml file that does not exist on the managed repository, but
370 * exists on multiple remote repositories.
372 * Expected result: the maven-metadata.xml file is downloaded from the remote into the repository specific
373 * file location on the managed repository, a merge of the contents to the requested
374 * maven-metadata.xml is performed, and then the merged maven-metadata.xml file is
375 * returned to the client.
377 public void testGetReleaseMetadataProxiedNotLocalMultipleRemotes()
380 String requestedResource = "org/apache/maven/test/get-default-layout/1.0/maven-metadata.xml";
381 setupTestableManagedRepository( requestedResource );
383 // Configure Connector (usually done within archiva.xml configuration)
384 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
385 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO );
386 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
387 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO );
389 assertResourceNotFound( requestedResource );
390 assertNoRepoMetadata( ID_PROXIED1, requestedResource );
391 assertNoRepoMetadata( ID_PROXIED2, requestedResource );
393 assertFetchVersioned( requestedResource );
395 assertReleaseMetadataContents( requestedResource );
396 assertRepoReleaseMetadataContents( ID_PROXIED1, requestedResource );
397 assertRepoReleaseMetadataContents( ID_PROXIED2, requestedResource );
401 * A request for a maven-metadata.xml file that does not exist locally, nor does it exist in a remote
402 * proxied repository.
404 * Expected result: the maven-metadata.xml file is created locally, based off of managed repository
405 * information, and then return to the client.
407 public void testGetReleaseMetadataProxiedNotLocalNotRemote()
410 String requestedResource = "org/apache/maven/test/get-bad-metadata/1.0/maven-metadata.xml";
411 setupTestableManagedRepository( requestedResource );
413 // Configure Connector (usually done within archiva.xml configuration)
414 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
415 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO );
417 assertResourceNotFound( requestedResource );
419 assertFetchProjectFailed( requestedResource );
421 assertResourceNotFound( requestedResource );
422 assertNoRepoMetadata( ID_PROXIED1, requestedResource );
426 * A request for a maven-metadata.xml file that does not exist on the managed repository, but
427 * exists on 1 remote repository.
429 * Expected result: the maven-metadata.xml file is downloaded from the remote into the repository specific
430 * file location on the managed repository, a merge of the contents to the requested
431 * maven-metadata.xml is performed, and then the merged maven-metadata.xml file is
432 * returned to the client.
434 public void testGetReleaseMetadataProxiedNotLocalOnRemote()
437 String requestedResource = "org/apache/maven/test/get-default-layout/1.0/maven-metadata.xml";
438 setupTestableManagedRepository( requestedResource );
440 // Configure Connector (usually done within archiva.xml configuration)
441 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
442 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO );
444 assertNoRepoMetadata( ID_PROXIED1, requestedResource );
446 assertFetchVersioned( requestedResource );
448 assertReleaseMetadataContents( requestedResource );
449 assertRepoReleaseMetadataContents( ID_PROXIED1, requestedResource );
453 * A request for a maven-metadata.xml file that exists in the managed repository, but
454 * not on any remote repository.
456 * Expected result: the maven-metadata.xml file does not exist on the remote proxied repository and
457 * is not downloaded. There is no repository specific metadata file on the managed
458 * repository. The managed repository maven-metadata.xml is returned to the
461 public void testGetReleaseMetadataProxiedOnLocalNotRemote()
464 String requestedResource = "org/apache/maven/test/get-not-on-remotes/1.0-beta-2/maven-metadata.xml";
465 setupTestableManagedRepository( requestedResource );
467 // Configure Connector (usually done within archiva.xml configuration)
468 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
469 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO );
471 assertReleaseMetadataContents( requestedResource );
473 assertFetchVersioned( requestedResource );
475 assertReleaseMetadataContents( requestedResource );
476 assertNoRepoMetadata( ID_PROXIED1, requestedResource );
480 * A request for a maven-metadata.xml file that exists in the managed repository, and on multiple
481 * remote repositories.
483 * Expected result: the maven-metadata.xml file on the remote proxied repository is downloaded
484 * and merged into the contents of the existing managed repository copy of
485 * the maven-metadata.xml file.
487 public void testGetReleaseMetadataProxiedOnLocalMultipleRemote()
490 String requestedResource = "org/apache/maven/test/get-on-multiple-repos/1.0/maven-metadata.xml";
491 setupTestableManagedRepository( requestedResource );
493 // Configure Connector (usually done within archiva.xml configuration)
494 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
495 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO );
496 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
497 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO );
499 assertReleaseMetadataContents( requestedResource );
500 assertNoRepoMetadata( ID_PROXIED1, requestedResource );
501 assertNoRepoMetadata( ID_PROXIED2, requestedResource );
503 assertFetchVersioned( requestedResource );
505 assertReleaseMetadataContents( requestedResource );
506 assertRepoReleaseMetadataContents( ID_PROXIED1, requestedResource );
507 assertRepoReleaseMetadataContents( ID_PROXIED2, requestedResource );
511 * A request for a maven-metadata.xml file that exists in the managed repository, and on one
514 * Expected result: the maven-metadata.xml file on the remote proxied repository is downloaded
515 * and merged into the contents of the existing managed repository copy of
516 * the maven-metadata.xml file.
518 public void testGetReleaseMetadataProxiedOnLocalOnRemote()
521 String requestedResource = "org/apache/maven/test/get-on-local-on-remote/1.0.22/maven-metadata.xml";
522 setupTestableManagedRepository( requestedResource );
524 // Configure Connector (usually done within archiva.xml configuration)
525 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
526 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO );
528 assertReleaseMetadataContents( requestedResource );
529 assertNoRepoMetadata( ID_PROXIED1, requestedResource );
531 assertFetchVersioned( requestedResource );
533 assertReleaseMetadataContents( requestedResource );
534 assertRepoReleaseMetadataContents( ID_PROXIED1, requestedResource );
537 public void testGetSnapshotMetadataNotProxiedNotLocal()
540 // The artifactId "get-default-metadata-nonexistant" does not exist (intentionally).
541 String requestedResource = "org/apache/maven/test/get-default-metadata-nonexistant/1.0-SNAPSHOT/maven-metadata.xml";
542 setupTestableManagedRepository( requestedResource );
544 assertNoMetadata( requestedResource );
546 // No proxy setup, nothing fetched, no local file, failure expected.
547 assertFetchVersionedFailed( requestedResource );
549 // No local artifactId, and no fetch, should equal no metadata file downloaded / created / updated.
550 assertNoMetadata( requestedResource );
553 public void testGetSnapshotMetadataNotProxiedOnLocal()
556 // The artifactId exists locally (but not on a remote repo)
557 String requestedResource = "org/apache/maven/test/get-snapshot-on-local-not-remote/2.0-alpha-2-SNAPSHOT/maven-metadata.xml";
558 setupTestableManagedRepository( requestedResource );
560 assertResourceExists( requestedResource );
562 // No proxy setup, nothing fetched from remote, local file exists, fetch should succeed.
563 assertFetchVersioned( requestedResource );
565 // Local metadata exists, should be updated to reflect the latest release.
566 assertSnapshotMetadataContents( requestedResource, "20070821", "220304", 2 );
569 public void testGetSnapshotMetadataProxiedNotLocalMultipleRemotes()
572 String requestedResource = "org/apache/maven/test/get-timestamped-snapshot-in-both/1.0-SNAPSHOT/maven-metadata.xml";
573 setupTestableManagedRepository( requestedResource );
575 // Configure Connector (usually done within archiva.xml configuration)
576 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
577 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO );
578 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
579 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO );
581 assertResourceNotFound( requestedResource );
582 assertNoRepoMetadata( ID_PROXIED1, requestedResource );
583 assertNoRepoMetadata( ID_PROXIED2, requestedResource );
585 // Proxying 2 repos, both have content, local file updated.
586 assertFetchVersioned( requestedResource );
588 assertSnapshotMetadataContents( requestedResource, "20070101", "000103", 2 );
589 assertRepoSnapshotMetadataContents( ID_PROXIED1, requestedResource, "20061227", "112101", 2 );
590 assertRepoSnapshotMetadataContents( ID_PROXIED2, requestedResource, "20070101", "000103", 2 );
593 public void testGetSnapshotMetadataProxiedNotLocalNotRemote()
596 // The artifactId "get-default-metadata-nonexistant" does not exist (intentionally).
597 String requestedResource = "org/apache/maven/test/get-default-metadata-nonexistant/1.0-SNAPSHOT/maven-metadata.xml";
598 setupTestableManagedRepository( requestedResource );
600 // Configure Connector (usually done within archiva.xml configuration)
601 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
602 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO );
604 assertNoMetadata( requestedResource );
606 // One proxy setup, nothing fetched, no local file, failure expected.
607 assertFetchVersionedFailed( requestedResource );
609 // No local artifactId, and no fetch, should equal no metadata file downloaded / created / updated.
610 assertNoMetadata( requestedResource );
611 assertNoRepoMetadata( ID_PROXIED1, requestedResource );
614 public void testGetSnapshotMetadataProxiedNotLocalOnRemote()
617 // Artifact exists only in the proxied1 location.
618 String requestedResource = "org/apache/maven/test/get-metadata-snapshot/1.0-SNAPSHOT/maven-metadata.xml";
619 setupTestableManagedRepository( requestedResource );
621 // Configure Connector (usually done within archiva.xml configuration)
622 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
623 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO );
625 assertResourceNotFound( requestedResource );
627 // One proxy setup, one metadata fetched, local file created/updated.
628 assertFetchVersioned( requestedResource );
630 // Local artifact Id should contain latest (which in this case is from proxied download)
631 assertSnapshotMetadataContents( requestedResource, "20050831", "101112", 1 );
632 assertRepoSnapshotMetadataContents( ID_PROXIED1, requestedResource, "20050831", "101112", 1 );
635 public void testGetSnapshotMetadataProxiedOnLocalMultipleRemote()
638 String requestedResource = "org/apache/maven/test/get-snapshot-popular/2.0-SNAPSHOT/maven-metadata.xml";
639 setupTestableManagedRepository( requestedResource );
641 // Configure Connector (usually done within archiva.xml configuration)
642 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
643 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO );
644 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
645 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO );
647 assertSnapshotMetadataContents( requestedResource, "20070822", "021008", 3 );
648 assertNoRepoMetadata( ID_PROXIED1, requestedResource );
649 assertNoRepoMetadata( ID_PROXIED2, requestedResource );
651 // Proxying 2 repos, both have content, local file updated.
652 assertFetchVersioned( requestedResource );
654 assertSnapshotMetadataContents( requestedResource, "20070823", "212711", 6 );
655 assertRepoSnapshotMetadataContents( ID_PROXIED1, requestedResource, "20070822", "145534", 9 );
656 assertRepoSnapshotMetadataContents( ID_PROXIED2, requestedResource, "20070823", "212711", 6 );
659 public void testGetSnapshotMetadataProxiedOnLocalNotRemote()
662 // The artifactId exists locally (but not on a remote repo)
663 String requestedResource = "org/apache/maven/test/get-snapshot-on-local-not-remote/2.0-alpha-2-SNAPSHOT/maven-metadata.xml";
664 setupTestableManagedRepository( requestedResource );
666 // Configure Connector (usually done within archiva.xml configuration)
667 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
668 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO );
669 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
670 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO );
672 assertResourceExists( requestedResource );
673 assertNoRepoMetadata( ID_PROXIED1, requestedResource );
674 assertNoRepoMetadata( ID_PROXIED2, requestedResource );
676 // two proxies setup, nothing fetched from either remote, local file exists, fetch should succeed.
677 assertFetchVersioned( requestedResource );
679 // Local metadata exists, repo metadatas should not exist, local file updated.
680 assertSnapshotMetadataContents( requestedResource, "20070821", "220304", 2 );
681 assertNoRepoMetadata( ID_PROXIED1, requestedResource );
682 assertNoRepoMetadata( ID_PROXIED2, requestedResource );
685 public void testGetSnapshotMetadataProxiedOnLocalOnRemote()
688 // The artifactId exists locally (but not on a remote repo)
689 String requestedResource = "org/apache/maven/test/get-present-metadata-snapshot/1.0-SNAPSHOT/maven-metadata.xml";
690 setupTestableManagedRepository( requestedResource );
692 // Configure Connector (usually done within archiva.xml configuration)
693 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
694 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO );
696 assertSnapshotMetadataContents( requestedResource, "20050831", "101112", 1 );
697 assertNoRepoMetadata( ID_PROXIED1, requestedResource );
699 // two proxies setup, nothing fetched from either remote, local file exists, fetch should succeed.
700 assertFetchVersioned( requestedResource );
702 // Local metadata exists, repo metadata exists, local file updated.
703 assertSnapshotMetadataContents( requestedResource, "20050831", "101112", 1 );
704 assertRepoSnapshotMetadataContents( ID_PROXIED1, requestedResource, "20050831", "101112", 1 );
708 * Transfer the metadata file.
710 * @param requestedResource the requested resource
713 private void assertFetchProject( String requestedResource )
716 File expectedFile = new File( managedDefaultDir, requestedResource );
718 ProjectReference metadata = createProjectReference( requestedResource );
720 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, metadata );
722 assertNotNull( "Should have downloaded a file.", downloadedFile );
723 assertNoTempFiles( expectedFile );
726 private ProjectReference createProjectReference( String path )
727 throws RepositoryMetadataException
729 return metadataTools.toProjectReference( path );
733 * Transfer the metadata file, not expected to succeed.
735 * @param requestedResource the requested resource
738 private void assertFetchProjectFailed( String requestedResource )
741 File expectedFile = new File( managedDefaultDir, requestedResource );
742 ProjectReference metadata = createProjectReference( requestedResource );
744 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, metadata );
746 assertNull( downloadedFile );
747 assertNoTempFiles( expectedFile );
751 * Transfer the metadata file.
753 * @param requestedResource the requested resource
756 private void assertFetchVersioned( String requestedResource )
759 File expectedFile = new File( managedDefaultDir, requestedResource );
761 VersionedReference metadata = createVersionedReference( requestedResource );
763 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, metadata );
765 assertNotNull( "Should have downloaded a file.", downloadedFile );
766 assertNoTempFiles( expectedFile );
769 private VersionedReference createVersionedReference( String path )
770 throws RepositoryMetadataException
772 return metadataTools.toVersionedReference( path );
776 * Transfer the metadata file, not expected to succeed.
778 * @param requestedResource the requested resource
781 private void assertFetchVersionedFailed( String requestedResource )
784 File expectedFile = new File( managedDefaultDir, requestedResource );
785 VersionedReference metadata = createVersionedReference( requestedResource );
787 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, metadata );
789 assertNull( downloadedFile );
790 assertNoTempFiles( expectedFile );
794 * Test for the existance of the requestedResource in the default managed repository.
796 * @param requestedResource the requested resource
799 private void assertResourceExists( String requestedResource )
802 File actualFile = new File( managedDefaultDir, requestedResource );
803 assertTrue( "Resource should exist: " + requestedResource, actualFile.exists() );
806 private void assertMetadataEquals( String expectedMetadataXml, File actualFile )
809 assertNotNull( "Actual File should not be null.", actualFile );
811 assertTrue( "Actual file exists.", actualFile.exists() );
813 StringWriter actualContents = new StringWriter();
814 ArchivaRepositoryMetadata metadata = RepositoryMetadataReader.read( actualFile );
815 RepositoryMetadataWriter.write( metadata, actualContents );
817 DetailedDiff detailedDiff = new DetailedDiff( new Diff( expectedMetadataXml, actualContents.toString() ) );
818 if ( !detailedDiff.similar() )
820 assertEquals( expectedMetadataXml, actualContents );
823 // assertEquals( "Check file contents.", expectedMetadataXml, actualContents );
827 * Ensures that the requested resource is not present in the managed repository.
829 * @param requestedResource the requested resource
832 private void assertNoMetadata( String requestedResource )
835 File expectedFile = new File( managedDefaultDir, requestedResource );
836 assertFalse( "metadata should not exist: " + expectedFile, expectedFile.exists() );
840 * Ensures that the proxied repository specific maven metadata file does NOT exist in the
841 * managed repository.
842 * @param proxiedRepoId the proxied repository id to validate with.
843 * @param requestedResource the resource requested.
845 private void assertNoRepoMetadata( String proxiedRepoId, String requestedResource )
847 String proxiedFile = metadataTools.getRepositorySpecificName( proxiedRepoId, requestedResource );
849 File actualFile = new File( managedDefaultDir, proxiedFile );
850 assertFalse( "Repo specific metadata should not exist: " + actualFile, actualFile.exists() );
854 * Test for the existance of the requestedResource in the default managed repository, and if it exists,
855 * does it contain the specified list of expected versions?
857 * @param requestedResource the requested resource
860 private void assertProjectMetadataContents( String requestedResource, String expectedVersions[],
861 String latestVersion, String releaseVersion )
864 File actualFile = new File( managedDefaultDir, requestedResource );
865 assertTrue( actualFile.exists() );
867 ProjectReference metadata = createProjectReference( requestedResource );
869 // Build expected metadata XML
870 StringWriter expectedMetadataXml = new StringWriter();
871 ArchivaRepositoryMetadata m = new ArchivaRepositoryMetadata();
872 m.setGroupId( metadata.getGroupId() );
873 m.setArtifactId( metadata.getArtifactId() );
874 m.setLatestVersion( latestVersion );
875 m.setReleasedVersion( releaseVersion );
877 if ( expectedVersions != null )
879 m.getAvailableVersions().addAll( Arrays.asList( expectedVersions ) );
882 m.setModelEncoding( null );
883 RepositoryMetadataWriter.write( m, expectedMetadataXml );
885 // Compare the file to the actual contents.
886 assertMetadataEquals( expectedMetadataXml.toString(), actualFile );
890 * Test for the existance of the requestedResource in the default managed repository, and if it exists,
891 * does it contain the expected release maven-metadata.xml contents?
893 * @param requestedResource the requested resource
896 private void assertReleaseMetadataContents( String requestedResource )
899 File actualFile = new File( managedDefaultDir, requestedResource );
900 assertTrue( "Release Metadata should exist: " + requestedResource, actualFile.exists() );
902 VersionedReference metadata = createVersionedReference( requestedResource );
904 // Build expected metadata XML
905 StringWriter expectedMetadataXml = new StringWriter();
906 ArchivaRepositoryMetadata m = new ArchivaRepositoryMetadata();
907 m.setGroupId( metadata.getGroupId() );
908 m.setArtifactId( metadata.getArtifactId() );
909 m.setVersion( metadata.getVersion() );
910 m.setModelEncoding( null );
911 RepositoryMetadataWriter.write( m, expectedMetadataXml );
913 // Compare the file to the actual contents.
914 assertMetadataEquals( expectedMetadataXml.toString(), actualFile );
918 * Test for the existance of the snapshot metadata in the default managed repository, and if it exists,
919 * does it contain the expected release maven-metadata.xml contents?
921 * @param requestedResource the requested resource
922 * @param expectedDate the date in "yyyyMMdd" format
923 * @param expectedTime the time in "hhmmss" format
924 * @param expectedBuildnumber the build number
928 private void assertSnapshotMetadataContents( String requestedResource, String expectedDate, String expectedTime,
929 int expectedBuildnumber )
932 File actualFile = new File( managedDefaultDir, requestedResource );
933 assertTrue( "Snapshot Metadata should exist: " + requestedResource, actualFile.exists() );
935 VersionedReference actualMetadata = createVersionedReference( requestedResource );
937 assertSnapshotMetadata( actualFile, actualMetadata, expectedDate, expectedTime, expectedBuildnumber );
941 * Test for the existance of the proxied repository specific snapshot metadata in the default managed
942 * repository, and if it exists, does it contain the expected release maven-metadata.xml contents?
944 * @param proxiedRepoId the repository id of the proxied repository.
945 * @param requestedResource the requested resource
946 * @param expectedDate the date in "yyyyMMdd" format
947 * @param expectedTime the time in "hhmmss" format
948 * @param expectedBuildnumber the build number
952 private void assertRepoSnapshotMetadataContents( String proxiedRepoId, String requestedResource,
953 String expectedDate, String expectedTime, int expectedBuildnumber )
956 String proxiedFile = metadataTools.getRepositorySpecificName( proxiedRepoId, requestedResource );
958 File actualFile = new File( managedDefaultDir, proxiedFile );
959 assertTrue( "Repo Specific Snapshot Metadata should exist: " + requestedResource, actualFile.exists() );
961 VersionedReference actualMetadata = createVersionedReference( requestedResource );
963 assertSnapshotMetadata( actualFile, actualMetadata, expectedDate, expectedTime, expectedBuildnumber );
966 private void assertSnapshotMetadata( File actualFile, VersionedReference actualMetadata, String expectedDate,
967 String expectedTime, int expectedBuildnumber )
968 throws RepositoryMetadataException, Exception
970 // Build expected metadata XML
971 StringWriter expectedMetadataXml = new StringWriter();
972 ArchivaRepositoryMetadata m = new ArchivaRepositoryMetadata();
973 m.setGroupId( actualMetadata.getGroupId() );
974 m.setArtifactId( actualMetadata.getArtifactId() );
975 m.setVersion( VersionUtil.getBaseVersion( actualMetadata.getVersion() ) );
977 m.setSnapshotVersion( new SnapshotVersion() );
979 if ( StringUtils.isNotBlank( expectedDate ) && StringUtils.isNotBlank( expectedTime ) )
981 m.getSnapshotVersion().setTimestamp( expectedDate + "." + expectedTime );
984 m.getSnapshotVersion().setBuildNumber( expectedBuildnumber );
986 m.setLastUpdated( expectedDate + expectedTime );
988 m.setModelEncoding( null );
989 RepositoryMetadataWriter.write( m, expectedMetadataXml );
991 // Compare the file to the actual contents.
992 assertMetadataEquals( expectedMetadataXml.toString(), actualFile );
996 * Ensures that the repository specific maven metadata file exists, and contains the appropriate
997 * list of expected versions within.
998 * @param proxiedRepoId
999 * @param requestedResource
1000 * @param expectedProxyVersions
1002 private void assertRepoProjectMetadata( String proxiedRepoId, String requestedResource,
1003 String[] expectedProxyVersions )
1006 String proxiedFile = metadataTools.getRepositorySpecificName( proxiedRepoId, requestedResource );
1008 File actualFile = new File( managedDefaultDir, proxiedFile );
1009 assertTrue( actualFile.exists() );
1011 ProjectReference metadata = createProjectReference( requestedResource );
1013 // Build expected metadata XML
1014 StringWriter expectedMetadataXml = new StringWriter();
1015 ArchivaRepositoryMetadata m = new ArchivaRepositoryMetadata();
1016 m.setGroupId( metadata.getGroupId() );
1017 m.setArtifactId( metadata.getArtifactId() );
1019 if ( expectedProxyVersions != null )
1021 m.getAvailableVersions().addAll( Arrays.asList( expectedProxyVersions ) );
1024 m.setModelEncoding( null );
1025 RepositoryMetadataWriter.write( m, expectedMetadataXml );
1027 // Compare the file to the actual contents.
1028 assertMetadataEquals( expectedMetadataXml.toString(), actualFile );
1032 * Ensures that the repository specific maven metadata file exists, and contains the appropriate
1033 * list of expected versions within.
1035 * @param proxiedRepoId
1036 * @param requestedResource
1038 private void assertRepoReleaseMetadataContents( String proxiedRepoId, String requestedResource )
1041 String proxiedFile = metadataTools.getRepositorySpecificName( proxiedRepoId, requestedResource );
1043 File actualFile = new File( managedDefaultDir, proxiedFile );
1044 assertTrue( "Release metadata for repo should exist: " + actualFile, actualFile.exists() );
1046 VersionedReference metadata = createVersionedReference( requestedResource );
1048 // Build expected metadata XML
1049 StringWriter expectedMetadataXml = new StringWriter();
1050 ArchivaRepositoryMetadata m = new ArchivaRepositoryMetadata();
1051 m.setGroupId( metadata.getGroupId() );
1052 m.setArtifactId( metadata.getArtifactId() );
1053 m.setVersion( metadata.getVersion() );
1054 m.setModelEncoding( null );
1055 RepositoryMetadataWriter.write( m, expectedMetadataXml );
1057 // Compare the file to the actual contents.
1058 assertMetadataEquals( expectedMetadataXml.toString(), actualFile );
1062 * Test for the non-existance of the requestedResource in the default managed repository.
1064 * @param requestedResource the requested resource
1067 private void assertResourceNotFound( String requestedResource )
1070 File actualFile = new File( managedDefaultDir, requestedResource );
1071 assertFalse( "Resource should not exist: " + requestedResource, actualFile.exists() );
1074 protected void setUp()
1079 metadataTools = (MetadataTools) lookup( MetadataTools.class );