1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.internal.impl;
20
21 import java.io.File;
22 import java.net.URI;
23 import java.util.Calendar;
24 import java.util.Date;
25 import java.util.TimeZone;
26
27 import org.eclipse.aether.DefaultRepositorySystemSession;
28 import org.eclipse.aether.RepositorySystemSession;
29 import org.eclipse.aether.artifact.Artifact;
30 import org.eclipse.aether.artifact.DefaultArtifact;
31 import org.eclipse.aether.impl.UpdateCheck;
32 import org.eclipse.aether.internal.test.util.TestFileUtils;
33 import org.eclipse.aether.internal.test.util.TestUtils;
34 import org.eclipse.aether.metadata.DefaultMetadata;
35 import org.eclipse.aether.metadata.Metadata;
36 import org.eclipse.aether.repository.RemoteRepository;
37 import org.eclipse.aether.repository.RepositoryPolicy;
38 import org.eclipse.aether.transfer.ArtifactNotFoundException;
39 import org.eclipse.aether.transfer.ArtifactTransferException;
40 import org.eclipse.aether.transfer.MetadataNotFoundException;
41 import org.eclipse.aether.transfer.MetadataTransferException;
42 import org.eclipse.aether.util.repository.SimpleResolutionErrorPolicy;
43 import org.junit.jupiter.api.AfterEach;
44 import org.junit.jupiter.api.BeforeEach;
45 import org.junit.jupiter.api.Test;
46
47 import static org.junit.jupiter.api.Assertions.*;
48
49
50
51 public class DefaultUpdateCheckManagerTest {
52
53 private static final long HOUR = 60L * 60L * 1000L;
54
55 private DefaultUpdateCheckManager manager;
56
57 private DefaultRepositorySystemSession session;
58
59 private Metadata metadata;
60
61 private RemoteRepository repository;
62
63 private Artifact artifact;
64
65 @BeforeEach
66 void setup() throws Exception {
67 File dir = TestFileUtils.createTempDir("");
68 TestFileUtils.deleteFile(dir);
69
70 File metadataFile = new File(dir, "metadata.txt");
71 TestFileUtils.writeString(metadataFile, "metadata");
72 File artifactFile = new File(dir, "artifact.txt");
73 TestFileUtils.writeString(artifactFile, "artifact");
74
75 session = TestUtils.newSession();
76 repository = new RemoteRepository.Builder(
77 "id",
78 "default",
79 TestFileUtils.createTempDir().toURI().toURL().toString())
80 .build();
81 manager = new DefaultUpdateCheckManager(
82 new DefaultTrackingFileManager(), new DefaultUpdatePolicyAnalyzer(), new DefaultPathProcessor());
83 metadata = new DefaultMetadata(
84 "gid", "aid", "ver", "maven-metadata.xml", Metadata.Nature.RELEASE_OR_SNAPSHOT, metadataFile);
85 artifact = new DefaultArtifact("gid", "aid", "", "ext", "ver").setFile(artifactFile);
86 }
87
88 @AfterEach
89 void teardown() throws Exception {
90 new File(metadata.getFile().getParent(), "resolver-status.properties").delete();
91 new File(artifact.getFile().getPath() + ".lastUpdated").delete();
92 metadata.getFile().delete();
93 artifact.getFile().delete();
94 TestFileUtils.deleteFile(new File(new URI(repository.getUrl())));
95 }
96
97 static void resetSessionData(RepositorySystemSession session) {
98 session.getData().set(DefaultUpdateCheckManager.SESSION_CHECKS, null);
99 }
100
101 private UpdateCheck<Metadata, MetadataTransferException> newMetadataCheck() {
102 UpdateCheck<Metadata, MetadataTransferException> check = new UpdateCheck<>();
103 check.setItem(metadata);
104 check.setFile(metadata.getFile());
105 check.setRepository(repository);
106 check.setAuthoritativeRepository(repository);
107 check.setArtifactPolicy(RepositoryPolicy.UPDATE_POLICY_INTERVAL + ":10");
108 check.setMetadataPolicy(RepositoryPolicy.UPDATE_POLICY_INTERVAL + ":10");
109 return check;
110 }
111
112 private UpdateCheck<Artifact, ArtifactTransferException> newArtifactCheck() {
113 UpdateCheck<Artifact, ArtifactTransferException> check = new UpdateCheck<>();
114 check.setItem(artifact);
115 check.setFile(artifact.getFile());
116 check.setRepository(repository);
117 check.setArtifactPolicy(RepositoryPolicy.UPDATE_POLICY_INTERVAL + ":10");
118 return check;
119 }
120
121 @Test
122 void testCheckMetadataFailOnNoFile() {
123 UpdateCheck<Metadata, MetadataTransferException> check = newMetadataCheck();
124 check.setItem(metadata.setFile(null));
125 check.setFile(null);
126
127 assertThrows(NullPointerException.class, () -> manager.checkMetadata(session, check));
128 }
129
130 @Test
131 void testCheckMetadataUpdatePolicyRequired() {
132 UpdateCheck<Metadata, MetadataTransferException> check = newMetadataCheck();
133
134 Calendar cal = Calendar.getInstance();
135 cal.add(Calendar.DATE, -1);
136 check.setLocalLastUpdated(cal.getTimeInMillis());
137
138 check.setArtifactPolicy(RepositoryPolicy.UPDATE_POLICY_ALWAYS);
139 manager.checkMetadata(session, check);
140 assertNull(check.getException());
141 assertTrue(check.isRequired());
142
143 check.setArtifactPolicy(RepositoryPolicy.UPDATE_POLICY_DAILY);
144 manager.checkMetadata(session, check);
145 assertNull(check.getException());
146 assertTrue(check.isRequired());
147
148 check.setArtifactPolicy(RepositoryPolicy.UPDATE_POLICY_INTERVAL + ":60");
149 manager.checkMetadata(session, check);
150 assertNull(check.getException());
151 assertTrue(check.isRequired());
152 }
153
154 @Test
155 void testCheckMetadataUpdatePolicyNotRequired() {
156 UpdateCheck<Metadata, MetadataTransferException> check = newMetadataCheck();
157
158 check.setLocalLastUpdated(System.currentTimeMillis());
159
160 check.setArtifactPolicy(RepositoryPolicy.UPDATE_POLICY_NEVER);
161 manager.checkMetadata(session, check);
162 assertFalse(check.isRequired());
163
164 check.setArtifactPolicy(RepositoryPolicy.UPDATE_POLICY_DAILY);
165 manager.checkMetadata(session, check);
166 assertFalse(check.isRequired());
167
168 check.setArtifactPolicy(RepositoryPolicy.UPDATE_POLICY_INTERVAL + ":61");
169 manager.checkMetadata(session, check);
170 assertFalse(check.isRequired());
171
172 check.setArtifactPolicy("no particular policy");
173 manager.checkMetadata(session, check);
174 assertFalse(check.isRequired());
175 }
176
177 @Test
178 void testCheckMetadata() {
179 UpdateCheck<Metadata, MetadataTransferException> check = newMetadataCheck();
180 check.setArtifactPolicy(RepositoryPolicy.UPDATE_POLICY_DAILY);
181
182
183 manager.checkMetadata(session, check);
184 assertTrue(check.isRequired());
185
186
187 manager.touchMetadata(session, check);
188 resetSessionData(session);
189
190 check = newMetadataCheck();
191 check.setArtifactPolicy(RepositoryPolicy.UPDATE_POLICY_INTERVAL + ":60");
192
193 manager.checkMetadata(session, check);
194 assertFalse(check.isRequired());
195
196
197 check.getFile().delete();
198 manager.checkMetadata(session, check);
199 assertTrue(check.isRequired());
200
201 }
202
203 @Test
204 void testCheckMetadataNoLocalFile() {
205 metadata.getFile().delete();
206
207 UpdateCheck<Metadata, MetadataTransferException> check = newMetadataCheck();
208
209 long lastUpdate = new Date().getTime() - HOUR;
210 check.setLocalLastUpdated(lastUpdate);
211
212
213 check.setLocalLastUpdated(lastUpdate);
214 manager.checkMetadata(session, check);
215 assertTrue(check.isRequired());
216 }
217
218 @Test
219 void testCheckMetadataNotFoundInRepoCachingEnabled() {
220 metadata.getFile().delete();
221 session.setResolutionErrorPolicy(new SimpleResolutionErrorPolicy(true, false));
222
223 UpdateCheck<Metadata, MetadataTransferException> check = newMetadataCheck();
224
225 check.setException(new MetadataNotFoundException(metadata, repository, ""));
226 manager.touchMetadata(session, check);
227 resetSessionData(session);
228
229
230 check = newMetadataCheck().setArtifactPolicy(RepositoryPolicy.UPDATE_POLICY_DAILY);
231 manager.checkMetadata(session, check);
232 assertFalse(check.isRequired());
233 assertTrue(check.getException() instanceof MetadataNotFoundException);
234 assertTrue(check.getException().isFromCache());
235 }
236
237 @Test
238 void testCheckMetadataNotFoundInRepoCachingDisabled() {
239 metadata.getFile().delete();
240 session.setResolutionErrorPolicy(new SimpleResolutionErrorPolicy(false, false));
241
242 UpdateCheck<Metadata, MetadataTransferException> check = newMetadataCheck();
243
244 check.setException(new MetadataNotFoundException(metadata, repository, ""));
245 manager.touchMetadata(session, check);
246 resetSessionData(session);
247
248
249 check = newMetadataCheck().setArtifactPolicy(RepositoryPolicy.UPDATE_POLICY_DAILY);
250 manager.checkMetadata(session, check);
251 assertTrue(check.isRequired());
252 assertNull(check.getException());
253 }
254
255 @Test
256 void testCheckMetadataErrorFromRepoCachingEnabled() {
257 metadata.getFile().delete();
258
259 UpdateCheck<Metadata, MetadataTransferException> check = newMetadataCheck();
260 check.setArtifactPolicy(RepositoryPolicy.UPDATE_POLICY_DAILY);
261
262 check.setException(new MetadataTransferException(metadata, repository, "some error"));
263 manager.touchMetadata(session, check);
264 resetSessionData(session);
265
266
267 check = newMetadataCheck();
268 session.setResolutionErrorPolicy(new SimpleResolutionErrorPolicy(false, true));
269 manager.checkMetadata(session, check);
270 assertFalse(check.isRequired());
271 assertTrue(check.getException() instanceof MetadataTransferException);
272 assertTrue(check.getException().getMessage().contains("some error"), String.valueOf(check.getException()));
273 assertTrue(check.getException().isFromCache());
274 }
275
276 @Test
277 void testCheckMetadataErrorFromRepoCachingDisabled() {
278 metadata.getFile().delete();
279
280 UpdateCheck<Metadata, MetadataTransferException> check = newMetadataCheck();
281 check.setArtifactPolicy(RepositoryPolicy.UPDATE_POLICY_DAILY);
282
283 check.setException(new MetadataTransferException(metadata, repository, "some error"));
284 manager.touchMetadata(session, check);
285 resetSessionData(session);
286
287
288 check = newMetadataCheck();
289 session.setResolutionErrorPolicy(new SimpleResolutionErrorPolicy(false, false));
290 manager.checkMetadata(session, check);
291 assertTrue(check.isRequired());
292 assertNull(check.getException());
293 }
294
295 @Test
296 void testCheckMetadataAtMostOnceDuringSessionEvenIfUpdatePolicyAlways() {
297 UpdateCheck<Metadata, MetadataTransferException> check = newMetadataCheck();
298 check.setArtifactPolicy(RepositoryPolicy.UPDATE_POLICY_ALWAYS);
299 check.setMetadataPolicy(RepositoryPolicy.UPDATE_POLICY_ALWAYS);
300
301
302 manager.checkMetadata(session, check);
303 assertTrue(check.isRequired());
304
305 manager.touchMetadata(session, check);
306
307
308 manager.checkMetadata(session, check);
309 assertFalse(check.isRequired());
310 }
311
312 @Test
313 void testCheckMetadataSessionStateModes() {
314 UpdateCheck<Metadata, MetadataTransferException> check = newMetadataCheck();
315 check.setArtifactPolicy(RepositoryPolicy.UPDATE_POLICY_ALWAYS);
316 check.setMetadataPolicy(RepositoryPolicy.UPDATE_POLICY_ALWAYS);
317 manager.touchMetadata(session, check);
318
319 session.setConfigProperty(DefaultUpdateCheckManager.CONFIG_PROP_SESSION_STATE, "bypass");
320 manager.checkMetadata(session, check);
321 assertTrue(check.isRequired());
322
323 resetSessionData(session);
324 manager.touchMetadata(session, check);
325
326
327 session.setConfigProperty(DefaultUpdateCheckManager.CONFIG_PROP_SESSION_STATE, "true");
328 manager.checkMetadata(session, check);
329 assertFalse(check.isRequired());
330
331
332 session.setConfigProperty(DefaultUpdateCheckManager.CONFIG_PROP_SESSION_STATE, "false");
333 manager.checkMetadata(session, check);
334 assertTrue(check.isRequired());
335 }
336
337 @Test
338 void testCheckMetadataAtMostOnceDuringSessionEvenIfUpdatePolicyAlways_InvalidFile() {
339 UpdateCheck<Metadata, MetadataTransferException> check = newMetadataCheck();
340 check.setArtifactPolicy(RepositoryPolicy.UPDATE_POLICY_ALWAYS);
341 check.setMetadataPolicy(RepositoryPolicy.UPDATE_POLICY_ALWAYS);
342 check.setFileValid(false);
343
344
345 manager.checkMetadata(session, check);
346 assertTrue(check.isRequired());
347
348
349 manager.touchMetadata(session, check);
350
351
352 manager.checkMetadata(session, check);
353 assertTrue(check.isRequired());
354
355
356 check.setException(new MetadataNotFoundException(check.getItem(), check.getRepository()));
357 manager.touchMetadata(session, check);
358
359
360 manager.checkMetadata(session, check);
361 assertFalse(check.isRequired());
362 }
363
364 @Test
365 void testCheckMetadataAtMostOnceDuringSessionEvenIfUpdatePolicyAlways_DifferentRepoIdSameUrl() {
366 UpdateCheck<Metadata, MetadataTransferException> check = newMetadataCheck();
367 check.setArtifactPolicy(RepositoryPolicy.UPDATE_POLICY_ALWAYS);
368 check.setMetadataPolicy(RepositoryPolicy.UPDATE_POLICY_ALWAYS);
369 check.setFileValid(false);
370
371
372 manager.checkMetadata(session, check);
373 assertTrue(check.isRequired());
374
375 manager.touchMetadata(session, check);
376
377
378 check.setRepository(new RemoteRepository.Builder(check.getRepository())
379 .setId("check")
380 .build());
381 manager.checkMetadata(session, check);
382 assertTrue(check.isRequired());
383 }
384
385 @Test
386 void testCheckMetadataWhenLocallyMissingEvenIfUpdatePolicyIsNever() {
387 UpdateCheck<Metadata, MetadataTransferException> check = newMetadataCheck();
388 check.setArtifactPolicy(RepositoryPolicy.UPDATE_POLICY_NEVER);
389 session.setResolutionErrorPolicy(new SimpleResolutionErrorPolicy(true, false));
390
391 check.getFile().delete();
392 assertFalse(check.getFile().exists(), check.getFile().getAbsolutePath());
393
394 manager.checkMetadata(session, check);
395 assertTrue(check.isRequired());
396 }
397
398 @Test
399 void testCheckMetadataWhenLocallyPresentButInvalidEvenIfUpdatePolicyIsNever() {
400 UpdateCheck<Metadata, MetadataTransferException> check = newMetadataCheck();
401 check.setArtifactPolicy(RepositoryPolicy.UPDATE_POLICY_NEVER);
402 session.setResolutionErrorPolicy(new SimpleResolutionErrorPolicy(true, false));
403
404 manager.touchMetadata(session, check);
405 resetSessionData(session);
406
407 check.setFileValid(false);
408
409 manager.checkMetadata(session, check);
410 assertTrue(check.isRequired());
411 }
412
413 @Test
414 void testCheckMetadataWhenLocallyDeletedEvenIfTimestampUpToDate() {
415 UpdateCheck<Metadata, MetadataTransferException> check = newMetadataCheck();
416 session.setResolutionErrorPolicy(new SimpleResolutionErrorPolicy(true, false));
417
418 manager.touchMetadata(session, check);
419 resetSessionData(session);
420
421 check.getFile().delete();
422 assertFalse(check.getFile().exists(), check.getFile().getAbsolutePath());
423
424 manager.checkMetadata(session, check);
425 assertTrue(check.isRequired());
426 }
427
428 @Test
429 void testCheckMetadataNotWhenUpdatePolicyIsNeverAndTimestampIsUnavailable() {
430 UpdateCheck<Metadata, MetadataTransferException> check = newMetadataCheck();
431 check.setArtifactPolicy(RepositoryPolicy.UPDATE_POLICY_NEVER);
432 check.setMetadataPolicy(RepositoryPolicy.UPDATE_POLICY_NEVER);
433 session.setResolutionErrorPolicy(new SimpleResolutionErrorPolicy(true, false));
434
435 manager.checkMetadata(session, check);
436 assertFalse(check.isRequired());
437 }
438
439 @Test
440 void testCheckArtifactFailOnNoFile() {
441 UpdateCheck<Artifact, ArtifactTransferException> check = newArtifactCheck();
442 check.setItem(artifact.setFile(null));
443 check.setFile(null);
444
445 assertThrows(NullPointerException.class, () -> manager.checkArtifact(session, check));
446 }
447
448 @Test
449 void testCheckArtifactUpdatePolicyRequired() {
450 UpdateCheck<Artifact, ArtifactTransferException> check = newArtifactCheck();
451 check.setItem(artifact);
452 check.setFile(artifact.getFile());
453
454 Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
455 cal.add(Calendar.DATE, -1);
456 long lastUpdate = cal.getTimeInMillis();
457 artifact.getFile().setLastModified(lastUpdate);
458 check.setLocalLastUpdated(lastUpdate);
459
460 check.setArtifactPolicy(RepositoryPolicy.UPDATE_POLICY_ALWAYS);
461 manager.checkArtifact(session, check);
462 assertNull(check.getException());
463 assertTrue(check.isRequired());
464
465 check.setArtifactPolicy(RepositoryPolicy.UPDATE_POLICY_DAILY);
466 manager.checkArtifact(session, check);
467 assertNull(check.getException());
468 assertTrue(check.isRequired());
469
470 check.setArtifactPolicy(RepositoryPolicy.UPDATE_POLICY_INTERVAL + ":60");
471 manager.checkArtifact(session, check);
472 assertNull(check.getException());
473 assertTrue(check.isRequired());
474 }
475
476 @Test
477 void testCheckArtifactUpdatePolicyNotRequired() {
478 UpdateCheck<Artifact, ArtifactTransferException> check = newArtifactCheck();
479 check.setItem(artifact);
480 check.setFile(artifact.getFile());
481
482 Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
483 cal.add(Calendar.HOUR_OF_DAY, -1);
484 check.setLocalLastUpdated(cal.getTimeInMillis());
485
486 check.setArtifactPolicy(RepositoryPolicy.UPDATE_POLICY_NEVER);
487 manager.checkArtifact(session, check);
488 assertFalse(check.isRequired());
489
490 check.setArtifactPolicy(RepositoryPolicy.UPDATE_POLICY_DAILY);
491 manager.checkArtifact(session, check);
492 assertFalse(check.isRequired());
493
494 check.setArtifactPolicy(RepositoryPolicy.UPDATE_POLICY_INTERVAL + ":61");
495 manager.checkArtifact(session, check);
496 assertFalse(check.isRequired());
497
498 check.setArtifactPolicy("no particular policy");
499 manager.checkArtifact(session, check);
500 assertFalse(check.isRequired());
501 }
502
503 @Test
504 void testCheckArtifact() {
505 UpdateCheck<Artifact, ArtifactTransferException> check = newArtifactCheck();
506 long fifteenMinutes = new Date().getTime() - (15L * 60L * 1000L);
507 check.getFile().setLastModified(fifteenMinutes);
508
509 fifteenMinutes = check.getFile().lastModified();
510
511
512 manager.checkArtifact(session, check);
513 assertTrue(check.isRequired());
514
515
516 check.setLocalLastUpdated(0L);
517 long lastUpdate = new Date().getTime();
518 check.getFile().setLastModified(lastUpdate);
519 lastUpdate = check.getFile().lastModified();
520
521 manager.checkArtifact(session, check);
522 assertFalse(check.isRequired());
523
524
525 check.setLocalLastUpdated(0L);
526 check.getFile().delete();
527 manager.checkArtifact(session, check);
528 assertTrue(check.isRequired());
529 }
530
531 @Test
532 void testCheckArtifactNoLocalFile() {
533 artifact.getFile().delete();
534 UpdateCheck<Artifact, ArtifactTransferException> check = newArtifactCheck();
535
536 long lastUpdate = new Date().getTime() - HOUR;
537
538
539 check.setLocalLastUpdated(lastUpdate);
540 manager.checkArtifact(session, check);
541 assertTrue(check.isRequired());
542 }
543
544 @Test
545 void testCheckArtifactNotFoundInRepoCachingEnabled() {
546 artifact.getFile().delete();
547 session.setResolutionErrorPolicy(new SimpleResolutionErrorPolicy(true, false));
548
549 UpdateCheck<Artifact, ArtifactTransferException> check = newArtifactCheck();
550 check.setException(new ArtifactNotFoundException(artifact, repository));
551 manager.touchArtifact(session, check);
552 resetSessionData(session);
553
554
555 check = newArtifactCheck().setArtifactPolicy(RepositoryPolicy.UPDATE_POLICY_DAILY);
556 manager.checkArtifact(session, check);
557 assertFalse(check.isRequired());
558 assertTrue(check.getException() instanceof ArtifactNotFoundException);
559 assertTrue(check.getException().isFromCache());
560 }
561
562 @Test
563 void testCheckArtifactNotFoundInRepoCachingDisabled() {
564 artifact.getFile().delete();
565 session.setResolutionErrorPolicy(new SimpleResolutionErrorPolicy(false, false));
566
567 UpdateCheck<Artifact, ArtifactTransferException> check = newArtifactCheck();
568 check.setException(new ArtifactNotFoundException(artifact, repository));
569 manager.touchArtifact(session, check);
570 resetSessionData(session);
571
572
573 check = newArtifactCheck().setArtifactPolicy(RepositoryPolicy.UPDATE_POLICY_DAILY);
574 manager.checkArtifact(session, check);
575 assertTrue(check.isRequired());
576 assertNull(check.getException());
577 }
578
579 @Test
580 void testCheckArtifactErrorFromRepoCachingEnabled() {
581 artifact.getFile().delete();
582
583 UpdateCheck<Artifact, ArtifactTransferException> check = newArtifactCheck();
584 check.setArtifactPolicy(RepositoryPolicy.UPDATE_POLICY_DAILY);
585 check.setException(new ArtifactTransferException(artifact, repository, "some error"));
586 manager.touchArtifact(session, check);
587 resetSessionData(session);
588
589
590 check = newArtifactCheck();
591 session.setResolutionErrorPolicy(new SimpleResolutionErrorPolicy(false, true));
592 manager.checkArtifact(session, check);
593 assertFalse(check.isRequired());
594 assertTrue(check.getException() instanceof ArtifactTransferException);
595 assertTrue(check.getException().isFromCache());
596 }
597
598 @Test
599 void testCheckArtifactErrorFromRepoCachingDisabled() {
600 artifact.getFile().delete();
601
602 UpdateCheck<Artifact, ArtifactTransferException> check = newArtifactCheck();
603 check.setArtifactPolicy(RepositoryPolicy.UPDATE_POLICY_DAILY);
604 check.setException(new ArtifactTransferException(artifact, repository, "some error"));
605 manager.touchArtifact(session, check);
606 resetSessionData(session);
607
608
609 check = newArtifactCheck();
610 session.setResolutionErrorPolicy(new SimpleResolutionErrorPolicy(false, false));
611 manager.checkArtifact(session, check);
612 assertTrue(check.isRequired());
613 assertNull(check.getException());
614 }
615
616 @Test
617 void testCheckArtifactAtMostOnceDuringSessionEvenIfUpdatePolicyAlways() {
618 UpdateCheck<Artifact, ArtifactTransferException> check = newArtifactCheck();
619 check.setArtifactPolicy(RepositoryPolicy.UPDATE_POLICY_ALWAYS);
620
621
622 manager.checkArtifact(session, check);
623 assertTrue(check.isRequired());
624
625 manager.touchArtifact(session, check);
626
627
628 manager.checkArtifact(session, check);
629 assertFalse(check.isRequired());
630 }
631
632 @Test
633 void testCheckArtifactSessionStateModes() {
634 UpdateCheck<Artifact, ArtifactTransferException> check = newArtifactCheck();
635 check.setArtifactPolicy(RepositoryPolicy.UPDATE_POLICY_ALWAYS);
636 manager.touchArtifact(session, check);
637
638 session.setConfigProperty(DefaultUpdateCheckManager.CONFIG_PROP_SESSION_STATE, "bypass");
639 manager.checkArtifact(session, check);
640 assertTrue(check.isRequired());
641
642 resetSessionData(session);
643 manager.touchArtifact(session, check);
644
645 session.setConfigProperty(DefaultUpdateCheckManager.CONFIG_PROP_SESSION_STATE, "true");
646 manager.checkArtifact(session, check);
647 assertFalse(check.isRequired());
648
649 session.setConfigProperty(DefaultUpdateCheckManager.CONFIG_PROP_SESSION_STATE, "false");
650 manager.checkArtifact(session, check);
651 assertTrue(check.isRequired());
652 }
653
654 @Test
655 void testCheckArtifactAtMostOnceDuringSessionEvenIfUpdatePolicyAlways_InvalidFile() {
656 UpdateCheck<Artifact, ArtifactTransferException> check = newArtifactCheck();
657 check.setArtifactPolicy(RepositoryPolicy.UPDATE_POLICY_ALWAYS);
658 check.setFileValid(false);
659
660
661 manager.checkArtifact(session, check);
662 assertTrue(check.isRequired());
663
664
665 manager.touchArtifact(session, check);
666
667
668 manager.checkArtifact(session, check);
669 assertTrue(check.isRequired());
670
671
672 check.setException(new ArtifactNotFoundException(check.getItem(), check.getRepository()));
673 manager.touchArtifact(session, check);
674
675
676 manager.checkArtifact(session, check);
677 assertFalse(check.isRequired());
678 }
679
680 @Test
681 void testCheckArtifactAtMostOnceDuringSessionEvenIfUpdatePolicyAlways_DifferentRepoIdSameUrl() {
682 UpdateCheck<Artifact, ArtifactTransferException> check = newArtifactCheck();
683 check.setArtifactPolicy(RepositoryPolicy.UPDATE_POLICY_ALWAYS);
684
685
686 manager.checkArtifact(session, check);
687 assertTrue(check.isRequired());
688
689 manager.touchArtifact(session, check);
690
691
692 check.setRepository(new RemoteRepository.Builder(check.getRepository())
693 .setId("check")
694 .build());
695 manager.checkArtifact(session, check);
696 assertTrue(check.isRequired());
697 }
698
699 @Test
700 void testCheckArtifactWhenLocallyMissingEvenIfUpdatePolicyIsNever() {
701 UpdateCheck<Artifact, ArtifactTransferException> check = newArtifactCheck();
702 check.setArtifactPolicy(RepositoryPolicy.UPDATE_POLICY_NEVER);
703 session.setResolutionErrorPolicy(new SimpleResolutionErrorPolicy(true, false));
704
705 check.getFile().delete();
706 assertFalse(check.getFile().exists(), check.getFile().getAbsolutePath());
707
708 manager.checkArtifact(session, check);
709 assertTrue(check.isRequired());
710 }
711
712 @Test
713 void testCheckArtifactWhenLocallyPresentButInvalidEvenIfUpdatePolicyIsNever() {
714 UpdateCheck<Artifact, ArtifactTransferException> check = newArtifactCheck();
715 check.setArtifactPolicy(RepositoryPolicy.UPDATE_POLICY_NEVER);
716 session.setResolutionErrorPolicy(new SimpleResolutionErrorPolicy(true, false));
717
718 manager.touchArtifact(session, check);
719 resetSessionData(session);
720
721 check.setFileValid(false);
722
723 manager.checkArtifact(session, check);
724 assertTrue(check.isRequired());
725 }
726
727 @Test
728 void testCheckArtifactWhenLocallyDeletedEvenIfTimestampUpToDate() {
729 UpdateCheck<Artifact, ArtifactTransferException> check = newArtifactCheck();
730 session.setResolutionErrorPolicy(new SimpleResolutionErrorPolicy(true, false));
731
732 manager.touchArtifact(session, check);
733 resetSessionData(session);
734
735 check.getFile().delete();
736 assertFalse(check.getFile().exists(), check.getFile().getAbsolutePath());
737
738 manager.checkArtifact(session, check);
739 assertTrue(check.isRequired());
740 }
741
742 @Test
743 void testCheckArtifactNotWhenUpdatePolicyIsNeverAndTimestampIsUnavailable() {
744 UpdateCheck<Artifact, ArtifactTransferException> check = newArtifactCheck();
745 check.setArtifactPolicy(RepositoryPolicy.UPDATE_POLICY_NEVER);
746 session.setResolutionErrorPolicy(new SimpleResolutionErrorPolicy(true, false));
747
748 manager.checkArtifact(session, check);
749 assertFalse(check.isRequired());
750 }
751 }