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.After;
44 import org.junit.Before;
45 import org.junit.Test;
46
47 import static org.junit.Assert.*;
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 @Before
66 public void setup() throws Exception {
67 File dir = TestFileUtils.createTempFile("");
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 .setUpdatePolicyAnalyzer(new DefaultUpdatePolicyAnalyzer())
83 .setTrackingFileManager(new DefaultTrackingFileManager());
84 metadata = new DefaultMetadata(
85 "gid", "aid", "ver", "maven-metadata.xml", Metadata.Nature.RELEASE_OR_SNAPSHOT, metadataFile);
86 artifact = new DefaultArtifact("gid", "aid", "", "ext", "ver").setFile(artifactFile);
87 }
88
89 @After
90 public void teardown() throws Exception {
91 new File(metadata.getFile().getParent(), "resolver-status.properties").delete();
92 new File(artifact.getFile().getPath() + ".lastUpdated").delete();
93 metadata.getFile().delete();
94 artifact.getFile().delete();
95 TestFileUtils.deleteFile(new File(new URI(repository.getUrl())));
96 }
97
98 static void resetSessionData(RepositorySystemSession session) {
99 session.getData().set(DefaultUpdateCheckManager.SESSION_CHECKS, null);
100 }
101
102 private UpdateCheck<Metadata, MetadataTransferException> newMetadataCheck() {
103 UpdateCheck<Metadata, MetadataTransferException> check = new UpdateCheck<>();
104 check.setItem(metadata);
105 check.setFile(metadata.getFile());
106 check.setRepository(repository);
107 check.setAuthoritativeRepository(repository);
108 check.setPolicy(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.setPolicy(RepositoryPolicy.UPDATE_POLICY_INTERVAL + ":10");
118 return check;
119 }
120
121 @Test(expected = Exception.class)
122 public void testCheckMetadataFailOnNoFile() {
123 UpdateCheck<Metadata, MetadataTransferException> check = newMetadataCheck();
124 check.setItem(metadata.setFile(null));
125 check.setFile(null);
126
127 manager.checkMetadata(session, check);
128 }
129
130 @Test
131 public 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.setPolicy(RepositoryPolicy.UPDATE_POLICY_ALWAYS);
139 manager.checkMetadata(session, check);
140 assertNull(check.getException());
141 assertTrue(check.isRequired());
142
143 check.setPolicy(RepositoryPolicy.UPDATE_POLICY_DAILY);
144 manager.checkMetadata(session, check);
145 assertNull(check.getException());
146 assertTrue(check.isRequired());
147
148 check.setPolicy(RepositoryPolicy.UPDATE_POLICY_INTERVAL + ":60");
149 manager.checkMetadata(session, check);
150 assertNull(check.getException());
151 assertTrue(check.isRequired());
152 }
153
154 @Test
155 public void testCheckMetadataUpdatePolicyNotRequired() {
156 UpdateCheck<Metadata, MetadataTransferException> check = newMetadataCheck();
157
158 check.setLocalLastUpdated(System.currentTimeMillis());
159
160 check.setPolicy(RepositoryPolicy.UPDATE_POLICY_NEVER);
161 manager.checkMetadata(session, check);
162 assertFalse(check.isRequired());
163
164 check.setPolicy(RepositoryPolicy.UPDATE_POLICY_DAILY);
165 manager.checkMetadata(session, check);
166 assertFalse(check.isRequired());
167
168 check.setPolicy(RepositoryPolicy.UPDATE_POLICY_INTERVAL + ":61");
169 manager.checkMetadata(session, check);
170 assertFalse(check.isRequired());
171
172 check.setPolicy("no particular policy");
173 manager.checkMetadata(session, check);
174 assertFalse(check.isRequired());
175 }
176
177 @Test
178 public void testCheckMetadata() {
179 UpdateCheck<Metadata, MetadataTransferException> check = newMetadataCheck();
180 check.setPolicy(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.setPolicy(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 public 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 public 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().setPolicy(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 public 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().setPolicy(RepositoryPolicy.UPDATE_POLICY_DAILY);
250 manager.checkMetadata(session, check);
251 assertTrue(check.isRequired());
252 assertNull(check.getException());
253 }
254
255 @Test
256 public void testCheckMetadataErrorFromRepoCachingEnabled() {
257 metadata.getFile().delete();
258
259 UpdateCheck<Metadata, MetadataTransferException> check = newMetadataCheck();
260 check.setPolicy(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(
273 String.valueOf(check.getException()),
274 check.getException().getMessage().contains("some error"));
275 assertTrue(check.getException().isFromCache());
276 }
277
278 @Test
279 public void testCheckMetadataErrorFromRepoCachingDisabled() {
280 metadata.getFile().delete();
281
282 UpdateCheck<Metadata, MetadataTransferException> check = newMetadataCheck();
283 check.setPolicy(RepositoryPolicy.UPDATE_POLICY_DAILY);
284
285 check.setException(new MetadataTransferException(metadata, repository, "some error"));
286 manager.touchMetadata(session, check);
287 resetSessionData(session);
288
289
290 check = newMetadataCheck();
291 session.setResolutionErrorPolicy(new SimpleResolutionErrorPolicy(false, false));
292 manager.checkMetadata(session, check);
293 assertTrue(check.isRequired());
294 assertNull(check.getException());
295 }
296
297 @Test
298 public void testCheckMetadataAtMostOnceDuringSessionEvenIfUpdatePolicyAlways() {
299 UpdateCheck<Metadata, MetadataTransferException> check = newMetadataCheck();
300 check.setPolicy(RepositoryPolicy.UPDATE_POLICY_ALWAYS);
301
302
303 manager.checkMetadata(session, check);
304 assertTrue(check.isRequired());
305
306 manager.touchMetadata(session, check);
307
308
309 manager.checkMetadata(session, check);
310 assertFalse(check.isRequired());
311 }
312
313 @Test
314 public void testCheckMetadataSessionStateModes() {
315 UpdateCheck<Metadata, MetadataTransferException> check = newMetadataCheck();
316 check.setPolicy(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 public void testCheckMetadataAtMostOnceDuringSessionEvenIfUpdatePolicyAlways_InvalidFile() {
339 UpdateCheck<Metadata, MetadataTransferException> check = newMetadataCheck();
340 check.setPolicy(RepositoryPolicy.UPDATE_POLICY_ALWAYS);
341 check.setFileValid(false);
342
343
344 manager.checkMetadata(session, check);
345 assertTrue(check.isRequired());
346
347
348 manager.touchMetadata(session, check);
349
350
351 manager.checkMetadata(session, check);
352 assertTrue(check.isRequired());
353
354
355 check.setException(new MetadataNotFoundException(check.getItem(), check.getRepository()));
356 manager.touchMetadata(session, check);
357
358
359 manager.checkMetadata(session, check);
360 assertFalse(check.isRequired());
361 }
362
363 @Test
364 public void testCheckMetadataAtMostOnceDuringSessionEvenIfUpdatePolicyAlways_DifferentRepoIdSameUrl() {
365 UpdateCheck<Metadata, MetadataTransferException> check = newMetadataCheck();
366 check.setPolicy(RepositoryPolicy.UPDATE_POLICY_ALWAYS);
367 check.setFileValid(false);
368
369
370 manager.checkMetadata(session, check);
371 assertTrue(check.isRequired());
372
373 manager.touchMetadata(session, check);
374
375
376 check.setRepository(new RemoteRepository.Builder(check.getRepository())
377 .setId("check")
378 .build());
379 manager.checkMetadata(session, check);
380 assertTrue(check.isRequired());
381 }
382
383 @Test
384 public void testCheckMetadataWhenLocallyMissingEvenIfUpdatePolicyIsNever() {
385 UpdateCheck<Metadata, MetadataTransferException> check = newMetadataCheck();
386 check.setPolicy(RepositoryPolicy.UPDATE_POLICY_NEVER);
387 session.setResolutionErrorPolicy(new SimpleResolutionErrorPolicy(true, false));
388
389 check.getFile().delete();
390 assertFalse(check.getFile().getAbsolutePath(), check.getFile().exists());
391
392 manager.checkMetadata(session, check);
393 assertTrue(check.isRequired());
394 }
395
396 @Test
397 public void testCheckMetadataWhenLocallyPresentButInvalidEvenIfUpdatePolicyIsNever() {
398 UpdateCheck<Metadata, MetadataTransferException> check = newMetadataCheck();
399 check.setPolicy(RepositoryPolicy.UPDATE_POLICY_NEVER);
400 session.setResolutionErrorPolicy(new SimpleResolutionErrorPolicy(true, false));
401
402 manager.touchMetadata(session, check);
403 resetSessionData(session);
404
405 check.setFileValid(false);
406
407 manager.checkMetadata(session, check);
408 assertTrue(check.isRequired());
409 }
410
411 @Test
412 public void testCheckMetadataWhenLocallyDeletedEvenIfTimestampUpToDate() {
413 UpdateCheck<Metadata, MetadataTransferException> check = newMetadataCheck();
414 session.setResolutionErrorPolicy(new SimpleResolutionErrorPolicy(true, false));
415
416 manager.touchMetadata(session, check);
417 resetSessionData(session);
418
419 check.getFile().delete();
420 assertFalse(check.getFile().getAbsolutePath(), check.getFile().exists());
421
422 manager.checkMetadata(session, check);
423 assertTrue(check.isRequired());
424 }
425
426 @Test
427 public void testCheckMetadataNotWhenUpdatePolicyIsNeverAndTimestampIsUnavailable() {
428 UpdateCheck<Metadata, MetadataTransferException> check = newMetadataCheck();
429 check.setPolicy(RepositoryPolicy.UPDATE_POLICY_NEVER);
430 session.setResolutionErrorPolicy(new SimpleResolutionErrorPolicy(true, false));
431
432 manager.checkMetadata(session, check);
433 assertFalse(check.isRequired());
434 }
435
436 @Test(expected = NullPointerException.class)
437 public void testCheckArtifactFailOnNoFile() {
438 UpdateCheck<Artifact, ArtifactTransferException> check = newArtifactCheck();
439 check.setItem(artifact.setFile(null));
440 check.setFile(null);
441
442 manager.checkArtifact(session, check);
443 assertNotNull(check.getException());
444 }
445
446 @Test
447 public void testCheckArtifactUpdatePolicyRequired() {
448 UpdateCheck<Artifact, ArtifactTransferException> check = newArtifactCheck();
449 check.setItem(artifact);
450 check.setFile(artifact.getFile());
451
452 Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
453 cal.add(Calendar.DATE, -1);
454 long lastUpdate = cal.getTimeInMillis();
455 artifact.getFile().setLastModified(lastUpdate);
456 check.setLocalLastUpdated(lastUpdate);
457
458 check.setPolicy(RepositoryPolicy.UPDATE_POLICY_ALWAYS);
459 manager.checkArtifact(session, check);
460 assertNull(check.getException());
461 assertTrue(check.isRequired());
462
463 check.setPolicy(RepositoryPolicy.UPDATE_POLICY_DAILY);
464 manager.checkArtifact(session, check);
465 assertNull(check.getException());
466 assertTrue(check.isRequired());
467
468 check.setPolicy(RepositoryPolicy.UPDATE_POLICY_INTERVAL + ":60");
469 manager.checkArtifact(session, check);
470 assertNull(check.getException());
471 assertTrue(check.isRequired());
472 }
473
474 @Test
475 public void testCheckArtifactUpdatePolicyNotRequired() {
476 UpdateCheck<Artifact, ArtifactTransferException> check = newArtifactCheck();
477 check.setItem(artifact);
478 check.setFile(artifact.getFile());
479
480 Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
481 cal.add(Calendar.HOUR_OF_DAY, -1);
482 check.setLocalLastUpdated(cal.getTimeInMillis());
483
484 check.setPolicy(RepositoryPolicy.UPDATE_POLICY_NEVER);
485 manager.checkArtifact(session, check);
486 assertFalse(check.isRequired());
487
488 check.setPolicy(RepositoryPolicy.UPDATE_POLICY_DAILY);
489 manager.checkArtifact(session, check);
490 assertFalse(check.isRequired());
491
492 check.setPolicy(RepositoryPolicy.UPDATE_POLICY_INTERVAL + ":61");
493 manager.checkArtifact(session, check);
494 assertFalse(check.isRequired());
495
496 check.setPolicy("no particular policy");
497 manager.checkArtifact(session, check);
498 assertFalse(check.isRequired());
499 }
500
501 @Test
502 public void testCheckArtifact() {
503 UpdateCheck<Artifact, ArtifactTransferException> check = newArtifactCheck();
504 long fifteenMinutes = new Date().getTime() - (15L * 60L * 1000L);
505 check.getFile().setLastModified(fifteenMinutes);
506
507 fifteenMinutes = check.getFile().lastModified();
508
509
510 manager.checkArtifact(session, check);
511 assertTrue(check.isRequired());
512
513
514 check.setLocalLastUpdated(0L);
515 long lastUpdate = new Date().getTime();
516 check.getFile().setLastModified(lastUpdate);
517 lastUpdate = check.getFile().lastModified();
518
519 manager.checkArtifact(session, check);
520 assertFalse(check.isRequired());
521
522
523 check.setLocalLastUpdated(0L);
524 check.getFile().delete();
525 manager.checkArtifact(session, check);
526 assertTrue(check.isRequired());
527 }
528
529 @Test
530 public void testCheckArtifactNoLocalFile() {
531 artifact.getFile().delete();
532 UpdateCheck<Artifact, ArtifactTransferException> check = newArtifactCheck();
533
534 long lastUpdate = new Date().getTime() - HOUR;
535
536
537 check.setLocalLastUpdated(lastUpdate);
538 manager.checkArtifact(session, check);
539 assertTrue(check.isRequired());
540 }
541
542 @Test
543 public void testCheckArtifactNotFoundInRepoCachingEnabled() {
544 artifact.getFile().delete();
545 session.setResolutionErrorPolicy(new SimpleResolutionErrorPolicy(true, false));
546
547 UpdateCheck<Artifact, ArtifactTransferException> check = newArtifactCheck();
548 check.setException(new ArtifactNotFoundException(artifact, repository));
549 manager.touchArtifact(session, check);
550 resetSessionData(session);
551
552
553 check = newArtifactCheck().setPolicy(RepositoryPolicy.UPDATE_POLICY_DAILY);
554 manager.checkArtifact(session, check);
555 assertFalse(check.isRequired());
556 assertTrue(check.getException() instanceof ArtifactNotFoundException);
557 assertTrue(check.getException().isFromCache());
558 }
559
560 @Test
561 public void testCheckArtifactNotFoundInRepoCachingDisabled() {
562 artifact.getFile().delete();
563 session.setResolutionErrorPolicy(new SimpleResolutionErrorPolicy(false, false));
564
565 UpdateCheck<Artifact, ArtifactTransferException> check = newArtifactCheck();
566 check.setException(new ArtifactNotFoundException(artifact, repository));
567 manager.touchArtifact(session, check);
568 resetSessionData(session);
569
570
571 check = newArtifactCheck().setPolicy(RepositoryPolicy.UPDATE_POLICY_DAILY);
572 manager.checkArtifact(session, check);
573 assertTrue(check.isRequired());
574 assertNull(check.getException());
575 }
576
577 @Test
578 public void testCheckArtifactErrorFromRepoCachingEnabled() {
579 artifact.getFile().delete();
580
581 UpdateCheck<Artifact, ArtifactTransferException> check = newArtifactCheck();
582 check.setPolicy(RepositoryPolicy.UPDATE_POLICY_DAILY);
583 check.setException(new ArtifactTransferException(artifact, repository, "some error"));
584 manager.touchArtifact(session, check);
585 resetSessionData(session);
586
587
588 check = newArtifactCheck();
589 session.setResolutionErrorPolicy(new SimpleResolutionErrorPolicy(false, true));
590 manager.checkArtifact(session, check);
591 assertFalse(check.isRequired());
592 assertTrue(check.getException() instanceof ArtifactTransferException);
593 assertTrue(check.getException().isFromCache());
594 }
595
596 @Test
597 public void testCheckArtifactErrorFromRepoCachingDisabled() {
598 artifact.getFile().delete();
599
600 UpdateCheck<Artifact, ArtifactTransferException> check = newArtifactCheck();
601 check.setPolicy(RepositoryPolicy.UPDATE_POLICY_DAILY);
602 check.setException(new ArtifactTransferException(artifact, repository, "some error"));
603 manager.touchArtifact(session, check);
604 resetSessionData(session);
605
606
607 check = newArtifactCheck();
608 session.setResolutionErrorPolicy(new SimpleResolutionErrorPolicy(false, false));
609 manager.checkArtifact(session, check);
610 assertTrue(check.isRequired());
611 assertNull(check.getException());
612 }
613
614 @Test
615 public void testCheckArtifactAtMostOnceDuringSessionEvenIfUpdatePolicyAlways() {
616 UpdateCheck<Artifact, ArtifactTransferException> check = newArtifactCheck();
617 check.setPolicy(RepositoryPolicy.UPDATE_POLICY_ALWAYS);
618
619
620 manager.checkArtifact(session, check);
621 assertTrue(check.isRequired());
622
623 manager.touchArtifact(session, check);
624
625
626 manager.checkArtifact(session, check);
627 assertFalse(check.isRequired());
628 }
629
630 @Test
631 public void testCheckArtifactSessionStateModes() {
632 UpdateCheck<Artifact, ArtifactTransferException> check = newArtifactCheck();
633 check.setPolicy(RepositoryPolicy.UPDATE_POLICY_ALWAYS);
634 manager.touchArtifact(session, check);
635
636 session.setConfigProperty(DefaultUpdateCheckManager.CONFIG_PROP_SESSION_STATE, "bypass");
637 manager.checkArtifact(session, check);
638 assertTrue(check.isRequired());
639
640 resetSessionData(session);
641 manager.touchArtifact(session, check);
642
643 session.setConfigProperty(DefaultUpdateCheckManager.CONFIG_PROP_SESSION_STATE, "true");
644 manager.checkArtifact(session, check);
645 assertFalse(check.isRequired());
646
647 session.setConfigProperty(DefaultUpdateCheckManager.CONFIG_PROP_SESSION_STATE, "false");
648 manager.checkArtifact(session, check);
649 assertTrue(check.isRequired());
650 }
651
652 @Test
653 public void testCheckArtifactAtMostOnceDuringSessionEvenIfUpdatePolicyAlways_InvalidFile() {
654 UpdateCheck<Artifact, ArtifactTransferException> check = newArtifactCheck();
655 check.setPolicy(RepositoryPolicy.UPDATE_POLICY_ALWAYS);
656 check.setFileValid(false);
657
658
659 manager.checkArtifact(session, check);
660 assertTrue(check.isRequired());
661
662
663 manager.touchArtifact(session, check);
664
665
666 manager.checkArtifact(session, check);
667 assertTrue(check.isRequired());
668
669
670 check.setException(new ArtifactNotFoundException(check.getItem(), check.getRepository()));
671 manager.touchArtifact(session, check);
672
673
674 manager.checkArtifact(session, check);
675 assertFalse(check.isRequired());
676 }
677
678 @Test
679 public void testCheckArtifactAtMostOnceDuringSessionEvenIfUpdatePolicyAlways_DifferentRepoIdSameUrl() {
680 UpdateCheck<Artifact, ArtifactTransferException> check = newArtifactCheck();
681 check.setPolicy(RepositoryPolicy.UPDATE_POLICY_ALWAYS);
682
683
684 manager.checkArtifact(session, check);
685 assertTrue(check.isRequired());
686
687 manager.touchArtifact(session, check);
688
689
690 check.setRepository(new RemoteRepository.Builder(check.getRepository())
691 .setId("check")
692 .build());
693 manager.checkArtifact(session, check);
694 assertTrue(check.isRequired());
695 }
696
697 @Test
698 public void testCheckArtifactWhenLocallyMissingEvenIfUpdatePolicyIsNever() {
699 UpdateCheck<Artifact, ArtifactTransferException> check = newArtifactCheck();
700 check.setPolicy(RepositoryPolicy.UPDATE_POLICY_NEVER);
701 session.setResolutionErrorPolicy(new SimpleResolutionErrorPolicy(true, false));
702
703 check.getFile().delete();
704 assertFalse(check.getFile().getAbsolutePath(), check.getFile().exists());
705
706 manager.checkArtifact(session, check);
707 assertTrue(check.isRequired());
708 }
709
710 @Test
711 public void testCheckArtifactWhenLocallyPresentButInvalidEvenIfUpdatePolicyIsNever() {
712 UpdateCheck<Artifact, ArtifactTransferException> check = newArtifactCheck();
713 check.setPolicy(RepositoryPolicy.UPDATE_POLICY_NEVER);
714 session.setResolutionErrorPolicy(new SimpleResolutionErrorPolicy(true, false));
715
716 manager.touchArtifact(session, check);
717 resetSessionData(session);
718
719 check.setFileValid(false);
720
721 manager.checkArtifact(session, check);
722 assertTrue(check.isRequired());
723 }
724
725 @Test
726 public void testCheckArtifactWhenLocallyDeletedEvenIfTimestampUpToDate() {
727 UpdateCheck<Artifact, ArtifactTransferException> check = newArtifactCheck();
728 session.setResolutionErrorPolicy(new SimpleResolutionErrorPolicy(true, false));
729
730 manager.touchArtifact(session, check);
731 resetSessionData(session);
732
733 check.getFile().delete();
734 assertFalse(check.getFile().getAbsolutePath(), check.getFile().exists());
735
736 manager.checkArtifact(session, check);
737 assertTrue(check.isRequired());
738 }
739
740 @Test
741 public void testCheckArtifactNotWhenUpdatePolicyIsNeverAndTimestampIsUnavailable() {
742 UpdateCheck<Artifact, ArtifactTransferException> check = newArtifactCheck();
743 check.setPolicy(RepositoryPolicy.UPDATE_POLICY_NEVER);
744 session.setResolutionErrorPolicy(new SimpleResolutionErrorPolicy(true, false));
745
746 manager.checkArtifact(session, check);
747 assertFalse(check.isRequired());
748 }
749 }