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.artifact.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.MetadataResolutionRequest;
30  import org.codehaus.plexus.component.annotations.Component;
31  import org.codehaus.plexus.component.annotations.Requirement;
32  
33  @Component(role = ArtifactMetadataSource.class, hint = "test")
34  public class TestMetadataSource implements ArtifactMetadataSource {
35      @Requirement
36      private ArtifactFactory factory;
37  
38      public ResolutionGroup retrieve(
39              Artifact artifact, ArtifactRepository localRepository, List<ArtifactRepository> remoteRepositories)
40              throws ArtifactMetadataRetrievalException {
41          Set<Artifact> dependencies = new HashSet<>();
42  
43          if ("g".equals(artifact.getArtifactId())) {
44              Artifact a = null;
45              try {
46                  a = factory.createBuildArtifact("org.apache.maven", "h", "1.0", "jar");
47                  dependencies.add(a);
48              } catch (Exception e) {
49                  throw new ArtifactMetadataRetrievalException("Error retrieving metadata", e, a);
50              }
51          }
52  
53          if ("i".equals(artifact.getArtifactId())) {
54              Artifact a = null;
55              try {
56                  a = factory.createBuildArtifact("org.apache.maven", "j", "1.0-SNAPSHOT", "jar");
57                  dependencies.add(a);
58              } catch (Exception e) {
59                  throw new ArtifactMetadataRetrievalException("Error retrieving metadata", e, a);
60              }
61          }
62  
63          return new ResolutionGroup(artifact, dependencies, remoteRepositories);
64      }
65  
66      public List<ArtifactVersion> retrieveAvailableVersions(
67              Artifact artifact, ArtifactRepository localRepository, List<ArtifactRepository> remoteRepositories)
68              throws ArtifactMetadataRetrievalException {
69          throw new UnsupportedOperationException("Cannot get available versions in this test case");
70      }
71  
72      public List<ArtifactVersion> retrieveAvailableVersionsFromDeploymentRepository(
73              Artifact artifact, ArtifactRepository localRepository, ArtifactRepository remoteRepository)
74              throws ArtifactMetadataRetrievalException {
75          throw new UnsupportedOperationException("Cannot get available versions in this test case");
76      }
77  
78      public ResolutionGroup retrieve(MetadataResolutionRequest request) throws ArtifactMetadataRetrievalException {
79          return retrieve(request.getArtifact(), request.getLocalRepository(), request.getRemoteRepositories());
80      }
81  
82      public List<ArtifactVersion> retrieveAvailableVersions(MetadataResolutionRequest request)
83              throws ArtifactMetadataRetrievalException {
84          return retrieveAvailableVersions(
85                  request.getArtifact(), request.getLocalRepository(), request.getRemoteRepositories());
86      }
87  }