View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.repository.metadata;
20  
21  import java.util.HashSet;
22  import java.util.List;
23  import java.util.Set;
24  
25  import org.apache.maven.artifact.Artifact;
26  import org.apache.maven.artifact.factory.ArtifactFactory;
27  import org.apache.maven.artifact.repository.ArtifactRepository;
28  import org.apache.maven.artifact.versioning.ArtifactVersion;
29  import org.apache.maven.repository.legacy.metadata.ArtifactMetadataRetrievalException;
30  import org.apache.maven.repository.legacy.metadata.ArtifactMetadataSource;
31  import org.apache.maven.repository.legacy.metadata.MetadataResolutionRequest;
32  import org.apache.maven.repository.legacy.metadata.ResolutionGroup;
33  import org.codehaus.plexus.component.annotations.Component;
34  import org.codehaus.plexus.component.annotations.Requirement;
35  
36  @Component(role = ArtifactMetadataSource.class)
37  public class TestMetadataSource implements ArtifactMetadataSource {
38      @Requirement
39      private ArtifactFactory factory;
40  
41      public ResolutionGroup retrieve(
42              Artifact artifact, ArtifactRepository localRepository, List<ArtifactRepository> remoteRepositories)
43              throws ArtifactMetadataRetrievalException {
44          Set<Artifact> dependencies = new HashSet<>();
45  
46          if ("g".equals(artifact.getArtifactId())) {
47              Artifact a = null;
48              try {
49                  a = factory.createBuildArtifact("org.apache.maven", "h", "1.0", "jar");
50                  dependencies.add(a);
51              } catch (Exception e) {
52                  throw new ArtifactMetadataRetrievalException("Error retrieving metadata", e, a);
53              }
54          }
55  
56          if ("i".equals(artifact.getArtifactId())) {
57              Artifact a = null;
58              try {
59                  a = factory.createBuildArtifact("org.apache.maven", "j", "1.0-SNAPSHOT", "jar");
60                  dependencies.add(a);
61              } catch (Exception e) {
62                  throw new ArtifactMetadataRetrievalException("Error retrieving metadata", e, a);
63              }
64          }
65  
66          return new ResolutionGroup(artifact, dependencies, remoteRepositories);
67      }
68  
69      public List<ArtifactVersion> retrieveAvailableVersions(
70              Artifact artifact, ArtifactRepository localRepository, List<ArtifactRepository> remoteRepositories)
71              throws ArtifactMetadataRetrievalException {
72          throw new UnsupportedOperationException("Cannot get available versions in this test case");
73      }
74  
75      public List<ArtifactVersion> retrieveAvailableVersionsFromDeploymentRepository(
76              Artifact artifact, ArtifactRepository localRepository, ArtifactRepository remoteRepository)
77              throws ArtifactMetadataRetrievalException {
78          throw new UnsupportedOperationException("Cannot get available versions in this test case");
79      }
80  
81      public ResolutionGroup retrieve(MetadataResolutionRequest request) throws ArtifactMetadataRetrievalException {
82          return retrieve(request.getArtifact(), request.getLocalRepository(), request.getRemoteRepositories());
83      }
84  }