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<>();
50  
51      private Set<Artifact> artifactRegistrations = new HashSet<>();
52  
53      private Set<Metadata> metadataRegistrations = new HashSet<>();
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          return String.format( "%s/%s/%s/%s-%s-%s%s.%s", groupId, artifactId, version, groupId, artifactId, version,
81                         classifier, extension );
82      }
83  
84      public String getPathForRemoteArtifact( Artifact artifact, RemoteRepository repository, String context )
85      {
86          return getPathForLocalArtifact( artifact );
87      }
88  
89      public String getPathForLocalMetadata( Metadata metadata )
90      {
91          String artifactId = metadata.getArtifactId();
92          String groupId = metadata.getGroupId();
93          String version = metadata.getVersion();
94          return String.format( "%s/%s/%s/%s-%s-%s.xml", groupId, artifactId, version, groupId, artifactId, version );
95      }
96  
97      public String getPathForRemoteMetadata( Metadata metadata, RemoteRepository repository, String context )
98      {
99          return getPathForLocalMetadata( metadata );
100     }
101 
102     public LocalArtifactResult find( RepositorySystemSession session, LocalArtifactRequest request )
103     {
104         Artifact artifact = request.getArtifact();
105 
106         LocalArtifactResult result = new LocalArtifactResult( request );
107         File file = new File( localRepository.getBasedir(), getPathForLocalArtifact( artifact ) );
108         result.setFile( file.isFile() ? file : null );
109         result.setAvailable( file.isFile() && !unavailableArtifacts.contains( artifact ) );
110 
111         return result;
112     }
113 
114     public void add( RepositorySystemSession session, LocalArtifactRegistration request )
115     {
116         artifactRegistrations.add( request.getArtifact() );
117     }
118 
119     public LocalMetadataResult find( RepositorySystemSession session, LocalMetadataRequest request )
120     {
121         Metadata metadata = request.getMetadata();
122 
123         LocalMetadataResult result = new LocalMetadataResult( request );
124         File file = new File( localRepository.getBasedir(), getPathForLocalMetadata( metadata ) );
125         result.setFile( file.isFile() ? file : null );
126 
127         return result;
128     }
129 
130     public void add( RepositorySystemSession session, LocalMetadataRegistration request )
131     {
132         metadataRegistrations.add( request.getMetadata() );
133     }
134 
135     public Set<Artifact> getArtifactRegistration()
136     {
137         return artifactRegistrations;
138     }
139 
140     public Set<Metadata> getMetadataRegistration()
141     {
142         return metadataRegistrations;
143     }
144 
145     public void setArtifactAvailability( Artifact artifact, boolean available )
146     {
147         if ( available )
148         {
149             unavailableArtifacts.remove( artifact );
150         }
151         else
152         {
153             unavailableArtifacts.add( artifact );
154         }
155     }
156 
157 }