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.eclipse.aether.internal.test.util;
20  
21  import java.io.IOException;
22  import java.nio.file.Files;
23  import java.nio.file.Path;
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 implements LocalRepositoryManager {
46  
47      private LocalRepository localRepository;
48  
49      private final Set<Artifact> unavailableArtifacts = new HashSet<>();
50  
51      private final Set<Artifact> artifactRegistrations = new HashSet<>();
52  
53      private final Set<Metadata> metadataRegistrations = new HashSet<>();
54  
55      public TestLocalRepositoryManager() {
56          try {
57              Path tempDirectory = Files.createTempDirectory("test-local-repo");
58              tempDirectory.toFile().deleteOnExit();
59              localRepository = new LocalRepository(tempDirectory);
60          } catch (IOException e) {
61              throw new IllegalStateException(e);
62          }
63      }
64  
65      public LocalRepository getRepository() {
66          return localRepository;
67      }
68  
69      @Deprecated
70      @Override
71      public String getPathForLocalArtifact(Artifact artifact) {
72          requireNonNull(artifact, "artifact cannot be null");
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(
81                  "%s/%s/%s/%s-%s-%s%s.%s",
82                  groupId, artifactId, version, groupId, artifactId, version, classifier, extension);
83      }
84  
85      @Deprecated
86      @Override
87      public String getPathForRemoteArtifact(Artifact artifact, RemoteRepository repository, String context) {
88          requireNonNull(artifact, "artifact cannot be null");
89          requireNonNull(repository, "repository cannot be null");
90  
91          return getPathForLocalArtifact(artifact);
92      }
93  
94      @Deprecated
95      @Override
96      public String getPathForLocalMetadata(Metadata metadata) {
97          requireNonNull(metadata, "metadata cannot be null");
98  
99          String artifactId = metadata.getArtifactId();
100         String groupId = metadata.getGroupId();
101         String version = metadata.getVersion();
102         return String.format("%s/%s/%s/%s-%s-%s.xml", groupId, artifactId, version, groupId, artifactId, version);
103     }
104 
105     @Deprecated
106     @Override
107     public String getPathForRemoteMetadata(Metadata metadata, RemoteRepository repository, String context) {
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         requireNonNull(session, "session cannot be null");
116         requireNonNull(request, "request cannot be null");
117 
118         Artifact artifact = request.getArtifact();
119 
120         LocalArtifactResult result = new LocalArtifactResult(request);
121         Path file = getAbsolutePathForLocalArtifact(artifact);
122         boolean isFile = Files.isRegularFile(file);
123         result.setPath(isFile ? file : null);
124         result.setAvailable(isFile && !unavailableArtifacts.contains(artifact));
125 
126         return result;
127     }
128 
129     public void add(RepositorySystemSession session, LocalArtifactRegistration request) {
130         requireNonNull(session, "session cannot be null");
131         requireNonNull(request, "request cannot be null");
132 
133         artifactRegistrations.add(request.getArtifact());
134     }
135 
136     public LocalMetadataResult find(RepositorySystemSession session, LocalMetadataRequest request) {
137         requireNonNull(session, "session cannot be null");
138         requireNonNull(request, "request cannot be null");
139 
140         Metadata metadata = request.getMetadata();
141 
142         LocalMetadataResult result = new LocalMetadataResult(request);
143         Path file = getAbsolutePathForLocalMetadata(metadata);
144         result.setPath(Files.isRegularFile(file) ? file : null);
145 
146         return result;
147     }
148 
149     public void add(RepositorySystemSession session, LocalMetadataRegistration request) {
150         requireNonNull(session, "session cannot be null");
151         requireNonNull(request, "request cannot be null");
152 
153         metadataRegistrations.add(request.getMetadata());
154     }
155 
156     public Set<Artifact> getArtifactRegistration() {
157         return artifactRegistrations;
158     }
159 
160     public Set<Metadata> getMetadataRegistration() {
161         return metadataRegistrations;
162     }
163 
164     public void setArtifactAvailability(Artifact artifact, boolean available) {
165         if (available) {
166             unavailableArtifacts.remove(artifact);
167         } else {
168             unavailableArtifacts.add(artifact);
169         }
170     }
171 }