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.spi.connector.checksum.ChecksumPolicy.ChecksumKind;
22  import org.eclipse.aether.transfer.ChecksumFailureException;
23  import org.eclipse.aether.transfer.TransferResource;
24  import org.junit.Before;
25  import org.junit.Test;
26  
27  import static org.junit.Assert.*;
28  
29  public class FailChecksumPolicyTest {
30  
31      private FailChecksumPolicy policy;
32  
33      private ChecksumFailureException exception;
34  
35      @Before
36      public void setup() {
37          policy = new FailChecksumPolicy(new TransferResource("null", "file:/dev/null", "file.txt", null, null));
38          exception = new ChecksumFailureException("test");
39      }
40  
41      @Test
42      public void testOnTransferChecksumFailure() {
43          assertFalse(policy.onTransferChecksumFailure(exception));
44      }
45  
46      @Test
47      public void testOnChecksumMatch() {
48          assertTrue(policy.onChecksumMatch("SHA-1", ChecksumKind.REMOTE_EXTERNAL));
49          assertTrue(policy.onChecksumMatch("SHA-1", ChecksumKind.REMOTE_INCLUDED));
50          assertTrue(policy.onChecksumMatch("SHA-1", ChecksumKind.PROVIDED));
51      }
52  
53      @Test
54      public void testOnChecksumMismatch() throws Exception {
55          try {
56              policy.onChecksumMismatch("SHA-1", ChecksumKind.REMOTE_EXTERNAL, exception);
57              fail("No exception");
58          } catch (ChecksumFailureException e) {
59              assertSame(exception, e);
60          }
61          try {
62              policy.onChecksumMismatch("SHA-1", ChecksumKind.REMOTE_INCLUDED, exception);
63              fail("No exception");
64          } catch (ChecksumFailureException e) {
65              assertSame(exception, e);
66          }
67          try {
68              policy.onChecksumMismatch("SHA-1", ChecksumKind.PROVIDED, exception);
69              fail("No exception");
70          } catch (ChecksumFailureException e) {
71              assertSame(exception, e);
72          }
73      }
74  
75      @Test
76      public void testOnChecksumError() throws Exception {
77          policy.onChecksumError("SHA-1", ChecksumKind.REMOTE_EXTERNAL, exception);
78      }
79  
80      @Test
81      public void testOnNoMoreChecksums() {
82          try {
83              policy.onNoMoreChecksums();
84              fail("No exception");
85          } catch (ChecksumFailureException e) {
86              assertTrue(e.getMessage().contains("no checksums available"));
87          }
88      }
89  }