View Javadoc
1   package org.eclipse.aether.internal.impl;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static org.eclipse.aether.repository.RepositoryPolicy.*;
23  import static org.junit.Assert.*;
24  
25  import java.util.Calendar;
26  
27  import org.eclipse.aether.DefaultRepositorySystemSession;
28  import org.eclipse.aether.internal.test.util.TestUtils;
29  import org.eclipse.aether.repository.RepositoryPolicy;
30  import org.junit.Before;
31  import org.junit.Test;
32  
33  /**
34   */
35  public class DefaultUpdatePolicyAnalyzerTest
36  {
37  
38      private DefaultUpdatePolicyAnalyzer analyzer;
39  
40      private DefaultRepositorySystemSession session;
41  
42      @Before
43      public void setup()
44          throws Exception
45      {
46          analyzer = new DefaultUpdatePolicyAnalyzer();
47          session = TestUtils.newSession();
48      }
49  
50      private long now()
51      {
52          return System.currentTimeMillis();
53      }
54  
55      @Test
56      public void testIsUpdateRequired_PolicyNever()
57          throws Exception
58      {
59          String policy = RepositoryPolicy.UPDATE_POLICY_NEVER;
60          assertEquals( false, analyzer.isUpdatedRequired( session, Long.MIN_VALUE, policy ) );
61          assertEquals( false, analyzer.isUpdatedRequired( session, Long.MAX_VALUE, policy ) );
62          assertEquals( false, analyzer.isUpdatedRequired( session, 0, policy ) );
63          assertEquals( false, analyzer.isUpdatedRequired( session, 1, policy ) );
64          assertEquals( false, analyzer.isUpdatedRequired( session, now() - 604800000, policy ) );
65      }
66  
67      @Test
68      public void testIsUpdateRequired_PolicyAlways()
69          throws Exception
70      {
71          String policy = RepositoryPolicy.UPDATE_POLICY_ALWAYS;
72          assertEquals( true, analyzer.isUpdatedRequired( session, Long.MIN_VALUE, policy ) );
73          assertEquals( true, analyzer.isUpdatedRequired( session, Long.MAX_VALUE, policy ) );
74          assertEquals( true, analyzer.isUpdatedRequired( session, 0, policy ) );
75          assertEquals( true, analyzer.isUpdatedRequired( session, 1, policy ) );
76          assertEquals( true, analyzer.isUpdatedRequired( session, now() - 1000, policy ) );
77      }
78  
79      @Test
80      public void testIsUpdateRequired_PolicyDaily()
81          throws Exception
82      {
83          Calendar cal = Calendar.getInstance();
84          cal.set( Calendar.HOUR_OF_DAY, 0 );
85          cal.set( Calendar.MINUTE, 0 );
86          cal.set( Calendar.SECOND, 0 );
87          cal.set( Calendar.MILLISECOND, 0 );
88          long localMidnight = cal.getTimeInMillis();
89  
90          String policy = RepositoryPolicy.UPDATE_POLICY_DAILY;
91          assertEquals( true, analyzer.isUpdatedRequired( session, Long.MIN_VALUE, policy ) );
92          assertEquals( false, analyzer.isUpdatedRequired( session, Long.MAX_VALUE, policy ) );
93          assertEquals( false, analyzer.isUpdatedRequired( session, localMidnight + 0, policy ) );
94          assertEquals( false, analyzer.isUpdatedRequired( session, localMidnight + 1, policy ) );
95          assertEquals( true, analyzer.isUpdatedRequired( session, localMidnight - 1, policy ) );
96      }
97  
98      @Test
99      public void testIsUpdateRequired_PolicyInterval()
100         throws Exception
101     {
102         String policy = RepositoryPolicy.UPDATE_POLICY_INTERVAL + ":5";
103         assertEquals( true, analyzer.isUpdatedRequired( session, Long.MIN_VALUE, policy ) );
104         assertEquals( false, analyzer.isUpdatedRequired( session, Long.MAX_VALUE, policy ) );
105         assertEquals( false, analyzer.isUpdatedRequired( session, now(), policy ) );
106         assertEquals( false, analyzer.isUpdatedRequired( session, now() - 5 - 1, policy ) );
107         assertEquals( false, analyzer.isUpdatedRequired( session, now() - 1000 * 5 - 1, policy ) );
108         assertEquals( true, analyzer.isUpdatedRequired( session, now() - 1000 * 60 * 5 - 1, policy ) );
109 
110         policy = RepositoryPolicy.UPDATE_POLICY_INTERVAL + ":invalid";
111         assertEquals( false, analyzer.isUpdatedRequired( session, now(), policy ) );
112     }
113 
114     @Test
115     public void testEffectivePolicy()
116     {
117         assertEquals( UPDATE_POLICY_ALWAYS,
118                       analyzer.getEffectiveUpdatePolicy( session, UPDATE_POLICY_ALWAYS, UPDATE_POLICY_DAILY ) );
119         assertEquals( UPDATE_POLICY_ALWAYS,
120                       analyzer.getEffectiveUpdatePolicy( session, UPDATE_POLICY_ALWAYS, UPDATE_POLICY_NEVER ) );
121         assertEquals( UPDATE_POLICY_DAILY,
122                       analyzer.getEffectiveUpdatePolicy( session, UPDATE_POLICY_DAILY, UPDATE_POLICY_NEVER ) );
123         assertEquals( UPDATE_POLICY_INTERVAL + ":60",
124                       analyzer.getEffectiveUpdatePolicy( session, UPDATE_POLICY_DAILY, UPDATE_POLICY_INTERVAL + ":60" ) );
125         assertEquals( UPDATE_POLICY_INTERVAL + ":60",
126                       analyzer.getEffectiveUpdatePolicy( session, UPDATE_POLICY_INTERVAL + ":100",
127                                                          UPDATE_POLICY_INTERVAL + ":60" ) );
128     }
129 
130 }