1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.repository.legacy;
20
21 import javax.inject.Inject;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import org.apache.maven.artifact.Artifact;
29 import org.apache.maven.artifact.DefaultArtifact;
30 import org.apache.maven.artifact.factory.ArtifactFactory;
31 import org.apache.maven.artifact.metadata.ArtifactMetadata;
32 import org.apache.maven.artifact.repository.ArtifactRepository;
33 import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
34 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
35 import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
36 import org.apache.maven.artifact.versioning.VersionRange;
37 import org.apache.maven.repository.legacy.repository.ArtifactRepositoryFactory;
38 import org.apache.maven.wagon.ResourceDoesNotExistException;
39 import org.apache.maven.wagon.TransferFailedException;
40 import org.apache.maven.wagon.UnsupportedProtocolException;
41 import org.apache.maven.wagon.Wagon;
42 import org.apache.maven.wagon.events.TransferEvent;
43 import org.apache.maven.wagon.events.TransferListener;
44 import org.apache.maven.wagon.observers.AbstractTransferListener;
45 import org.apache.maven.wagon.observers.Debug;
46 import org.codehaus.plexus.testing.PlexusTest;
47 import org.codehaus.plexus.util.FileUtils;
48 import org.junit.jupiter.api.Disabled;
49 import org.junit.jupiter.api.Test;
50
51 import static org.codehaus.plexus.testing.PlexusExtension.getTestFile;
52 import static org.junit.jupiter.api.Assertions.assertEquals;
53 import static org.junit.jupiter.api.Assertions.assertFalse;
54 import static org.junit.jupiter.api.Assertions.assertNotNull;
55 import static org.junit.jupiter.api.Assertions.assertNotSame;
56 import static org.junit.jupiter.api.Assertions.assertThrows;
57 import static org.junit.jupiter.api.Assertions.assertTrue;
58
59
60
61 @PlexusTest
62 @Deprecated
63 class DefaultWagonManagerTest {
64 @Inject
65 private WagonManager wagonManager;
66
67 private final TransferListener transferListener = new Debug();
68
69 @Inject
70 private ArtifactFactory artifactFactory;
71
72 @Inject
73 private ArtifactRepositoryFactory artifactRepositoryFactory;
74
75 @Test
76 void testUnnecessaryRepositoryLookup() throws Exception {
77 Artifact artifact = createTestPomArtifact("target/test-data/get-missing-pom");
78
79 List<ArtifactRepository> repos = new ArrayList<>();
80 repos.add(artifactRepositoryFactory.createArtifactRepository(
81 "repo1", "string://url1", new ArtifactRepositoryLayoutStub(), null, null));
82 repos.add(artifactRepositoryFactory.createArtifactRepository(
83 "repo2", "string://url2", new ArtifactRepositoryLayoutStub(), null, null));
84
85 StringWagon wagon = (StringWagon) wagonManager.getWagon("string");
86 wagon.addExpectedContent(repos.get(0).getLayout().pathOf(artifact), "expected");
87 wagon.addExpectedContent(
88 repos.get(0).getLayout().pathOf(artifact) + ".md5", "cd26d9e10ce691cc69aa2b90dcebbdac");
89 wagon.addExpectedContent(repos.get(1).getLayout().pathOf(artifact), "expected");
90 wagon.addExpectedContent(
91 repos.get(1).getLayout().pathOf(artifact) + ".md5", "cd26d9e10ce691cc69aa2b90dcebbdac");
92
93 class TransferListener extends AbstractTransferListener {
94 public List<TransferEvent> events = new ArrayList<>();
95
96 @Override
97 public void transferInitiated(TransferEvent transferEvent) {
98 events.add(transferEvent);
99 }
100 }
101
102 TransferListener listener = new TransferListener();
103 wagonManager.getArtifact(artifact, repos, listener, false);
104 assertEquals(1, listener.events.size());
105 }
106
107 @Test
108 void testGetMissingJar() throws TransferFailedException, UnsupportedProtocolException, IOException {
109 Artifact artifact = createTestArtifact("target/test-data/get-missing-jar", "jar");
110
111 ArtifactRepository repo = createStringRepo();
112
113 assertThrows(ResourceDoesNotExistException.class, () -> wagonManager.getArtifact(artifact, repo, null, false));
114
115 assertFalse(artifact.getFile().exists());
116 }
117
118 @Test
119 void testGetMissingJarForced() throws TransferFailedException, UnsupportedProtocolException, IOException {
120 Artifact artifact = createTestArtifact("target/test-data/get-missing-jar", "jar");
121
122 ArtifactRepository repo = createStringRepo();
123
124 assertThrows(ResourceDoesNotExistException.class, () -> wagonManager.getArtifact(artifact, repo, null, true));
125
126 assertFalse(artifact.getFile().exists());
127 }
128
129 @Test
130 void testGetRemoteJar()
131 throws TransferFailedException, ResourceDoesNotExistException, UnsupportedProtocolException, IOException {
132 Artifact artifact = createTestArtifact("target/test-data/get-remote-jar", "jar");
133
134 ArtifactRepository repo = createStringRepo();
135
136 StringWagon wagon = (StringWagon) wagonManager.getWagon("string");
137 wagon.addExpectedContent(repo.getLayout().pathOf(artifact), "expected");
138 wagon.addExpectedContent(repo.getLayout().pathOf(artifact) + ".md5", "cd26d9e10ce691cc69aa2b90dcebbdac");
139
140 wagonManager.getArtifact(artifact, repo, null, false);
141
142 assertTrue(artifact.getFile().exists());
143 assertEquals("expected", FileUtils.fileRead(artifact.getFile(), "UTF-8"));
144 }
145
146 private Artifact createTestPomArtifact(String directory) throws IOException {
147 File testData = getTestFile(directory);
148 FileUtils.deleteDirectory(testData);
149 testData.mkdirs();
150
151 Artifact artifact = artifactFactory.createProjectArtifact("test", "test", "1.0");
152 artifact.setFile(new File(testData, "test-1.0.pom"));
153 assertFalse(artifact.getFile().exists());
154 return artifact;
155 }
156
157 private Artifact createTestArtifact(String directory, String type) throws IOException {
158 return createTestArtifact(directory, "1.0", type);
159 }
160
161 private Artifact createTestArtifact(String directory, String version, String type) throws IOException {
162 File testData = getTestFile(directory);
163 FileUtils.deleteDirectory(testData);
164 testData.mkdirs();
165
166 Artifact artifact = artifactFactory.createBuildArtifact("test", "test", version, type);
167 artifact.setFile(new File(
168 testData,
169 "test-" + version + "." + artifact.getArtifactHandler().getExtension()));
170 assertFalse(artifact.getFile().exists());
171 return artifact;
172 }
173
174 private ArtifactRepository createStringRepo() {
175 return artifactRepositoryFactory.createArtifactRepository(
176 "id", "string://url", new ArtifactRepositoryLayoutStub(), null, null);
177 }
178
179
180
181
182
183
184
185
186 private ArtifactRepository getRepo(String id, String url) {
187 return artifactRepositoryFactory.createArtifactRepository(id, url, new DefaultRepositoryLayout(), null, null);
188 }
189
190
191
192
193
194
195
196 private ArtifactRepository getRepo(String id) {
197 return getRepo(id, "http://something");
198 }
199
200 @Test
201 void testDefaultWagonManager() throws Exception {
202 assertWagon("a");
203
204 assertWagon("b");
205
206 assertWagon("c");
207
208 assertWagon("string");
209
210 assertThrows(UnsupportedProtocolException.class, () -> assertWagon("d"));
211 }
212
213
214
215
216 @Test
217 void testWagonTransferListenerRemovedAfterGetArtifactAndPutArtifact() throws Exception {
218 Artifact artifact = createTestArtifact("target/test-data/transfer-listener", "jar");
219 ArtifactRepository repo = createStringRepo();
220 StringWagon wagon = (StringWagon) wagonManager.getWagon("string");
221 wagon.addExpectedContent(repo.getLayout().pathOf(artifact), "expected");
222 wagon.addExpectedContent(repo.getLayout().pathOf(artifact) + ".md5", "cd26d9e10ce691cc69aa2b90dcebbdac");
223
224
225 assertFalse(
226 wagon.getTransferEventSupport().hasTransferListener(transferListener),
227 "Transfer listener is registered before test");
228 wagonManager.getArtifact(artifact, repo, transferListener, false);
229 assertFalse(
230 wagon.getTransferEventSupport().hasTransferListener(transferListener),
231 "Transfer listener still registered after getArtifact");
232
233
234 File sampleFile = getTestFile("target/test-file");
235 FileUtils.fileWrite(sampleFile.getAbsolutePath(), "sample file");
236
237 assertFalse(
238 wagon.getTransferEventSupport().hasTransferListener(transferListener),
239 "Transfer listener is registered before test");
240 wagonManager.putArtifact(sampleFile, artifact, repo, transferListener);
241 assertFalse(
242 wagon.getTransferEventSupport().hasTransferListener(transferListener),
243 "Transfer listener still registered after putArtifact");
244 }
245
246
247
248
249 @Disabled
250 @Test
251 void testChecksumVerification() throws Exception {
252 ArtifactRepositoryPolicy policy = new ArtifactRepositoryPolicy(
253 true, ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS, ArtifactRepositoryPolicy.CHECKSUM_POLICY_FAIL);
254
255 ArtifactRepository repo = artifactRepositoryFactory.createArtifactRepository(
256 "id", "string://url", new ArtifactRepositoryLayoutStub(), policy, policy);
257
258 Artifact artifact = new DefaultArtifact(
259 "sample.group",
260 "sample-art",
261 VersionRange.createFromVersion("1.0"),
262 "scope",
263 "jar",
264 "classifier",
265 null);
266 artifact.setFile(getTestFile("target/sample-art"));
267
268 StringWagon wagon = (StringWagon) wagonManager.getWagon("string");
269
270 wagon.clearExpectedContent();
271 wagon.addExpectedContent("path", "lower-case-checksum");
272 wagon.addExpectedContent("path.sha1", "2a25dc564a3b34f68237fc849066cbc7bb7a36a1");
273 wagonManager.getArtifact(artifact, repo, null, false);
274
275 wagon.clearExpectedContent();
276 wagon.addExpectedContent("path", "upper-case-checksum");
277 wagon.addExpectedContent("path.sha1", "B7BB97D7D0B9244398D9B47296907F73313663E6");
278 wagonManager.getArtifact(artifact, repo, null, false);
279
280 wagon.clearExpectedContent();
281 wagon.addExpectedContent("path", "expected-failure");
282 wagon.addExpectedContent("path.sha1", "b7bb97d7d0b9244398d9b47296907f73313663e6");
283 assertThrows(
284 ChecksumFailedException.class,
285 () -> wagonManager.getArtifact(artifact, repo, null, false),
286 "Checksum verification did not fail");
287
288 wagon.clearExpectedContent();
289 wagon.addExpectedContent("path", "lower-case-checksum");
290 wagon.addExpectedContent("path.md5", "50b2cf50a103a965efac62b983035cac");
291 wagonManager.getArtifact(artifact, repo, null, false);
292
293 wagon.clearExpectedContent();
294 wagon.addExpectedContent("path", "upper-case-checksum");
295 wagon.addExpectedContent("path.md5", "842F568FCCFEB7E534DC72133D42FFDC");
296 wagonManager.getArtifact(artifact, repo, null, false);
297
298 wagon.clearExpectedContent();
299 wagon.addExpectedContent("path", "expected-failure");
300 wagon.addExpectedContent("path.md5", "b7bb97d7d0b9244398d9b47296907f73313663e6");
301 assertThrows(
302 ChecksumFailedException.class,
303 () -> wagonManager.getArtifact(artifact, repo, null, false),
304 "Checksum verification did not fail");
305 }
306
307 @Test
308 void testPerLookupInstantiation() throws Exception {
309 String protocol = "perlookup";
310
311 Wagon one = wagonManager.getWagon(protocol);
312 Wagon two = wagonManager.getWagon(protocol);
313
314 assertNotSame(one, two);
315 }
316
317 private void assertWagon(String protocol) throws Exception {
318 Wagon wagon = wagonManager.getWagon(protocol);
319
320 assertNotNull(wagon, "Check wagon, protocol=" + protocol);
321 }
322
323 private final class ArtifactRepositoryLayoutStub implements ArtifactRepositoryLayout {
324 public String getId() {
325 return "test";
326 }
327
328 public String pathOfRemoteRepositoryMetadata(ArtifactMetadata metadata) {
329 return "path";
330 }
331
332 public String pathOfLocalRepositoryMetadata(ArtifactMetadata metadata, ArtifactRepository repository) {
333 return "path";
334 }
335
336 public String pathOf(Artifact artifact) {
337 return "path";
338 }
339 }
340 }