1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }