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.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   * UT helper for {@link RemoteRepositoryFilterSource} UTs.
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  }