View Javadoc

1   package org.apache.maven.shared.dependency.tree;
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 java.util.Collections;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Set;
27  
28  import org.apache.maven.artifact.Artifact;
29  import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
30  import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
31  import org.apache.maven.artifact.metadata.ResolutionGroup;
32  import org.apache.maven.artifact.repository.ArtifactRepository;
33  import org.apache.maven.artifact.versioning.ArtifactVersion;
34  
35  /**
36   * Provides a stub to simulate an artifact metadata source.
37   * 
38   * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
39   * @version $Id: ArtifactMetadataSourceStub.java 1100703 2011-05-08 08:27:33Z hboutemy $
40   */
41  public class ArtifactMetadataSourceStub
42      implements ArtifactMetadataSource
43  {
44      // TODO: move to maven-plugin-testing-harness?
45      
46      // fields -----------------------------------------------------------------
47      
48      /**
49       * Map of resolution groups by artifact.
50       */
51      private final Map<Artifact, ResolutionGroup> resolutionGroupsByArtifact;
52  
53      /**
54       * Map of available versions by artifact.
55       */
56      private final Map<Artifact, List<ArtifactVersion>> availableVersionsByArtifact;
57  
58      // constructors -----------------------------------------------------------
59      
60      /**
61       * Creates a new artifact metadata source stub.
62       */
63      public ArtifactMetadataSourceStub()
64      {
65          resolutionGroupsByArtifact = new HashMap<Artifact, ResolutionGroup>();
66          availableVersionsByArtifact = new HashMap<Artifact, List<ArtifactVersion>>();
67      }
68  
69      // ArtifactMetadataSource methods -----------------------------------------
70  
71      /**
72       * {@inheritDoc}
73       */
74      public ResolutionGroup retrieve( Artifact artifact, ArtifactRepository localRepository, List remoteRepositories )
75          throws ArtifactMetadataRetrievalException
76      {
77          ResolutionGroup resolution = resolutionGroupsByArtifact.get( artifact );
78          
79          // if we return null then the artifact gets excluded in DefaultArtifactCollector
80          if ( resolution == null )
81          {
82              resolution = new ResolutionGroup( artifact, Collections.EMPTY_SET, Collections.EMPTY_LIST );
83          }
84          
85          return resolution;
86      }
87  
88      /**
89       * {@inheritDoc}
90       */
91      public List retrieveAvailableVersions( Artifact artifact, ArtifactRepository localRepository,
92                                             List remoteRepositories )
93          throws ArtifactMetadataRetrievalException
94      {
95          List<ArtifactVersion> availableVersions = availableVersionsByArtifact.get( artifact );
96  
97          return availableVersions != null ? availableVersions : Collections.EMPTY_LIST;
98      }
99  
100     // public methods ---------------------------------------------------------
101     
102     /**
103      * Adds the specified dependency artifacts for the specified artifact to this artifact metadata source stub.
104      * 
105      * @param artifact
106      *            the artifact to add metadata to
107      * @param dependencyArtifacts
108      *            the set of artifacts to register as dependencies of the specified artifact
109      */
110     public void addArtifactMetadata( Artifact artifact, Set<Artifact> dependencyArtifacts )
111     {
112         ResolutionGroup resolution = new ResolutionGroup( artifact, dependencyArtifacts, Collections.EMPTY_LIST );
113 
114         resolutionGroupsByArtifact.put( artifact, resolution );
115     }
116 
117     /**
118      * Adds versions for the specified artifact to this artifact metadata source stub.
119      * 
120      * @param artifact
121      *            the artifact to add metadata to
122      * @param versions
123      *            the list of versions to register as available for the specified artifact
124      */
125     public void addAvailableVersions( Artifact artifact, List<ArtifactVersion> versions )
126     {
127         availableVersionsByArtifact.put( artifact, versions );
128     }
129 
130     public Artifact retrieveRelocatedArtifact( Artifact artifact, ArtifactRepository localRepository,
131                                                List remoteRepositories )
132         throws ArtifactMetadataRetrievalException
133     {
134         return artifact;
135     }
136 }