View Javadoc
1   package org.eclipse.aether.internal.test.util;
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.io.File;
23  import java.io.IOException;
24  import java.util.HashSet;
25  import java.util.Set;
26  
27  import org.eclipse.aether.RepositorySystemSession;
28  import org.eclipse.aether.artifact.Artifact;
29  import org.eclipse.aether.metadata.Metadata;
30  import org.eclipse.aether.repository.LocalArtifactRegistration;
31  import org.eclipse.aether.repository.LocalArtifactRequest;
32  import org.eclipse.aether.repository.LocalArtifactResult;
33  import org.eclipse.aether.repository.LocalMetadataRegistration;
34  import org.eclipse.aether.repository.LocalMetadataRequest;
35  import org.eclipse.aether.repository.LocalMetadataResult;
36  import org.eclipse.aether.repository.LocalRepository;
37  import org.eclipse.aether.repository.LocalRepositoryManager;
38  import org.eclipse.aether.repository.RemoteRepository;
39  
40  /**
41   * A simplistic local repository manager that uses a temporary base directory.
42   */
43  public class TestLocalRepositoryManager
44      implements LocalRepositoryManager
45  {
46  
47      private LocalRepository localRepository;
48  
49      private Set<Artifact> unavailableArtifacts = new HashSet<Artifact>();
50  
51      private Set<Artifact> artifactRegistrations = new HashSet<Artifact>();
52  
53      private Set<Metadata> metadataRegistrations = new HashSet<Metadata>();
54  
55      public TestLocalRepositoryManager()
56      {
57          try
58          {
59              localRepository = new LocalRepository( TestFileUtils.createTempDir( "test-local-repo" ) );
60          }
61          catch ( IOException e )
62          {
63              throw new IllegalStateException( e );
64          }
65      }
66  
67      public LocalRepository getRepository()
68      {
69          return localRepository;
70      }
71  
72      public String getPathForLocalArtifact( Artifact artifact )
73      {
74          String artifactId = artifact.getArtifactId();
75          String groupId = artifact.getGroupId();
76          String extension = artifact.getExtension();
77          String version = artifact.getVersion();
78          String classifier = artifact.getClassifier();
79  
80          String path =
81              String.format( "%s/%s/%s/%s-%s-%s%s.%s", groupId, artifactId, version, groupId, artifactId, version,
82                             classifier, extension );
83          return path;
84      }
85  
86      public String getPathForRemoteArtifact( Artifact artifact, RemoteRepository repository, String context )
87      {
88          return getPathForLocalArtifact( artifact );
89      }
90  
91      public String getPathForLocalMetadata( Metadata metadata )
92      {
93          String artifactId = metadata.getArtifactId();
94          String groupId = metadata.getGroupId();
95          String version = metadata.getVersion();
96          return String.format( "%s/%s/%s/%s-%s-%s.xml", groupId, artifactId, version, groupId, artifactId, version );
97      }
98  
99      public String getPathForRemoteMetadata( Metadata metadata, RemoteRepository repository, String context )
100     {
101         return getPathForLocalMetadata( metadata );
102     }
103 
104     public LocalArtifactResult find( RepositorySystemSession session, LocalArtifactRequest request )
105     {
106         Artifact artifact = request.getArtifact();
107 
108         LocalArtifactResult result = new LocalArtifactResult( request );
109         File file = new File( localRepository.getBasedir(), getPathForLocalArtifact( artifact ) );
110         result.setFile( file.isFile() ? file : null );
111         result.setAvailable( file.isFile() && !unavailableArtifacts.contains( artifact ) );
112 
113         return result;
114     }
115 
116     public void add( RepositorySystemSession session, LocalArtifactRegistration request )
117     {
118         artifactRegistrations.add( request.getArtifact() );
119     }
120 
121     public LocalMetadataResult find( RepositorySystemSession session, LocalMetadataRequest request )
122     {
123         Metadata metadata = request.getMetadata();
124 
125         LocalMetadataResult result = new LocalMetadataResult( request );
126         File file = new File( localRepository.getBasedir(), getPathForLocalMetadata( metadata ) );
127         result.setFile( file.isFile() ? file : null );
128 
129         return result;
130     }
131 
132     public void add( RepositorySystemSession session, LocalMetadataRegistration request )
133     {
134         metadataRegistrations.add( request.getMetadata() );
135     }
136 
137     public Set<Artifact> getArtifactRegistration()
138     {
139         return artifactRegistrations;
140     }
141 
142     public Set<Metadata> getMetadataRegistration()
143     {
144         return metadataRegistrations;
145     }
146 
147     public void setArtifactAvailability( Artifact artifact, boolean available )
148     {
149         if ( available )
150         {
151             unavailableArtifacts.remove( artifact );
152         }
153         else
154         {
155             unavailableArtifacts.add( artifact );
156         }
157     }
158 
159 }