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