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.spi.connector.layout;
20  
21  import java.net.URI;
22  
23  import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithm;
24  import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactory;
25  import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactorySupport;
26  import org.eclipse.aether.spi.connector.layout.RepositoryLayout.ChecksumLocation;
27  import org.junit.jupiter.api.Test;
28  
29  import static org.junit.jupiter.api.Assertions.*;
30  
31  public class ChecksumLocationTest {
32      private final ChecksumAlgorithmFactory SHA512 = new ChecksumAlgorithmFactorySupport("SHA-512", "sha512") {
33          @Override
34          public ChecksumAlgorithm getAlgorithm() {
35              throw new RuntimeException("this should not happen");
36          }
37      };
38  
39      private final ChecksumAlgorithmFactory SHA256 = new ChecksumAlgorithmFactorySupport("SHA-256", "sha256") {
40          @Override
41          public ChecksumAlgorithm getAlgorithm() {
42              throw new RuntimeException("this should not happen");
43          }
44      };
45  
46      private final ChecksumAlgorithmFactory SHA1 = new ChecksumAlgorithmFactorySupport("SHA-1", "sha1") {
47          @Override
48          public ChecksumAlgorithm getAlgorithm() {
49              throw new RuntimeException("this should not happen");
50          }
51      };
52  
53      private final ChecksumAlgorithmFactory MD5 = new ChecksumAlgorithmFactorySupport("MD5", "md5") {
54          @Override
55          public ChecksumAlgorithm getAlgorithm() {
56              throw new RuntimeException("this should not happen");
57          }
58      };
59  
60      @Test
61      void testForLocation() {
62          ChecksumLocation cs = ChecksumLocation.forLocation(URI.create("dir/sub%20dir/file.txt"), SHA512);
63          assertEquals(SHA512, cs.getChecksumAlgorithmFactory());
64          assertEquals("dir/sub%20dir/file.txt.sha512", cs.getLocation().toString());
65  
66          cs = ChecksumLocation.forLocation(URI.create("dir/sub%20dir/file.txt"), SHA256);
67          assertEquals(SHA256, cs.getChecksumAlgorithmFactory());
68          assertEquals("dir/sub%20dir/file.txt.sha256", cs.getLocation().toString());
69  
70          cs = ChecksumLocation.forLocation(URI.create("dir/sub%20dir/file.txt"), SHA1);
71          assertEquals(SHA1, cs.getChecksumAlgorithmFactory());
72          assertEquals("dir/sub%20dir/file.txt.sha1", cs.getLocation().toString());
73  
74          cs = ChecksumLocation.forLocation(URI.create("dir/sub%20dir/file.txt"), MD5);
75          assertEquals(MD5, cs.getChecksumAlgorithmFactory());
76          assertEquals("dir/sub%20dir/file.txt.md5", cs.getLocation().toString());
77      }
78  
79      @Test
80      void testForLocation_WithQueryParams() {
81          assertThrows(
82                  IllegalArgumentException.class,
83                  () -> ChecksumLocation.forLocation(URI.create("file.php?param=1"), SHA1));
84      }
85  
86      @Test
87      void testForLocation_WithFragment() {
88          assertThrows(
89                  IllegalArgumentException.class,
90                  () -> ChecksumLocation.forLocation(URI.create("file.html#fragment"), SHA1));
91      }
92  }