001package org.eclipse.aether.internal.impl;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 * 
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 * 
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import static org.eclipse.aether.repository.RepositoryPolicy.*;
023import static org.junit.Assert.*;
024
025import java.util.Calendar;
026
027import org.eclipse.aether.DefaultRepositorySystemSession;
028import org.eclipse.aether.internal.test.util.TestUtils;
029import org.eclipse.aether.repository.RepositoryPolicy;
030import org.junit.Before;
031import org.junit.Test;
032
033/**
034 */
035public class DefaultUpdatePolicyAnalyzerTest
036{
037
038    private DefaultUpdatePolicyAnalyzer analyzer;
039
040    private DefaultRepositorySystemSession session;
041
042    @Before
043    public void setup()
044        throws Exception
045    {
046        analyzer = new DefaultUpdatePolicyAnalyzer();
047        session = TestUtils.newSession();
048    }
049
050    private long now()
051    {
052        return System.currentTimeMillis();
053    }
054
055    @Test
056    public void testIsUpdateRequired_PolicyNever()
057        throws Exception
058    {
059        String policy = RepositoryPolicy.UPDATE_POLICY_NEVER;
060        assertEquals( false, analyzer.isUpdatedRequired( session, Long.MIN_VALUE, policy ) );
061        assertEquals( false, analyzer.isUpdatedRequired( session, Long.MAX_VALUE, policy ) );
062        assertEquals( false, analyzer.isUpdatedRequired( session, 0, policy ) );
063        assertEquals( false, analyzer.isUpdatedRequired( session, 1, policy ) );
064        assertEquals( false, analyzer.isUpdatedRequired( session, now() - 604800000, policy ) );
065    }
066
067    @Test
068    public void testIsUpdateRequired_PolicyAlways()
069        throws Exception
070    {
071        String policy = RepositoryPolicy.UPDATE_POLICY_ALWAYS;
072        assertEquals( true, analyzer.isUpdatedRequired( session, Long.MIN_VALUE, policy ) );
073        assertEquals( true, analyzer.isUpdatedRequired( session, Long.MAX_VALUE, policy ) );
074        assertEquals( true, analyzer.isUpdatedRequired( session, 0, policy ) );
075        assertEquals( true, analyzer.isUpdatedRequired( session, 1, policy ) );
076        assertEquals( true, analyzer.isUpdatedRequired( session, now() - 1000, policy ) );
077    }
078
079    @Test
080    public void testIsUpdateRequired_PolicyDaily()
081        throws Exception
082    {
083        Calendar cal = Calendar.getInstance();
084        cal.set( Calendar.HOUR_OF_DAY, 0 );
085        cal.set( Calendar.MINUTE, 0 );
086        cal.set( Calendar.SECOND, 0 );
087        cal.set( Calendar.MILLISECOND, 0 );
088        long localMidnight = cal.getTimeInMillis();
089
090        String policy = RepositoryPolicy.UPDATE_POLICY_DAILY;
091        assertEquals( true, analyzer.isUpdatedRequired( session, Long.MIN_VALUE, policy ) );
092        assertEquals( false, analyzer.isUpdatedRequired( session, Long.MAX_VALUE, policy ) );
093        assertEquals( false, analyzer.isUpdatedRequired( session, localMidnight + 0, policy ) );
094        assertEquals( false, analyzer.isUpdatedRequired( session, localMidnight + 1, policy ) );
095        assertEquals( true, analyzer.isUpdatedRequired( session, localMidnight - 1, policy ) );
096    }
097
098    @Test
099    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}