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.junit.Assert.*;
023
024import org.eclipse.aether.DefaultRepositorySystemSession;
025import org.eclipse.aether.internal.test.util.TestUtils;
026import org.eclipse.aether.repository.RemoteRepository;
027import org.eclipse.aether.repository.RepositoryPolicy;
028import org.eclipse.aether.spi.connector.checksum.ChecksumPolicy;
029import org.eclipse.aether.transfer.TransferResource;
030import org.junit.After;
031import org.junit.Before;
032import org.junit.Test;
033
034public class DefaultChecksumPolicyProviderTest
035{
036
037    private static final String CHECKSUM_POLICY_UNKNOWN = "unknown";
038
039    private DefaultRepositorySystemSession session;
040
041    private DefaultChecksumPolicyProvider provider;
042
043    private RemoteRepository repository;
044
045    private TransferResource resource;
046
047    @Before
048    public void setup()
049        throws Exception
050    {
051        session = TestUtils.newSession();
052        provider = new DefaultChecksumPolicyProvider();
053        repository = new RemoteRepository.Builder( "test", "default", "file:/void" ).build();
054        resource = new TransferResource( repository.getId(), repository.getUrl(), "file.txt", null, null );
055    }
056
057    @After
058    public void teardown()
059        throws Exception
060    {
061        provider = null;
062        session = null;
063        repository = null;
064        resource = null;
065    }
066
067    @Test
068    public void testNewChecksumPolicy_Fail()
069    {
070        ChecksumPolicy policy =
071            provider.newChecksumPolicy( session, repository, resource, RepositoryPolicy.CHECKSUM_POLICY_FAIL );
072        assertNotNull( policy );
073        assertEquals( FailChecksumPolicy.class, policy.getClass() );
074    }
075
076    @Test
077    public void testNewChecksumPolicy_Warn()
078    {
079        ChecksumPolicy policy =
080            provider.newChecksumPolicy( session, repository, resource, RepositoryPolicy.CHECKSUM_POLICY_WARN );
081        assertNotNull( policy );
082        assertEquals( WarnChecksumPolicy.class, policy.getClass() );
083    }
084
085    @Test
086    public void testNewChecksumPolicy_Ignore()
087    {
088        ChecksumPolicy policy =
089            provider.newChecksumPolicy( session, repository, resource, RepositoryPolicy.CHECKSUM_POLICY_IGNORE );
090        assertNull( policy );
091    }
092
093    @Test
094    public void testNewChecksumPolicy_Unknown()
095    {
096        ChecksumPolicy policy = provider.newChecksumPolicy( session, repository, resource, CHECKSUM_POLICY_UNKNOWN );
097        assertNotNull( policy );
098        assertEquals( WarnChecksumPolicy.class, policy.getClass() );
099    }
100
101    @Test
102    public void testGetEffectiveChecksumPolicy_EqualPolicies()
103    {
104        String[] policies =
105            { RepositoryPolicy.CHECKSUM_POLICY_FAIL, RepositoryPolicy.CHECKSUM_POLICY_WARN,
106                RepositoryPolicy.CHECKSUM_POLICY_IGNORE, CHECKSUM_POLICY_UNKNOWN };
107        for ( String policy : policies )
108        {
109            assertEquals( policy, policy, provider.getEffectiveChecksumPolicy( session, policy, policy ) );
110        }
111    }
112
113    @Test
114    public void testGetEffectiveChecksumPolicy_DifferentPolicies()
115    {
116        String[][] testCases =
117            { { RepositoryPolicy.CHECKSUM_POLICY_WARN, RepositoryPolicy.CHECKSUM_POLICY_FAIL },
118                { RepositoryPolicy.CHECKSUM_POLICY_IGNORE, RepositoryPolicy.CHECKSUM_POLICY_FAIL },
119                { RepositoryPolicy.CHECKSUM_POLICY_IGNORE, RepositoryPolicy.CHECKSUM_POLICY_WARN } };
120        for ( String[] testCase : testCases )
121        {
122            assertEquals( testCase[0] + " vs " + testCase[1], testCase[0],
123                          provider.getEffectiveChecksumPolicy( session, testCase[0], testCase[1] ) );
124            assertEquals( testCase[0] + " vs " + testCase[1], testCase[0],
125                          provider.getEffectiveChecksumPolicy( session, testCase[1], testCase[0] ) );
126        }
127    }
128
129    @Test
130    public void testGetEffectiveChecksumPolicy_UnknownPolicies()
131    {
132        String[][] testCases =
133            { { RepositoryPolicy.CHECKSUM_POLICY_WARN, RepositoryPolicy.CHECKSUM_POLICY_FAIL },
134                { RepositoryPolicy.CHECKSUM_POLICY_WARN, RepositoryPolicy.CHECKSUM_POLICY_WARN },
135                { RepositoryPolicy.CHECKSUM_POLICY_IGNORE, RepositoryPolicy.CHECKSUM_POLICY_IGNORE } };
136        for ( String[] testCase : testCases )
137        {
138            assertEquals( "unknown vs " + testCase[1], testCase[0],
139                          provider.getEffectiveChecksumPolicy( session, CHECKSUM_POLICY_UNKNOWN, testCase[1] ) );
140            assertEquals( "unknown vs " + testCase[1], testCase[0],
141                          provider.getEffectiveChecksumPolicy( session, testCase[1], CHECKSUM_POLICY_UNKNOWN ) );
142        }
143    }
144
145}