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.filter;
20
21 import org.eclipse.aether.DefaultRepositorySystemSession;
22 import org.eclipse.aether.artifact.Artifact;
23 import org.eclipse.aether.artifact.DefaultArtifact;
24 import org.eclipse.aether.internal.test.util.TestUtils;
25 import org.eclipse.aether.repository.RemoteRepository;
26 import org.eclipse.aether.spi.connector.filter.RemoteRepositoryFilter;
27 import org.eclipse.aether.spi.connector.filter.RemoteRepositoryFilterSource;
28 import org.junit.Before;
29 import org.junit.Test;
30
31 import static org.hamcrest.MatcherAssert.assertThat;
32 import static org.hamcrest.Matchers.containsString;
33 import static org.hamcrest.Matchers.equalTo;
34 import static org.hamcrest.Matchers.notNullValue;
35 import static org.hamcrest.Matchers.nullValue;
36
37
38
39
40 public abstract class RemoteRepositoryFilterSourceTestSupport {
41 private final Artifact acceptedArtifact = new DefaultArtifact("org.one:aid:1.0");
42
43 private final Artifact notAcceptedArtifact = new DefaultArtifact("org.two:aid:1.0");
44
45 private DefaultRepositorySystemSession session;
46
47 private RemoteRepository remoteRepository;
48
49 private RemoteRepositoryFilterSource subject;
50
51 @Before
52 public void setup() {
53 remoteRepository = new RemoteRepository.Builder("test", "default", "https://irrelevant.com").build();
54 session = TestUtils.newSession();
55 subject = getRemoteRepositoryFilterSource(session, remoteRepository);
56 }
57
58 protected abstract RemoteRepositoryFilterSource getRemoteRepositoryFilterSource(
59 DefaultRepositorySystemSession session, RemoteRepository remoteRepository);
60
61 protected abstract void enableSource(DefaultRepositorySystemSession session);
62
63 protected abstract void allowArtifact(
64 DefaultRepositorySystemSession session, RemoteRepository remoteRepository, Artifact artifact);
65
66 @Test
67 public void notEnabled() {
68 RemoteRepositoryFilter filter = subject.getRemoteRepositoryFilter(session);
69 assertThat(filter, nullValue());
70 }
71
72 @Test
73 public void acceptedArtifact() {
74 allowArtifact(session, remoteRepository, acceptedArtifact);
75 enableSource(session);
76
77 RemoteRepositoryFilter filter = subject.getRemoteRepositoryFilter(session);
78 assertThat(filter, notNullValue());
79
80 RemoteRepositoryFilter.Result result = filter.acceptArtifact(remoteRepository, acceptedArtifact);
81
82 assertThat(result.isAccepted(), equalTo(true));
83 assertThat(result.reasoning(), containsString("allowed from test"));
84 }
85
86 @Test
87 public void notAcceptedArtifact() {
88 allowArtifact(session, remoteRepository, acceptedArtifact);
89 enableSource(session);
90
91 RemoteRepositoryFilter filter = subject.getRemoteRepositoryFilter(session);
92 assertThat(filter, notNullValue());
93
94 RemoteRepositoryFilter.Result result = filter.acceptArtifact(remoteRepository, notAcceptedArtifact);
95
96 assertThat(result.isAccepted(), equalTo(false));
97 assertThat(result.reasoning(), containsString("NOT allowed from test"));
98 }
99 }