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