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