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.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.Before;
27  import org.junit.Test;
28  
29  import static org.eclipse.aether.repository.RepositoryPolicy.*;
30  import static org.junit.Assert.*;
31  
32  /**
33   */
34  public class DefaultUpdatePolicyAnalyzerTest {
35  
36      private DefaultUpdatePolicyAnalyzer analyzer;
37  
38      private DefaultRepositorySystemSession session;
39  
40      @Before
41      public 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      public 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      public 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      public 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      public 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     public 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 }