View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.internal.impl;
20  
21  import org.eclipse.aether.DefaultRepositorySystemSession;
22  import org.eclipse.aether.internal.test.util.TestUtils;
23  import org.eclipse.aether.repository.RemoteRepository;
24  import org.eclipse.aether.repository.RepositoryPolicy;
25  import org.eclipse.aether.spi.connector.checksum.ChecksumPolicy;
26  import org.eclipse.aether.transfer.TransferResource;
27  import org.junit.After;
28  import org.junit.Before;
29  import org.junit.Test;
30  
31  import static org.hamcrest.MatcherAssert.assertThat;
32  import static org.hamcrest.core.Is.is;
33  import static org.junit.Assert.assertEquals;
34  import static org.junit.Assert.assertNotNull;
35  import static org.junit.Assert.assertNull;
36  import static org.junit.Assert.assertThrows;
37  
38  public class DefaultChecksumPolicyProviderTest {
39  
40      private static final String CHECKSUM_POLICY_UNKNOWN = "unknown";
41  
42      private DefaultRepositorySystemSession session;
43  
44      private DefaultChecksumPolicyProvider provider;
45  
46      private RemoteRepository repository;
47  
48      private TransferResource resource;
49  
50      @Before
51      public void setup() {
52          session = TestUtils.newSession();
53          provider = new DefaultChecksumPolicyProvider();
54          repository = new RemoteRepository.Builder("test", "default", "file:/void").build();
55          resource = new TransferResource(repository.getId(), repository.getUrl(), "file.txt", null, null);
56      }
57  
58      @After
59      public void teardown() {
60          provider = null;
61          session = null;
62          repository = null;
63          resource = null;
64      }
65  
66      @Test
67      public void testNewChecksumPolicy_Fail() {
68          ChecksumPolicy policy =
69                  provider.newChecksumPolicy(session, repository, resource, RepositoryPolicy.CHECKSUM_POLICY_FAIL);
70          assertNotNull(policy);
71          assertEquals(FailChecksumPolicy.class, policy.getClass());
72      }
73  
74      @Test
75      public void testNewChecksumPolicy_Warn() {
76          ChecksumPolicy policy =
77                  provider.newChecksumPolicy(session, repository, resource, RepositoryPolicy.CHECKSUM_POLICY_WARN);
78          assertNotNull(policy);
79          assertEquals(WarnChecksumPolicy.class, policy.getClass());
80      }
81  
82      @Test
83      public void testNewChecksumPolicy_Ignore() {
84          ChecksumPolicy policy =
85                  provider.newChecksumPolicy(session, repository, resource, RepositoryPolicy.CHECKSUM_POLICY_IGNORE);
86          assertNull(policy);
87      }
88  
89      @Test(expected = IllegalArgumentException.class)
90      public void testNewChecksumPolicy_Unknown() {
91          ChecksumPolicy policy = provider.newChecksumPolicy(session, repository, resource, CHECKSUM_POLICY_UNKNOWN);
92          assertNotNull(policy);
93          assertEquals(WarnChecksumPolicy.class, policy.getClass());
94      }
95  
96      @Test
97      public void testGetEffectiveChecksumPolicy_EqualPolicies() {
98          String[] policies = {
99              RepositoryPolicy.CHECKSUM_POLICY_FAIL,
100             RepositoryPolicy.CHECKSUM_POLICY_WARN,
101             RepositoryPolicy.CHECKSUM_POLICY_IGNORE
102         };
103         for (String policy : policies) {
104             assertEquals(policy, policy, provider.getEffectiveChecksumPolicy(session, policy, policy));
105         }
106     }
107 
108     @Test
109     public void testGetEffectiveChecksumPolicy_DifferentPolicies() {
110         String[][] testCases = {
111             {RepositoryPolicy.CHECKSUM_POLICY_WARN, RepositoryPolicy.CHECKSUM_POLICY_FAIL},
112             {RepositoryPolicy.CHECKSUM_POLICY_IGNORE, RepositoryPolicy.CHECKSUM_POLICY_FAIL},
113             {RepositoryPolicy.CHECKSUM_POLICY_IGNORE, RepositoryPolicy.CHECKSUM_POLICY_WARN}
114         };
115         for (String[] testCase : testCases) {
116             assertEquals(
117                     testCase[0] + " vs " + testCase[1],
118                     testCase[0],
119                     provider.getEffectiveChecksumPolicy(session, testCase[0], testCase[1]));
120             assertEquals(
121                     testCase[0] + " vs " + testCase[1],
122                     testCase[0],
123                     provider.getEffectiveChecksumPolicy(session, testCase[1], testCase[0]));
124         }
125     }
126 
127     @Test
128     public void testGetEffectiveChecksumPolicy_UnknownPolicies() {
129         String[][] testCases = {
130             {RepositoryPolicy.CHECKSUM_POLICY_WARN, RepositoryPolicy.CHECKSUM_POLICY_FAIL},
131             {RepositoryPolicy.CHECKSUM_POLICY_WARN, RepositoryPolicy.CHECKSUM_POLICY_WARN},
132             {RepositoryPolicy.CHECKSUM_POLICY_IGNORE, RepositoryPolicy.CHECKSUM_POLICY_IGNORE}
133         };
134         for (String[] testCase : testCases) {
135             IllegalArgumentException e = assertThrows(
136                     IllegalArgumentException.class,
137                     () -> provider.getEffectiveChecksumPolicy(session, CHECKSUM_POLICY_UNKNOWN, testCase[1]));
138             assertThat(e.getMessage(), is("Unsupported policy: unknown"));
139             e = assertThrows(
140                     IllegalArgumentException.class,
141                     () -> provider.getEffectiveChecksumPolicy(session, testCase[1], CHECKSUM_POLICY_UNKNOWN));
142             assertThat(e.getMessage(), is("Unsupported policy: unknown"));
143         }
144     }
145 }