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.jupiter.api.BeforeEach;
29  import org.junit.jupiter.api.Test;
30  
31  import static org.junit.jupiter.api.Assertions.*;
32  
33  /**
34   * UT helper for {@link RemoteRepositoryFilterSource} UTs.
35   */
36  public abstract class RemoteRepositoryFilterSourceTestSupport {
37      private final Artifact acceptedArtifact = new DefaultArtifact("org.one:aid:1.0");
38  
39      private final Artifact notAcceptedArtifact = new DefaultArtifact("org.two:aid:1.0");
40  
41      private DefaultRepositorySystemSession session;
42  
43      private RemoteRepository remoteRepository;
44  
45      private RemoteRepositoryFilterSource subject;
46  
47      @BeforeEach
48      void setup() {
49          remoteRepository = new RemoteRepository.Builder("test", "default", "https://irrelevant.com").build();
50          session = TestUtils.newSession();
51          subject = getRemoteRepositoryFilterSource(session, remoteRepository);
52      }
53  
54      protected abstract RemoteRepositoryFilterSource getRemoteRepositoryFilterSource(
55              DefaultRepositorySystemSession session, RemoteRepository remoteRepository);
56  
57      protected abstract void enableSource(DefaultRepositorySystemSession session);
58  
59      protected abstract void allowArtifact(
60              DefaultRepositorySystemSession session, RemoteRepository remoteRepository, Artifact artifact);
61  
62      @Test
63      void notEnabled() {
64          RemoteRepositoryFilter filter = subject.getRemoteRepositoryFilter(session);
65          assertNull(filter);
66      }
67  
68      @Test
69      void acceptedArtifact() {
70          allowArtifact(session, remoteRepository, acceptedArtifact);
71          enableSource(session);
72  
73          RemoteRepositoryFilter filter = subject.getRemoteRepositoryFilter(session);
74          assertNotNull(filter);
75  
76          RemoteRepositoryFilter.Result result = filter.acceptArtifact(remoteRepository, acceptedArtifact);
77  
78          assertTrue(result.isAccepted());
79          assertTrue(result.reasoning().contains("allowed from test"));
80      }
81  
82      @Test
83      void notAcceptedArtifact() {
84          allowArtifact(session, remoteRepository, acceptedArtifact);
85          enableSource(session);
86  
87          RemoteRepositoryFilter filter = subject.getRemoteRepositoryFilter(session);
88          assertNotNull(filter);
89  
90          RemoteRepositoryFilter.Result result = filter.acceptArtifact(remoteRepository, notAcceptedArtifact);
91  
92          assertFalse(result.isAccepted());
93          assertTrue(result.reasoning().contains("NOT allowed from test"));
94      }
95  }