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