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