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.util.Calendar;
22
23 import org.eclipse.aether.DefaultRepositorySystemSession;
24 import org.eclipse.aether.internal.test.util.TestUtils;
25 import org.eclipse.aether.repository.RepositoryPolicy;
26 import org.junit.jupiter.api.BeforeEach;
27 import org.junit.jupiter.api.Test;
28
29 import static org.eclipse.aether.repository.RepositoryPolicy.*;
30 import static org.junit.jupiter.api.Assertions.*;
31
32
33
34 public class DefaultUpdatePolicyAnalyzerTest {
35
36 private DefaultUpdatePolicyAnalyzer analyzer;
37
38 private DefaultRepositorySystemSession session;
39
40 @BeforeEach
41 void setup() {
42 analyzer = new DefaultUpdatePolicyAnalyzer();
43 session = TestUtils.newSession();
44 }
45
46 private long now() {
47 return System.currentTimeMillis();
48 }
49
50 @Test
51 void testIsUpdateRequired_PolicyNever() {
52 String policy = RepositoryPolicy.UPDATE_POLICY_NEVER;
53 assertFalse(analyzer.isUpdatedRequired(session, Long.MIN_VALUE, policy));
54 assertFalse(analyzer.isUpdatedRequired(session, Long.MAX_VALUE, policy));
55 assertFalse(analyzer.isUpdatedRequired(session, 0, policy));
56 assertFalse(analyzer.isUpdatedRequired(session, 1, policy));
57 assertFalse(analyzer.isUpdatedRequired(session, now() - 604800000, policy));
58 }
59
60 @Test
61 void testIsUpdateRequired_PolicyAlways() {
62 String policy = RepositoryPolicy.UPDATE_POLICY_ALWAYS;
63 assertTrue(analyzer.isUpdatedRequired(session, Long.MIN_VALUE, policy));
64 assertTrue(analyzer.isUpdatedRequired(session, Long.MAX_VALUE, policy));
65 assertTrue(analyzer.isUpdatedRequired(session, 0, policy));
66 assertTrue(analyzer.isUpdatedRequired(session, 1, policy));
67 assertTrue(analyzer.isUpdatedRequired(session, now() - 1000, policy));
68 }
69
70 @Test
71 void testIsUpdateRequired_PolicyDaily() {
72 Calendar cal = Calendar.getInstance();
73 cal.set(Calendar.HOUR_OF_DAY, 0);
74 cal.set(Calendar.MINUTE, 0);
75 cal.set(Calendar.SECOND, 0);
76 cal.set(Calendar.MILLISECOND, 0);
77 long localMidnight = cal.getTimeInMillis();
78
79 String policy = RepositoryPolicy.UPDATE_POLICY_DAILY;
80 assertTrue(analyzer.isUpdatedRequired(session, Long.MIN_VALUE, policy));
81 assertFalse(analyzer.isUpdatedRequired(session, Long.MAX_VALUE, policy));
82 assertFalse(analyzer.isUpdatedRequired(session, localMidnight, policy));
83 assertFalse(analyzer.isUpdatedRequired(session, localMidnight + 1, policy));
84 assertTrue(analyzer.isUpdatedRequired(session, localMidnight - 1, policy));
85 }
86
87 @Test
88 void testIsUpdateRequired_PolicyInterval() {
89 String policy = RepositoryPolicy.UPDATE_POLICY_INTERVAL + ":5";
90 assertTrue(analyzer.isUpdatedRequired(session, Long.MIN_VALUE, policy));
91 assertFalse(analyzer.isUpdatedRequired(session, Long.MAX_VALUE, policy));
92 assertFalse(analyzer.isUpdatedRequired(session, now(), policy));
93 assertFalse(analyzer.isUpdatedRequired(session, now() - 5 - 1, policy));
94 assertFalse(analyzer.isUpdatedRequired(session, now() - 1000 * 5 - 1, policy));
95 assertTrue(analyzer.isUpdatedRequired(session, now() - 1000 * 60 * 5 - 1, policy));
96
97 policy = RepositoryPolicy.UPDATE_POLICY_INTERVAL + ":invalid";
98 assertFalse(analyzer.isUpdatedRequired(session, now(), policy));
99 }
100
101 @Test
102 void testEffectivePolicy() {
103 assertEquals(
104 UPDATE_POLICY_ALWAYS,
105 analyzer.getEffectiveUpdatePolicy(session, UPDATE_POLICY_ALWAYS, UPDATE_POLICY_DAILY));
106 assertEquals(
107 UPDATE_POLICY_ALWAYS,
108 analyzer.getEffectiveUpdatePolicy(session, UPDATE_POLICY_ALWAYS, UPDATE_POLICY_NEVER));
109 assertEquals(
110 UPDATE_POLICY_DAILY,
111 analyzer.getEffectiveUpdatePolicy(session, UPDATE_POLICY_DAILY, UPDATE_POLICY_NEVER));
112 assertEquals(
113 UPDATE_POLICY_INTERVAL + ":60",
114 analyzer.getEffectiveUpdatePolicy(session, UPDATE_POLICY_DAILY, UPDATE_POLICY_INTERVAL + ":60"));
115 assertEquals(
116 UPDATE_POLICY_INTERVAL + ":60",
117 analyzer.getEffectiveUpdatePolicy(
118 session, UPDATE_POLICY_INTERVAL + ":100", UPDATE_POLICY_INTERVAL + ":60"));
119 }
120 }