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    {
050        session = TestUtils.newSession();
051        provider = new DefaultChecksumPolicyProvider();
052        repository = new RemoteRepository.Builder( "test", "default", "file:/void" ).build();
053        resource = new TransferResource( repository.getId(), repository.getUrl(), "file.txt", null, null );
054    }
055
056    @After
057    public void teardown()
058    {
059        provider = null;
060        session = null;
061        repository = null;
062        resource = null;
063    }
064
065    @Test
066    public void testNewChecksumPolicy_Fail()
067    {
068        ChecksumPolicy policy =
069            provider.newChecksumPolicy( session, repository, resource, RepositoryPolicy.CHECKSUM_POLICY_FAIL );
070        assertNotNull( policy );
071        assertEquals( FailChecksumPolicy.class, policy.getClass() );
072    }
073
074    @Test
075    public void testNewChecksumPolicy_Warn()
076    {
077        ChecksumPolicy policy =
078            provider.newChecksumPolicy( session, repository, resource, RepositoryPolicy.CHECKSUM_POLICY_WARN );
079        assertNotNull( policy );
080        assertEquals( WarnChecksumPolicy.class, policy.getClass() );
081    }
082
083    @Test
084    public void testNewChecksumPolicy_Ignore()
085    {
086        ChecksumPolicy policy =
087            provider.newChecksumPolicy( session, repository, resource, RepositoryPolicy.CHECKSUM_POLICY_IGNORE );
088        assertNull( policy );
089    }
090
091    @Test
092    public void testNewChecksumPolicy_Unknown()
093    {
094        ChecksumPolicy policy = provider.newChecksumPolicy( session, repository, resource, CHECKSUM_POLICY_UNKNOWN );
095        assertNotNull( policy );
096        assertEquals( WarnChecksumPolicy.class, policy.getClass() );
097    }
098
099    @Test
100    public void testGetEffectiveChecksumPolicy_EqualPolicies()
101    {
102        String[] policies =
103            { RepositoryPolicy.CHECKSUM_POLICY_FAIL, RepositoryPolicy.CHECKSUM_POLICY_WARN,
104                RepositoryPolicy.CHECKSUM_POLICY_IGNORE, CHECKSUM_POLICY_UNKNOWN };
105        for ( String policy : policies )
106        {
107            assertEquals( policy, policy, provider.getEffectiveChecksumPolicy( session, policy, policy ) );
108        }
109    }
110
111    @Test
112    public void testGetEffectiveChecksumPolicy_DifferentPolicies()
113    {
114        String[][] testCases =
115            { { RepositoryPolicy.CHECKSUM_POLICY_WARN, RepositoryPolicy.CHECKSUM_POLICY_FAIL },
116                { RepositoryPolicy.CHECKSUM_POLICY_IGNORE, RepositoryPolicy.CHECKSUM_POLICY_FAIL },
117                { RepositoryPolicy.CHECKSUM_POLICY_IGNORE, RepositoryPolicy.CHECKSUM_POLICY_WARN } };
118        for ( String[] testCase : testCases )
119        {
120            assertEquals( testCase[0] + " vs " + testCase[1], testCase[0],
121                          provider.getEffectiveChecksumPolicy( session, testCase[0], testCase[1] ) );
122            assertEquals( testCase[0] + " vs " + testCase[1], testCase[0],
123                          provider.getEffectiveChecksumPolicy( session, testCase[1], testCase[0] ) );
124        }
125    }
126
127    @Test
128    public void testGetEffectiveChecksumPolicy_UnknownPolicies()
129    {
130        String[][] testCases =
131            { { RepositoryPolicy.CHECKSUM_POLICY_WARN, RepositoryPolicy.CHECKSUM_POLICY_FAIL },
132                { RepositoryPolicy.CHECKSUM_POLICY_WARN, RepositoryPolicy.CHECKSUM_POLICY_WARN },
133                { RepositoryPolicy.CHECKSUM_POLICY_IGNORE, RepositoryPolicy.CHECKSUM_POLICY_IGNORE } };
134        for ( String[] testCase : testCases )
135        {
136            assertEquals( "unknown vs " + testCase[1], testCase[0],
137                          provider.getEffectiveChecksumPolicy( session, CHECKSUM_POLICY_UNKNOWN, testCase[1] ) );
138            assertEquals( "unknown vs " + testCase[1], testCase[0],
139                          provider.getEffectiveChecksumPolicy( session, testCase[1], CHECKSUM_POLICY_UNKNOWN ) );
140        }
141    }
142
143}